把74HC595驱动程序翻译成类似单片机IO口直接驱动的方式

/**原理图以及各74HC595的IO口功能见上一例****/

#include "REG52.H"
#define const_time_level 200
void initial_myself();
void initial_peripheral();
void delay_short(unsigned int uiDelayShort);
void delay_long(unsigned int uiDelayLong);
void led_flicker();
void hc595_drive(unsigned char ucLedStatusTemp08_01);
void led_updata(); //LED更新函数 
void T0_time(); //定时中断函数
sbit hc595_sh_dr=P3^6;  //上升沿时,数据寄存器数据移位
sbit hc595_st_dr=P3^5;  //上升沿时移位寄存器的数据进入数据寄存器,下降沿时数据不变。当移位结束后,会产生一个正脉冲,用于更新显示数据。
sbit hc595_ds_dr=P3^4;  //串行数据输入端,级联的话接上一级的Q7。
unsigned char ucLed_dr1=1;  //8个灯,1代表灭,0代表亮
unsigned char ucLed_dr2=1;
unsigned char ucLed_dr3=1;
unsigned char ucLed_dr4=1;
unsigned char ucLed_dr5=1;
unsigned char ucLed_dr6=1;
unsigned char ucLed_dr7=1;
unsigned char ucLed_dr8=1;
unsigned char ucLed_updata=0; //刷新变量。每次更改LED灯的状态都要更新一次
unsigned char ucLedStep=0; //步骤变量
unsigned int uiTimeCnt=0; //统计定时中断次数的延时计数器
unsigned char ucLedStatus08_01=0; //代表底层74HC595输出状态的中间变量
void main()
{
 initial_myself();
 delay_long(100);
 initial_peripheral();
 while(1)
 {
  led_flicker();
  led_updata(); //LED更新函数
 }
}
/*把74HC595驱动程序翻译成类似单片机IO口直接驱动方式的过程。每次更新LED输出,记得都要把ucLed_uodata置1表示更新*/
void led_updata() //LED更新函数
{
 if(ucLed_updata==1)
 {
  ucLed_updata=0;  //及时清零,让它产生只更新一次的效果,避免一直更新
  if(ucLed_dr1==1)
   ucLedStatus08_01=ucLedStatus08_01|0x01;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xfe;
  
  if(ucLed_dr2==1)
   ucLedStatus08_01=ucLedStatus08_01|0x02;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xfd;
  
  if(ucLed_dr3==1)
   ucLedStatus08_01=ucLedStatus08_01|0x04;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xfb;
  
  if(ucLed_dr4==1)
   ucLedStatus08_01=ucLedStatus08_01|0x08;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xf7;
  
  if(ucLed_dr5==1)
   ucLedStatus08_01=ucLedStatus08_01|0x10;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xef;
  
  if(ucLed_dr6==1)
   ucLedStatus08_01=ucLedStatus08_01|0x20;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xdf;
  
  if(ucLed_dr7==1)
   ucLedStatus08_01=ucLedStatus08_01|0x40;
  else
   ucLedStatus08_01=ucLedStatus08_01&0xbf;
  
  if(ucLed_dr8==1)
   ucLedStatus08_01=ucLedStatus08_01|0x80;
  else
   ucLedStatus08_01=ucLedStatus08_01&0x7f;
  
  
  hc595_drive(ucLedStatus08_01); //74HC595底层驱动函数
 }
}

void hc595_drive(unsigned char ucLedStatusTemp08_01)
{
 unsigned char i;
 unsigned char ucTempData;
 hc595_sh_dr=0;
 hc595_st_dr=0;
 
 ucTempData=ucLedStatusTemp08_01; //送8位
 for(i=0;i<8;i++)
 {
  if(ucTempData>=0x80) //更新数据
   hc595_ds_dr=1;
  else
   hc595_ds_dr=0;
  
  hc595_sh_dr=0; //SH引脚的上升沿把数据送入寄存器
  delay_short(15);
  hc595_sh_dr=1;
  delay_short(15);
  
  ucTempData=ucTempData<<1;
 }
 
 hc595_st_dr=0;
 delay_short(15);
 hc595_st_dr=1;  //ST引脚负责把寄存器的数据更新输出到74HC595的输出引脚上并且锁存起来。
 delay_short(15);
 
 hc595_sh_dr=0; //拉低,抗干扰就增强
 hc595_st_dr=0;
 hc595_ds_dr=0;
}
void led_flicker() // 第三区  LED闪烁应用程序
{
 switch(ucLedStep)
 {
  case 0:
   if(uiTimeCnt>const_time_level) //时间到
   {
    uiTimeCnt=0;
    
    ucLed_dr1=1;  //每个变量都代表一个LED灯的状态
    ucLed_dr2=0;
    ucLed_dr3=1;
    ucLed_dr4=0;
    ucLed_dr5=1;
    ucLed_dr6=0;
    ucLed_dr7=1;
    ucLed_dr8=0;
    
    ucLed_updata=1; //更新显示
    ucLedStep=1; //切换到下一步骤
   }
   break;
   
  case 1:
   if(uiTimeCnt>const_time_level)
   {
    uiTimeCnt=0;
    
    ucLed_dr1=0;  //每个变量都代表一个LED灯的状态
    ucLed_dr2=1;
    ucLed_dr3=0;
    ucLed_dr4=1;
    ucLed_dr5=0;
    ucLed_dr6=1;
    ucLed_dr7=0;
    ucLed_dr8=1;
    
    ucLed_updata=1; //更新显示
    ucLedStep=0; //返回到上一步骤
   }
   break;
 }
void T0_time() interrupt 1
{
 TF0=0; //清除中断标志
 TR0=0; //关中断
 if(uiTimeCnt<0xffff) //设定这个条件,防止uiTimeCnt超范围
 {
  uiTimeCnt++; //累加定时中断的次数
 }
 
 TH0=0xf8;
 TL0=0x2f;
 TR0=1;  //开中断
}
void delay_short(unsigned int uiDelayShort)
{
 unsigned int i;
 for(i=0;i<uiDelayShort;i++)
  ;
}
void delay_long(unsigned int uiDelayLong)
{
 unsigned int i;
 unsigned int j;
 for(i=0;i<uiDelayLong;i++)
  for(j=0;j<500;j++)
   ;
}
void initial_myself()  //第一区  初始化单片机
{
 TMOD=0x01;
 TH0=0xf8;
 TL0=0x2f;
}
void initial_peripheral()
{
 EA=1;
 ET0=1;
 TR0=1; 
}
 

猜你喜欢

转载自www.cnblogs.com/TheFly/p/12009981.html