1.jpg (47.76 KB)
下載附件
2021-8-13 23:46 上傳
hc595.h
- #ifndef _HC595_h_
- #define _HC595_h_
- #include "stm324xg_eval.h"
- #define SHCP_PIN GPIO_Pin_1 //clk
- #define STCP_PIN GPIO_Pin_2 //lck
- #define QS_SDI_PIN GPIO_Pin_0
- //#define QS_L_PIN GPIO_Pin_0
- //#define QS_L1_PIN GPIO_Pin_0
-
- #define SHCP_SET(x) GPIOI->ODR=(GPIOI->ODR&~SHCP_PIN)|(x ? SHCP_PIN:0)
- #define STCP_SET(x) GPIOI->ODR=(GPIOI->ODR&~STCP_PIN)|(x ? STCP_PIN:0)
- #define QS_SDI(x) GPIOI->ODR=(GPIOI->ODR&~QS_SDI_PIN)|(x ? QS_SDI_PIN:0)
- //#define QS_L(x) GPIOB->ODR=(GPIOB->ODR&~QS_L_PIN)|(x ? QS_L_PIN:0)
- //#define QS_L1(x) GPIOB->ODR=(GPIOB->ODR&~QS_L1_PIN)|(x ? QS_L1_PIN:0)
- typedef struct{
- GPIO_TypeDef* Port;
- unsigned short int Pin;
- }PortPin595;
- typedef struct{
- PortPin595 Clk;
- PortPin595 Lck;
- PortPin595 Data;
- }HC595;
- void Write_74HC595(HC595 HC595x,unsigned char ChipNum,unsigned char *DataBuf);
- void GPIO_HC595_Configuration(void);
- void Write_595_ENABLE(void);
- #endif
復制代碼 h595.c
- #include "hc595.h"
- static void HC595_delay(int num);
- void GPIO_HC595_Configuration()
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- /* Enable the GPIO_LED Clock */
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOI, ENABLE);
- /* Configure the GPIO_LED pin */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0|GPIO_Pin_1|GPIO_Pin_2;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitStructure.GPIO_OType = GPIO_OType_OD;
- GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_100MHz;
- GPIO_Init(GPIOI, &GPIO_InitStructure);
- }
- /************************************************************************
- Function: EI2C_ReadDataBuffer
- Description: 從E2PROM讀數據
- Calls:
- Data Accessed: 無
- Data Updated: 無
- Input: DeviceAddr:設備地址
- RegisterAddr:寄存器地址
- buf:緩存區地址
- count:讀數據個數
- Output: 無
- Return: read_verif:操作成功
- TIMEOU:超時錯誤
- Others: 無
- *************************************************************************/
- void Write_byte_595(u8 byte)
- {
- u8 i;
- for(i=0;i<8;i++)
- {
- byte <<= 1;
- //DS = CY;
- // SH_CP = 1;
- //// _nop_();
- // _nop_();
- // SH_CP = 0;
- }
- }
- /************************************************************************
- Function: EI2C_ReadDataBuffer
- Description: 從E2PROM讀數據
- Calls:
- Data Accessed: 無
- Data Updated: 無
- Input: DeviceAddr:設備地址
- RegisterAddr:寄存器地址
- buf:緩存區地址
- count:讀數據個數
- Output: 無
- Return: read_verif:操作成功
- TIMEOU:超時錯誤
- Others: 無
- *************************************************************************/
- void Write_595_ENABLE()
- {
- STCP_SET(0);
- HC595_delay(100);
- STCP_SET(1);
- }
- /************************************************************
- ***********************************************************/
- static void HC595_delay(int num);
- /************************************************************************
- Function: Read_74HC595
- Description: 讀取n片74HC595的輸入數據
- Calls: HC595_delay;GPIO_ResetBits;GPIO_SetBits;
- Data Accessed: 無
- Data Updated: 無
- Input:
- HC595x:用戶使用的595端口,類型定義在74HC595.h中
- ChipNum: 用戶使用的595端口上連接的芯片個數
- Output:
- DataBuf: 輸出數據存放緩沖區
- Return: 無
- Others: 此模塊為Stm32單片機中使用,調試時在72M系統時鐘下
- *************************************************************************/
- void Write_74HC595(HC595 HC595x,unsigned char ChipNum,unsigned char *DataBuf)
- {
- unsigned char i = 0;
- unsigned char DataBufTmp = 0;
-
- GPIO_ResetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //設置LCK為低電平,上升沿數據鎖存
-
- for(; ChipNum>0; ChipNum--)
- {
- DataBufTmp = *DataBuf;
- for(i=0; i<8; i++)
- {
- GPIO_ResetBits(HC595x.Clk.Port, HC595x.Clk.Pin); //時鐘低電平
-
- if (DataBufTmp & 0x80)
- {
- GPIO_SetBits(HC595x.Data.Port, HC595x.Data.Pin); //輸出1
- }
- else
- {
- GPIO_ResetBits(HC595x.Data.Port, HC595x.Data.Pin); //輸出0
- }
-
- HC595_delay(5);
-
- GPIO_SetBits(HC595x.Clk.Port, HC595x.Clk.Pin); //時鐘高電平,上升沿數據移位
-
- HC595_delay(5);
-
- DataBufTmp = DataBufTmp << 1;
- }
- DataBuf++;
- }
- GPIO_SetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //設置LCK為高電平,上升沿數據鎖存
- HC595_delay(10);
- GPIO_ResetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //設置LCK為低電平,上升沿數據鎖存
- }
- /************************************************************************
- Function: HC595_delay
- Description: 74HC595模塊延時函數
- Calls: 無
- Data Accessed: 無
- Data Updated: 無
- Input:
- num:延時個數
- Output: 無
- Return: 無
- Others: 此模塊為Stm32單片機中使用,調試時在100M系統時鐘下
- *************************************************************************/
- static void HC595_delay(int num)
- {
- while(num>0)
- num--;
- }
復制代碼
【必讀】版權免責聲明
1、本主題所有言論和內容純屬會員個人意見,與本論壇立場無關。2、本站對所發內容真實性、客觀性、可用性不做任何保證也不負任何責任,網友之間僅出于學習目的進行交流。3、對提供的數字內容不擁有任何權利,其版權歸原著者擁有。請勿將該數字內容進行商業交易、轉載等行為,該內容只為學習所提供,使用后發生的一切問題與本站無關。 4、本網站不保證本站提供的下載資源的準確性、安全性和完整性;同時本網站也不承擔用戶因使用這些下載資源對自己和他人造成任何形式的損失或傷害。 5、本網站所有軟件和資料均為網友推薦收集整理而來,僅供學習用途使用,請務必下載后兩小時內刪除,禁止商用。6、如有侵犯你版權的,請及時聯系我們(電子郵箱1370723259@qq.com)指出,本站將立即改正。
|