本帖最后由 8385810 于 2013-2-25 16:31 编辑
#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/iom128.h>
#define uchar unsigned char
#define uint unsigned int
void uart0_init(void);
void uart0_send(unsigned char c);
void Usart_Sendstring(unsigned char *Str);
uchar RX_buffer[7];
uchar Tx_buffer[6];
static int i=0;
static int aflag=0;
/******************端口初始化***************************/
void port_init(void)
{ //端口设置
PORTA = 0x00;//LCD口,总线低八位地址
DDRA = 0x00;
PORTB = 0x00;//SPI接口
DDRB = 0xff;
PORTC = 0x00; //总线高八位地址
DDRC = 0x00;
PORTD = 0x00;//USART1接口
DDRD = 0x00;
PORTE = 0x0f;//键盘口
DDRE = 0xf0;
PORTF = 0x00;//LED口
DDRF = 0xff;
PORTG = 0xff;
DDRG = 0xff;
}
/***************串口0初始化********************/
void uart0_init()
{
UCSR0B = 0x00; //disable while setting baud rate
UCSR0A = 0x00;
UCSR0C = 0x06;
UBRR0L = 0x33; //baud rate=9600
UBRR0H = 0x00; //
UCSR0B = 0x98;
}
/**************发送采用查询方式**************/
void uart0_send(unsigned char c)
{
while( !(UCSR0A & (1<<UDRE0)) );
UDR0=c;
}
/**************接收采用中断******************/
ISR(USART0_RX_vect)
{
char temp=0;//?GData*
temp=UDR0;
if((RX_buffer[0]=='?')||(temp=='?'))
{
RX_buffer[i++]=temp;
if(temp=='*')
{
//TCCR0=00;
aflag=1;
i=0;
}
}
if(i>8)
{
RX_buffer[0]='a';
i=0;
}
}
/********************************************/
void Usart_Sendstring(unsigned char *Str)
{
while(*Str)
{
uart0_send(*Str++);
}
}
int main(void)
{
cli();
port_init();
uart0_init();
sei();
while(1)
{
if(aflag)
{
Usart_Sendstring("1234");
aflag=0;
}
}
}
我用串口调试工具发送字符串“?GData*”可为什么收不到“1234” |