STM32驱动74hc595代码



#include "bsp_74HC595.h"
#define HC595_DS PCout(13)//
#define HC595_OE PAout(0)//
#define HC595_ST_CP        PBout(9)//
#define HC595_SH_CP PBout(8)//
static void HC595_Delay(u32 t)
{
u32 i;
while(t--)
for (i = 0; i < 1; i++);
}


void HC595_Init(void)
{

GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOC, ENABLE); //

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; //
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz; //IO口速度为10MHz
GPIO_Init(GPIOC, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8|GPIO_Pin_9; //
GPIO_Init(GPIOB, &GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0; //
GPIO_Init(GPIOA, &GPIO_InitStructure); //
HC595_OE = 1;
HC595_DS = 1;
HC595_ST_CP = 1;
HC595_SH_CP = 1;
HC595_OE = 0;
}
void Send595Data(u8 data)
{
u8 i;
for (i = 0;i<8;i++)
{
if ( ((data<<i) & 0x80) == 0x80)
{
HC595_DS=1;//数据位置位
}
else
{
HC595_DS=0;//数据位清零
}

HC595_SH_CP=0;//需延时至少24NS
HC595_Delay(1);
HC595_SH_CP=1;
}

HC595_ST_CP=0;//需延时24NS
HC595_Delay(1);
HC595_ST_CP=1;
}