Menu

Adskeeper - header widge

728x90

468x60

Navbar

Monday, June 8, 2020

Using ADC of ATMega32 to adjust PWM duty cycle

In this module we have discuss about using ADC with some typical applications. Using a POT we can adjust the analog value fed to the ADC input pin manually and linearly. With this advantage we can use the ADC value to adjust the duty cycle of PWM signal.

In this example, I use ADC to vary the PWM duty cycle between 0 to 100%. The ADC value is scaled from 1024 to 100 to fit the PWM timing. PWM signal is created by timer 0 interrupt. Each interrupt occurs for every 10 micro seconds.

Using ADC of ATMega32 to adjust PWM duty cycle

Schematic diagram

Source code:

  1. #include <avr/io.h>
  2. #include "avr/interrupt.h"
  3. unsigned char cnt=0;
  4. void speedControl(unsigned int speed){
  5. float _dutyCycle;
  6. unsigned int dutyCycle;
  7. _dutyCycle=speed*(100.0/1024);
  8. //dutyCycle=20;
  9. dutyCycle=10+(int)_dutyCycle;
  10. if(cnt<dutyCycle) PORTD|=(1<<0);
  11. if(cnt>=dutyCycle) PORTD&=~(1<<0);
  12. if(cnt>=100) cnt=0;
  13. }
  14. int main(void)
  15. { unsigned int adcResult;
  16. //PortD as output
  17. DDRD=0xFF;
  18. //PA0 or ADC0 as an analog input
  19. DDRA=0;
  20. //Set 1:8 pre-scaler
  21. TCCR0=0x02;
  22. //Clear overflow flag
  23. TIFR=0x01;
  24. //Enable Timer 0 interrupt
  25. TIMSK=0x01;
  26. //set global interrupt
  27. sei();
  28. //Turn on the ADC module
  29. ADCSRA|=(1<<ADEN);
  30. /*Select ADC7*/
  31. ADMUX|=0x07;
  32. while (1)
  33. {
  34. //Start the conversion
  35. ADCSRA|=(1<<ADSC);
  36. //Wait for the completion
  37. while((ADCSRA&(1<<ADSC))==1); //Read the result
  38. adcResult=ADCL+(ADCH<<8);
  39. speedControl(adcResult);
  40. }
  41. }
  42. ISR(TIMER0_OVF_vect){
  43. //Load -5 to make 10 uS interrupt time
  44. TCNT0=-5;
  45. cnt+=1;
  46. TIFR=0x01;
  47. }
  48.  


Click here to download its source file. Back to main tutorial page ATMega32 tutorials in C with Atmel Studio 7.

No comments:

Post a Comment

Adskeep- Standard Ads

336x280

Search This Blog

Labels

23K256 (1) 24C16B (2) 25AA010A (2) 8051 (7) 93AA46B (1) ADC (34) Analog Comparator (1) Arduino (16) ARM (13) AT89C52 (7) ATMega32 (58) ATMega644P (27) AVR (87) AVR PCB (2) Bootloader (1) CCS PICC (31) Change Notify Interrupt (CNI) (2) DAC (2) DHT11 (4) Display (135) Distance Sensor (3) dot matrix (1) ds1307 (10) DS18B20 (5) ds3231 (1) ds3232 (2) dsPIC (5) dsPIC30F1010 (3) dsPIC30F2010 (5) EEPROM (5) Environment Sensor (5) ESP32 (1) esp8266 (1) Graphical LCD (2) I2C (38) ILI9341 (1) Input/Output (76) Interrupt (22) IoT PCB (1) Keil (5) Keypad (16) KS0108 (2) LCD (75) LM35 (3) Master/Slave (2) MAX7221 (1) MCP23017 (8) MCP23S17 (7) MCP4921 (1) MCP4922 (2) Meter (3) MikroC (2) Module PCB (1) Motor (15) MPLABX (73) Nokia 5110 LCD (4) OLED (2) One-Wire (7) Oscillator (8) PCB (12) PCD8544 (3) PCF8574 (10) PIC (108) PIC12F (3) PIC16 (105) PIC16F628A (3) PIC16F630 (2) PIC16F716 (4) PIC16F818 (11) PIC16F818/819 (3) PIC16F84A (16) PIC16F876A (2) PIC16F877A (9) PIC16F88 (2) PIC16F887 (60) PIC18 (19) PIC18F1220 (5) PIC18F2550 (5) PIC18F4550 (12) PICKit2 (1) PICMicro PCB (4) Pin Change Interrupt (1) PWM (12) RTC (12) SBN0064G (1) Sensor (13) SH1106 (3) Shift Register (13) Shift Registers (12) SN74HC595N (2) Software TWI (2) SPI (39) ST7735 (1) STM32 (11) STM32 Black Pill (1) STM32 Blue Pill (12) STM32 HAL (5) STM32CubeIDE (13) STM32F103C8T6 (9) STM32F401CCU6 (1) SysTick (4) temperature sensor (13) TFT (1) TG12864A (1) Thermometer (22) Timer/Counter (32) TM1637 (2) twi (10) UART (8) Ultrasonic (4) Voltmeter (7) WDT (1) XC16 (2) XC8 (96)