0

我想通过串行总线向 PC 发送一些字符串。在可爱的 com 中,它显示的字符串和一些字符丢失了,并且在字符串的开头和结尾附加了一些十六进制数字。为什么会出现这个问题我不知道请帮助解决这个问题。我的代码在这里。

 #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;
}
4

1 回答 1

0

首先,这个问题属于http://electronics.stackexchange.com

要回答您的问题,功能strcpy()strlen()期望 achar *而不是在这里unsigned char *检查

于 2013-02-22T06:21:38.990 回答