I need some help getting timer1 working on atmega168

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I am trying to set up a CTC timer to blink an led for 1 second on pin PC0 but I cant get it to work. The led wont blink at all.  Any help is much appreciated, thanks

 

#define F_CPU 8000000UL
#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>

void timer1_init()
{
    TCCR1B |= (1 << WGM12)|(1 << CS12);    //sets up timer with prescaler 256 and CTC mode
    TCNT1 = 0;                            //initialize counter
    OCR1A = 15623;                        //initialize compare value
    TIMSK1 |= (1 << OCIE1A);            //enable compare interrupt
}

ISR(TIMER1_COMPA_vect)                    //Interrupt service routine
{
    PORTC ^= (1 << 0);                    //Toggle led
}

int main(void)
{
    sei();
    DDRC |= (1 << 0);
    
    timer1_init();                        //Initialize timer 1
    
    while(1)
    {
        
    }    
    
}

 

This topic has a solution.
Last Edited: Fri. Oct 16, 2020 - 07:50 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok,

Whats the problem?

How do you know this?

What is the blink rate of the LED supposed to be?

What is the blink rate you are seeing?

Are you using the internal RC clock or an external crystal?

Do you have the DIV8 Fuse programmed?  If yes then the internal clock is 8 times SLOWER than your Crystal/RC oscillator.

 

Jim

 

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't know where you get your values in your code from, but here is what I get from the CV wizard:

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 31.250 kHz
// Mode: CTC top=OCR1A
// OC1A output: Toggle on compare match
// OC1B output: Disconnected
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer Period: 1 s
// Output Pulse(s):
// OC1A Period: 2 s Width: 1 s
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=(0<<COM1A1) | (1<<COM1A0) | (0<<COM1B1) | (0<<COM1B0) | (0<<WGM11) | (0<<WGM10);
TCCR1B=(0<<ICNC1) | (0<<ICES1) | (0<<WGM13) | (1<<WGM12) | (1<<CS12) | (0<<CS11) | (0<<CS10);
TCNT1H=0x0B;
TCNT1L=0xDC;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x7A;
OCR1AL=0x11;
OCR1BH=0x00;
OCR1BL=0x00;

// Timer/Counter 1 Interrupt(s) initialization
TIMSK1=(0<<ICIE1) | (0<<OCIE1B) | (1<<OCIE1A) | (0<<TOIE1);

My code uses the OCRA output to toggle so you would only have to reload the base count in the ISR.

 

Untested, but eh Wizard is pretty good/excellent.

 

Jim

I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie

 

"The critical shortage here is not stuff, but time." - Johan Ekdahl

 

"Step N is required before you can do step N+1!" - ka7ehk

 

"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman

"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?"  - Lee "theusch"

 

Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-)  - Source Unknown

Please Read: Code-of-Conduct

Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1
TCCR1B |= (1 << WGM12)|(1 << CS12);    //sets up timer with prescaler 256 and CTC mode

Why are you oring your register, rather than setting it up to the desired value? (using  =).   Ask yourself what values you are doing the oring with?    Do you know? You probably know, but using =  avoids any  possible confusion, since it  is explicit. TCCR requirequires  all the bits to be set properly, not just oring 2  of the bits with who knows what (even though you probably know).   Why didn't you do OCR1A |= 15623;   hmmm...reasons reasons.

Who told you to use or in the first place? 

 

Does the led light up when you just simply set the pin high? Does it go off when you set the pin low?  Did you bother checking?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
int main(void)
{
    sei();

sei() is soemthing you only do when you have everything else "ready to go". So you set up all your interrupt sources and configure your peripherals and then you SEI. It's not your problem at the moment but as you have it you start to set timer registers while I is active. Luckily you left the setting of the OCIE bit to the last action.

 

BTW as it is a 168 did you know that instead of:

ISR(TIMER1_COMPA_vect)                    //Interrupt service routine
{
	PORTC ^= (1 << 0);                    //Toggle led
}

you could use:

ISR(TIMER1_COMPA_vect)                    //Interrupt service routine
{
	PINC = (1 << 0);                    //Toggle led
}

because the 168 was one of the first AVRs to support the "toggle on PIN write" feature?

 

Anyway I put your code in the simulator and it hit a breakpoint in the ISR after 3,999,786 cycles so that would be about 4 seconds if the 168 was running at the default 1MHz. To complete a whole "flash cycle" the LED would be on for 4s and off for 4s so it would have an 8s period (0.125Hz).

 

(running 4 million cycles in the AS7 simulator takes a LONG(!) time)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So it must have been a hardware issue because today I did not change any code, I powered up the controller and now it is flashing at 4 seconds like you had said. Before Nothing was happening at all. 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

So it must have been a hardware issue because today I did not change any code,

Now you allow yourself to fall into the software flytrap, where you will be crushed and eaten.  So in a few months all of your washing machines, electronic locks, or whatever uses this code will need recalled due to a mysterious "problem".

How do you know you don't have a random initialization problem or improper condition of some sort. Always check for such things before quickly dismissing a sign of something wrong.  Being quickly dismissive is the flytrap's secret weapon. 

  

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Spoken like someone who has fallen for it many times!