News:

;) This forum is the property of Proton software developers

Main Menu

PWM feedback and variable frequency

Started by Abdullah, Aug 31, 2025, 06:04 PM

Previous topic - Next topic

Stephen Moss

#60
Quote from: Abdullah on Nov 05, 2025, 09:18 AMOne reason for the difficulty is that I don't understand English well, so I find it a bit hard to read the datasheet
Then you will find it very difficult to do anything, can you not use a translator on it?

Quote from: Abdullah on Nov 05, 2025, 01:55 PMThank you very much Sir I expect it will take me about a week to understand it, but once i do, I'll try to create different things with it
That is the problem with being asking for and just being given code rather then learning how to do it yourself - you don't understand how it works. This may help....
Timer 1 value increments every 1 microsecond (1us)
1 second / 50Hz = 20 Milliseconds (20ms) per cycle
Duty = 0 to 255
20mS / 255 = 78uS

wTMR1 = 65536 - 78Load Timer1 with a value that causes an interrupt to happen after 78mS (one 255th of 20mS), the code then jumps to the Interrupt handler (ISR)

In the interrupt handler the Timer interrupt flag is cleared
TMR1IF = 0 otherwise you will not get another interrupt.

wTMR1 = 65536 - 63Reloads Timer1 so that it triggers another interrupts (value roll over from 65525 to 0) again in another 78us - we subtract 63 instead of the initial 78 to compensate for execution time of the code in the ISR.

The pin used for the PWM output is set high (5V).
The count is incremented
The count is compared to the Duty value read by the ADC
When the count is greater then the duty the PWM output goes Low (0V)
When the count = 255 (end of one 20mS cycle) the count is reset to 0 and we begin again

Context Restore Exit the ISR and return to the main code (reading the ADC)

Pepe

demo proteus for Abdullah