Don't ad-block us - support your favorite websites. We have safe, unobstrusive, robotics related ads that you actually want to see - see here for more.
0 Members and 1 Guest are viewing this topic.
#include <avr/io.h>int main(){ DDRD |= 1 << PD6; // Set PD6(OC0A) Output TCCR0A = 0b10000011; // Compare Match Low // 8 bit High Speed PWM TCCR0B = 0b00000101; // clk/1024 125nsX1024=128us OCR0A = 128; // Set Output Compare Register while (1) { asm("sleep"::); } return 0;}
you need higher frequency. if you calculated it at 250 ms per phase (500 ms cycle) , that's 2 hertz.please explain the rationale behind this comment: clk/1024 125nsX1024=128us/ means division, not multiplication. you divide your main (crystal, osc, external, internal, whatever) clock by 1024 and that gives you the frequency of the output. quick fix: decrease prescaler.
How can I slow down the prescaler? What register should I be modifying/looking at?
or you can use the pwm.h and pwm.c source code for AVRlib
#include <avr/io.h>#include <avr/interrupt.h>#include <avr/pgmspace.h>#include <conf/global.h>#include "timer.h"#include "timer.c"int main(void){ timer1PWMInit(10); timer1PWMAOn(); timer1PWMASet(512); //Set the PWM to 50%. 10 bit, so 0-1023, 50% = 512 while(1){ asm("nop"); } return 0;}
**** Build of configuration Release for project avrlibctest ****make all Building file: ../main.cInvoking: AVR Compileravr-gcc -I/usr/avrlib -Wall -Os -fpack-struct -fshort-enums -funsigned-char -funsigned-bitfields -mmcu=atmega168 -DF_CPU=1000000UL -MMD -MP -MF"main.d" -MT"main.d" -c -o"main.o" "../main.c"In file included from ../main.c:11:/usr/avrlib/conf/global.h:35:1: warning: "F_CPU" redefined<command-line>: warning: this is the location of the previous definitionFinished building: ../main.c Building target: avrlibctest.elfInvoking: AVR C Linkeravr-gcc -Wl,-Map,avrlibctest.map -mmcu=atmega168 -o"avrlibctest.elf" ./main.o ./main.o: In function `main':main.c:(.text+0xa): undefined reference to `timer1PWMInit'main.c:(.text+0xe): undefined reference to `timer1PWMAOn'main.c:(.text+0x16): undefined reference to `timer1PWMASet'make: *** [avrlibctest.elf] Error 1
**** Build of configuration Release for project avrlibctest ****make all Building file: ../main.cInvoking: AVR Compileravr-gcc -I/usr/avrlib -Wall -Os -fpack-struct -fshort-enums -funsigned-char -funsigned-bitfields -mmcu=atmega168 -DF_CPU=1000000UL -MMD -MP -MF"main.d" -MT"main.d" -c -o"main.o" "../main.c"In file included from ../main.c:11:/usr/avrlib/conf/global.h:35:1: warning: "F_CPU" redefined<command-line>: warning: this is the location of the previous definitionIn file included from ../main.c:13:/usr/avrlib/timer.c: In function ‘timer0Init’:/usr/avrlib/timer.c:98: error: ‘TIMSK’ undeclared (first use in this function)/usr/avrlib/timer.c:98: error: (Each undeclared identifier is reported only once/usr/avrlib/timer.c:98: error: for each function it appears in.)/usr/avrlib/timer.c: In function ‘timer1Init’:/usr/avrlib/timer.c:109: error: ‘TIMSK’ undeclared (first use in this function)/usr/avrlib/timer.c: In function ‘timer2Init’:/usr/avrlib/timer.c:118: error: ‘TIMSK’ undeclared (first use in this function)/usr/avrlib/timer.c: In function ‘timer0SetPrescaler’:/usr/avrlib/timer.c:127: error: ‘TCCR0’ undeclared (first use in this function)/usr/avrlib/timer.c: In function ‘timer2SetPrescaler’:/usr/avrlib/timer.c:140: error: ‘TCCR2’ undeclared (first use in this function)/usr/avrlib/timer.c: In function ‘timer0GetPrescaler’:/usr/avrlib/timer.c:147: error: ‘TCCR0’ undeclared (first use in this function)/usr/avrlib/timer.c: In function ‘timer2GetPrescaler’:/usr/avrlib/timer.c:163: error: ‘TCCR2’ undeclared (first use in this function)/usr/avrlib/timer.c: At top level:/usr/avrlib/timer.c:464: warning: ‘SIG_OUTPUT_COMPARE2’ appears to be a misspelled signal handlermake: *** [main.o] Error 1
here is PWM.c http://www.koders.com/c/fidFCE5C25781F27ABEDFC8DA53F7B2BF292124161D.aspxan here is PWM.hhttp://www.koders.com/c/fid06E27846D356B2DCE071D003D53635AA6776D9DA.aspx