Hi guys, I am trying a real time clock on a STK500 kit. (I know there are multiple projects on this but I just want to learn it from scratch). I am using interrupt based counting using timer1 of Mega16. I want to transmit the time updates to my PC with the USART. So far,I have tested each individual part (the USART, the RTC) and it works just fine. However my problem is I want to display the time on my hyperterminal as follows:
HH:MM:SS
However, my the receive window shows a running display of my time. Here is what I have so far:
//Real time clock program using interrupts with timer 1 //transmit time to PC using Bray's terminal program #include "clock_header.h" volatile unsigned char seconds,minutes,hours,days,months,years; volatile unsigned char transmit_time[8]; volatile unsigned int i; int main (void) { rtc_init(); USART_init(); timer_init(); //setup timer interrupt to fire every 1s while(1) { go_to_sleep(); } } void timer_init() { TIMSK |= (1 << OCIE1A); //enable channel 1A interrupt sei(); //enable global interrupts TCCR1B |= (1 << CS10) | ( 1 << CS11); //start timer at Fcpu/64 TCCR1B |= (1 << WGM12);//set up for CTC mode OCR1A = 65000; //Value that the micro will compare with current count } ISR(TIMER1_COMPA_vect) { rtc_op(); } void rtc_init() { seconds = minutes = hours = days = months = years = 0; } void USART_update(char array[]) { while ((UCSRA & (1 << UDRE)) == 0) {}; //check is the transmit buffer is full for(i=0;i<8;i++) { UDR = array[i]; i++; _delay_ms(100); } } void rtc_op() { seconds++; if (seconds == 60) { seconds = 0; // PORTB ^= (1 << PA1); //toggle LED if needed on PORTA to check counter minutes++; } if (minutes == 60) { minutes=0; // PORTB ^= (1 << PA2); hours++; } if(hours == 24) { hours = 0; // PORTB ^= (1 << PA3); days++; } //array positions: // 0,1 --> hours //2 --> : //3,4 ---> minutes //5 ---> : //6,7 ---> seconds transmit_time[7] = seconds / 10; transmit_time[6] = seconds % 10; transmit_time[5] = ':'; transmit_time[4] = minutes / 10; transmit_time[3] = minutes % 10; transmit_time[2] = ':'; transmit_time[1] = hours / 10; transmit_time[0] = hours % 10; USART_update(transmit_time); } void USART_init() //setup USART in transmit mode { UCSRB |= (1 << TXEN); // Turn on the transmission circuitry UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // Use 8-bit character sizes UBRRL = BAUD_PRESCALE; // Load lower 8-bits of the baud rate value into the low byte of the UBRR register UBRRH = (BAUD_PRESCALE >> 8); // Load upper 8-bits of the baud rate value into the high byte of the UBRR register } void go_to_sleep() { sleep_cpu(); }
I am collecting the timer updates in a character array and then passing it to the USART_update function. What am I missing? Thanks for any help.