一、使用proteus绘制简单的电路图,用于后续仿真
二、编写程序
/********************************************************************************************************************
---- @Project: LED-74HC595
---- @File: main.c
---- @Edit: ZHQ
---- @Version: V1.0
---- @CreationTime: 20200524
---- @ModifiedTime: 20200524
---- @Description: 两片联级的74HC595驱动的16个LED灯交叉闪烁。比如,先是第1,3,5,7,9,11,13,15八个灯亮,其它的灯都灭。然后再反过来,原来亮的就灭,原来灭的就亮。交替闪烁。
---- 单片机:AT89C52
********************************************************************************************************************/
#include "reg52.h"
/*——————宏定义——————*/
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000) /*1ms timer calculation method in 12Tmode*/
#define const_time_level 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 = 0; /*步骤变量*/
unsigned int uiTimeCnt = 0; /*统计定时中断次数的延时计数器*/
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= 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(void)
{
switch(ucLedStep)
{
case 0:
if(uiTimeCnt >= const_time_level)
{
uiTimeCnt = 0; /*时间计数器清零*/
ucLED1 = 1; /*每个变量都代表一个LED灯的状态*/
ucLED2 = 0;
ucLED3 = 1;
ucLED4 = 0;
ucLED5 = 1;
ucLED6 = 0;
ucLED7 = 1;
ucLED8 = 0;
ucLED9 = 1;
ucLED10 = 0;
ucLED11 = 1;
ucLED12 = 0;
ucLED13 = 1;
ucLED14 = 0;
ucLED15 = 1;
ucLED16 = 0;
ucLed_update = 1; /*更新显示*/
ucLedStep = 1;
}
break;
case 1:
if(uiTimeCnt >= const_time_level)
{
uiTimeCnt = 0; /*时间计数器清零*/
ucLED1 = 0; /*每个变量都代表一个LED灯的状态*/
ucLED2 = 1;
ucLED3 = 0;
ucLED4 = 1;
ucLED5 = 0;
ucLED6 = 1;
ucLED7 = 0;
ucLED8 = 1;
ucLED9 = 0;
ucLED10 = 1;
ucLED11 = 0;
ucLED12 = 1;
ucLED13 = 0;
ucLED14 = 1;
ucLED15 = 0;
ucLED16 = 1;
ucLed_update = 1; /*更新显示*/
ucLedStep = 0;
}
break;
}
}
/**
* @brief 定时器0中断函数
* @param 无
* @retval 无
**/
void ISR_T0(void) interrupt 1
{
TF0 = 0; /*清除中断标志*/
TR0 = 0; /*关中断*/
if(uiTimeCnt < 0xffff) /*设定这个条件,防止uiTimeCnt超范围。*/
{
uiTimeCnt ++;
}
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();
/*LED更新函数*/
LED_Update();
}
}
三、仿真实现