国产免费AV|泡泡玛特欧洲总部将设在伦敦|中文天堂网www新版资源在线|一本久道综合在线中文|国精产品一二三产区的使用方法|香蕉鱼在线观看|www.27eee
ELEOK
標(biāo)題:
stm32使用三片74HC595級(jí)聯(lián)程序代碼
[打印本頁(yè)]
作者:
coolice
時(shí)間:
2021-8-13 23:47
標(biāo)題:
stm32使用三片74HC595級(jí)聯(lián)程序代碼
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
復(fù)制代碼
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讀數(shù)據(jù)
Calls:
Data Accessed: 無(wú)
Data Updated: 無(wú)
Input: DeviceAddr:設(shè)備地址
RegisterAddr:寄存器地址
buf:緩存區(qū)地址
count:讀數(shù)據(jù)個(gè)數(shù)
Output: 無(wú)
Return: read_verif:操作成功
TIMEOU:超時(shí)錯(cuò)誤
Others: 無(wú)
*************************************************************************/
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讀數(shù)據(jù)
Calls:
Data Accessed: 無(wú)
Data Updated: 無(wú)
Input: DeviceAddr:設(shè)備地址
RegisterAddr:寄存器地址
buf:緩存區(qū)地址
count:讀數(shù)據(jù)個(gè)數(shù)
Output: 無(wú)
Return: read_verif:操作成功
TIMEOU:超時(shí)錯(cuò)誤
Others: 無(wú)
*************************************************************************/
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的輸入數(shù)據(jù)
Calls: HC595_delay;GPIO_ResetBits;GPIO_SetBits;
Data Accessed: 無(wú)
Data Updated: 無(wú)
Input:
HC595x:用戶使用的595端口,類型定義在74HC595.h中
ChipNum: 用戶使用的595端口上連接的芯片個(gè)數(shù)
Output:
DataBuf: 輸出數(shù)據(jù)存放緩沖區(qū)
Return: 無(wú)
Others: 此模塊為Stm32單片機(jī)中使用,調(diào)試時(shí)在72M系統(tǒng)時(shí)鐘下
*************************************************************************/
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); //設(shè)置LCK為低電平,上升沿?cái)?shù)據(jù)鎖存
for(; ChipNum>0; ChipNum--)
{
DataBufTmp = *DataBuf;
for(i=0; i<8; i++)
{
GPIO_ResetBits(HC595x.Clk.Port, HC595x.Clk.Pin); //時(shí)鐘低電平
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); //時(shí)鐘高電平,上升沿?cái)?shù)據(jù)移位
HC595_delay(5);
DataBufTmp = DataBufTmp << 1;
}
DataBuf++;
}
GPIO_SetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //設(shè)置LCK為高電平,上升沿?cái)?shù)據(jù)鎖存
HC595_delay(10);
GPIO_ResetBits(HC595x.Lck.Port, HC595x.Lck.Pin); //設(shè)置LCK為低電平,上升沿?cái)?shù)據(jù)鎖存
}
/************************************************************************
Function: HC595_delay
Description: 74HC595模塊延時(shí)函數(shù)
Calls: 無(wú)
Data Accessed: 無(wú)
Data Updated: 無(wú)
Input:
num:延時(shí)個(gè)數(shù)
Output: 無(wú)
Return: 無(wú)
Others: 此模塊為Stm32單片機(jī)中使用,調(diào)試時(shí)在100M系統(tǒng)時(shí)鐘下
*************************************************************************/
static void HC595_delay(int num)
{
while(num>0)
num--;
}
復(fù)制代碼
歡迎光臨 ELEOK (http://m.afoofa.cn/)
Powered by Discuz! X5.0