51/AVR单片机技术驿站!  <在线翻译>     便利工具    特色网址   无弹窗、无插件的绿色站点...  英才招聘   学历查询  喜欢>>收藏我站 

当前位置:首页 > 单片机源码 > 详细内容
51单片机的串口通信程序(C语言)
发布时间:2009/6/2  阅读次数:4343  字体大小: 【】 【】【

作者:佚名    来源:不详

#include <reg52.h>  
#include<intrins.h>
#include <stdio.h>
#include <math.h>
#define uchar unsigned char
#define uint unsigned int
sbit Key1 = P2^3;
sbit Key2 = P2^2;
sbit Key3 = P2^1;
sbit Key4 = P2^0;
sbit BELL = P3^6;
sbit CONNECT = P3^7;
unsigned int   Key1_flag = 0;
unsigned int   Key2_flag = 0;
unsigned int   Key3_flag = 0;
unsigned int   Key4_flag = 0;
unsigned char b;
unsigned char code Num[21]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,
                                                       0x90,0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,
                                                       0x10,0x89};
unsigned char code Disdigit[4] = {0x7F,0xBF,0xDF,0xEF};
unsigned char Disbuf[4];
void delayms(uint t)
{
  uint i;
  while(t--)
  {
    /* 对于11.0592M时钟,约延时1ms */
    for (i=0;i<125;i++)
    {}
  }
}
  
//-----------------------------------------------------
void SendData(uchar Dat)
{
uchar i=0;
SBUF = Dat;
while (1)
   {
       if(TI)
           {
               TI=0;
               break;
     }
   }
}
void ScanKey()
{
       if(Key1 == 0)
       {
             delayms(100);
       if(Key1 == 0)
       {
             Key1_flag = 1;
             Key2_flag = 0;
       Key3_flag = 0;
       Key4_flag = 0;
             Key1 = 1;
       }
       else;
       }
       if(Key2 == 0)
       {
       delayms(100);
       if(Key2 == 0)
       {
             Key2_flag = 1;
             Key1_flag = 0;
       Key3_flag = 0;
       Key4_flag = 0;
       Key2 = 1;
       }
       else;
       }
     if(Key3 == 0)
       {
       delayms(50);
       if(Key3 == 0)
       {
             Key3_flag = 1;
             Key1_flag = 0;
       Key2_flag = 0;
       Key4_flag = 0;
       Key3 = 1;
       }
       else;
       }
  if(Key4 == 0)
       {
       delayms(50);
       if(Key4 == 0)
       {
             Key4_flag = 1;
             Key1_flag = 0;
       Key2_flag = 0;
       Key3_flag = 0;
       Key4 = 1;
       }
       else;
       }
  else;  
}
void KeyProc()
{
       if(Key1_flag)
       {
         TR1 = 1;
   SendData(0x55);  
         Key1_flag = 0;        
  }    
  else if(Key2_flag)
       {
         TR1 = 1;
         SendData(0x11);      
         Key2_flag = 0;  
       }
       else if(Key3_flag)
       {
       P1=0xff;
       BELL = 0;
  CONNECT = 1;
       Key3_flag = 0;
       }
  else if(Key4_flag)
  {
  CONNECT = 0;
  BELL = 1;
       Key4_flag = 0;
  }
       else;          
}
void Initdisplay(void)
{
Disbuf[0] = 1;
Disbuf[1] = 2;
Disbuf[2] = 3;
Disbuf[3] = 4;
}
void Display()         //显示
{
unsigned int         i = 0;
unsigned int temp,count;
               temp = Disdigit[count];      
    P2 =temp;
    temp = Disbuf[count];
    temp = Num[temp];
               P0 =temp;
               count++;          
       if   (count==4)
               count=0;              
}    
  
void time0() interrupt 1   using 2
{  
     Display();    
   TH0 = (65535 - 2000)/256;
   TL0 = (65535 - 2000)%256;
}
  
void main()
{
Initdisplay();
TMOD = 0x21;
TH0 = (65535 - 2000)/256;
TL0 = (65535 - 2000)%256;
TR0 = 1;
ET0 = 1;  
TH1 = 0xFD; //11.0592M
TL1 = 0xFD;
PCON&=0x80;
TR1 = 1;
ET1 = 1;
SCON = 0x40; //串口方式
REN = 1;
PT1 = 0;
PT0 = 1;
EA = 1;
   while(1)
   {
     ScanKey();
     KeyProc();        
    
     if(RI)
     {
     Disbuf[0] = 0;
     Disbuf[1] = 20;
     Disbuf[2] = SBUF>>4;
     Disbuf[3] = SBUF&0x0f;
     RI = 0;
     }
     else;      
   }
}
  
我要评论
  • 匿名发表
  • [添加到收藏夹]
  • 发表评论:(匿名发表无需登录,已登录用户可直接发表。) 登录状态:未登录
最新评论
所有评论[1]
  • 评论人:[nongnongqing] 时间: [2010/4/17 9:45:04] IP:[218.62.42.*]
  • 不错

网站导航 管理登陆 ┊ 免责声明 问题反馈  友链说明
本站部分内容来自网络共享资源,如有冒犯您的权利请来信告之删除或纠正!
不得对本站进行复制、盗链或镜像,转载内容须获得同意或授权;欢迎友情链接、站务合作!

    我要报警 Alexa
 mcusy_cn#126.com (请把#改成@) 交流:522422171
本站学习交流群:138..158(高级群1-)、77930286(高级群2)、61804809(群3)
Copyright© MCUSY All Rights Reserved
本站网警备案号: WZ36040002485
  ICP备案证书号:粤ICP备09034963号