problem with code.

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

I am having a slight problem with the code I have to drive a basic binary clock with an atmega168. the problem I have is the variables that contain the seconds minutes and hours. don't seem to be updating to the ports, out side of the interrupt loop. or at least that was what I thought initially, if i set the variables during initiation to different values, those will update to the ports during the loop to drive the LEDs, so I must be doing something wrong with incrementing the variables inside the interrupt. can some one please give it a quick look over and give me some insight to what i am doing wrong.

#include 
#include 
#include 
#include 
//global variables... maybe?
int sec=0;
int min=0;
int hour=0;

ISR(TIMER1_COMPA_vect)
{
	if(++sec == 60)
	{
    sec=0;
    if(++min == 60)
	{
        min=0;
        if(++hour == 24)
		 	{
         		hour=0;
       		}
    	}
	}
	PORTB ^= 0x01; //1hz led toggle.
}

int main(void)
{
	//set up ports for input and output.
	DDRD = 0xFF;
	PORTD = 0x00;
	DDRC = 0x07;
	PORTC = 0x07;
	DDRB = 0xFF;
	PORTB = 0x00;
	//Init timer
	//Setup timer in CTC mode
	TCCR1B |= (1<<WGM12);
	//Enable CTC interrupts
	TIMSK1 |= (1<<OCIE1A);
	OCR1A=15625; //->1Hz
	TCCR1B |= ((1 << CS10) | (1 << CS11)); // Start timer at Fcpu/64
	sei();
	while(1)//this is a stupid loop to drive a multiplexed group of leds. in 3 groups of 5...
	{
		PORTD=sec;
		PORTC=0x06;
		_delay_ms(1);//these delays allows the pins to catch up with the changes.
		PORTD=0x00;//reset portd to 00000000b
		PORTC=0x07;//reset portc to 00000111b
		PORTD=min;
		PORTC=0x05;
		_delay_ms(1);
		PORTD=0x00;
		PORTC=0x07;
		PORTD=hour;
		PORTC=0x03;
		_delay_ms(1);
		PORTD=0x00;
		PORTC=0x07;
	}  
	return 1;
} 
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

FAQ#1

(and moving to GCC)

Presumably you are not aware of this bit of the user manual?

http://www.nongnu.org/avr-libc/u...

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

Thanks! I had not seen the user manual (I am going to bookmark it :) ,) it was exactly what I needed. Also thanks for moving the thread.