PWM Timer0  Ver.1.0.
Felix S.
GccApplication1.cpp
Go to the documentation of this file.
1 
23 #define F_CPU 16000000UL
24 #include <avr/io.h>
25 #include <stdlib.h>
26 #include <avr/interrupt.h>
27 #include <util/delay.h>
28 extern "C" {
29  #include "lcd-routines.h"
30 }
31 
32 #define DEBUGCYCLETIME 5000 //In ms
33 
34 void setup(); //Setup IO Registers
35 void LCD_WRITE(const char buffer[16]); //Write in the first Line of the LCD Display from left to right
36 
37 int main(void)
38 {
39  //LCD_WRITE("Started");
40  //_delay_ms(3000); //Time for reading the Message
41 
42  setup();
43 
44  while (1)
45  {
46  //do nothing
47  volatile int i; i++; //dummy
48  }
49 }
50 
51 void setup()
52 {
53  //LCD_WRITE("Setup");
54  //_delay_ms(1); //Time for reading the Message
55 
56  DDRB = 0;
57  PORTB = 0;
58  DDRC = 0;
59  PORTC = 0;
60  DDRD = _BV(6); //Led pin as Output
61  PORTD = 0;
62 
63  //setup LCD
64  //lcd_init();
65  // LCD Pins are defined in lcd-routines.h
66 
67  //setup PWM Module
68  TCCR0A |= _BV(WGM01) | _BV(WGM00); TCCR0B |= _BV(WGM02);//Fast PWM
69  TCCR0A |= _BV(COM0A1); //Non-Inverting mode
70  TCCR0B |= _BV(CS01); //Prescaler
71  TIMSK0 |= _BV(OCIE0A)| _BV(TOIE0); //just for fun
72 
73 
74 
75  //setup ADC:
76 
77  ADCSRA |=_BV(ADEN); //enable ADC
78  ADMUX |= _BV(REFS0) | _BV(ADLAR); //Reverence Voltage
79  ADCSRA |= _BV(ADPS2); //Prescaler & ENABLE
80  ADCSRA |= _BV(ADIE); //Enable conversion finnished Interrupt
81  //ADCSRA |= _BV(ADATE); //free running mode
82 
83  //start PWM Module
84  ADCSRA |= _BV(ADSC); //start first Conversion
85  sei();
86 }
87 
88 ISR(ADC_vect)
89 {
90  OCR0A = ADCH; //set value in PWM Module
91  OCR0A = 55; // simulation only
92  //LCD_WRITE("ADC_ISR");
93 
94  //_delay_ms(DEBUGCYCLETIME);
95  ADCSRA |= _BV(ADSC); //start next Conversion
96  sei();
97 }
98 
99 void LCD_WRITE(const char buffer[16])
100 {
101  lcd_clear();
102  lcd_home();
103  lcd_string(buffer);
104 }
105 ISR(TIMER0_COMPA_vect)
106 {
107  volatile int i; i++; //dummy
108 
109 }
110 ISR(TIMER0_OVF_vect)
111 {
112  volatile int i; i++; //dummy
113 
114 }