EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Topics / Issue regarding waveform generation

Issue regarding waveform generation

|

Microcontroller › AVR › Issue regarding waveform generation

  • This topic has 1 reply, 2 voices, and was last updated 12 years, 9 months ago by AJISH ALFRED.
Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • March 5, 2013 at 7:32 am #2160
    lokesh tiwari
    Participant

    I 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;
    }
    else
    counter++;
    }
     
    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 #9249
    AJISH ALFRED
    Participant

    Hi 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;                               //modification
    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++;                                                //modification
    if(counter == 60)
    {
    counter=0;
    flag=1;
    }
    else
    counter++;
    }
     
    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);             //modification
    flag = 0;                          //modification
    }
    }
    }
  • Author
    Posts
Viewing 2 posts - 1 through 2 (of 2 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • mechanism to shutdown feeding when sensor temperature rises December 13, 2025
  • Droplet1 December 13, 2025
  • What is involved to convert a small town to fiber optic? December 13, 2025
  • Oshonsoft MSSP simulation question December 13, 2025
  • Measuring controller current output with a meter December 13, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise