Title: PWM using PIC18f4520
Program:
//Author:PPalak Patel
//Date:5/8/2014
//Contact No:9173211683
//Title:PWM
//Platform:Pic18f4520
//Fosc=20Mhz
//Timer Frequency=20Mhz/4=5Mhz
//Timer Period=1/5Mhz=0.0000002=0.2us=0.0002ms
//Max time=256*0.2us=0.0000512=51.2us=
#include<p18f4520.h>
#pragma config OSC = INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#define sw PORTCbits.RC4
#define sw1 PORTCbits.RC6
void Delay_ms(unsigned char j)
{
unsigned char i,l;
for(i=0;i<j;i++)
{
for(l=0;l<200;l++);
}
}
void main()
{
unsigned char i=0;
TRISCbits.TRISC2=0; //CCP1 Pin as output
TRISCbits.TRISC4=1;
TRISCbits.TRISC6=1;
T2CONbits.T2OUTPS3=0; //Postscaler=1:1
T2CONbits.T2OUTPS2=0;
T2CONbits.T2OUTPS1=0;
T2CONbits.T2OUTPS0=0;
T2CONbits.T2CKPS1=0;
T2CONbits.T2CKPS0=1; //Prescaler=1:1
while(1)
{
if(sw==0)
{
Delay_ms(10);
i++;
}
if(sw1==0)
{
Delay_ms(10);
i--;
}
if(i<0)
{
Delay_ms(10);
i=255;
}
if(i>255)
{
Delay_ms(10);
i=0;
}
while(1)
{
PR2=200;
CCP1CON=0x0F;
CCPR1L=i;
TMR2=0;
T2CONbits.TMR2ON=1;
while(PIR1bits.TMR2IF==0);
PIR1bits.TMR2IF=0;
T2CONbits.TMR2ON=0;
if(sw==0 || sw1==0)
{
break;
}
}
}
}
Simulation:
Program:
//Author:PPalak Patel
//Date:5/8/2014
//Contact No:9173211683
//Title:PWM
//Platform:Pic18f4520
//Fosc=20Mhz
//Timer Frequency=20Mhz/4=5Mhz
//Timer Period=1/5Mhz=0.0000002=0.2us=0.0002ms
//Max time=256*0.2us=0.0000512=51.2us=
#include<p18f4520.h>
#pragma config OSC = INTIO67
#pragma config FCMEN = OFF
#pragma config IESO = OFF
#pragma config PWRT = OFF
#pragma config BOREN = OFF
#pragma config WDT = OFF
#pragma config MCLRE = ON
#pragma config PBADEN = OFF
#pragma config STVREN = OFF
#pragma config LVP = OFF
#define sw PORTCbits.RC4
#define sw1 PORTCbits.RC6
void Delay_ms(unsigned char j)
{
unsigned char i,l;
for(i=0;i<j;i++)
{
for(l=0;l<200;l++);
}
}
void main()
{
unsigned char i=0;
TRISCbits.TRISC2=0; //CCP1 Pin as output
TRISCbits.TRISC4=1;
TRISCbits.TRISC6=1;
T2CONbits.T2OUTPS3=0; //Postscaler=1:1
T2CONbits.T2OUTPS2=0;
T2CONbits.T2OUTPS1=0;
T2CONbits.T2OUTPS0=0;
T2CONbits.T2CKPS1=0;
T2CONbits.T2CKPS0=1; //Prescaler=1:1
while(1)
{
if(sw==0)
{
Delay_ms(10);
i++;
}
if(sw1==0)
{
Delay_ms(10);
i--;
}
if(i<0)
{
Delay_ms(10);
i=255;
}
if(i>255)
{
Delay_ms(10);
i=0;
}
while(1)
{
PR2=200;
CCP1CON=0x0F;
CCPR1L=i;
TMR2=0;
T2CONbits.TMR2ON=1;
while(PIR1bits.TMR2IF==0);
PIR1bits.TMR2IF=0;
T2CONbits.TMR2ON=0;
if(sw==0 || sw1==0)
{
break;
}
}
}
}
Simulation:
No comments:
Post a Comment