Atmega128中的串行通信

时间:2013-02-22 06:06:56

标签: serial-port avr-gcc serial-communication

我想通过串行总线向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;
}

1 个答案:

答案 0 :(得分:0)

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

要回答您的问题,功能strcpy()strlen()需要char *而非unsigned char *支票here