Newbie alert: trying to blink led using attiny13 with CTC

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

Hi all,

I am just starting with the avr processors and I am trying to create a little project in which I let a led blink at a fixed frequency. The code is inspired on the tutorial from maxEmbedded.

Somehow I am not able let the led blink and I am at a loss.... (to be honest I have some C programming experience, mostly Objective-C for Mac and iOS)

This is the code I am trying at the moment, with the led connected to port PB1 (pin 6):

#ifndef F_CPU
#define F_CPU 1200000UL
#endif

#include 
#include 

// TIMER0 -- Only one in attiny13
void setup_timer(void) {
    // Set up the timer 
    // Mode: CTC
    TCCR0A |= (1 << WGM01);
    // Prescaler: 1024
    TCCR0A |= (1 << CS00) | (1 << CS02);

    // Clear OC0A on compare match
    //TCCR0A |= (1 << COM0A0) | (1 << COM0A1);;

    // Initialize the count
    TCNT0 = 0;

    // Initialize the compare value
    OCR0A = 116;

    // Enable compare interrupt
    TIMSK0 |= (1 << OCIE0A);

    // Enable global interrupts
    sei();
}

// Fire this ISR whenever a match occurs
ISR (TIM0_COMPA_vect) {
    // Toggle our port
    PORTB ^= (1 << PB1);
}

int main(void) {
    // Connect the LED to pin PB0
    DDRB |= (1 << PB1);

    // Initialize the timer
    setup_timer();

    // Loop forever
    for (;;) {
    }
}
This topic has a solution.

It's hard to return to basics when you always had everything available

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

If your eyes can see an LED flashing at 100.16Hz you have far better eyesight than I do!!

12MHz/1024/117 = 100.16025641

8bit timers are never great (even at /1024) for getting "visible" flashing frequencies and especially with fast CPU speeds like 12MHz.

Either try 1MHz and possibly a higher OCR or switch to a 16 bit timer.

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

Oops, I thought I was running the tiny at 1.2MHz, so I should get a 10Hz blink

It's hard to return to basics when you always had everything available

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

Oh apologies. I misread the number of zeros. In which case divide my figures by 10. So if it switches at 10.016Hz and it takes two switches to complete a cycle it should be flashing at about 5Hz which is quite visible.

In that case I don't know what's wrong - did you get any warning from the build - maybe the _vect name is wrong?

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

Quote:
with the led connected to port PB1
And a series resistor I hope!

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Fuses set correctly?

 

In consideration of others, please RTFM!

Last Edited: Thu. Aug 28, 2014 - 09:26 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@clawson - To be honest, no. The project builds without any errors or warnings. The complete code would be around 108 bytes. The fuses are default.

@js - as it should ;)

@jpmargis - The fuses are default, lfuse 0x6a hfuse 0xff

It's hard to return to basics when you always had everything available

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

I would suggest first using _delay_ms() to blink your LED. This will confirm that you have your LED hooked up correctly, are configuring and driving the correct pin, and that your AVR is running at the frequency you think it is (if your F_CPU number is the correct frequency, your delays will be what you set them to be, otherwise they'll be off). So something like

#define F_CPU 1200000
// your includes, including avr/delay.h

int main(void)
{
  DDRB |= 2; // PB1
  while (1)
  {
    _delay_ms(100);  // 200ms/blink, 5Hz
    PORTB ^= 2;  // PB1
  }
}

If this all works, then try switching over to a timer. You could even switch over to a timer but just poll the compare flag and clear it in the main loop (no interrupt), to confirm that your timer is working correctly. Then the last step is to set up a timer interrupt and get your interrupt and ISR working.

The point is, a simple bit of code to test each bit of your hardware and software in turn, from LED to timer to interrupt/ISR. Makes it easier to figure out what's wrong when the LED just sits there being defiant.

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

I'll try it again tomorrow.
The simple test (with a simple delay works)

It's hard to return to basics when you always had everything available

This reply has been marked as the solution. 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

TCCR0A |= (1 << CS00) | (1 << CS02);
TCCR0B is for clock select

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

vk0vk0 wrote:
TCCR0A |= (1 << CS00) | (1 << CS02);
TCCR0B is for clock select

If I understand your comment correct, you mean I choose the wrong register.... :cry:

The correct code snippet should be (for a start...)

void setup_timer(void) { 
    // Set up the timer 
    // Mode: CTC 
    TCCR0A |= (1 << WGM01); 
    // Prescaler: 1024
    // BEWARE: Clock Select bits are in register TCCR0B, ***NOT*** TCCR0A
    TCCR0B |= (1 << CS00) | (1 << CS02); 

Unfortunately I don't have my programmer here at hand (@work right now), so I can only test this tonight.

I'll post back

Thanks for the info/remark

(/me feels stupid [RTFM])

It's hard to return to basics when you always had everything available

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

Quote:

Unfortunately I don't have my programmer here at hand (@work right now), so I can only test this tonight.

Remember you can always test things like this in the simulator in AS6 - no need for a real AVR just to test the concept.

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

Just installed AS6 in VirtualBox (I have only Macs @work and @home)
Seems TCNT0 isn't incrementing when I step through the program
(I changed my register for the prescaler to TCCR0B, still feel dumb about it)

It's hard to return to basics when you always had everything available

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

Quote:

Seems TCNT0 isn't incrementing when I step through the program

Assuming you got the right register you are using /1024. so it's going to take 1,000 opcode cycles in the foreground code before you see an increment in the timer. That could well be a LOT of single stepping in:

    // Loop forever
    for (;;) {
    }

That's almost certainly compiled as a single RJMP and I seem to remember RJMP takes 2 cycles. So you are going to have to step that 512 times before you see the TCNT increment just once.

Try letting it "free run" for a bit then hit the "||" toolbar icon to break/pause. The "Processor" tab will tell you how many cycles have executed while it was running (it does about 50,000 per second). You should see some TCNT activity by then.

The other approach, just while simulating/stepping, is to change the /1024 to be /1 so you see the TCNT increase at the same rate as CPU cycles. In theory you'd then see it going up in 2's each time you single step.

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

@clawson - Thank you for the info (again)

Too bad no 'thank you' button is available, otherwise I would have given you all big "thank you's"

It's hard to return to basics when you always had everything available

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

Quote:

Too bad no 'thank you' button is available, otherwise I would have given you all big "thank you's"

See here:

http://beta.avrfreaks.net/forums...

When we move to that site each post has "stars" - you can give a particular reply from 1 to 5 stars.

(I've never been very clear what's actually achieved by setting such stars but I guess the poster who gets the stars can at least feel their ego has been massaged a bit? ;-))

PS if tempted to use that new site take heed of:

Quote:
NOTE: Please DO NOT use this site for real, active discussions – continue to use www.avrfreaks.net. All posts made here will be removed before this site is launched

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

Quote:

I've never been very clear what's actually achieved by setting such stars

It could be the first step towards a real meritocracy. (OTOH, there is always the danger that the first step will also be the only one.) As in "this user has a rating of N for his replies".

Happy 75th anniversary to one of the best movies ever made! Rick Blane [Bogart]: "Of all the gin joints, in all the towns, in all the world, she walks into mine."

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

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

A huge "THANK YOU" to you all!

Success!

[/me feels so happy, like a child with an ice-cream :lol: ]

It's hard to return to basics when you always had everything available