2

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

uart_atmel

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) { 

   } 
} 
Student
  • 805
  • 1
  • 8
  • 11
Enes Kuz
  • 168
  • 7
  • For one, why do you not follow one type? You have used `unsigned char` in some places and `char` in some places. The cast from `char` to `unsigned char` might not be defined properly and hence just passes `0xff` always. – Ajay Brahmakshatriya Jul 23 '18 at 12:20
  • String constants are stored in flash instead of ram. Don't you have to access them differently or copy them to ram first? – Goswin von Brederlow Jul 23 '18 at 12:25
  • @GoswinvonBrederlow no. It is copied automatically when you declare it as a array (like here) not the pointer. It should be done by the startup routines before the main function call. – 0___________ Jul 23 '18 at 12:41
  • @AjayBrahmakshatriya i changed all variables to char but it did not work. Also changed all of them unsigned char but no luck – Enes Kuz Jul 23 '18 at 17:39
  • @ryyker can you elaborate ? This a ide for programming microcontrollers. – Enes Kuz Jul 23 '18 at 17:40

1 Answers1

0

My code is working on proteus simulation program .I have tried on atmega2560/328/32 All is work . on board when i defined const or static variable (string array) it was work . But when const or static array length is big than 16 ,it didn't work. on simulation everythings is work but on Arduino Mega(atmega2560 ) did not working. (Sorry for English).