[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[avr-gcc-list] Strange problem with UART
From: |
Olav Torheim |
Subject: |
[avr-gcc-list] Strange problem with UART |
Date: |
Sat, 26 Apr 2003 13:39:13 +0200 (CEST) |
Finally I made my simple UART program work the way it should by leaving the read/write operations to interrupts routines. However, the main() function and the interrupts routines seemed not to be able to read/write the same variables, althought I suppose they should. To make the program work I had to force the variables UART_CHARACTER_RECEIVED and UART_READY to specific registers, then the main program and the interrupts finally would access the same data. Does anybody know why this is happening? Source code is attached below.
Olav Torheim
// Simple UART program returning received input as output:
#include <io.h>
#include <interrupt.h>
#include <signal.h>
#include <progmem.h>
#define UART_CPU 8000000 /* 4.00Mhz */
#define UART_BAUD_RATE 19200 /* baud rate*/
#define UART_BAUD_SELECT (UART_CPU/(UART_BAUD_RATE*16l)-1)
register unsigned char UART_CHARACTER_RECEIVED asm("r3");
register unsigned char UART_READY asm("r4");
unsigned char UART_RXCHAR;
SIGNAL(SIG_UART_RECV)
{
UART_RXCHAR = inp(UDR);
UART_CHARACTER_RECEIVED = 1;
}
// Interrupt-rutina som køyrer når yverføringi er avslutta:
SIGNAL(SIG_UART_TRANS)
{
UART_READY = 1;
}
void UARTInit(void)
{
// Initializing UART:
outp(BV(RXCIE)|BV(TXCIE)|BV(RXEN)|BV(TXEN),UCSRB); // Setting the control register
outp(UART_BAUD_SELECT, UBRR); //Baudrate 19200
sei(); // enabling global interrupts
UART_READY = 1;
UART_CHARACTER_RECEIVED = 0;
}
void UARTSend(char outchar)
{
while(UART_READY != 1);
UART_READY = 0;
outp(outchar, UDR);
}
unsigned char UARTReceive(void)
{
while(UART_CHARACTER_RECEIVED != 1);
UART_CHARACTER_RECEIVED = 0;
return inp(UART_RXCHAR);
}
int main(void)
{
UARTInit();
while(1)
{
UARTSend(UARTReceive());
};
}
Ny versjon av Yahoo! Messenger
Nye ikoner og bakgrunner, webkamera med superkvalitet og dobbelt så morsom
- [avr-gcc-list] Strange problem with UART,
Olav Torheim <=