|
//ICC-AVR application builder : 2010-10-11 14:28:07
// Target : M48
// Crystal: 7.3728Mhz
/*----------------------------------
#ifndef _Uart_h_
#define _Uart_h_
#include ".\head\SD2405.h"
//晶振和波特率
#define Fosc_CPU 7372800
#define Baud 9600
void Init_Uart0();
void Uart0_SendByte(uint8 u8_Data);
uint8 Uart_getchar();
void Uart_TXD_String(char *str,uint8 Enter);
//UART初始化函数
void Init_Uart0()
{
//允许收发打开接收中断
UCSR0B=(1<<RXEN0)|(1<<TXEN0)|(1<<RXCIE0);
UBRR0L=((Fosc_CPU/16/Baud)-1)%256;//设置波特率寄存器
UBRR0H=((Fosc_CPU/16/Baud)-1)/256;
UCSR0C=(1<<UCSZ01)|(1<<UCSZ00);//8位数据+1位STOP
}
//字符输出
void Uart0_SendByte(uint8 u8_Data)
{
while(!(UCSR0A&(1<<UDRE0)));//上次发送有没有完成
UDR0=u8_Data;
}
//字符输入
uint8 Uart_getchar()
{
while(!(UCSR0A&(1<<RXC0)));//有没有接收到数据
return UDR0;
}
//*****************串口发送字符串******************************
// 入口参数: *str:字符串首地址
// ENTER:(回车换行) 1:不换行 0:换行
// 出口参数: 无
// 函数功能: 从串口发送一字符串
//*************************************************************
void Uart_TXD_String(char *str,uint8 Enter)
{
while( *str )
{
Uart0_SendByte(*str);
str++;
}
if(Enter==0)
{
Uart0_SendByte(0x0D);
Uart0_SendByte(0x0A);
}
}
#pragma interrupt_handler uart0_rx_isr:19
void uart0_rx_isr(void)
{
//uart has received a character in UDR0
static uint8 number;
static uint8 Recevive_Data_Flag;//串口接收数据标志
static uint8 Uart_Recevive_Data[7]={"0"};//串口接收数据缓存
if(UDR0==0x5a)
{
number=0;//包头已经到达
Recevive_Data_Flag=1;
}
if(Recevive_Data_Flag==1)
{
Uart_Recevive_Data[number]=UDR0;
//Uart0_SendByte(UDR0);
number++;
//Uart_TXD_String("ok",0);
}
//Uart0_SendByte(Recevive_Data_Flag);
if(number==7)
{
SD2405Time.year=Uart_Recevive_Data[1];
SD2405Time.month=Uart_Recevive_Data[2];
SD2405Time.day=Uart_Recevive_Data[3];
SD2405Time.hour=Uart_Recevive_Data[4];
SD2405Time.minute=Uart_Recevive_Data[5];
SD2405Time.second=Uart_Recevive_Data[6];
//Recevive_Data_Flag==0;
SD2405_SetTime();
Uart0_SendByte(Uart_Recevive_Data[0]);
Uart0_SendByte(Uart_Recevive_Data[1]);
Uart0_SendByte(Uart_Recevive_Data[2]);
Uart0_SendByte(Uart_Recevive_Data[3]);
Uart0_SendByte(Uart_Recevive_Data[4]);
Uart0_SendByte(Uart_Recevive_Data[5]);
Uart0_SendByte(Uart_Recevive_Data[6]);
//Uart_TXD_String(Uart_Recevive_Data,0);
//Uart_TXD_String("ok,you have succeed",0);
}
}
#endif
如我发:5a 10 12 04 12 33 45 应该回5a 10 12 04 12 33 45 但它回的是45 5a 10 12 04 12 33,,请大侠指点啊,,,谢谢
(原文件名:ee.jpg)
(原文件名:未命名.jpg) |
|