基于STM32的74HC595 IO扩展板卡设计

描述

自己做了一块74HC595的IO扩展板卡,出于灵活考虑,没有都采用级联的方式,而是2组多芯片级联的结构。基于STM32平台设计。
扩展板卡

结构示意图

扩展板卡

74HV595内部结构图

头文件:

#ifndef __74HC595_H__
#define __74HC595_H__


#include "main.h"


typedef struct{
  GPIO_TypeDef* GPIOx;
  uint16_t GPIO_Pin;
}strHC595_Port;


//非级联74HC595芯片组数
#define HC595_GROUP_NUMBER  2


/* 第一组芯片 */
//使能
#define HC595_OE1_RCC_GPIOX_EN      __HAL_RCC_GPIOA_CLK_ENABLE()
#define HC595_OE1_GPIO          GPIOA
#define HC595_OE1_PIN          GPIO_PIN_4
//锁存
#define HC595_RCLK1_RCC_GPIOX_EN    __HAL_RCC_GPIOA_CLK_ENABLE()
#define HC595_RCLK1_GPIO        GPIOA
#define HC595_RCLK1_PIN          GPIO_PIN_6
//时钟
#define HC595_SRCLK1_RCC_GPIOX_EN    __HAL_RCC_GPIOA_CLK_ENABLE()
#define HC595_SRCLK1_GPIO        GPIOA
#define HC595_SRCLK1_PIN        GPIO_PIN_7
//清除
#define HC595_SRCLR1_RCC_GPIOX_EN    __HAL_RCC_GPIOA_CLK_ENABLE()
#define HC595_SRCLR1_GPIO        GPIOA
#define HC595_SRCLR1_PIN        GPIO_PIN_5
//数据
#define HC595_DATA1_RCC_GPIOX_EN    __HAL_RCC_GPIOC_CLK_ENABLE()
#define HC595_DATA1_GPIO        GPIOC
#define HC595_DATA1_PIN          GPIO_PIN_4




/* 第二组芯片 */
//使能
#define HC595_OE2_RCC_GPIOX_EN      __HAL_RCC_GPIOA_CLK_ENABLE()
#define HC595_OE2_GPIO          GPIOB
#define HC595_OE2_PIN          GPIO_PIN_3
//锁存
#define HC595_RCLK2_RCC_GPIOX_EN    __HAL_RCC_GPIOB_CLK_ENABLE()
#define HC595_RCLK2_GPIO        GPIOB
#define HC595_RCLK2_PIN          GPIO_PIN_5
//时钟
#define HC595_SRCLK2_RCC_GPIOX_EN    __HAL_RCC_GPIOB_CLK_ENABLE()
#define HC595_SRCLK2_GPIO        GPIOB
#define HC595_SRCLK2_PIN        GPIO_PIN_6
//清除
#define HC595_SRCLR2_RCC_GPIOX_EN    __HAL_RCC_GPIOB_CLK_ENABLE()
#define HC595_SRCLR2_GPIO        GPIOB
#define HC595_SRCLR2_PIN        GPIO_PIN_4
//数据
#define HC595_DATA2_RCC_GPIOX_EN    __HAL_RCC_GPIOB_CLK_ENABLE()
#define HC595_DATA2_GPIO        GPIOB
#define HC595_DATA2_PIN          GPIO_PIN_7


extern void HC595_Init(void);
extern uint8_t HC595_write(uint8_t group,uint8_t nbit,uint32_t datas);


#endif /*__74HC595_H__*/

源文件:

#include "74HC595.h"


strHC595_Port HC595_OE_Port[HC595_GROUP_NUMBER]=
{        
  {HC595_OE1_GPIO,HC595_OE1_PIN},
  {HC595_OE2_GPIO,HC595_OE2_PIN},
  //元素个数与HC595_GROUP_NUMBER对应
};


strHC595_Port HC595_RCLK_Port[HC595_GROUP_NUMBER]=
{        
  {HC595_RCLK1_GPIO,HC595_RCLK1_PIN},
  {HC595_RCLK2_GPIO,HC595_RCLK2_PIN},
  //元素个数与HC595_GROUP_NUMBER对应
};


strHC595_Port HC595_SRCLK_Port[HC595_GROUP_NUMBER]=
{        
  {HC595_SRCLK1_GPIO,HC595_SRCLK1_PIN},
  {HC595_SRCLK2_GPIO,HC595_SRCLK2_PIN},
  //元素个数与HC595_GROUP_NUMBER对应
};


