This is a problem I am not able to solve for over 3 months. I tried various things but couldn't understand why my code is not working properly.
I am trying to send values over UART
. When I send the values like this
(Atmel Studio 7.0 , Atmega2560
)
USART_Transmit('O');
USART_Transmit('k');
USART_Transmit('!');
USART_Transmit(0x0d);
USART_Transmit(0x0a);//new line
There is no problem. The output is as I expected
However, when I tried sending the first element of a char array called text
, all it showed was garbage values.
Here is my full code
#define F_CPU 16000000L
#include <avr/io.h>
char Text[6] = "Hello"; // this is
void USART_Init( unsigned int speed)
{
UBRR0H = (unsigned char)(speed>>8); // baudrate setting
UBRR0L = (unsigned char)speed;
UCSR0B=(1<<RXEN0)|( 1<<TXEN0); // rx and tx pins activated
UCSR0C = (0<<USBS0)|(1<<UCSZ01)|(1<<UCSZ00); character size 8
}
void USART_Transmit( unsigned char data )
{
// ucsr0a has a bit called UDRE0, is uart data register empty , if yes wait
while ( !( UCSR0A & (1<<UDRE0))) {;}
UDR0 = data;
}
int main(void) {
USART_Init(103); // 103 is UBRR , (16000000/9600)*16 =104 104-1=103
USART_Transmit('O');
USART_Transmit('k');
USART_Transmit('!');
USART_Transmit(0x0d);
USART_Transmit(0x0a);//new line
//SendString(Text);
USART_Transmit(Text[0]); //GARBAGE VALUE, PRINTS 0xFF should be ascii H ...
while (1) {
}
}