一、使用proteus绘制简单的电路图,用于后续仿真
二、编写程序
/********************************************************************************************************************
---- @Project: LED-74HC595
---- @File: main.c
---- @Edit: ZHQ
---- @Version: V1.0
---- @CreationTime: 20200525
---- @ModifiedTime: 20200525
---- @Description: 第1个至第8个LED灯,先依次逐个亮,再依次逐个灭。第9至第16个LED灯一直灭。
---- 单片机:AT89C52
********************************************************************************************************************/
#include "reg52.h"
/*——————宏定义——————*/
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) /*1ms timer calculation method in 12Tmode*/
#define const_time_level_01_08 400
/*——————变量函数定义及声明——————*/
/*定义74HC595*/
sbit Hc595_Sh = P2^3;
sbit Hc595_St = P2^4;
sbit Hc595_Ds = P2^5;
unsigned char ucLED1 = 0; /*代表16个灯的亮灭状态,0代表灭,1代表亮*/
unsigned char ucLED2 = 0;
unsigned char ucLED3 = 0;
unsigned char ucLED4 = 0;
unsigned char ucLED5 = 0;
unsigned char ucLED6 = 0;
unsigned char ucLED7 = 0;
unsigned char ucLED8 = 0;
unsigned char ucLED9 = 0;
unsigned char ucLED10 = 0;
unsigned char ucLED11 = 0;
unsigned char ucLED12 = 0;
unsigned char ucLED13 = 0;
unsigned char ucLED14 = 0;
unsigned char ucLED15 = 0;
unsigned char ucLED16 = 0;
unsigned char ucLed_update = 0; /*刷新变量。每次更改LED灯的状态都要更新一次。*/
unsigned char ucLedStep_01_08 = 0; /*第1个至第8个LED跑马灯的步骤变量*/
unsigned int uiTimeCnt_01_08 = 0; /*第1个至第8个LED跑马灯的统计定时中断次数的延时计数器*/
unsigned char ucLedStatus16_09 = 0; /*代表底层74HC595输出状态的中间变量*/
unsigned char ucLedStatus08_01 = 0; /*代表底层74HC595输出状态的中间变量*/
/**
* @brief 定时器0初始化函数
* @param 无
* @retval 初始化T0
**/
void Init_T0(void)
{
TMOD = 0x01; /*set timer0 as mode1 (16-bit)*/
TL0 = T1MS; /*initial timer0 low byte*/
TH0 = T1MS >> 8; /*initial timer0 high byte*/
}
/**
* @brief 外围初始化函数
* @param 无
* @retval 初始化外围
**/
void Init_Peripheral(void)
{
ET0 = 1;/*允许定时中断*/
TR0 = 1;/*启动定时中断*/
EA = 1;/*开总中断*/
}
/**
* @brief 初始化函数
* @param 无
* @retval 初始化单片机
**/
void Init(void)
{
Init_T0();
}
/**
* @brief 延时函数
* @param 无
* @retval 无
**/
void Delay_Long(unsigned int uiDelayLong)
{
unsigned int i;
unsigned int j;
for(i=0;i<uiDelayLong;i++)
{
for(j=0;j<500;j++) /*内嵌循环的空指令数量*/
{
; /*一个分号相当于执行一条空语句*/
}
}
}
/**
* @brief 延时函数
* @param 无
* @retval 无
**/
void Delay_Short(unsigned int uiDelayShort)
{
unsigned int i;
for(i=0;i<uiDelayShort;i++)
{
; /*一个分号相当于执行一条空语句*/
}
}
/**
* @brief 595驱动函数
* @param 无
* @retval * 两个联级74HC595的工作过程:
* 每个74HC595内部都有一个8位的寄存器,两个联级起来就有两个寄存器。ST引脚就相当于一个刷新
* 信号引脚,当ST引脚产生一个上升沿信号时,就会把寄存器的数值输出到74HC595的输出引脚并且锁存起来,
* DS是数据引脚,SH是把新数据送入寄存器的时钟信号。也就是说,SH引脚负责把数据送入到寄存器里,ST引脚
* 负责把寄存器的数据更新输出到74HC595的输出引脚上并且锁存起来。
**/
void HC595_Drive(unsigned char ucLedStatusTemp16_09, unsigned char ucLedStatusTemp08_01)
{
unsigned char i;
unsigned char ucTempData;
Hc595_Sh = 0;
Hc595_St = 0;
ucTempData = ucLedStatusTemp16_09; /*先送高8位*/
for(i = 0; i < 8; i ++)
{
if(ucTempData >= 0x80)
{
Hc595_Ds = 1;
}
else
{
Hc595_Ds = 0;
}
Hc595_Sh = 0; /*SH引脚的上升沿把数据送入寄存器*/
Delay_Short(15);
Hc595_Sh = 1;
Delay_Short(15);
ucTempData = ucTempData <<1;
}
ucTempData = ucLedStatusTemp08_01; /*再先送低8位*/
for(i = 0; i < 8; i ++)
{
if(ucTempData >= 0x80)
{
Hc595_Ds = 1;
}
else
{
Hc595_Ds = 0;
}
Hc595_Sh = 0; /*SH引脚的上升沿把数据送入寄存器*/
Delay_Short(15);
Hc595_Sh = 1;
Delay_Short(15);
ucTempData = ucTempData <<1;
}
Hc595_St = 0; /*ST引脚把两个寄存器的数据更新输出到74HC595的输出引脚上并且锁存起来*/
Delay_Short(15);
Hc595_St = 1;
Delay_Short(15);
Hc595_Sh = 0; /*拉低,抗干扰就增强*/
Hc595_St = 0;
Hc595_Ds = 0;
}
/**
* @brief LED更新函数
* @param 无
* @retval
* 把74HC595驱动程序翻译成类似单片机IO口直接驱动方式的过程。
* 每次更新LED输出,记得都要把ucLed_update置1表示更新。
**/
void LED_Update()
{
if(ucLed_update == 1)
{
ucLed_update = 0; /*及时清零,让它产生只更新一次的效果,避免一直更新。*/
if(ucLED1 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x01;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xfe;
}
if(ucLED2 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x02;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xfd;
}
if(ucLED3 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x04;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xfb;
}
if(ucLED4 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x08;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xf7;
}
if(ucLED5 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x10;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xef;
}
if(ucLED6 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x20;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xdf;
}
if(ucLED7 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x40;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0xbf;
}
if(ucLED8 == 1)
{
ucLedStatus08_01 = ucLedStatus08_01 | 0x80;
}
else
{
ucLedStatus08_01 = ucLedStatus08_01 & 0x7f;
}
if(ucLED9 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x01;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xfe;
}
if(ucLED10 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x02;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xfd;
}
if(ucLED11 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x04;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xfb;
}
if(ucLED12 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x08;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xf7;
}
if(ucLED13 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x10;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xef;
}
if(ucLED14 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x20;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xdf;
}
if(ucLED15 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x40;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0xbf;
}
if(ucLED16 == 1)
{
ucLedStatus16_09 = ucLedStatus16_09 | 0x80;
}
else
{
ucLedStatus16_09 = ucLedStatus16_09 & 0x7f;
}
HC595_Drive(ucLedStatus16_09, ucLedStatus08_01);
}
}
/**
* @brief LED闪烁函数
* @param 无
* @retval 无
**/
void LED_Flicker_01_08(void)
{
switch(ucLedStep_01_08)
{
case 0:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED1 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 1;
}
break;
case 1:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED2 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 2;
}
break;
case 2:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED3 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 3;
}
break;
case 3:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED4 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 4;
}
break;
case 4:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED5 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 5;
}
break;
case 5:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED6 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 6;
}
break;
case 6:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED7 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 7;
}
break;
case 7:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED8 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 8;
}
break;
case 8:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED8 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 9;
}
break;
case 9:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED7 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 10;
}
break;
case 10:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED6 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 11;
}
break;
case 11:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED5 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 12;
}
break;
case 12:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED4 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 13;
}
break;
case 13:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED3 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 14;
}
break;
case 14:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED2 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 15;
}
break;
case 15:
if(uiTimeCnt_01_08 >= const_time_level_01_08)
{
uiTimeCnt_01_08 = 0; /*时间计数器清零*/
ucLED1 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep_01_08 = 0;
}
break;
}
}
/**
* @brief 定时器0中断函数
* @param 无
* @retval 无
**/
void ISR_T0(void) interrupt 1
{
TF0 = 0; /*清除中断标志*/
TR0 = 0; /*关中断*/
if(uiTimeCnt_01_08 < 0xffff) /*设定这个条件,防止uiTimeCnt超范围。*/
{
uiTimeCnt_01_08 ++;
}
TL0 = T1MS; /*initial timer0 low byte*/
TH0 = T1MS >> 8; /*initial timer0 high byte*/
TR0 = 1; /*开中断*/
}
/*——————主函数——————*/
/**
* @brief 主函数
* @param 无
* @retval 实现LED灯闪烁
**/
void main()
{
/*单片机初始化*/
Init();
/*延时,延时时间一般是0.3秒到2秒之间,等待外围芯片和模块上电稳定*/
Delay_Long(100);
/*单片机外围初始化*/
Init_Peripheral();
while(1)
{
/*按键服务函数*/
LED_Flicker_01_08();
/*LED更新函数*/
LED_Update();
}
}
三、仿真实现