/*****************************************************************************
FileName : hc595.c
Function : 74HC595驱动程序---驱动74HC595,3入8出
Author : mike
Email : hxtiou@163.com
Version : V1.0
Date : 2019-07-12
Note :
*****************************************************************************/
#define u8 unsigned char
#define BIT(n) (1 << n)
#define HC595_INIT() //初始化为输出
#define HC595_SI_HIGH() P2 |= BIT(3)//Serial data---串行数据输入
#define HC595_SI_LOW() P2 &= ~BIT(3)
#define HC595_SCK_HIGH() P2 |= BIT(6)//storage register clock---存储寄存器时钟
#define HC595_SCK_LOW() P2 &= ~BIT(6)
#define HC595_RCK_HIGH() P2 |= BIT(4)//shift register clock---移位寄存器时钟
#define HC595_RCK_LOW() P2 &= ~BIT(4)
void hc595_send_data(u8 dat)//写数据---1片74HC595
{
u8 i;
for(i = 0;i < 8;i ++)
{
if(dat & 0x80)
{
HC595_SI_LOW(); //低亮< 如果需要高亮HC595_SI_HIGH() >
}
else
{
HC595_SI_HIGH();
}
dat <<= 1;
HC595_SCK_LOW(); //从SCK产生一上升沿(移入数据)
//asm("nop");asm("nop");asm("nop");asm("nop"); //稍做延时
HC595_SCK_HIGH();
}
//送数据
HC595_RCK_LOW();
//asm("nop");asm("nop");asm("nop");asm("nop"); //稍做延时
HC595_RCK_HIGH();
as//asm("nop");asm("nop");asm("nop");asm("nop"); //稍做延时
HC595_RCK_LOW();
}
void hc595_send_data(u32 dat)//写数据---4片74HC595级联
{
u8 i;
for(i = 0;i < 32;i ++)
{
if(dat & 0x80000000)
{
HC595_SI_LOW(); //低亮 <如果需要高亮HC595_SI_HIGH() >
}
else
{
HC595_SI_HIGH();
}
dat <<= 1;
HC595_SCK_LOW();//从SCK产生一上升沿(移入数据)
//asm("nop");asm("nop");asm("nop");asm("nop"); //稍做延时
HC595_SCK_HIGH();
}
//送数据
HC595_RCK_LOW();
//asm("nop");asm("nop");asm("nop");asm("nop"); //稍做延时
HC595_RCK_HIGH();
//asm("nop");asm("nop");asm("nop");asm("nop"); //稍做延时
HC595_RCK_LOW();
}