Microcontroller › AVR › Issue regarding waveform generation
- This topic has 1 reply, 2 voices, and was last updated 12 years, 1 month ago by
AJISH ALFRED.
Viewing 2 posts - 1 through 2 (of 2 total)
-
AuthorPosts
-
March 5, 2013 at 7:32 am #2160
lokesh tiwari
ParticipantI have used !Mhz internal clock with prescale as 64…after each 60 seconds i have to generate waveform at 4 differnt pins….with that wave i have to drive minute’s niddle of aa watch….
i am posting 2 codes here….1st one is working while 2nd is not….but the second code is more accurate….
1st code is:—
#include <avr/io.h>#define F_CPU 1000000UL#include <util/delay.h>#include <avr/interrupt.h>int counter;void timer_init(){TCCR1B |= (1 << WGM12)|(1 << CS10)|(1 << CS11);TIMSK |= (1 << OCIE1A);TCNT1 = 0;}void timer_set(){OCR1A = 15624;sei();}ISR(TIMER1_COMPA_vect){counter++;if(counter == 60){counter=0;cli();PORTD = 0x80;_delay_ms(125);PORTD = 0x00;_delay_ms(125);PORTC=0x01;_delay_ms(125);PORTC = 0x00;_delay_ms(125);PORTC=0x02;_delay_ms(125);PORTC = 0x00;_delay_ms(125);PORTC=0x80;_delay_ms(125);PORTC = 0x00;TCNT1=0;counter=1;sei();}}int main(){DDRC = 0xFF;DDRD = 0xFF;timer_init();timer_set();while(1);}2nd code is
#include <avr/io.h>#define F_CPU 1000000UL#include <util/delay.h>#include <avr/interrupt.h>int counter,flag;void timer_init(){TCCR1B |= (1 << WGM12)|(1 << CS10)|(1 << CS11);TIMSK |= (1 << OCIE1A);TCNT1 = 0;}void timer_set(){OCR1A = 15624;sei();}ISR(TIMER1_COMPA_vect){counter++;if(counter == 60){counter=0;flag=1;}elsecounter++;}int main(){DDRC = 0xFF;DDRD = 0xFF;timer_init();timer_set();while(1){while(flag){PORTD = 0x80;_delay_ms(125);PORTD = 0x00;_delay_ms(125);PORTC=0x01;_delay_ms(125);PORTC = 0x00;_delay_ms(125);PORTC=0x02;_delay_ms(125);PORTC = 0x00;_delay_ms(125);PORTC=0x80;_delay_ms(125);PORTC = 0x00;}}}The only differnece is in the ISR…..but 2nd code is compiling well,generating Hex code also but does not provide the waveform at particular pins as mentiones……both codes are 99% same……March 5, 2013 at 4:19 pm #9249AJISH ALFRED
ParticipantHi Lokesh,
Try these modifications.
#include <avr/io.h>#define F_CPU 1000000UL#include <util/delay.h>#include <avr/interrupt.h>//int counter,flag;int counter = 0, flag = 0; //modificationvoid timer_init(){TCCR1B |= (1 << WGM12)|(1 << CS10)|(1 << CS11);TIMSK |= (1 << OCIE1A);TCNT1 = 0;}void timer_set(){OCR1A = 15624;sei();}ISR(TIMER1_COMPA_vect){//counter++; //modificationif(counter == 60){counter=0;flag=1;}elsecounter++;}int main(){DDRC = 0xFF;DDRD = 0xFF;timer_init();timer_set();while(1){if(flag) //modification{PORTD = 0x80;_delay_ms(125);PORTD = 0x00;_delay_ms(125);PORTC=0x01;_delay_ms(125);PORTC = 0x00;_delay_ms(125);PORTC=0x02;_delay_ms(125);PORTC = 0x00;_delay_ms(125);PORTC=0x80;_delay_ms(125);PORTC = 0x00;_delay_ms(125); //modificationflag = 0; //modification}}} -
AuthorPosts
Viewing 2 posts - 1 through 2 (of 2 total)
- You must be logged in to reply to this topic.