What is the intention of this?! while(1); does "loop in one spot" it will never change / set / do anything .. the app will just "stand still" at that position....
disregarding the config bits etc ..
Code:
#include <p18f4520.h>
void main ( )
{
TRISB = 0;
TRISC = 0b11111011; // RC2 as output
T2CON = 0b00000101; // Timer 2 On, postscale = 1:1, prescale = 1:4
PR2 = 64; // Set PR2 = 64 for 52 us
CCPR1L = 0b00001101; // CCPR1L:CCP1CON<5:4> = 52
CCP1CON = 0b00001111; // DC1B1 & DC1B0 = 0, PWM mode
while(1){
PORTBbits.RB1 = PORTCbits.RC2; //blinking LED according to PWM
}
}
In theory this will blink B1 almost the same time as RC2
RC2 is "blinked" by hardware ccp (pwm) module.
if you only need to blink the RC2 then you do not need the b1 part
Code:
#include <p18f4520.h>
void main ( )
{
TRISB = 0;
TRISC = 0b11111011; // RC2 as output
T2CON = 0b00000101; // Timer 2 On, postscale = 1:1, prescale = 1:4
PR2 = 64; // Set PR2 = 64 for 52 us
CCPR1L = 0b00001101; // CCPR1L:CCP1CON<5:4> = 52
CCP1CON = 0b00001111; // DC1B1 & DC1B0 = 0, PWM mode
while(1); // DO NOTHING - hw pwm module will blink led
}
I do not see the config here but if you are running it at 4MHz the speed of the pwm will be too fast and there is no way you can see the blinking with naked eye .. if you want blinking that you can see, you have to forget about pwm as you cannot make pwm go that slow (max that you can notice is about 100Hz and even that is almost impossible to tell ... the 15Hz is kinda max for "noticeable" blink) and PWM usually do not go under few KHz because:
PWM Period = [(PR2) + 1] • 4 • TOSC • (TMR2 Prescale Value)
your TOSC is 1/4000000, max TMR2 prescale is 16, max PR2 is 255 so max period is
256 * 4 * (1/4000000) * 16 = 0.004096 => 1/0.004096 = 244.14Hz
So with 4MHz the lowest you can go is 244Hz - there is NO WAY you can see that with naked eye. not to mention that you are using 1:4 prescale and not 1:16 so your led is blinking at almost 1KHz
If you need to be able to see the led blink, use the delay function
Code:
#include <p18f4520.h>
void main ( )
{
TRISB = 0;
TRISC = 0b11111011; // RC2 as output
LATBbits.LATB1 = 0;
LATCbits.LATC2 = 0;
while(1){
LATBbits.LATB1 ^= 1;
LATCbits.LATC2 ^= 1;
Delay10KTCYx(200); //wait 2000000 cycles
}
}