我有一個用codevision AVR寫的代碼,用於ATmega2560發送格式化輸出到USART 0和3.我的USART 0連接的是RS232接口,而USART 3是zigbee接口。當我用簡單的測試程序測試zigbee接口時,它是可以的,所以zigbee應該沒有問題。當使用這個時,USART 0接收字符串沒有任何問題,但是USART 3有問題,所以我決定在這裏發佈它以知道這裏有什麼問題。上述sprintf在avr中的奇怪行爲
#include <mega2560.h>
#include <stdio.h>
#include <string.h>
#define BUFF_SIZE 200
unsigned char out_buf[BUFF_SIZE];
void USART0_init(void) /*************** USART 0 ***********************/
{
UCSR0A = 0x00;
UCSR0B |= (1<<RXEN0)|(1<<TXEN0);
UCSR0C |= (1<<UCSZ01)|(1<<UCSZ00);
UBRR0H=0x00;
UBRR0L=103; //baud rate 9600
}
void USART0_putc(unsigned char data)
{
while(!(UCSR0A & (1<<UDRE0)));
UDR0=data;
}
void USART0_puts(unsigned char* str){
while(*str) {USART0_putc(*str++);}
}
void USART3_init(void) /*************** USART 3 ***********************/
{
UCSR3A = 0x00;
UCSR3B |= (1<<RXEN3)|(1<<TXEN3);
UCSR3C |= (1<<UCSZ31)|(1<<UCSZ30);
UBRR3H=0x00;
UBRR3L=8; //baud rate 115200;
}
void USART3_putc(unsigned char data)
{
while(!(UCSR3A & (1<<UDRE3)));
UDR3=data;
}
void USART3_puts(unsigned char* str){
while(*str) {USART3_putc(*str++);}
}
該代碼可用於USART 0和3 每60秒在通過USART 0和3中顯示的代碼是在這裏。
if (tick_60sec == 60){
tick_60sec = 0;
axis_store();axis_disp();
sens_avg_call();
t3_l = 1; t6_l = 1;
t9_l = 1; t12_l = 1;
t15_l = 1; t17_l = 1;
t19_l = 1;
}
void sens_avg_call(void)
{
sprintf(out_buf,"wind current avg: %d",(windc_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
sprintf(out_buf,"wind voltage avg: %d",(windv_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
sprintf(out_buf,"solar current avg: %d",(solc_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
sprintf(out_buf,"solar voltage avg: %d",(solv_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
sprintf(out_buf,"led current avg: %d",(ledc_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
sprintf(out_buf,"battery voltage avg: %d",(battv_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
sprintf(out_buf,"temperature avg: %d",(temp_av/3));
USART3_puts(out_buf);
USART0_puts(out_buf);
}
您無法獲得任何使用速度二進制文字超過'_BV()'調用。 –
我已經編輯了上面的寄存器設置。 –