Buenas, estoy intentando hacer un programa para comprobar el puerto serie en el microcontrolador AT90CAN128 usando la UART1, es un programa eco es decir, el caracter que metas en el hyperterminal, tiene que llegar a la UART1 y te tiene que volver a salir en el hyperterminal, el problema es que me envia el caracter pero no me lo devuelve la UART1, he revisado el valor de los registros, he comprado mi programa con otros que están hechos y no sé cual puede ser el problema, ¿alguien me podría echar un mano? aquí pongo el código.
Código:
/* Program for testing serial communication with AT90CAN128 processor
* Compile with AVRStudio+WinAVR (gcc version 3.4.6)
* Baud Rate: 9600 baud
* No parity , 1 Stop bit
*/
#define __AVR_AT90CAN128__ 1
#define OSCSPEED 16000000 /* in Hz */
#include "avr/io.h"
#include "inttypes.h"
void PORT_Init()
{
PORTA = 0x00;
DDRA = 0x00;
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00;
DDRC = 0x00;
PORTD = 0x00;
DDRD = 0x08; // set TXD1 as output
PORTE = 0x00;
DDRE = 0x00;
PORTF = 0x00;
DDRF = 0x00;
}
void UART_Init(uint32_t Baud)
{
unsigned int BaudRate = OSCSPEED / (16 * Baud) - 1; //calculate BaudRate
//set BaudRate into registers
UBRR1L = (unsigned char) BaudRate;
UBRR1H = (unsigned char) (BaudRate>>8);
/* Set frame format: 8data, no parity & 1 stop bits */
UCSR1C |= 0x06;
/* Enable receiver and transmitter */
UCSR1B |= 0x18;
}
//This function is used to read the available data
//from USART. This function will wait untill data is
//available.
unsigned char USART1_Receive (void)
{
/* Wait for data to be received */
while ( ! (UCSR1A & (1<<RXC1)));
/* Get and return received data from buffer */
return UDR1;
}
//This fuction writes the given "data" to
//the USART which then transmit it via TX line
void USART1_Transmit (unsigned char data)
{
/* Wait for empty transmit buffer */
while ( ! ( UCSR1A & (1<<UDRE1)));
/* Put data into buffer, sends the data */
UDR1 = data;
}
int main()
{
//Varriable Declaration
char data;
/*First Initialize the USART with baud rate = 19200bps for Baud rate = 9600bps
*/
PORT_Init();
UART_Init(9600);
//Loop forever
while(1)
{
//Read data
data = USART1_Receive ();
USART1_Transmit(data);
}
}