Hello everybody,

I'm making a scoreboard with uart and 7-segment multiplexing and I've got a problem.
When I receive code from the UART, multiplexing doesn't work any more.
And when I'm multiplexing with timer1, the UART_receive doesn't work any more.

Please look at my code below and help me.

Best regards,
Floris

/********************************************************************************
Includes
********************************************************************************/
#include <avr/io.h>
#include <stdio.h>
#include <avr/interrupt.h>
#include <util/delay.h>
 
/********************************************************************************
             Defines
********************************************************************************/
#define USART_BAUDRATE 9600 
#define BAUD_PRESCALE 51 
/********************************************************************************
             Functies
********************************************************************************/

/********************************************************************************
             usart
********************************************************************************/

void UART_send(uint8_t data)  //uart zenden 
{ 
  while (!(UCSRA & (1<<UDRE))); 
  UDR = data; 
} 

uint8_t UART_receive(void) //uart ontvangen
{ 
 while (!(UCSRA & (1<<RXC))); 
  return UDR; 
} 

void UART_sendstring(char * str) 
{ 
  while (*str) 
  { 
    UART_send((uint8_t) *str); 
    str++; 
  } 
} 

int numbers[10]=
	{0b01000000,	// 0
	0b11111001,		// 1
	0b00100100,		// 2
	0b10110000,		// 3
	0b00011001,		// 4
	0b10010010,		// 5
//	0b00000011,		// 6
	0b00000010,		// 6 
	0b11111000,		// 7
	0b00000000,		// 8
//	0b10011000,		// 9 
	0b10010000	};

	int i=0;
	int z=0;

/********************************************************************************
Main
********************************************************************************/
 
int main (void) 
{ 
   UCSRB |= (1 << RXEN) | (1 << TXEN);   //zend en ontvang
   UCSRC |= (1 << URSEL) | (1 << UCSZ0) | (1 << UCSZ1); // 8 bit

   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 
 




DDRA=0xFF;
DDRB=0xFF;
DDRC=0xFF;										    /* Pull-ups enabled */
PORTA=0x00;
PORTB=numbers[0];
PORTC=0x00;
 
 unsigned char data;	
 int b;
 
 	TCCR1B |= (1 << WGM12); 					// Configure timer 1 for CTC mode 
	TIMSK |= (1 << OCIE1A); 					// Enable CTC interrupt 
	sei();								// Enable global interrupts
	OCR1A   = 31.248;						// Compared met 31248 voordat een interrupt wordt gegenereerd
	TCCR1B |= ((1 << CS12)); 					//Timer met Fcpu/256(prescaler)
 
 	while (1) 
	 { 


  
 if ('b'==UART_receive())
 {
 i=i++;
 }
 
	
 
	
 


  									   		   
 	} 
}
 

ISR(TIMER1_COMPA_vect) 	
{
	z=z++;
	PORTC=(~(0x02));
	PORTB=numbers[i];
	_delay_ms(1);
	PORTC=(~(0x01));
	PORTB=numbers[2];
	_delay_ms(1);



}

Your code doesn't try to send anything.

Most UARTs also produce an interrupt, normally on data received, on error and possibly on ready to send data.

It is common (and therefore I assume I good approach) to handle data receive using the UARTs interrupt and it is often a requirement to handle the error states of the UART which is also most easily done using the interrupt. Send can either be polled or interrupt driven, polled works well if you want to be sure the data has been sent before your program continues while interrupt driven works if you want to just fire and forget i.e. you don't need to know when the data has been written.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.