Justin Wenger
Newbie level 5
I want to make several codes work together.(ex. seven segment code+ LED code+ timer code + ....)
Each code works properly on its own, but when I try to combine it, problem happens.
Only one code works even though I combined these codes together.
Code below is a code using seven segment + timer + UART at the same time,
and I want it to count it down from 30:00 to 00:00 while I can use UART at the same time.
But only the timer code works and UART code doesn't.
If you know what the problem is, please give me an advice.
Each code works properly on its own, but when I try to combine it, problem happens.
Only one code works even though I combined these codes together.
Code below is a code using seven segment + timer + UART at the same time,
and I want it to count it down from 30:00 to 00:00 while I can use UART at the same time.
But only the timer code works and UART code doesn't.
If you know what the problem is, please give me an advice.
Code:
#define F_CPU 1600000
#include <avr/io.h>
#include <avr/interrupt.h>
#include <util/delay.h>
#include <stdio.h>
const unsigned char Segment_Data[] =
{0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x27,0x7F,0x6F};
char COLUMN[4]={0,0,0,0};
int Cnt=0;
int SHOW_NUMBER=0, SHOW_NUMBER12=30, SHOW_NUMBER34=0;
void Show4Digit(int number);
void ShowDigit(int i, int digit);
void uart0_init(void);
int Putchar(char message);
void uart(void);
char getchar1(void);
ISR(TIMER0_OVF_vect) { // TIMER0 has overflowed
Cnt++;
if(Cnt==244){
SHOW_NUMBER34--;
Cnt=0;
}
if(SHOW_NUMBER34<0){
SHOW_NUMBER12--;
SHOW_NUMBER34=59;
}
if(SHOW_NUMBER12<0){
SHOW_NUMBER12=0;
SHOW_NUMBER34=0;
}
}
int main(void) {
uart0_init();
char c;
fdevopen(Putchar, 0);
printf("\n\rUART_Test");
DDRC = 0xff; DDRA = 0xff; PORTC = 0x00;
TCCR0 = 0x06; // Normal mode, prescale 256, 16M/256=62500Hz
TCNT0 = 0x00; // n=0, 256 count, 62500Hz/256=244.14Hz
TIMSK = 0x01; // timer0 OVERFLOW interrupt enable
SREG |= 0x80; // Global Interrupt Enable
while (1) {
SHOW_NUMBER=SHOW_NUMBER12*100+SHOW_NUMBER34;
Show4Digit(SHOW_NUMBER);
}
}
void uart(void){
uart0_init();
char c;
fdevopen(Putchar, 0);
printf("\n\rUART_Test");
while(1){
c = getchar1();
printf("%c", c);
}
}
void Show4Digit(int number) {
COLUMN[0] = number/1000; COLUMN[1] = (number%1000)/100;
COLUMN[2] = (number%100)/10; COLUMN[3] = (number%10);
for(int i=0;i<4;i++) {
ShowDigit(COLUMN[i],i);
_delay_ms(2); // wait for a second
}
}
void ShowDigit(int i, int digit) {
PORTC=~(0x01<<digit);
PORTA = Segment_Data[i];
}
void uart0_init(void) {
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x67; //set baud rate lo
UBRR0H = 0x00; //set baud rate hi
UCSR0B = 0x18;
}
int Putchar(char message) {// Putchar "P" cVde
while (((UCSR0A>>UDRE0)&0x01) == 0) ; // UDRE, data register empty
UDR0 = message;
return 0;
}
char getchar1(void) {
while(!(UCSR0A&0x80));
return UDR0;
}