+ Post New Thread
Results 1 to 4 of 4
  1. #1
    Newbie level 3
    Points: 356, Level: 4

    Join Date
    Jan 2011
    Location
    Khulna,Bangladesh
    Posts
    3
    Helped
    0 / 0
    Points
    356
    Level
    4

    Clear Timer on Compare Match(CTC) Mode problem

    I have faced problems in working with CTC in atmega. I can not produce interrupt after 1 second using CTC. I observed that the value of OCR1AH is not compared with with TCNT1H .What can be done?
    Suggest needed.My code is following........

    #include <built_in.h>

    int a=0;

    void Timer1compa_ISR() org IVT_ADDR_TIMER1_COMPA
    {
    PORTB=~PORTB;
    PORTD=0xFF;
    }


    void main()
    {
    int i;
    DDRB=0xFF;
    DDRD=0xFF;
    DDRC=0xFF;

    SREG_I_bit=1;

    TIMSK |= (1<<OCIE1A);

    TCNT1L=0;
    TCNT1H=0
    i=4870;
    OCR1AL=Lo(i);
    OCR1AH=Hi(i);
    TCCR1B |= (1<<WGM12);

    TCCR1B=0x0D; /*start timer with presscaler of clk/1024*/
    /*Timer/Counter1, Output Compare A Match Interrupt Enable*/


    while(1)
    {
    PORTB=0x0F;
    }
    }

    •   Alt14th July 2011, 19:49

      advertising

        
       

  2. #2
    Super Moderator
    Points: 43,813, Level: 51
    Achievements:
    Created Album picturesCreated Blog entry7 years registered
    alexan_e's Avatar
    Join Date
    Mar 2008
    Location
    Greece
    Posts
    11,032
    Helped
    2016 / 2016
    Points
    43,813
    Level
    51
    Blog Entries
    6

    Re: Clear Timer on Compare Match(CTC) Mode problem

    You are missing

    // timer1 compare match A ISR on
    TIMSK=0x10;

    I also don't know why you use TCCR1B |= (1<<WGM12); when in the next line you write a new value TCCR1B=0x0D;

    Alex

    P.S. actually the TIMSK exists in your code.
    I an not familiar with your compiler but maybe the value is the problem, can you use OCR1A=i ?
    There should also be somewhere a global enable interrupt (for example it is sei() in avrgcc)
    Please don't make requests for help in private using PM. Create a thread in the forum so that other members can benefit from the posted answers.

    Consider reading this before posting : How To Ask Questions The Smart Way



    •   Alt14th July 2011, 21:43

      advertising

        
       

  3. #3
    Newbie level 3
    Points: 356, Level: 4

    Join Date
    Jan 2011
    Location
    Khulna,Bangladesh
    Posts
    3
    Helped
    0 / 0
    Points
    356
    Level
    4

    Re: Clear Timer on Compare Match(CTC) Mode problem

    Here i have a problem like i am using mikroC as compiler , in it the following code is not accepted ....
    OCR1A=i;
    when i declare such as......
    OCR1AL=i;
    then it is accepted but it is only possible when ( i<255 )
    if i>255
    then i have to declare it like
    OCR1AL=Lo(i);
    OCR1AH=Hi(i);
    but in this case the OCR1AH value is not compared with the value of TCNT1H only the value of OCR1L value is compared as a result i could not able to compare a large time.
    I can not understand actually what i have to do. Help needed..



    •   Alt15th July 2011, 08:11

      advertising

        
       

  4. #4
    Newbie level 3
    Points: 356, Level: 4

    Join Date
    Jan 2011
    Location
    Khulna,Bangladesh
    Posts
    3
    Helped
    0 / 0
    Points
    356
    Level
    4

    CTC problem in atmega with mikroC compiler

    I need a normal code of Timer1 CTC for atmega in mikroC. I have made a simple code but i can not understand where is my error i can not compare time when i>255.
    Help needed.
    #include<built_in.h>

    unsigned int i;

    void inerrupt_ISP() org IVT_ADDR_TIMER1_COMPA
    {
    PORTB = ~PORTB ;
    }

    void square_wave_init(void)
    {
    DDRB = 0xFF;
    TIMSK |= (1<<OCIE1A);
    TCNT1L=0;
    i=4870;
    OCR1AL = i;
    OCR1AH=Hi(i);
    TCCR1B |= (1<<WGM12);
    TCCR1B |= (1<<CS10) | (1<<CS12);
    //TCCR1A=0b01010000;
    }
    void main()
    {
    square_wave_init();
    SREG_I_bit=1;
    while(1){ ;
    }

    }
    Last edited by Razu Ahmmed; 15th July 2011 at 22:51.



+ Post New Thread
Please login