新闻  |   论坛  |   博客  |   在线研讨会
115.2k串行口通信程序
tongxin | 2009-04-13 15:32:22    阅读:825   发布文章

115.2k串行口通信程序
%A //115.2k串行口通信程序,校验方式为简单双向校验,
%A
%A
%A //而且是必要的,因为任何采用中断、crc等的方法
%A
%A
%A //均很难保证速度和避免错误.
%A
%A
%A //总体速度5KB/秒
%A
%A
%A //单片机程序
%A
%A
%A //使用22.1184晶振
%A
%A
%A unsigned char time,b_break,b_break_3;
%A
%A
%A //35.555ms
%A
%A
%A void int_t0(void) interrupt 1 {
%A
%A
%A        TH0=0;
%A
%A
%A        if ((++b_break_3)>2) b_break=1;
%A
%A
%A        time++;
%A
%A
%A }
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A unsigned char rec(void) {
%A
%A
%A        TH0=0;b_break_3=0;b_break=0;
%A
%A
%A        while (RI==0) {
%A
%A
%A               if (b_break) return(1);           //等待pc发送100ms超时
%A
%A
%A        }
%A
%A
%A        RI=0;
%A
%A
%A        SBUF=SBUF;
%A
%A
%A        return(SBUF);
%A
%A
%A }
%A
%A
%A /*返回1表示失败*/
%A
%A
%A bit sen(unsigned char d) {
%A
%A
%A        SBUF=d;
%A
%A
%A        TH0=0;b_break_3=0;b_break=0;
%A
%A
%A        while (RI==0) {
%A
%A
%A               if (b_break) return(1);           //等待pc送回相同字节数据,100ms超时
%A
%A
%A        }
%A
%A
%A        RI=0;
%A
%A
%A        if (SBUF!=d) return(1);
%A
%A
%A        return(0);
%A
%A
%A }
%A
%A
%A
%A
%A
%A
%A void main(void) {
%A
%A
%A        unsigned int i;
%A
%A
%A        unsigned char d;
%A
%A
%A        EA=1;
%A
%A
%A        SCON=0xd8;PCON=0x80;
%A
%A
%A        TMOD=0x21;
%A
%A
%A        TL1=TH1=0xff;TR1=1;
%A
%A
%A        TH0=0;ET0=TR0=1;
%A
%A
%A        while (1) {
%A
%A
%A               d=rec();
%A
%A
%A               if ((!b_break)&&(d==0x55)) {
%A
%A
%A                      d=rec();
%A
%A
%A                      if (!b_break) {
%A
%A
%A                             if (d==0xaa) {
%A
%A
%A                                    //接收到pc送来的要求发送指令0xaa55
%A
%A
%A                                    for (i=0;i<1000;i++) {
%A
%A
%A                                           if (sen(i)) {
%A
%A
%A                                                  //发送错误
%A
%A
%A                                                  break;
%A
%A
%A                                           }
%A
%A
%A                                    }
%A
%A
%A                             } else if (dd==0xaa) {
%A
%A
%A                                    //接收到pc送来的要求接收指令0xaaaa
%A
%A
%A                                    for (i=0;i<1000;i++) {
%A
%A
%A                                           d=rec();
%A
%A
%A                                           if (!b_break) {
%A
%A
%A                                                  //正确接收处理
%A
%A
%A                                           } else {
%A
%A
%A                                                  //接收错误处理
%A
%A
%A                                                  break;
%A
%A
%A                                           }
%A
%A
%A                                    }
%A
%A
%A                             }
%A
%A
%A                      }
%A
%A
%A               }
%A
%A
%A        }
%A
%A
%A }
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A //电脑程序tc2.0
%A
%A
%A #include "stdio.h"
%A
%A
%A #include "dos.h"
%A
%A
%A #include "process.h"
%A
%A
%A #include "string.h"
%A
%A
%A #include "conio.h"
%A
%A
%A #include <dir.h>
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A
%A unsigned char time_out;
%A
%A
%A
%A
%A
%A
%A void init_com1(void) {
%A
%A
%A        outportb(0x3fb,0x80);
%A
%A
%A        outportb(0x3f8,1);
%A
%A
%A        outportb(0x3f9,0x0);
%A
%A
%A        outportb(0x3fb,0x3);
%A
%A
%A }
%A
%A
%A void sen_com1(unsigned char d) {
%A
%A
%A        long i;
%A
%A
%A        time_out=0;
%A
%A
%A        while (!(inportb(0x3fd)&0x20));
%A
%A
%A        outportb(0x3fd,0x0);
%A
%A
%A        outportb(0x3f8,d);
%A
%A
%A        i=clock();
%A
%A
%A        while (1) {
%A
%A
%A               if (clock()>(i+8)) {time_out=1;return;}
%A
%A
%A               if ((inp(0x3fd)&0x1)==1) break;
%A
%A
%A        }
%A
%A
%A        if (inp(0x3f8)!=d) {time_out=2;return;}
%A
%A
%A }
%A
%A
%A unsigned char rec_com1(void) {
%A
%A
%A        long i;
%A
%A
%A        unsigned char d;
%A
%A
%A        time_out=0;
%A
%A
%A        i=clock();
%A
%A
%A        while (1) {
%A
%A
%A               if (clock()>(i+8)) {time_out=1;return(0);}
%A
%A
%A               if ((inp(0x3fd)&1)==1) break;
%A
%A
%A        }
%A
%A
%A        d=inp(0x3f8);
%A
%A
%A        while (!(inportb(0x3fd)&0x20));
%A
%A
%A        outportb(0x3fd,0x0);
%A
%A
%A        outportb(0x3f8,d);
%A
%A
%A        return(d);
%A
%A
%A }
%A
%A
%A void main(void) {
%A
%A
%A        int i;
%A
%A
%A        long st;
%A
%A
%A        init_com1();
%A
%A
%A        st=clock();
%A
%A
%A        sen_com1(0xaa);
%A
%A
%A        if (time_out) {printf("CPU not ready!");exit(-1);}
%A
%A
%A        sen_com1(0xaa);
%A
%A
%A        if (time_out) {printf("CPU not ready!");exit(-1);}
%A
%A
%A        for (i=0;i<1000;i++) {
%A
%A
%A               sen_com1(i);
%A
%A
%A               if (time_out) {printf("CPU not ready!");exit(-1);}
%A
%A
%A        }
%A
%A
%A        printf("Total use %3.2f S.",(clock()-st)/18.2);
%A
%A
%A        exit(1);
%A
%A
%A }
%A
%A%A
%A

*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。

参与讨论
登录后参与讨论
最近文章
寂寞如雪
2009-05-19 19:01:18
夜色花
2009-05-19 18:56:22
没有爱可以重来
2009-05-19 18:54:59
推荐文章
最近访客