@@ -120,16 +120,21 @@ void pwmout_write(pwmout_t* obj, float value)
120120 value = 1.0f ;
121121 }
122122 uint32_t t_on = (uint32_t )((float )(obj -> pwm -> MATCHREL [obj -> pwm_ch * 2 ] + 1 ) * value );
123- if (t_on > 0 ) {
124- if (value != 1.0f ) {
123+ if (t_on > 0 ) { // duty is not 0%
124+ if (value != 1.0f ) { // duty is not 100%
125125 obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] = t_on - 1 ;
126+ // unhalt the counter
126127 obj -> pwm -> CTRL &= ~(1 << 2 );
127- } else {
128+ } else { // duty is 100%
129+ // halt and clear the counter
128130 obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
131+ // output level tied to high
129132 obj -> pwm -> OUTPUT |= (1 << obj -> pwm_ch );
130133 }
131- } else {
134+ } else { // duty is 0%
135+ // halt and clear the counter
132136 obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
137+ // output level tied to low
133138 obj -> pwm -> OUTPUT &= ~(1 << obj -> pwm_ch );
134139 }
135140}
@@ -155,15 +160,19 @@ void pwmout_period_ms(pwmout_t* obj, int ms)
155160// Set the PWM period, keeping the duty cycle the same.
156161void pwmout_period_us (pwmout_t * obj , int us )
157162{
163+ // The period are off by one for MATCHREL, so +1 to get actual value
158164 uint32_t t_off = obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 0 ] + 1 ;
159165 uint32_t t_on = obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] + 1 ;
160166 float v = (float )t_on /(float )t_off ;
161167 obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 0 ] = (uint32_t )us - 1 ;
162- if (us > 0 ) {
168+ if (us > 0 ) { // PWM period is not 0
163169 obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] = (uint32_t )((float )us * (float )v ) - 1 ;
170+ // unhalt the counter
164171 obj -> pwm -> CTRL &= ~(1 << 2 );
165- } else {
172+ } else { // PWM period is 0
173+ // halt and clear the counter
166174 obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
175+ // output level tied to low
167176 obj -> pwm -> OUTPUT &= ~(1 << obj -> pwm_ch );
168177 }
169178}
@@ -180,11 +189,13 @@ void pwmout_pulsewidth_ms(pwmout_t* obj, int ms)
180189
181190void pwmout_pulsewidth_us (pwmout_t * obj , int us )
182191{
183- if (us > 0 ) {
192+ if (us > 0 ) { // PWM peried is not 0
184193 obj -> pwm -> MATCHREL [(obj -> pwm_ch * 2 ) + 1 ] = (uint32_t )us - 1 ;
185194 obj -> pwm -> CTRL &= ~(1 << 2 );
186- } else {
195+ } else { //PWM period is 0
196+ // halt and clear the counter
187197 obj -> pwm -> CTRL |= (1 << 2 ) | (1 << 3 );
198+ // output level tied to low
188199 obj -> pwm -> OUTPUT &= ~(1 << obj -> pwm_ch );
189200 }
190201}
0 commit comments