Stopping the PWM-Output? HC908QY4 on the SofTec NitronKit

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Stopping the PWM-Output? HC908QY4 on the SofTec NitronKit

1,584 Views
sming
Contributor I
Hello there
 
I've got the Nitron-Kit from SofTec with the HC908QY4 and USB to MON08 Interface.
 
Now I wrote a little program wich generates a PWM-Signal on the channel 0. The dutycycle can be changed with the value from de ADC. If the value from the ADC is 0x00, i want to stop and set the PWM-Output to 0x00, so there is a clean "0" on the PTA0. Otherwise, if the ADC reaches the value for a 100% dutycycle, i want to set the PWM-Output to 0x01, so there is a clean "1" on the PTA0.
 
With my solution, setting the PWM-Output to "1" doesn't work very good. Sometimes it will be set to "1", sometimes it will be set to "0".... Any solutions?
 
 
Heres my Code:
 
Code:
Initialize--------------void pwm_init(){ TSC = 0x30;               //Timer stoppen und zurücksetzen TMODH = 0x00;                   //PWM-Grundfrequenz setzen (ca. 20kHz) TMODL = 0xA0;                   //PWM-Grundfrequenz setzen (ca. 20kHz) TCH0H = 0x00;                   //PWM-DutyCycle setzen TCH0L = 0x00;                   //PWM-DutyCycle setzen TSC0 = 0x1A;                    //PWM-Generator konfigurieren //TSC = 0x00;                     //Timer starten}Main-program------------------              //BETRIEBSART POTI         //////////////////         //Falls Poti in Nullstellung ist         while (TRUE){                             if (ADR == 0x00)                {                     TSC = 0x30;      //Timer stoppen und zurücksetzen             PTA = 0x00;           //PWM-Port löschen             ADR = 0x00;         }                  //Falls Poti in Maximalstellung (0xA0 = Maximum weil PWM Grundfrequenz!)         if (ADR > 0x9F)                {                      TSC = 0x30;      //Timer stoppen und zurücksetzen             PTA = 0x01;           //PWM-Port immer setzen             ADR = 0x00;                  }                  //Falls Poti in gültiger Stellung ist         if ((ADR > 0x00) && (ADR <= 0x9F))         {           TSC = 0x00;            //Timer starten           TCH0L = ADR;           //PWM-DutyCycle (lowerByte) fix einstellen              }                  }

 
Thank you very much!
 
(Alban: code format - SRC button)

Message Edited by Alban on 2007-01-15 12:06 PM

Labels (1)
0 Kudos
2 Replies

336 Views
rocco
Senior Contributor II
Hi, Sming:

The PTA register will not affect your output as long as the timer is in the PWM mode. What you are seeing on the output is the state that the pin was in when you stopped and reset the timer. Just stopping and reseting the timer does not take it out of PWM mode.

But the solution is easier than you think. First, the PWM should be a solid low when you set it to 0. So you should not need to treat zero as a special case.

For the "high" output, there is a bit in the TSC0 register called CH0MAX (bit 0). When you set this bit, the PWM will go to a solid high.

So something like this should work:
   if (ADR > 0x9F)        
      TSC0 = 0x1B;

   if (ADR <= 0x9F)
      TSC0 = 0x1A;
0 Kudos

336 Views
bigmac
Specialist III
Hello,
 
Further to Rocco's comments, since you have set TMOD to a value of 0x00A0,  the PWM period will actually be 161 cycles (not 160 cycles as you might have intended).  This is because the counter does not roll over to zero until one timer clock cycle after the count reaches the TMOD value.
 
Another effect is that, when TCH0 register is set to 0x0000, the PWM pulse width will remain at approximately one cycle - because the PWM output toggles (high) when the timer count reaches 0x00A0, but does not return low until the OC occurs, after the counter overflows to zero.  This is why special treatment of the zero duty cycle case is necessary (contrary to Rocco's comment) - simply clear the TOV0 bit within TCS0 register.  This also has the consequence that the TCH0 value should be set one less than the desired duty cycle.
 
See what happens if you set TCH0 to the same value as TMOD - it will depend on whether "toggle on overflow" or OC actually occurs first within the device.
 
To smoothly change the duty cycle, without glitching the PWM output, the OC interrupt should be enabled prior to making the change, and the new value for TCH0 entered during the ISR processing.  The OC interrupt can then be disabled prior to leaving the ISR, until the next change is required.
 
Regards,
Mac
 

Message Edited by bigmac on 2007-01-1503:20 PM

0 Kudos