Hello all,
I am reasonably new to microprocessor programing (atmega128) and I was wondering if anyone could give me some pointers as to why the only function which actually does anything in the following test code I compiled together from various odds ends is the UART_RX_interrupt which prints out a random character to my hyper terminal connection on my PC. I know for a fact i have set the baud rate correctly (8-n-1, no flow control)and it all compiles correctly on ICCAVR latest version. All the other functions putchars and etc seem to do nothing and i have no real idea why. I know my main is a bit messy but at the moment i just use it for testing and i can fix that easily. Antways any simple pointers are much appreciated, thanks folks.
#include#include #include // macros #define bit_get(p,m) ((p) & (m)) #define bit_set(p,m) ((p) |= (m)) #define bit_clear(p,m) ((p) &= ~(m)) #define bit_flip(p,m) ((p) ^= (m)) #define bit_write(c,p,m) (c ? bit_set(p,m) : bit_clear(p,m)) /* UART Buffer Defines */ #define UART_RX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */ #define UART_RX_BUFFER_MASK ( UART_RX_BUFFER_SIZE - 1 ) #define UART_TX_BUFFER_SIZE 128 /* 1,2,4,8,16,32,64,128 or 256 bytes */ #define UART_TX_BUFFER_MASK ( UART_TX_BUFFER_SIZE - 1 ) #if ( UART_RX_BUFFER_SIZE & UART_RX_BUFFER_MASK ) #error RX buffer size is not a power of 2 #endif #pragma interrupt_handler UART_RX_interrupt:10 UART_TX_interrupt:11 /* Prototypes */ void InitUART( unsigned char baudrate ); unsigned char ReceiveByte( void ); void TransmitByte( unsigned char data ); extern int _textmode; void delay_ms(int n) { //delay subroutine, in milliseconds int x; while(n--){ x=2443; //factor for 8mhz crystal while(x--); } } int putchar(char c) { //int _textmode; if (_textmode && c == '\n') putchar('\r'); /* Wait for empty transmit buffer */ while ( !(UCSR0A & (1<<UDRE0)) ){ /* Putting data into buffer , sends the data */ UDR0 = c; } return c; } int getchar(void) { while ((UCSR0A & 0x80) == 0); return UDR0; } /* Static Variables */ static unsigned char UART_RxBuf[UART_RX_BUFFER_SIZE]; static volatile unsigned char UART_RxHead; static volatile unsigned char UART_RxTail; static unsigned char UART_TxBuf[UART_TX_BUFFER_SIZE]; static volatile unsigned char UART_TxHead; static volatile unsigned char UART_TxTail; /* initialize UART */ void InitUART( unsigned char baudrate ) { unsigned char x; UBRR0H = 0x00; UBRR0L = baudrate; /* set the baud rate */ /* enable UART receiver and transmitter, and receive interrupt */ UCSR0A = ( (1<<RXCIE0) | (1<<RXEN0) | (1<<TXEN0) ); x = 0; /* flush receive buffer */ UART_RxTail = x; UART_RxHead = x; UART_TxTail = x; UART_TxHead = x; } void UsartInit(unsigned char baudrate){ /* Set baud rate */ UBRR0H = (unsigned char)(baudrate>>8); UBRR0L = (unsigned char)baudrate; /* Enable receiver and transmitter */ UCSR0B = (1<<RXEN0)|(1<<TXEN0); /* Set frame format: 8data, 2stop bit */ UCSR0C = (1<<USBS0); } /* interrupt handlers */ void UART_RX_interrupt( void ) { unsigned char data; unsigned char tmphead; data = UDR0; /* read the received data */ /* calculate buffer index */ tmphead = ( UART_RxHead + 1 ) & UART_RX_BUFFER_MASK; UART_RxHead = tmphead; /* store new index */ if ( tmphead == UART_RxTail ) { /* ERROR! Receive buffer overflow */ } UART_RxBuf[tmphead] = data; /* store received data in buffer */ } void UART_TX_interrupt( void ) { unsigned char tmptail; /* check if all data is transmitted */ if ( UART_TxHead != UART_TxTail ) { /* calculate buffer index */ tmptail = ( UART_TxTail + 1 ) & UART_TX_BUFFER_MASK; UART_TxTail = tmptail; /* store new index */ UDR0 = UART_TxBuf[tmptail]; /* start transmition */ } else { UCSR0A &= ~(1<<UDRIE0); /* disable UDRE interrupt */ } } /* Read and write functions */ unsigned char ReceiveByte( void ) { unsigned char tmptail; while ( UART_RxHead == UART_RxTail ) /* wait for incomming data */ ; tmptail = ( UART_RxTail + 1 ) & UART_RX_BUFFER_MASK;/* calculate buffer index */ UART_RxTail = tmptail; /* store new index */ return UART_RxBuf[tmptail]; /* return data */ } void TransmitByte( unsigned char data ) { unsigned char tmphead; /* calculate buffer index */ tmphead = ( UART_TxHead + 1 ) & UART_TX_BUFFER_MASK; /* wait for free space in buffer */ while ( tmphead == UART_TxTail ) ; UART_TxBuf[tmphead] = data; /* store data in buffer */ UART_TxHead = tmphead; /* store new index */ UCSR0A |= (1<<UDRIE0); /* enable UDRE interrupt */ } void USART_Flush( void ) { unsigned char dummy; while ( UCSR0A & (1<<RXC0) ) dummy = UDR0; } unsigned char DataInReceiveBuffer( void ) { return ( UART_RxHead != UART_RxTail ); /* return 0 (FALSE) if the receive buffer is empty */ } /* main test program*/ void main(void) { unsigned char text; unsigned char* test; unsigned char Rxok; unsigned char* data; int stationID; int Rx; int length; int timeout; int i; UsartInit(0x1F); //UBRR0L = 0x5F; /* 0xBF; //for 9600 bps // 0x07 for 115.2k bps */ UBRR0H = 0x00; _SEI(); /* enable interrupts => enable UART interrupts */ UART_TX_interrupt(); UART_RX_interrupt(); i = 0; timeout = 0; length = 16; //printf("f"); test = "x"; Rx = 0x1B; stationID = 0x01; Rxok = Rx; //printf(test); text = stationID; //TransmitByte('x'); data = "this is a data !"; //text = ReceiveByte(); while (1) /* forever */ { //printf("go!"); //putchar(text); //putchar('c'); //putchar('\n'); //printf(data); //UART_TX_interrupt(); printf("o"); putchar('!'); TransmitByte(Rx); TransmitByte('!'); UDR0 = '!'; UDR0 = ' '; TransmitByte(ReceiveByte()); //USART_Flush(); UART_TX_interrupt(); //TransmitByte(0x61); //TransmitByte( ReceiveByte() ); /* echo the received character */ /* test loop for comms */ //UART_RX_interrupt(); if(ReceiveByte() == 'O'){ USART_Flush(); UART_RX_interrupt(); if(ReceiveByte() == 'K'){ USART_Flush(); //if(ReceiveByte() == 'T'){ UART_TX_interrupt(); TransmitByte(text); USART_Flush(); timeout = 0; //if(ReceiveByte() != 'O' ){ //only O not OK at the moment while(timeout < 10000){ UART_TX_interrupt(); TransmitByte(Rxok); //watch for transmit timeouts here timeout = timeout + 1; //printf("this is a data !"); while(i < length){ UART_TX_interrupt(); TransmitByte(data[i]); USART_Flush(); i = i+ 1; } } //} //} } } } }