//網上看了下資料,借鑒了別人的一些好的東西,同時自己封裝了下
#ifndef __mycom__h__
#define __mycon__h__
#include <string.h>
//數據格式
//W 8
#define SUCCESS 1
#define ERROR 0
#define uchar unsigned char
#define uint unsinged int
#define BUF_LEN 30 //傳沖區大小
#define RECIEVED_MAX_DATA_LEN 16
#define SEND_MAX_DATA_LEN 25
#define END_NUM 4 //結束符長度
typedef void (*PTRFUN)(uchar*,uchar);
PTRFUN ptrFun;
uchar END_CODE[4]="#end";
uchar end_num=0;
uchar data_num=0; //接受數據個數
uchar data_buf[BUF_LEN];//緩沖區大小
uchar data_addr=0; //數據在data_buf位置
bit isComplete=0; //處理
typedef struct
{
char name[10]; //姓名
int age; //年齡
char sex[5]; //性別
}st;
st b;
//初始化
void init_serialcomm(void)
{
SCON = 0x50; //SCON: serail mode 1, 8-bit UART, enable ucvr
TMOD |= 0x20; //TMOD: timer 1, mode 2, 8-bit reload
PCON |= 0x80; //SMOD=1;
TH1 = 0xF4; //Baud:4800 fosc=11.0592MHz
IE |= 0x90; //Enable Serial Interrupt
TR1 = 1; // timer 1 run
// TI=1;
}
//*******數據轉換*********************
uchar decoderData(){
uchar state,i;
state=0;
i=2;
//-----------------------計算地址-------------------------------
while(1){
if( (data_buf[i]>47)&&(data_buf[i]<58) ) // 是‘0’-‘9’?
state = (state*10)+(data_buf[i]-48); // 計算
else if( data_buf[i]==' ' ) break; // 是空格,跳出
else // 非'0'-'9'和' '
{
return ERROR; // 返回錯誤
}
if( i>4 ) // 輸入數字過大
{
return ERROR;
}
i++;
}
data_buf[1]=state;//數據個數
data_addr=++i;
return SUCCESS;
}
//***************************接收處理**************************************
void afterRecived(void)
{
if( decoderData()==ERROR ) return; // 錯誤,返回
if( data_buf[1]>RECIEVED_MAX_DATA_LEN ) // 寫入個數判斷
{
return; // 數據個數太多,返回
}
ptrFun(&data_buf[data_addr],data_buf[1]);
}
//向串口發送一個字符
void send_char_com(unsigned char ch)
{
SBUF=ch;
while(TI==0);
TI=0;
}
//向串口發送一個字符串,strlen為該字符串長度
void send_string_com(unsigned char *str,uchar len)
{
unsigned int k=0;
do
{
send_char_com(*(str + k));
k++;
} while(k < len);
}
void sendBefore(void* p){
char* m=(char*)p;
uchar len=sizeof(st);
send_string_com(m,len);
}
//串口接收中斷函數
void serial () interrupt 4 using 3
{
unsigned char state;
if( RI==1 )
{
state = SBUF; // 緩存接收到的數據
RI = 0; // 接收標志清零
//---------------------檢測結束命令#end---------------------------
if( state==END_CODE[end_num] )
{
end_num++;
if( end_num==END_NUM )
{
end_num = 0;
isComplete = 1;
ES=0;
}
}
else end_num = 0;
//---------------------串口數據處理-------------------------------
if( data_num>SEND_MAX_DATA_LEN )
{
data_num = 0;
//ERROR 接受數據過長
}
data_buf[data_num++] = state;
}
}
#endif
#include <REGX52.H>
#include "51com.h"
#include "util.h"
unsigned char key_map[]={0,7,8,9,'/',4,5,6,'*',1,2,3,'-','c',0,'=','+'};
unsigned char tab[]={0xFE,0x30,0x6d,0x79,0x33,0x5b,0x5f,0x70,0x7f,0x7b};
void process(uchar* p,len){
P1=tab[string_to_int(p,len)];
}
void main(void)
{
init_serialcomm(); //初始化串口
ptrFun=process;
b.age=10;
sendBefore(&b);
//send_string_com("b",1);
while(1)
{
if( isComplete==1 ) // 串口接收到一串數據
{
isComplete = 0; // 標志清零
if( data_buf[0]=='R' )afterRecived();
else if( data_buf[0]=='S' )send_string_com("b",1);
data_num = 0; // 重新開始接收數據
ES = 1; // 允許串口中斷
}
}
}
posted on 2010-08-01 16:11
小果子 閱讀(754)
評論(0) 編輯 收藏 引用 所屬分類:
單片機