AVR串口通信程序,在PC端串口助手 一直接收到00 15
以下是代码,有没有高手指导一下,//ICC-AVRapplicationbuilder:2015/5/810:44:16//Target:m168//Crystal:8....
以下是代码,有没有高手指导一下,
//ICC-AVR application builder : 2015/5/8 10:44:16
// Target : m168
// Crystal: 8.0000Mhz
#include <iom168v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x01;
DDRD = 0x00;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0H = 0x00; //set baud rate hi
UBRR0L = 0x33; //set baud rate lo
UCSR0B = 0x18;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//字符输出函数
void putchar(unsigned char c)
{
while(!(UCSR0A&(1<<UDRE0)));//判断上次发送有没有完成
UDR0=c;
}
//字符输入函数
unsigned char getchar(void)
{
while(!(UCSR0A&(1<<RXC0)));
return UDR0;
}
//带回车换行控制的字符串输出函数
int puts(char *s)
{
while(*s)
{
putchar(*s);
s++;
}
putchar(0x0a); //回车换行
putchar(0x0d);
return 1;
}
//不带回车换行的字符串输出函数
void putstr(char *s)
{
while(*s)
{
putchar(*s);
s++;
}
}
void main(void)
{
init_devices();
//insert your functional code here...
puts("测试语句");
while(1);
} 展开
//ICC-AVR application builder : 2015/5/8 10:44:16
// Target : m168
// Crystal: 8.0000Mhz
#include <iom168v.h>
#include <macros.h>
void port_init(void)
{
PORTB = 0x00;
DDRB = 0x00;
PORTC = 0x00; //m103 output only
DDRC = 0x00;
PORTD = 0x01;
DDRD = 0x00;
}
//UART0 initialize
// desired baud rate: 9600
// actual: baud rate:9615 (0.2%)
// char size: 8 bit
// parity: Disabled
void uart0_init(void)
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0H = 0x00; //set baud rate hi
UBRR0L = 0x33; //set baud rate lo
UCSR0B = 0x18;
}
//call this routine to initialize all peripherals
void init_devices(void)
{
//stop errant interrupts until set up
CLI(); //disable all interrupts
port_init();
uart0_init();
MCUCR = 0x00;
EICRA = 0x00; //extended ext ints
EIMSK = 0x00;
TIMSK0 = 0x00; //timer 0 interrupt sources
TIMSK1 = 0x00; //timer 1 interrupt sources
TIMSK2 = 0x00; //timer 2 interrupt sources
PCMSK0 = 0x00; //pin change mask 0
PCMSK1 = 0x00; //pin change mask 1
PCMSK2 = 0x00; //pin change mask 2
PCICR = 0x00; //pin change enable
PRR = 0x00; //power controller
SEI(); //re-enable interrupts
//all peripherals are now initialized
}
//字符输出函数
void putchar(unsigned char c)
{
while(!(UCSR0A&(1<<UDRE0)));//判断上次发送有没有完成
UDR0=c;
}
//字符输入函数
unsigned char getchar(void)
{
while(!(UCSR0A&(1<<RXC0)));
return UDR0;
}
//带回车换行控制的字符串输出函数
int puts(char *s)
{
while(*s)
{
putchar(*s);
s++;
}
putchar(0x0a); //回车换行
putchar(0x0d);
return 1;
}
//不带回车换行的字符串输出函数
void putstr(char *s)
{
while(*s)
{
putchar(*s);
s++;
}
}
void main(void)
{
init_devices();
//insert your functional code here...
puts("测试语句");
while(1);
} 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询