strHC595_Port HC595_SRCLR_Port[HC595_GROUP_NUMBER]=
{        
  {HC595_SRCLR1_GPIO,HC595_SRCLR1_PIN},
  {HC595_SRCLR2_GPIO,HC595_SRCLR2_PIN},
  //元素个数与HC595_GROUP_NUMBER对应
};


strHC595_Port HC595_DATA_Port[HC595_GROUP_NUMBER]=
{        
  {HC595_DATA1_GPIO,HC595_DATA1_PIN},
  {HC595_DATA2_GPIO,HC595_DATA2_PIN},
 //元素个数与HC595_GROUP_NUMBER对应
};
//操作接口宏定义
#define HC595_OE_PIN(X)      HC595_OE_Port[X].GPIO_Pin
#define HC595_OE_GPIO(X)    HC595_OE_Port[X].GPIOx  
#define HC595_OE_H(X)      HAL_GPIO_WritePin(HC595_OE_GPIO(X), HC595_OE_PIN(X), GPIO_PIN_SET)
#define HC595_OE_L(X)      HAL_GPIO_WritePin(HC595_OE_GPIO(X), HC595_OE_PIN(X), GPIO_PIN_RESET)


#define HC595_RCLK_PIN(X)    HC595_RCLK_Port[X].GPIO_Pin
#define HC595_RCLK_GPIO(X)    HC595_RCLK_Port[X].GPIOx  
#define HC595_RCLK_H(X)      HAL_GPIO_WritePin(HC595_RCLK_GPIO(X), HC595_RCLK_PIN(X), GPIO_PIN_SET)
#define HC595_RCLK_L(X)      HAL_GPIO_WritePin(HC595_RCLK_GPIO(X), HC595_RCLK_PIN(X), GPIO_PIN_RESET)


#define HC595_SRCLK_PIN(X)    HC595_SRCLK_Port[X].GPIO_Pin
#define HC595_SRCLK_GPIO(X)    HC595_SRCLK_Port[X].GPIOx  
#define HC595_SRCLK_H(X)    HAL_GPIO_WritePin(HC595_SRCLK_GPIO(X), HC595_SRCLK_PIN(X), GPIO_PIN_SET)
#define HC595_SRCLK_L(X)    HAL_GPIO_WritePin(HC595_SRCLK_GPIO(X), HC595_SRCLK_PIN(X), GPIO_PIN_RESET)


#define HC595_SRCLR_PIN(X)    HC595_SRCLR_Port[X].GPIO_Pin
#define HC595_SRCLR_GPIO(X)    HC595_SRCLR_Port[X].GPIOx  
#define HC595_SRCLR_H(X)    HAL_GPIO_WritePin(HC595_SRCLR_GPIO(X), HC595_SRCLR_PIN(X), GPIO_PIN_SET)
#define HC595_SRCLR_L(X)    HAL_GPIO_WritePin(HC595_SRCLR_GPIO(X), HC595_SRCLR_PIN(X), GPIO_PIN_RESET)


#define HC595_DATA_PIN(X)    HC595_DATA_Port[X].GPIO_Pin
#define HC595_DATA_GPIO(X)    HC595_DATA_Port[X].GPIOx  
#define HC595_DATA_H(X)      HAL_GPIO_WritePin(HC595_DATA_GPIO(X), HC595_DATA_PIN(X), GPIO_PIN_SET)
#define HC595_DATA_L(X)      HAL_GPIO_WritePin(HC595_DATA_GPIO(X), HC595_DATA_PIN(X), GPIO_PIN_RESET)


void HC595_delay_us(uint8_t t_us)
{
  //修改为系统中对应的微秒级延时函数,根据实际需求可以换成更小的ns级延时
  delay_us(t_us);
}
/*
*描述:初始化HC595_GROUP_NUMBER组芯片对应的控制引脚
*/
void HC595_Init(void)
{
  GPIO_InitTypeDef GPIO_InitStruct = {0};
  uint8_t i = 0;

  /* GPIO Ports Clock Enable */
  //第一组芯片引脚时钟
  HC595_OE1_RCC_GPIOX_EN;
  HC595_RCLK1_RCC_GPIOX_EN;
  HC595_SRCLK1_RCC_GPIOX_EN;
  HC595_SRCLR1_RCC_GPIOX_EN;
  HC595_DATA1_RCC_GPIOX_EN;
  //第二组芯片引脚时钟
  HC595_OE2_RCC_GPIOX_EN;
  HC595_RCLK2_RCC_GPIOX_EN;
  HC595_SRCLK2_RCC_GPIOX_EN;
  HC595_SRCLR2_RCC_GPIOX_EN;
  HC595_DATA2_RCC_GPIOX_EN;
  /*Configure GPIO pin */
  for(i=0; i
打开APP阅读更多精彩内容
声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分