avr - Why I am always getting Zero PWM output? -


i want output 2 different analog values 10 bit resolution i.e. dac_value ranging 0-1023. using atmega16 external crystal 4mhz. have tried connecting rc filter @ output nothing changed. getting 0 output, can ??

#include <avr/io.h> #include <avr/interrupt.h>  void initpwm() {   tccr1a |= (1<<wgm11) | (1<<wgm10) | (1<<com1a1) | (1<<com1a0) | (1<<com1b1) | (1<<com1b0) ;   tccr1b |= (1<<wgm12) | (1<<cs10); }       uint16_t dac_value1 = 100, dac_value2 = 200; int main(void) {     initpwm();       while(1) {       ocr1a = dac_value1;       ocr1b = dac_value2;           }      (;;) {}   } 

you assigning wrong bits wrong registers. clarify: pwm not analog output. changed high or low output state. pwm value determines how long output in each state (high or low) withing timer period.

if want make "analog" output, need filter output signal, example, passing thru rc-filter, need make output fast possible, that's mean need select lower prescaler, , choose fast-pwm mode. in current configuration, pwm 1024 prescaler. i.e. less 4 periods of timer per second.

so, if assign fast pwm prescaler 1 (which give 3906 hz output) inverted output (i.e. higher ocr1x value leads lower output value) this:

void initpwm() {     tccr1a = (1<<wgm11) | (1<<wgm10) | (1<<com1a1) | (1<<com1a0) | (1<<com1b1) | (1<<com1b0);      // here bits wgm10 wgm11 (with wgm12 in tccr1b) select fast pwm mode 10 bit resolution;      // com1a1 com1a0 com1b1 com1b0 select inverted pwm output on pins oc1a oc1b     tccr1b = (1<<wgm12) | (1<<cs10);     // cs10 select  1:1 prescaler (i.e. 4000000/1024 timer periods per second)      // also, pwm output not visible on corresponding pins, unless configure ddr bits these pins output. pins pd5 , pd4 on atmega16     ddrd |= (1 << 4) | (1 << 5); } 

next thing need consider: when application main() function execution reaches it's end, jump reset vector. so, put empty loop end of main():

int main(void) {     uint16_t dac_value1, dac_value2;     dac_value1 = 123; // not forget init variables!!!     dac_value2 = 987;      initpwm();     ocr1a = dac_value1;     ocr1b = dac_value2;     for(;;) {         // main loop. it's empty     }  } 

Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -