1 。74HC595 初始化
#define GPIO_SEG_595CS_PIN GPIO_Pin_9 //
#define GPIO_SEG_595CS_PORT GPIOA //
#define GPIO_SEG_595CLK_PIN GPIO_Pin_10 //
#define GPIO_SEG_595CLK_PORT GPIOA //
#define GPIO_SEG_595DATA_PIN GPIO_Pin_1 //Pin14 ,data
#define GPIO_SEG_595DATA_PORT GPIOB //
static void __Init595Gpio(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_StructInit(&GPIO_InitStructure);
RCC_AHBPeriphClockCmd(SEG_595CS_RCC, ENABLE);
RCC_AHBPeriphClockCmd(SEG_595CLK_RCC, ENABLE);
RCC_AHBPeriphClockCmd(SEG_595DATA_RCC, ENABLE);
// segoutput
// 74HC595 segment cs
GPIO_InitStructure.GPIO_Pin = GPIO_SEG_595CS_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIO_SEG_595CS_PORT, &GPIO_InitStructure);
//74hc595 segment clk
GPIO_InitStructure.GPIO_Pin = GPIO_SEG_595CLK_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIO_SEG_595CLK_PORT, &GPIO_InitStructure);
//74hc595 segment data
GPIO_InitStructure.GPIO_Pin = GPIO_SEG_595DATA_PIN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_Init(GPIO_SEG_595DATA_PORT, &GPIO_InitStructure);
//digit out
//segment
GPIO_SetBits(GPIO_SEG_595CS_PORT, GPIO_SEG_595CS_PIN );
GPIO_SetBits(GPIO_SEG_595CLK_PORT, GPIO_SEG_595CLK_PIN );
GPIO_SetBits(GPIO_SEG_595DATA_PORT, GPIO_SEG_595DATA_PIN );
// digit
}
2。初始化74hc595信號腳
void Init595(void)
{
__Init595Gpio();
__SegOut595CS(1);
// __DigOut595CS(1);
}
``
3。輸出clk信號
static void __SegOut595Clk(uint8_t bEnable)
{
GPIO_WriteBit(GPIO_SEG_595CLK_PORT, GPIO_SEG_595CLK_PIN, (BitAction)bEnable );
}
4。輸出鎖存信號
static void __SegOut595CS(uint8_t bEnable)
{
GPIO_WriteBit(GPIO_SEG_595CS_PORT, GPIO_SEG_595CS_PIN, (BitAction)bEnable );
}
5。輸出數據信號
static void __SegOut595Data(uint8_t bEnable)
{
GPIO_WriteBit(GPIO_SEG_595DATA_PORT, GPIO_SEG_595DATA_PIN, (BitAction)bEnable );
}
6。 數據鎖存
void Output595Load(void)
{
// __SegOut595CS(1);
__SegOut595CS(0);
__SegOut595CS(1);
}
7。寫8bit 數據到74hc595
void SegWrite595(unsigned char iOutData1)
{
unsigned char i;
unsigned char bFlag;
for(i=0;i<8;i++)
{
if(iOutData1&0x80)
{
bFlag=1;
}
else
{
bFlag=0;
}
iOutData1=iOutData1<<1;
__SegOut595Data(bFlag);
__nop();
__nop();
__nop();
__nop();
__SegOut595Clk(0);
__nop();
__nop();
__nop();
__nop();
__SegOut595Clk(1);
__nop();
__nop();
__nop();
__nop(); }
}