November 22, 2024, 03:49:45 PM

Avr-mt-128 USART problem

Started by Ippocampo, October 21, 2015, 03:55:55 PM

Previous topic - Next topic

Ippocampo

i bought this product https://www.olimex.com/Products/AVR/Development/AVR-MT128/, reading the datasheet of the board and the processor i wanted to read barcode with a barcodereader attached on the icsp and send what i read to the computer with the other usart on the avrmt128 DB9-rs232. The problem i encounter is that when i read with the bar code the data arrive on the processor but i cant find a way to send it to the pc. i suppose i do something wrong during the initialitazion of the usart or port. Can somenone help me please ? even with the example i cant find a way to do it. I'm not asking for someone to do my job only to suggest me a way to solve a problem or to look where i could have failed the code. This is my USART Init and the PORT Init :


void PORT_Init()
{
PORTA = 0b00000000; DDRA = 0b01000000; //Relay set as output (Bit6 = 1)
PORTB = 0b00000000; DDRB = 0b00000000;
PORTC = 0b00000000; DDRC = 0b11110111;
PORTD = 0b11000000; DDRD = 0b00001000; //TX set as output (Bit3 = 1)
PORTE = 0b00000000; DDRE = 0b00000010; //Buzzer set as output (Bit4 = 1, Bit5 = 1) TX set as output (Bit1 = 1)
PORTF = 0b00000000; DDRF = 0b00000000;
PORTG = 0b00000000; DDRG = 0b00000000;
}

void UART_Init(uint32_t Baud)
{
unsigned int BaudRate = OSCSPEED / (16 * Baud) - 1; /* as per pg. 173 of the user manual */

//set BaudRate to registers UBRR1H and UBRR1L
UBRR1H = (unsigned char) (BaudRate>>8);
UBRR1L = (unsigned char) BaudRate;

UCSR1B = UCSR1B | 0b00011000; //enable Receiver and Transmitter (Bit3 = 1, Bit4 = 1)
UCSR1C = UCSR1C | 0b00000110; //Set frame format: 8 data (Bit1 = 1, Bit2 = 1), 1 stop bit (Bit3 = 0)

//set BaudRate to registers UBRR1H and UBRR1L
UBRR0H = (unsigned char) (BaudRate>>8);
UBRR0L = (unsigned char) BaudRate;

UCSR0B = UCSR0B | 0b00011000; //enable Receiver and Transmitter (Bit3 = 1, Bit4 = 1)
UCSR0C = UCSR0C | 0b00000110; //Set frame format: 8 data (Bit1 = 1, Bit2 = 1), 1 stop bit (Bit3 = 0)

}
unsigned char UART_Receive()
{
if (UCSR0A & 0b1000000) //if there is unreaded data
return UDR0;
else //no unreaded data
return 0;

}

unsigned char UART_Receive1()
{
if (UCSR1A & 0b1000000) //if there is unreaded data
return UDR1;
else //no unreaded data
return 0;

}

void UART_Transmit(unsigned char data)
{
UDR1 = data;
while (!(UCSR1A & 0b01000000)); //waiting until buffer is ready to receive

}

void UART_Transmit1(unsigned char data)
{
UDR0 = data;
while (!(UCSR0A & 0b01000000)); //waiting until buffer is ready to receive


}

void delay_ms(unsigned long d) {

i=DFACTOR*1000*d;
while (--i!=0);

}

int main()
{
unsigned char Ch;
PORT_Init();
UART_Init(9600);
while (1)
{
Ch = UART_Receive();
if (Ch!=0)
{
UART_Transmit(Ch);
}

Ch = UART_Receive1();
if (Ch!=0)
{
UART_Transmit(Ch);
}

}
return 0;
}

Ippocampo

seems i solved the problem thanks anyway and sorry for the disturb  :)  by the way i changed the transmit code instead of

if (UCSR0A & 0b1000000) //if there is unreaded data
return UDR0;
else //no unreaded data
return 0;

i wrote
while(UCSR0A & (1<<RXC0)) //if there is unreaded data
return UDR0;


if you think i did something wrong tell me anyway