GammaRay86
New Member
Hi guys,
I'm trying to understand some code that uses a variable as the values for the PWM duty cycle registers. The CCPR1L register contains the 8 MSbs and bits 5:4 of CCP1CON contain the 2 LSbs of the 10-bit value. Now the code pretty much says the maximum value is 1000 which is 1111101000 in binary. How do I put the number 1000 in these two registers? is it simply 11111010 in CCPR1L and 00 in CCP1CON<5:4>?
Take a look at this section of code:
What exactly is going on here? It looks like at the end the value is shifted by 2 so it is only an 8-bit value. The equation to get the pulse width is:
Pulse Width = (CCPR1L:CCP1CON<5:4>) / (4*(PR2+1))
According to the rest of the code, the maximum value of the numerator can be is 256. But this is 8-bits. Shouldn't the maximum be 1024 since it's 10-bits?
Also, it can be a negative number so 1 bit must be for the sign. Isn't it then effectively a 9-bit number?
I'd appreciate it so much if anyone could straighten this out for me. Thanks
I'm trying to understand some code that uses a variable as the values for the PWM duty cycle registers. The CCPR1L register contains the 8 MSbs and bits 5:4 of CCP1CON contain the 2 LSbs of the 10-bit value. Now the code pretty much says the maximum value is 1000 which is 1111101000 in binary. How do I put the number 1000 in these two registers? is it simply 11111010 in CCPR1L and 00 in CCP1CON<5:4>?
Take a look at this section of code:
Code:
if(Cn >= 1000) // Used to limit duty cycle not to have punch through
{
Cn = 1000;
}
if(Cn <= -1000)
{
Cn = -1000;
}
if(Cn == 0){ // Set the speed of the PWM
DC1B1 = DC1B1 = 0;
CCPR1L = 0;
}
if(Cn > 0){ // Motor should go forward and set the duty cycle to Cn
P1M1 = 0; // Motor is going forward
temp = Cn;
if(temp^0b00000001){
DC1B0 = 1;
}
else{
DC1B0 = 0;
}
if(temp^0b00000010){
DC1B1 = 1;
}
else{
DC1B1 = 0;
}
CCPR1L = Cn >> 2;
What exactly is going on here? It looks like at the end the value is shifted by 2 so it is only an 8-bit value. The equation to get the pulse width is:
Pulse Width = (CCPR1L:CCP1CON<5:4>) / (4*(PR2+1))
According to the rest of the code, the maximum value of the numerator can be is 256. But this is 8-bits. Shouldn't the maximum be 1024 since it's 10-bits?
Also, it can be a negative number so 1 bit must be for the sign. Isn't it then effectively a 9-bit number?
I'd appreciate it so much if anyone could straighten this out for me. Thanks
Last edited: