51单片机作为下位机程序

24 篇文章 5 订阅

本代码段是在网上下载,然后根据自己的需求修改的,亲测,效果很好

51单片机作为下位机程序,本代码段实现两个功能:

1、控制LED灯按指定规则显示

2、将接收的上位机数据返回给上位机

源代码在这儿:http://download.csdn.net/detail/tingzhiyi/9173485


#include <REG52.H>
#include <stdio.h>
#include <intrins.h>


#define uchar unsigned char<span style="white-space:pre">	</span>          //宏定义
#define uint unsigned int<span style="white-space:pre">	</span>         //宏定义




uchar b;                             //用来接收数据存储
uchar j=0;
uint i=0;<span style="white-space:pre">							</span> //用来判断相应的子程序
uchar LED1= 0xfe;<span style="white-space:pre">	</span>                 //作为流水灯的开始
uchar LED2=0Xfe;                     //作为跑马灯的开始
uchar LED3 = 0xfE;<span style="white-space:pre">	</span>                 //作为二进制加法灯的开始
uchar LED4=0XFF;                     //作为花样灯的开始
uchar code table[]={0xff,//全灭
0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f,//依次逐个点亮
0xfe,0xfc,0xf8,0xf0,0xe0,0xc0,0x80,0x00,//依次逐个叠加
0x80,0xc0,0xe0,0xf0,0xf8,0xfc,0xfe,0xff,//依次逐个递减
0x7e,0xbd,0xdb,0xe7,0xe7,0xdb,0xbd,0x7e,//两边靠拢后分开
0x7e,0x3c,0x18,0x00,0x00,0x18,0x3c,0x7e,//两边叠加后递减
0x00};//全亮


void delay(void);                    //延时函数的声明
void liushuideng(void);              //流水灯的声明
void paomadeng(void);                //跑马灯的声明
void huayangdeng(void);              //花样灯的声明
void stopLed();                      //停止所有灯的效果
void erjinzhideng(void);             //二进制加法灯的声明
void xianshi(uint j);<span style="white-space:pre">				</span> //显示程序
void init(void);                     //初始化函数


void main (void)
 {      
    init();                          //初始化  
<span style="white-space:pre">	</span>while(1)
    { 
<span style="white-space:pre">	</span> xianshi(i);<span style="white-space:pre">			</span>     <span style="white-space:pre">	</span> //显示
    }
}


void init(void)<span style="white-space:pre">					</span>   //初始化函数的定义
{
        SCON = 0x50;               //REN=1允许串行接受状态,串口工作模式1    <span style="white-space:pre">	</span>       <span style="white-space:pre">	</span>   
<span style="white-space:pre">	</span>    TMOD|= 0x20;               //定时器工作方式2                    
<span style="white-space:pre">		</span>PCON|= 0x80; 
<span style="white-space:pre">		</span>TH1 = 0xF3;<span style="white-space:pre">	</span>              //baud*2  /*  波特率4800、数据位8、停止位1。效验位无 (12M)
    <span style="white-space:pre">	</span>TL1 = 0xF3;         
<span style="white-space:pre">		</span>TR1  = 1; <span style="white-space:pre">				</span>  //开启定时器
<span style="white-space:pre">		</span>PS=1;                     //把串口中断的优先级设为最高                                       
<span style="white-space:pre">		</span>ES   = 1;                 //开串口中断                  
<span style="white-space:pre">		</span>EA   = 1;
 }
void delay(void)<span style="white-space:pre">	</span>             //延时函数的定义
{
uchar i;
uchar ms=250;
<span style="white-space:pre">	</span>while(ms--)
<span style="white-space:pre">	</span>{
<span style="white-space:pre">		</span>for(i = 0; i < 120; i++);
<span style="white-space:pre">	</span>}


}
void liushuideng(void)<span style="white-space:pre">	</span>          //流水灯
{
     P2 = LED1;
<span style="white-space:pre">	</span> delay();<span style="white-space:pre">	</span>   
<span style="white-space:pre">	</span> LED1 = LED1 << 1;        //循环右移1位,点亮下一个LED "<<"为左移位
 <span style="white-space:pre">		</span> if(P2 ==0X00 )
<span style="white-space:pre">	</span> {
<span style="white-space:pre">	</span>    LED1 = 0xfe;       <span style="white-space:pre">	</span>// 0xfe = 1111 1110  
<span style="white-space:pre">	</span>  }       
}


void paomadeng(void)<span style="white-space:pre">	</span>         //跑马灯
{
<span style="white-space:pre">	</span>P2 = LED2;
    LED2 = _crol_(LED2,1);<span style="white-space:pre">	</span>   <span style="white-space:pre">	</span>//循环右移1位,点亮下一个LED  此函数位库函数<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>delay();
}


void huayangdeng(void)<span style="white-space:pre">	</span>        //花样灯
{  <span style="white-space:pre">	</span>  P2=table[j++];
       delay();
<span style="white-space:pre">	</span>   if(j==42)
<span style="white-space:pre">	</span>   j=0;
} 


void erjinzhideng(void)<span style="white-space:pre">	</span>        //二进制加法灯
{
<span style="white-space:pre">	</span>  P2= LED4;
<span style="white-space:pre">	</span>  delay();
<span style="white-space:pre">	</span>  LED4--;
}


