Hi every body, and happy new year to all keil users ;) I am using the C515C with keil v2.39, I want to generate a PWM signal using one of the pins 1.0,1.1,1.2 or 1.3, I read the manual, put I found it difficult, because I have to configure different things. Like the following parameters:
T2CON |= 0x10; ET2=1; EXEN2=0; CRCL = 0x22; CRCH = 0xFF; EX3=0; CCL1 = 0xFF; CCH1 = 0x22; EX4=0; CCL2 = 0x00; CCH2 = 0x00; EX5=0; CCL3 = 0x000; CCH3 = 0x000; EX6=0; CCEN = 0xAA;
What I know, is that we have to use compare mode 0.
But how can I set T2 to be ready for all things, and how I change the duty cycle while from the program results...
Is there any ready function that has one input which is the duty cycle?
And thank you for your time.
This is dead simple if you do not try to be fancy here is some pseudocode
in main() where you find out what the PWM is to be you set two variables: Up and Down, Up being the number of T2 clocks you want the PWM output to be high and Down being the number of T2 clocks you want the PWM output to be low. For a contstant frequency Up plus Down must always be the same.
Then you have the variables High and Low High being (10000h minus Up) + fiddle and Low being (10000h minus Down) + fiddle. You also need a bit variable, let's call it On
then in the T2 ISR stop the timer if ON -- clear On -- set the timer value (TH and TL) to the value Low -- clear the port pin else -- set On -- set the timer value (TH and TL) to the value High -- set the port pin endif start the timer exit
fiddle above is the number of T2 clocks that would occur during ISR execution which you can calculate with "the bible" chapter 2. If you need ultimate precision code the T2 ISR in assembler so the time it takes will be constant.