Hi,
That WOULD be correct but you can't put 1000 in PR2 since it's only 8 bits wide. This means that you can't get 5kHz with a prescaler of 1 so you'll have to use 4.
Hi,
That WOULD be correct but you can't put 1000 in PR2 since it's only 8 bits wide. This means that you can't get 5kHz with a prescaler of 1 so you'll have to use 4.
ok i got the idea right now.
what about value in the lookup table? is it 8 bit wide or more because
from my calculation of duty cycle:
let say 50%
prescaler 4
PR2+1 =250
so duty2 = 250*4*0.5 = 500
and if the value of constant in lookup table only 8bit wide , what should i do?
change to prescaler 16?? that would decrease my resolution..
please help assist
photoelectric
Hi,
Have you had a look in the PBP manual?
It says: The LOOKUP statement can be used to retreive values from a table of 8-bit constants.
So, for LOOKUP you have to stick to 8bits in the table. You can do that by either use prescaler of 16 or stick with a prescaler of 4 and multiply the retreived value by 4 before putting it in the dutycycle register. Obviosuly both options reduces the effective resolution.
If you need more resolution then take a look at the LOOKUP2 statement, it's also described in the manual.
hi,
i stick with prescaler 4 and alrdy key in my dutycycle in my lookup using command lookup2 since it can support til 10bits wide.
below is my complete coding for 1 channel PWM:
DEFINE OSC 20
duty1 VAR WORD ' Duty cycle value (CCPR1L:CCP1CON<5:4>)
duty2 VAR WORD
TRISC.2 = 0 ' Set PORTC.2 (CCP1) to output
CCP1CON = %00001100 ' Set CCP1 to PWM
T2CON = %00000101 ' Turn on Timer2, Prescale=4
PR2 = 249 ' Set PR2 to get 5KHz out
'Set up a loop for 45 iterations, Duty1 will count 0,1,2,3,4....44
Main:
For duty1 = 0 TO 185
'Go into the table AND retrieve the value pointed at by Duty1 AND put that value in Duty2
LookUp2 duty1, [0,18,34,52,68,85,102,119,137,152,170,187,203,220,2 36,253,267,285,302,318,334,_
350,367,382,397,413,429,443,459,474,489,504,518,53 3,547,562,575,589,603,616,630,_
643,655,669,681,693,706,718,729,740,752,764,774,78 5,795,806,815,825,835,844,853,_
861,870,879,887,895,901,909,916,923,929,935,941,94 6,952,957,962,967,971,974,978,_
981,984,987,990,992,994,996,997,998,999,999,1000,9 99,999,998,997,997,996,994,992,_
990,987,984,981,978,974,971,967,962,957,952,946,94 1,935,929,923,916,909,901,895,_
887,879,870,861,853,844,835,825,815,806,795,785,77 4,764,752,740,729,718,706,693,_
681,669,655,643,630,616,603,589,575,562,547,533,51 8,504,489,474,459,443,429,413,_
397,382,367,350,334,318,302,285,267,253,220,203,18 7,170,152,137,119,102,85,68,_
52,34,18,0],duty2
'Duty2 now contains the value from the lookup table that Duty1 points at. First time thru the loop it's 0 second time it's 18 and so on.
CCP1CON.4 = duty2.0 ' Setup 10-bit duty cycle as
CCP1CON.5 = duty2.1 ' a 10-bit word
CCPR1L = duty2 >> 2
'Pause 1 'Use each dutycycle value 10ms before going to the next one.
Next duty1
GoTo Main 'And do it all over again
End
i compile then use real pic simulator to check the PWM pattern.
seem like the duty cycle only read up to value 253 in my lookup table and then keep generating same dutycyle.
did i miss sumthing in my code??
attached is my screen shot of the pwm pattern. at the red area pwm duty is correct but at the yellow area it starts to generate same duty and not looking up to the value in the lookup2 command.
please help assist
photoelectric
Hi,
I've tried it here, on a real physical PIC, and it works just fine.
I see that you've removed the Pause statement I had in there. If you don't have a delay there it will run thru the different dutycycles much faster then the actual PWM frequency. It tries to update the dutycycle registers several times during each PWM period which obviously won't work properly.
Your PWM frequency is 5kHz so if you really do want to see "all the steps" you must have atleast 1/5000=200us between updates. I measured the execution time of the loop to around 25us with a 20Mhz oscillator so you're basically trying to update the dutycycle 8 times every PWM period.....
yes, i remove the pause statement because i think adding it causing the pwm duty longer then the original pwm duty, hence changing my pwm pattern.
can we actually pause below 1 ms since i know pause statement can only be use for minimum 1ms right??
correct me if im wrong
when you tries on the real physical PIC, do you enable the pause statement??
i will try on real physical PIC later and feedback to you tmrw.
photoelectirc
Hi,
The Pause statement won't effect the PWM since it's generated in hardware. If you have Pause 1 you'll basically get 5 periods of each dutycycle value.
You can Pause with microsecond resolution by using PauseUs (again, it's in the green book....)
When testing I used 300us and you can see the result in the attached screenshot. This is not a simulation, it's captured on real hardware.
hi, yes you are correct, i test it on real physical and it works fine.
But i dint really understand on how you calculate the execution of my FOR NEXT loop. Can you explain a liltle bit detailed on that.
below is the previous quote on how you calculate the execution time.
Hi,
I didn't calculate it, I measured it - as it says in the message ;-)
I put a TOGGLE PortC.0 just before the NEXT statement a put the scope on the pin. I got a 20kHz square wave which, because the pin is toggled each time thru loop, means it executes roughly 40000 times per second. 1/40000=25us
Bookmarks