#define red PORTC.F1
#define green PORTC.F2
#define blue PORTC.F3
#define sw1 PORTC.F6
#define sw2 PORTC.F5
#define sw3 PORTC.F4
unsigned char press;
unsigned char i,j,k;
void main()
{
C1ON_bit=0;
C2ON_bit=0;
ANSEL=0x00;
ANSELH=0x00;
TRISC=0b11111000;
PORTC=0x00;
PWM1_Init(5000);
PWM2_Init(5000);
PWM1_Start();
PWM2_Start();
press=0;
while(1)
{
if(sw1==0)
{
press=press+1;
if(press=1)
{
red=1;
blue=0;
green=0;
}
else if(press=2)
{
red=0;
blue=1;
green=0;
}
else if(press=3)
{
press=0;
red=0;
blue=0;
green=1;
}
}
if(sw2==0)
{
for(i=0;i<255;i++)
{
PWM2_Set_Duty(i);
Delay_ms(10);
}
for(j=255;j<0;j--)
{
PWM1_Set_Duty(j);
Delay_ms(10);
}
for(k=0;k<255;k++)
{
blue=0;
Delay_ms(2);
Delay_us(500);
blue=1;
Delay_ms(7);
Delay_us(500);
}
}
if(sw3==0)
{
red=1;
green=1;
blue=1;
}
}
}
unsigned char dc ;
TRISC = 0 ; // set PORTC as output
PORTC = 0 ; // clear PORTC
//configure CCP module as 4000 Hz PWM output
PR2 = 0b01111100 ;
T2CON = 0b00000101 ;
CCP1CON = 0b00001100 ;
CCP2CON = 0b00111100 ;
void main()
{
unsigned char dc ;
TRISC = 0 ; // set PORTC as output
PORTC = 0 ; // clear PORTC
/*
* configure CCP module as 4000 Hz PWM output
*/
PR2 = 0b01111100 ;
T2CON = 0b00000101 ;
CCP1CON = 0b00001100 ;
CCP2CON = 0b00111100 ;
for(;;) // forever
{
/*
* PWM resolution is 10 bits
* don't use last 2 less significant bits CCPxCON,
* so only CCPRxL have to be touched to change duty cycle
*/
for(dc = 0 ; dc < 128 ; dc++)
{
CCPR1L = dc ;
CCPR2L = 128 - dc ;
Delay_ms(10) ;
}
for(dc = 127 ; dc > 0 ; dc--)
{
CCPR1L = dc ;
CCPR2L = 128 - dc ;
Delay_ms(10) ;
}
}
}
for(count=0;count<50;count++) //50 * 0.020 Seconds = 1 Second
{
PORTB = 0x01; //PortB Pin 0 = 5v (logic 1)
Delay_ms(7); //Delay 1.5mS
Delay_us(5);
PORTB = 0x00; //PortB Pin 0 = 0v (logic 0)
Delay_ms(92);//Delay 18.5mS
Delay_us(5);
}
Hi vinay
1) for creating the pwm signal to third channel, we can use timer concept, for creating timer delay, the rise and fall time of the signal is calculated, and also we need to consider the oscillator frequency
2) So a PWM wave is just a signal that changes between 0 volts & 5 volts (digital logic 0 and 1). for increase the wave timing and voltage variation, we can use timer delay.
based on the crystal frequency the timer isr gets called and the timer overflows on the limit
based on that (for 20 MHz)
Code:for(count=0;count<50;count++) //50 * 0.020 Seconds = 1 Second { PORTB = 0x01; //PortB Pin 0 = 5v (logic 1) Delay_ms(7); //Delay 1.5mS Delay_us(5); PORTB = 0x00; //PortB Pin 0 = 0v (logic 0) Delay_ms(92);//Delay 18.5mS Delay_us(5); }
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?