一.什么是移位寄存器
移位寄存器就是将数据在每个移位脉冲的作用下向左或向右移动一位。
二.74HC595的介绍
74HC595的功能:实现串行输入,并行输出,有扩展io的功能。
74HC595里面有2个8位寄存器:移位寄存器和存储寄存器。
三.74HC595的引脚介绍
74HC595是一款低噪声,低功率,高速的coms移位寄存器,包含一个8位串行输入,并行输出的移位寄存器及带有三态输出控制的8位d型存储器。移位寄存器和存储器分别由独立的时钟提供信号,时钟的上升沿触发移位寄存器和存储器。
管脚说明:
· 14脚:DS(SER)串行数据输入引脚
· 13脚:OE 输出使能控制脚,它是低电平才能使能输出,所以接GND
· 12脚:RCK(STCP)存储寄存器时钟输入引脚。上升沿时,数据从移位寄存器转存到存储寄存器
· 11脚:SCK(SHCP)移位寄存器时钟引脚,上升沿时,移位寄存器中的bit数据整体后移,并接收新的bit(从SER输入)
· 10脚:MR 低电平时,清空移位寄存器中已有的bit数据,一般不用,接高电平即可
· 9脚:串行数据出口引脚。当移位寄存器中的数据多余8bit时,会把已有的bit“挤出去”,就是从这里出去的。用于595的级联
· Q0~Q7脚:并行输出引脚
使用参数:
·VCC:2V~6V,5V最好
·IQN:+-35MA
四.74H595的电路
74hc595级联驱动4位数码管
五.74HC595驱动程序(stm32固件库)
#include "stm32f10x.h" // Device header
#include "Delay.h" // Device header
/************************
VCC------------>供电
DIO------------>PB15
RCLK------------>PB12
SCLK------------>PB13
GND------------>接地
**************************/
unsigned int num[] = {0xc0, 0xf9, 0xa4, 0xb0, 0x99, 0x92, 0x82, 0xf8, 0x80, 0x90, 0xff, 0x00};
//创建一个数组,0-9所对应的十六进制数
void HC595_GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_RESET);
}
/***********************************************************/
//串入
void HC595_Send_Byte(unsigned char byte)
{
unsigned int i;
for(i = 0; i < 8; i++)
{
if(byte & 0x80)
GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_SET);
else
GPIO_WriteBit(GPIOB, GPIO_Pin_15, Bit_RESET);
GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_RESET);
Delay_us(10);
GPIO_WriteBit(GPIOB, GPIO_Pin_13, Bit_SET);
Delay_us(10);
byte <<= 1;
}
}
/*********************************
参数:num 所要显示的数字,
show_bit 所显示的位数
**********************************/
//并出
void HC595_Send_Data(unsigned char num, unsigned char show_bit)
{
HC595_Send_Byte(num);
HC595_Send_Byte(1 << show_bit);
GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_RESET);
Delay_us(10);
GPIO_WriteBit(GPIOB, GPIO_Pin_12, Bit_SET);
Delay_us(10);
}
void display(unsigned int n)
{
static unsigned int thousand_bit, hundred_bit, ten_bit, single_bit;
thousand_bit = n / 1000;
hundred_bit = (n % 1000) / 100;
ten_bit = n % 1000 % 100 / 10;
single_bit = n % 10;
HC595_Send_Data(num[thousand_bit], 3);
HC595_Send_Data(num[hundred_bit], 2);
HC595_Send_Data(num[ten_bit], 1);
HC595_Send_Data(num[single_bit], 0);
}
#ifndef __74HC595_H
#define __74HC595_H
void HC595_GPIO_Configuration(void);
void HC595_Send_Data(unsigned char num, unsigned char show_bit);
void HC595_Send_Byte(unsigned char byte);
void display(unsigned int n);
#endif
六.主程序
#include "stm32f10x.h" // Device header
#include "Delay.h"
#include "74hc595.h"
int main(void)
{
/*模块初始化*/
HC595_GPIO_Configuration();
while (1)
{
display(666);
HC595_Send_Data(0xa4,1);
}
}