找回密码
 注册

QQ登录

只需一步,快速开始

搜索

stm32使用三片74HC595级联程序代码

[复制链接]
coolice 发表于 2021-8-13 23:47:44 | 显示全部楼层 |阅读模式
1.jpg
hc595.h
  1. #ifndef _HC595_h_
  2. #define _HC595_h_

  3. #include "stm324xg_eval.h"


  4. #define        SHCP_PIN  GPIO_Pin_1 //clk
  5. #define        STCP_PIN  GPIO_Pin_2   //lck

  6. #define        QS_SDI_PIN   GPIO_Pin_0
  7. //#define        QS_L_PIN    GPIO_Pin_0
  8. //#define        QS_L1_PIN  GPIO_Pin_0

  9. #define SHCP_SET(x)   GPIOI->ODR=(GPIOI->ODR&~SHCP_PIN)|(x ? SHCP_PIN:0)
  10. #define STCP_SET(x)   GPIOI->ODR=(GPIOI->ODR&~STCP_PIN)|(x ? STCP_PIN:0)

  11. #define QS_SDI(x)   GPIOI->ODR=(GPIOI->ODR&~QS_SDI_PIN)|(x ? QS_SDI_PIN:0)
  12. //#define QS_L(x)   GPIOB->ODR=(GPIOB->ODR&~QS_L_PIN)|(x ? QS_L_PIN:0)
  13. //#define QS_L1(x)   GPIOB->ODR=(GPIOB->ODR&~QS_L1_PIN)|(x ? QS_L1_PIN:0)

  14. typedef struct{
  15.     GPIO_TypeDef*      Port;
  16.     unsigned short int Pin;
  17. }PortPin595;

  18. typedef struct{
  19.         PortPin595 Clk;
  20.         PortPin595 Lck;
  21.         PortPin595 Data;   
  22. }HC595;


  23. void Write_74HC595(HC595 HC595x,unsigned char ChipNum,unsigned char *DataBuf);
  24. void GPIO_HC595_Configuration(void);
  25. void Write_595_ENABLE(void);
  26. #endif  
复制代码
h595.c
  1. #include "hc595.h"

  2. static void HC595_delay(int num);
  3. void GPIO_HC595_Configuration()
  4. {
  5.     GPIO_InitTypeDef  GPIO_InitStructure;
  6.    
  7.     /* Enable the GPIO_LED Clock */
  8.       RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);

  9.     /* Configure the GPIO_LED pin */
  10.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
  11.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
  12.     GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
  13.     GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
  14.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
  15.     GPIO_Init(GPIOI, &GPIO_InitStructure);

  16. }
  17. /************************************************************************
  18.   Function:       EI2C_ReadDataBuffer
  19.   Description:    从E2PROM读数据
  20.   Calls:         
  21.   Data Accessed:  无
  22.   Data Updated:   无
  23.   Input:          DeviceAddr:设备地址
  24.                   RegisterAddr:寄存器地址
  25.                                   buf:缓存区地址
  26.                                   count:读数据个数
  27.   Output:         无
  28.   Return:         read_verif:操作成功
  29.                   TIMEOU:超时错误
  30.   Others:         无
  31. *************************************************************************/
  32. void Write_byte_595(u8 byte)
  33. {
  34.         u8 i;
  35.         for(i=0;i<8;i++)
  36.         {
  37.                  byte <<= 1;
  38.                 //DS   = CY;
  39.         //        SH_CP = 1;
  40.         ////        _nop_();
  41.         //        _nop_();
  42.         //        SH_CP = 0;
  43.         }
  44. }
  45. /************************************************************************
  46.   Function:       EI2C_ReadDataBuffer
  47.   Description:    从E2PROM读数据
  48.   Calls:         
  49.   Data Accessed:  无
  50.   Data Updated:   无
  51.   Input:          DeviceAddr:设备地址
  52.                   RegisterAddr:寄存器地址
  53.                                   buf:缓存区地址
  54.                                   count:读数据个数
  55.   Output:         无
  56.   Return:         read_verif:操作成功
  57.                   TIMEOU:超时错误
  58.   Others:         无
  59. *************************************************************************/
  60. void Write_595_ENABLE()
  61. {
  62.   STCP_SET(0);
  63.   HC595_delay(100);
  64.   STCP_SET(1);

  65. }
  66. /************************************************************
  67. ***********************************************************/

  68. static void HC595_delay(int num);

  69. /************************************************************************
  70.   Function:       Read_74HC595
  71.   Description:    读取n片74HC595的输入数据
  72.   Calls:          HC595_delay;GPIO_ResetBits;GPIO_SetBits;
  73.   Data Accessed:  无
  74.   Data Updated:   无
  75.   Input:         
  76.                   HC595x:用户使用的595端口,类型定义在74HC595.h中
  77.                  ChipNum: 用户使用的595端口上连接的芯片个数
  78.   Output:         
  79.                  DataBuf: 输出数据存放缓冲区
  80.   Return:         无
  81.   Others:         此模块为Stm32单片机中使用,调试时在72M系统时钟下
  82. *************************************************************************/
  83. void Write_74HC595(HC595 HC595x,unsigned char ChipNum,unsigned char *DataBuf)
  84. {
  85.     unsigned char i = 0;
  86.     unsigned char DataBufTmp = 0;
  87.    
  88.     GPIO_ResetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //设置LCK为低电平,上升沿数据锁存
  89.    
  90.     for(; ChipNum>0; ChipNum--)
  91.     {
  92.         DataBufTmp = *DataBuf;
  93.         for(i=0; i<8; i++)
  94.         {
  95.             GPIO_ResetBits(HC595x.Clk.Port, HC595x.Clk.Pin); //时钟低电平
  96.             
  97.             if (DataBufTmp & 0x80)
  98.             {
  99.                 GPIO_SetBits(HC595x.Data.Port, HC595x.Data.Pin);   //输出1
  100.             }
  101.             else
  102.             {
  103.                 GPIO_ResetBits(HC595x.Data.Port, HC595x.Data.Pin); //输出0
  104.             }
  105.             
  106.             HC595_delay(5);
  107.             
  108.             GPIO_SetBits(HC595x.Clk.Port, HC595x.Clk.Pin); //时钟高电平,上升沿数据移位
  109.                        
  110.             HC595_delay(5);
  111.             
  112.             DataBufTmp = DataBufTmp << 1;
  113.         }
  114.         DataBuf++;
  115.     }
  116.     GPIO_SetBits(HC595x.Lck.Port, HC595x.Lck.Pin);   //设置LCK为高电平,上升沿数据锁存
  117.     HC595_delay(10);
  118.     GPIO_ResetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //设置LCK为低电平,上升沿数据锁存
  119. }

  120. /************************************************************************
  121.   Function:       HC595_delay
  122.   Description:    74HC595模块延时函数
  123.   Calls:          无
  124.   Data Accessed:  无
  125.   Data Updated:   无
  126.   Input:         
  127.                   num:延时个数
  128.   Output:         无
  129.   Return:         无
  130.   Others:         此模块为Stm32单片机中使用,调试时在100M系统时钟下
  131. *************************************************************************/
  132. static void HC595_delay(int num)
  133. {
  134.         while(num>0)
  135.                 num--;
  136. }
复制代码




您需要登录后才可以回帖 登录 | 注册

本版积分规则

QQ|手机版|小黑屋|ELEOK |网站地图

GMT+8, 2024-4-24 12:39

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表