自己做了一块74HC595的IO扩展板卡,出于灵活考虑,没有都采用级联的方式,而是2组多芯片级联的结构。基于STM32平台设计。
结构示意图
74HV595内部结构图
头文件:
#ifndef__74HC595_H__#define__74HC595_H__#include"main.h"typedefstruct{GPIO_TypeDef* GPIOx;uint16_tGPIO_Pin; }strHC595_Port;//非级联74HC595芯片组数#defineHC595_GROUP_NUMBER 2/* 第一组芯片 *///使能#defineHC595_OE1_RCC_GPIOX_EN __HAL_RCC_GPIOA_CLK_ENABLE()#defineHC595_OE1_GPIO GPIOA#defineHC595_OE1_PIN GPIO_PIN_4//锁存#defineHC595_RCLK1_RCC_GPIOX_EN __HAL_RCC_GPIOA_CLK_ENABLE()#defineHC595_RCLK1_GPIO GPIOA#defineHC595_RCLK1_PIN GPIO_PIN_6//时钟#defineHC595_SRCLK1_RCC_GPIOX_EN __HAL_RCC_GPIOA_CLK_ENABLE()#defineHC595_SRCLK1_GPIO GPIOA#defineHC595_SRCLK1_PIN GPIO_PIN_7//清除#defineHC595_SRCLR1_RCC_GPIOX_EN __HAL_RCC_GPIOA_CLK_ENABLE()#defineHC595_SRCLR1_GPIO GPIOA#defineHC595_SRCLR1_PIN GPIO_PIN_5//数据#defineHC595_DATA1_RCC_GPIOX_EN __HAL_RCC_GPIOC_CLK_ENABLE()#defineHC595_DATA1_GPIO GPIOC#defineHC595_DATA1_PIN GPIO_PIN_4/* 第二组芯片 *///使能#defineHC595_OE2_RCC_GPIOX_EN __HAL_RCC_GPIOA_CLK_ENABLE()#defineHC595_OE2_GPIO GPIOB#defineHC595_OE2_PIN GPIO_PIN_3//锁存#defineHC595_RCLK2_RCC_GPIOX_EN __HAL_RCC_GPIOB_CLK_ENABLE()#defineHC595_RCLK2_GPIO GPIOB#defineHC595_RCLK2_PIN GPIO_PIN_5//时钟#defineHC595_SRCLK2_RCC_GPIOX_EN __HAL_RCC_GPIOB_CLK_ENABLE()#defineHC595_SRCLK2_GPIO GPIOB#defineHC595_SRCLK2_PIN GPIO_PIN_6//清除#defineHC595_SRCLR2_RCC_GPIOX_EN __HAL_RCC_GPIOB_CLK_ENABLE()#defineHC595_SRCLR2_GPIO GPIOB#defineHC595_SRCLR2_PIN GPIO_PIN_4//数据#defineHC595_DATA2_RCC_GPIOX_EN __HAL_RCC_GPIOB_CLK_ENABLE()#defineHC595_DATA2_GPIO GPIOB#defineHC595_DATA2_PIN GPIO_PIN_7externvoidHC595_Init(void);externuint8_tHC595_write(uint8_tgroup,uint8_tnbit,uint32_tdatas);#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对应};//操作接口宏定义#defineHC595_OE_PIN(X) HC595_OE_Port[X].GPIO_Pin#defineHC595_OE_GPIO(X) HC595_OE_Port[X].GPIOx#defineHC595_OE_H(X) HAL_GPIO_WritePin(HC595_OE_GPIO(X), HC595_OE_PIN(X), GPIO_PIN_SET)#defineHC595_OE_L(X) HAL_GPIO_WritePin(HC595_OE_GPIO(X), HC595_OE_PIN(X), GPIO_PIN_RESET)#defineHC595_RCLK_PIN(X) HC595_RCLK_Port[X].GPIO_Pin#defineHC595_RCLK_GPIO(X) HC595_RCLK_Port[X].GPIOx#defineHC595_RCLK_H(X) HAL_GPIO_WritePin(HC595_RCLK_GPIO(X), HC595_RCLK_PIN(X), GPIO_PIN_SET)#defineHC595_RCLK_L(X) HAL_GPIO_WritePin(HC595_RCLK_GPIO(X), HC595_RCLK_PIN(X), GPIO_PIN_RESET)#defineHC595_SRCLK_PIN(X) HC595_SRCLK_Port[X].GPIO_Pin#defineHC595_SRCLK_GPIO(X) HC595_SRCLK_Port[X].GPIOx#defineHC595_SRCLK_H(X) HAL_GPIO_WritePin(HC595_SRCLK_GPIO(X), HC595_SRCLK_PIN(X), GPIO_PIN_SET)#defineHC595_SRCLK_L(X) HAL_GPIO_WritePin(HC595_SRCLK_GPIO(X), HC595_SRCLK_PIN(X), GPIO_PIN_RESET)#defineHC595_SRCLR_PIN(X) HC595_SRCLR_Port[X].GPIO_Pin#defineHC595_SRCLR_GPIO(X) HC595_SRCLR_Port[X].GPIOx#defineHC595_SRCLR_H(X) HAL_GPIO_WritePin(HC595_SRCLR_GPIO(X), HC595_SRCLR_PIN(X), GPIO_PIN_SET)#defineHC595_SRCLR_L(X) HAL_GPIO_WritePin(HC595_SRCLR_GPIO(X), HC595_SRCLR_PIN(X), GPIO_PIN_RESET)#defineHC595_DATA_PIN(X) HC595_DATA_Port[X].GPIO_Pin#defineHC595_DATA_GPIO(X) HC595_DATA_Port[X].GPIOx#defineHC595_DATA_H(X) HAL_GPIO_WritePin(HC595_DATA_GPIO(X), HC595_DATA_PIN(X), GPIO_PIN_SET)#defineHC595_DATA_L(X) HAL_GPIO_WritePin(HC595_DATA_GPIO(X), HC595_DATA_PIN(X), GPIO_PIN_RESET)voidHC595_delay_us(uint8_tt_us){//修改为系统中对应的微秒级延时函数,根据实际需求可以换成更小的ns级延时delay_us(t_us); }/* *描述:初始化HC595_GROUP_NUMBER组芯片对应的控制引脚 */voidHC595_Init(void){ GPIO_InitTypeDef GPIO_InitStruct = {0};uint8_ti =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
全部0条评论
快来发表一下你的评论吧 !