void stopLed()
{
<span style="white-space:pre">	</span>P2=0xff;<span style="white-space:pre">	</span>
}


void xianshi(uint j)<span style="white-space:pre">	</span>        //调用相对应的程序
{
   
       if(j==1)<span style="white-space:pre">	</span>  P2=0xfe;<span style="white-space:pre">						</span>//点亮第一个LED
<span style="white-space:pre">	</span>   if(j==2)<span style="white-space:pre">	</span>  P2=0xfd;<span style="white-space:pre">					</span> <span style="white-space:pre">	</span>//点亮第二个LED
<span style="white-space:pre">	</span>   if(j==3)<span style="white-space:pre">	</span>  P2=0xfb;<span style="white-space:pre">					</span>  <span style="white-space:pre">	</span>//点亮第三个LED
<span style="white-space:pre">	</span>   if(j==4)<span style="white-space:pre">	</span>  P2=0xf7;<span style="white-space:pre">						</span>//点亮第四个LED
<span style="white-space:pre">	</span>   if(j==5)<span style="white-space:pre">	</span>  P2=0xef;<span style="white-space:pre">					</span>  <span style="white-space:pre">	</span>//点亮第五个LED
<span style="white-space:pre">	</span>   if(j==6)   P2=0xdf;<span style="white-space:pre">					</span>   <span style="white-space:pre">	</span>//点亮第六个LED
<span style="white-space:pre">	</span>   if(j==7)<span style="white-space:pre">	</span>  P2=0xbf;<span style="white-space:pre">					</span>   <span style="white-space:pre">	</span>//点亮第七个LED
<span style="white-space:pre">	</span>   if(j==8)<span style="white-space:pre">	</span>  P2=0x7f;<span style="white-space:pre">					</span>   <span style="white-space:pre">	</span>//点亮第八个LED
<span style="white-space:pre">	</span>   if(j==9)<span style="white-space:pre">	</span>  liushuideng();<span style="white-space:pre">	</span>            //流水灯
<span style="white-space:pre">	</span>   if(j==10)  paomadeng();<span style="white-space:pre">	</span>            <span style="white-space:pre">	</span>//跑马灯
<span style="white-space:pre">	</span>   if(j==11)  huayangdeng();<span style="white-space:pre">				</span>//花样灯
<span style="white-space:pre">	</span>   if(j==12)  erjinzhideng();<span style="white-space:pre">	</span>            //二进制加法灯
<span style="white-space:pre">	</span>   if(j==13)  stopLed();<span style="white-space:pre">					</span>//停止灯效果
<span style="white-space:pre">	</span>
}
<span style="white-space:pre">			</span>
void com_isr(void) interrupt 4  //串口中断入口函数
{<span style="white-space:pre">		</span>
    ES=0;<span style="white-space:pre">	</span>                    //再中断进行处理程序的时候关闭串口中断<span style="white-space:pre">			</span>         
<span style="white-space:pre">	</span>b=SBUF;<span style="white-space:pre">		</span>            <span style="white-space:pre">	</span>//将接收的数据存于变量中
<span style="white-space:pre">	</span>switch(b)<span style="white-space:pre">						</span>//分支语句,进行判断
<span style="white-space:pre">	</span>{ 
<span style="white-space:pre">	</span>    case '1':i=1;break;         //点亮第一个LED的赋值
<span style="white-space:pre">		</span>case '2':i=2;break;         //点亮第二个LED的赋值
<span style="white-space:pre">	</span>    case '3':i=3;break;         //点亮第三个LED的赋值
<span style="white-space:pre">	</span>    case '4':i=4;break;         //点亮第四个LED的赋值
<span style="white-space:pre">		</span>case '5':i=5;break;         //点亮第五个LED的赋值
<span style="white-space:pre">		</span>case '6':i=6;break;         //点亮第六个LED的赋值
<span style="white-space:pre">	</span>    case '7':i=7;break;         //点亮第七个LED的赋值
<span style="white-space:pre">		</span>case '8':i=8;break;         //点亮第八个LED的赋值
<span style="white-space:pre">		</span>case '9':i=9;break;  <span style="white-space:pre">		</span>//流水灯的赋值
<span style="white-space:pre">		</span>case 'a':i=10;break; <span style="white-space:pre">		</span>//跑马灯的赋值
<span style="white-space:pre">	</span>    case 'b':i=11;break; <span style="white-space:pre">	</span>    //花样灯的赋值
<span style="white-space:pre">		</span>case 'c':i=12;break; <span style="white-space:pre">	</span>    //二进制加法灯的赋值
<span style="white-space:pre">		</span>case 'd':i=13;break;        //停止所有灯效果的赋值<span style="white-space:pre">	</span> 
<span style="white-space:pre">		</span>default:   break;
  <span style="white-space:pre">	</span> }
   <span style="white-space:pre">	</span>   RI=0;<span style="white-space:pre">								</span>//清除中断标志位
<span style="white-space:pre">	</span>   ES=1;<span style="white-space:pre">								</span>//处理完中断程序再开启串口中断
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值