Forum > Embedded - AVR
Help by coding a PWM_WRITE procedure
(1/1)
pascalbythree:
https://wiki.freepascal.org/AVR_Embedded_Tutorial_-_Analog_Write
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- var i : integer; // Loop counter begin // Configure PD5 and PD6 as outputs DDRD := %01100000; // Configure PD5 and PD6 in PWM mode TCCR0A := (%10 shl COM0A) or (%10 shl COM0B) or (1 shl WGM0); // Enable timer 0, clock / 1024 TCCR0B := TCCR0B or %101; // Main loop that continuously makes the LEDs brighter and darker repeat for i := 0 to 255 do begin OCR0A := i; OCR0B := 255 - i; sleep; end; for i := 0 to 255 do begin OCR0A := 255 - i; OCR0B := i; sleep; end; until False; end.
Can somebody help me out with this procedure, i have to cat it over to this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SETPWM(Port:Char; PIN:byte; Value:integer);begin // ???end;
I Want to use PB2 to PWM output. For the Attiny2313A. Great thanks...
Its to control the FAN Speed in this project. See Photo!
ccrause:
--- Quote from: pascalbythree on September 02, 2023, 08:03:36 pm ---Can somebody help me out with this procedure, i have to cat it over to this:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SETPWM(Port:Char; PIN:byte; Value:integer);begin // ???end;
I Want to use PB2 to PWM output. For the Attiny2313A. Great thanks...
--- End quote ---
One way of doing this is to check for valid port & pin combinations, then configure the appropriate timer attached to that pin:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SETPWM(Port: Char; pin: byte; value: uint16);begin if (Port = 'B') and (pin = 2) then begin // configure timer 0, channel A TCCR0A := TCCR0A or (%10 shl COM0A) or (1 shl WGM0); TCCR0B := TCCR0B or %101; OCR0A := byte(value); end else if (Port = 'A') and (pin = 5) then begin // configure timer 0, channel B end else if (Port = 'B') and (pin = 3) then begin // configure timer 1, channel A end else if (Port = 'B') and (pin = 4) then begin // configure timer 1, channel B end else // Do nothing, or output some errorend;This is an outline of one possible method of coding the SETPWM function. This is the bare minimum functionality, more code would be required to make this robust.
While a SETPWM procedure may seem helpful, it will always generate code to configure all four PWM pins, even if you only use a single PWM pin. Also note that you will end up with some repeated code in the different if branches.
Note that timer0 is 8 bit, while timer1 is 16 bit. This means that this generic SETPWM function has to range check the value parameter depending on which timer is used. Perhaps an unsigned type for value is more useful since the timer only works with positive or zero values.
While Arduino has made this kind of abstraction popular, it comes at the cost of larger code size and slower execution because of the extra code required for the mapping. Just think about the complication of switching off PWM on a pin. Do you just disable the OCn output to the pin, but keep the timer running? Or do you check if both OCnA and OCnB are disabled before disabling the timer? All of this logic requires extra code, which is not required if you know you only use fixed PWM pins in your project.
A general recommendation on the AVRfreaks forum is that you typically use fixed pins in a project, so just configure those pins specifically and be done.
pascalbythree:
Tank you very much! it already got to work ! Now i am going to work on the speeds...
What number to change for higher PWM speeds? It seems that i need a higher speed to PWM to my mosfet driver IC. You can hear it a little in the noise of the fan.
Wait got it already, no FASTPWM is needed value: uint16 is not a byte :-) Woops :P
pascalbythree:
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- // configure timer 0, channel A TCCR0A := TCCR0A or (%10 shl COM0A) or (1 shl WGM0); TCCR0B := TCCR0B or %101; OCR0A := byte(value);
Can anybody help me out changing this code for the PB1 pin on a Atmega328P ?
To: OC1A
Greets, PascalByThree
pascalbythree:
Yay! It got to work already!
--- Code: Pascal [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} --- // Configure PD5 and PD6 in PWM mode TCCR1A := (%10 shl COM1A) or (%10 shl COM1B) or (1 shl WGM1); // Enable timer 0, clock / 1024 TCCR1B := TCCR1B or %101; OCR1A := 125;
Stil thank you...
Navigation
[0] Message Index