user3978484
user3978484

Reputation:

I cant send char array values over USART (Avr Atmel studio )

I am trying to use usart with avr . But i couldn't send full string. 1 char at time works but char array i cant send.

Please i am open all suggestions . I am just missing something important and couldn't figure out own my own..

Here is my code:

void send_string(const char *str,uint8_t len);
int main(void)
{

  USART_Init(207);
  adc_init();  


  //----------------------------------------------------------------------------
  const char txt[5]="hello";  
  PORTB =0b1010000;
  USART_Transmit('O');// this part works 
  USART_Transmit('k');// works 
  USART_Transmit('!');// works  
  USART_Transmit(0x0d);//start of new line -> \r
  USART_Transmit(0x0a);//new line            -> \n
while (1) 
{

_delay_ms(500);
sensitivity = adc_read(0);
print_value((char*)'A',(int *)sensitivity); // works. definition is unsigned int sensitivity = 1;

send_string((char *)&txt[0],strlen(txt));  //sounds good does not work  :( 
send_newln();
defa((char*)&txt[0]);// çalışmıyor
defa((char*)'A');// çalışıyor
}
}

And here are the functions :

void send_string(const char *str,uint8_t len){

while (len--){
USART_Transmit((const char*)*str);
str++;
}
}



void defa(char aa){
for(uint8_t i=0;i<5;i++)
{
USART_Transmit(aa);
}
}

Upvotes: 0

Views: 812

Answers (1)

Jonathan Corriveau
Jonathan Corriveau

Reputation: 321

A few suggestions :

Your function "defa" sends the same character 5 times. You want that ?

Your "dafa" functions gets a 'char' from it's parameter... but when you call it, you send a pointer. Instead of showing the value you want, it will show the address in memory where the variable is. Worse, you show a 'char' so it shows a single byte from the address so it's basically unusable garbage.

You mix "const char" with "char". This isn't good. For this program you don't need 'const' at all yet. Remove it to simplify your code and add it later if you really want it. My guess is that you are experimenting so you do not need it at all.

char txt[5]="hello"; --- This is just WRONG. 'hello' is 5 caracters, but a C string ends with a "\0' so your string must be 6 caracters.

strlen(txt) will not work because it wants the '\0' to calculate the length of the string. This would work better because we skip the 'strlen' function : send_string((char *)&txt[0],5); And this would be simpler to read : send_string(txt,5);

I'll stop here, this should be enough to get you some better results, but I have a better advice : You should learn C in a Windows program, it will be simpler for you. If you want to program AVR, you should get 'sample programs' from atmel and check how it's working, you will have more fun programming.

Upvotes: 1

Related Questions