0

I want send some string to PC via serial bus. In cute com its displaying the string along with some chars are missing and at the starting and end of the string its appending some hex numbers. Why this problem occurring I don't know please help in this issue. My code is here.

 #include <avr/io.h>  
 #include <string.h>
 #include <avr/interrupt.h>
 #define F_CPU 16000000UL
 #include <util/delay.h>    
 #define USART_BAUDRATE 9600    // Baud Rate value
 #define BAUD_PRESCALE (((F_CPU / (USART_BAUDRATE * 16UL))) - 1)
 void usart_init() {

//Enable communication in duplex mode
UCSR1A = (1 << U2X1);
UCSR1B |= (1 << RXEN1) | (1 << TXEN1);   // Turn on the transmission and reception circuitry
UCSR1C &= ~(1 << UMSEL1);
UCSR1C |= (1<<USBS1) | (1 << UCSZ10) | (1 << UCSZ11);

UBRR1L = BAUD_PRESCALE;                 // Load lower 8-bits of the baud rate value into the low byte of the UBRR register
UBRR1H = (BAUD_PRESCALE >> 8);          // Load upper 8-bits of the baud rate value.. 
}

void serial_write(unsigned char data) {

while(!(UCSR1A & (1<<UDRE1)))
    ;
    UDR1 = data;
    _delay_ms(10);
}

 void transmitString(unsigned char *str) {

int i;
for(i=0;i<strlen(str);i++) {
    serial_write(str[i]);
    _delay_ms(1);
}
}

int main() {
cli();
usart_init();
unsigned char buffer[20];
strcpy(buffer, "Walk Alone");
while(1) {  
    transmitString(buffer);
    //_delay_ms(250);
}
return 0;
}
dsolimano
  • 8,870
  • 3
  • 48
  • 63
Sanjeev
  • 11
  • 1
  • 4
  • The AVR micro-controllers uses two frequency oscillators to run. By default the controller uses in-built oscillator which is of 1MHz. And in the program the F_CPU is 16MHz, which lead to data appear as junk. To select external Oscillator set the Fuse bits of the controller. The controller selects the external oscillator to operate. – Sanjeev Feb 12 '18 at 09:34

1 Answers1

0

Firstly, this question belongs in http://electronics.stackexchange.com

To answer your question, functions strcpy() and strlen() expect a char * and not unsigned char * check here

Madhur
  • 2,119
  • 1
  • 24
  • 31