newbie question about code

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

hello,

another question with a flashing led... i want to be sure about my understanding of avr programming.
first of all i precise the background. i want to blink a led each second. so i use timer0 with interrupt in CTC mode with ATTiny85. a brand new attiny works at 1mhz, i choose a prescaler of 1024.
so the clock frequency falls at about 976hz.
one cycle last ~0.001024. consequently in one second we have ~4 overflows (max value of the counter is 254). am i right ?
here the code :

#define F_CPU 1000000
#include
#include
#include
volatile int cpt = 0;

int main(void)
{
DDRB = (1<<0);
prescaler of 1024
TCCR0B |= (1<<CS02) | (0<<CS01) | (1<<CS00);
//enable CTC mode
TCCR0A |= (1<<WGM01);
//max value of the counter
OCR0A = 254;
//enable interrupt on timer0
TIMSK = (1<<OCIE0A);
sei();
while(1)
{
//if we reach 4 overflows (~1 second)
if(cpt>=4){
PORTB |= (1 << PB0);
_delay_ms(10);
PORTB &= ~(1 << PB0);
cpt=0;
}
}
}

ISR(TIMER0_COMPA_vect){

cpt++;
}

in order to prevent errors with delay i avoid puting too much things into ISR(), so i put the blink part into the loop. any advices about the organisation or optimization of this code ?

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

do you think you'll see much with the led turned on for 10ms? Try toggling the led. The led will be on for 1 second, then off for one second...... much easier for a human to observe.

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

i choose 10ms because i compare the led blinking with my watch :) and it was better at 10ms for that. i tested your suggestion but i found it harder for observing the regularity of the process.

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

You should be able to make the delay up to about 240ms without interfering with the counting. because the next count will come about 250ms after the LED turns on. If the LED is only only 240ms, then no problem.

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

Use code tags when posting code.

You need to access shared multibyte variables atomically. If you're not going to use values>255, declare the shared variable uint8_t, not int.

Your "prescaler of 1024" line needs a // prefix if you want the code to compile.

Sid

Life... is a state of mind

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

ok i change my code, now i can toggle my led directly with the hardware side of the ship. so now i'm thinking about blinking two leds, but alternate. the second one blink after the first one. can i keep making this with pure hardware CTC ?
with the following code the leds blink together at the same time.

#define F_CPU 1000000
#include 
#include 
#include 

int main(void)
{
    //pin0 and pin1 output
	DDRB |= (1<<PB0);
	DDRB |= (1<<PB1);
	//PORTB |= (1 << PB1);
	//prescaler of 1024
	TCCR0B |= (1<<CS02) | (0<<CS01) | (1<<CS00);
	//enable CTC mode 
	TCCR0A |= (1<<WGM01);
	//max value of timer0
	OCR0A = 254;
	OCR0B = 254;
	//enable toggle on pb0 and pb1
	TCCR0A |= (1<<COM0A0) | (1<<COM0B0);
	
	while(1)
    {
        
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

can i keep making this with pure hardware CTC ?

Yes. OC0A is on PB0 and OC0B is on PB1.

    //pin0 and pin1 output
   DDRB |= (1<<PB0);
   DDRB |= (1<<PB1);
   PORTB = 0x01; // PB0 starts out high, and PB1 the opposite.

Note also that the Tiny85 has a complementary output feature for those same pins, if you use timer1 (OC1A and /OC1A).

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

Preset the outputs to 1 and 0. Then when the CTC maxes out the outputs will reverse to 0 and 1 and so on...

Edit:
Lee hit submit first...

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

"Preset the outputs to 1 and 0."

how do you make it ?

i tried this to initialize pin0 and pin1 :
PORTB |= (1 << PB0) | (0 << PB1);

but it doesn't do the job... leds keeps blinking at the same time.

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

Quote:

but it doesn't do the job... leds keeps blinking at the same time.

Did you do it where I showed? >>Before<< the timer takes over the pins?

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

yes :

DDRB |= (1<<PB0);
DDRB |= (1<<PB1);
PORTB |= (1 << PB0) | (0 << PB1);
//prescaler of 1024
TCCR0B |= (1<<CS02) | (0<<CS01) | (1<<CS00);
...

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

I don't know if it is going to make any difference, but it would make sense to configure the timer prior to starting it. That is, do the clock selection last.

Sid

Life... is a state of mind

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

Quote:

I don't know if it is going to make any difference, but it would make sense to configure the timer prior to starting it. That is, do the clock selection last.

You might be on to something there. The timer is started--that might cause an immediate compare match. Then the mode is selected, so the timer is changing modes. >>Then<< the COM bits are set.

I have to see if I have a board from that family handy...

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

the timer start when i choose a prescaler ?
when does timer start ?

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

Yes, the timer starts when you select a clock. Read the datasheet.

Sid

Life... is a state of mind