//网上看了下资料,借鉴了别人的一些好的东西,同时自己封装了下
#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 
小果子 阅读(770) 
评论(0)  编辑 收藏 引用  所属分类: 
单片机