目录
前置知识学习
像素都是8的倍数,为了不浪费字节数(8的倍数)
74HC595
寄存器上面加横线一般是说低电平有效/下降源有效
RCLK:寄存器时钟
SRCLR:串行清零端
SRCLK:串行时钟
SER:串行数据
QH‘:多片级联
举例说明使用原理
填入SER = 0。。。。。。
上升沿八位全满后,
RCLk设为高电平,锁到右侧我
一次性输出
若继续给SER输入数据,则数据顺着QH‘(QH‘接着下一片的SER)输出去
是不是和数码管驱动方式有点像
列直接对P0赋值
行用74HC595控制那三个引脚,输入数据
这个元器件也是“弱上拉,强下拉”的模式
高电平承载电流小
低电平承载电流大
sfr和sbit关键字
实际代码演示
代码一:LED点阵屏显示静态图像
本来是要演示74HC595功能的,但是我的单片机没有74HC95对应的流水灯,所以演示不了,但是既然我能够使用这个元器件实现功能,就说明有该元器件
#include <REGX52.H>
#include "Delay.H"
sbit SER = P3^4; //SER
sbit RCK = P3^5; //RCLK
sbit SCK = P3^6; //SRCLK
/* *
* @brief 74HC595写入一个字节
* @param 要写入的字节
* @retval
*/
void _74HC595_WriteByte(unsigned char Byte)
{
unsigned char i;
for(i=0; i<8; i++)
{
SER = Byte & (0x80>>i);
SCK = 1;
SCK = 0;
}
RCK = 1;
RCK = 0;
}
#define MATRIX_LED_PORT P0
/* *
* @brief LED点阵屏显示一列数据
* @param column:要选择的列,范围:0~7,0在最左边
* @param Date 选择列所要显示的数据,高位在上,1为亮,0为灭
* @retval
*/
void MatrixLED_ShowColumn(unsigned char column, Date)
{
_74HC595_WriteByte(Date);
MATRIX_LED_PORT =~ (0x80>>column);
Delay(1);
MATRIX_LED_PORT = 0xFF;
}
void main()
{
SCK = 0;
RCK = 0;
while(1)
{
MatrixLED_ShowColumn(0, 0xA0);
MatrixLED_ShowColumn(1, 0xA1);
MatrixLED_ShowColumn(2, 0xA2);
MatrixLED_ShowColumn(3, 0xA3);
MatrixLED_ShowColumn(4, 0xA4);
MatrixLED_ShowColumn(5, 0xA5);
MatrixLED_ShowColumn(6, 0xA6);
MatrixLED_ShowColumn(7, 0xA7);
}
}
代码二:点阵屏显示流动图像
本质就是对上面的函数做了模块化,通过循环(列右移)实现了流动图像或者变化图像的效果
ps:流动图像的位数从取字/(图像)模软件上面来
#include <REGX52.H>
#include "MatrixLED.H"
#include "Delay.H"
//存在RAM中(内存小),放在FLASH中内存更大(code)(flash不可更改)
unsigned char code Animation[] = {
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0xFE,0x12,0x12,0x12,0x12,0x12,0x12,
0x12,0x12,0x12,0x12,0x12,0x12,0xFE,0x00,
0x00,0xFE,0x12,0x12,0x12,0x12,0x12,0x12,
0x12,0x12,0x12,0x12,0x12,0x12,0xFE,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00
};
unsigned char i,offset,count;
void main()
{
MatrixLED_init();
while(1)
{
for(i=0; i<8; i++)
{
MatrixLED_ShowColumn(i, Animation[i+offset]);
}
count++;
if(count>10)
{
count++;
offset++;
if(offset>40) //48 - 8
{
offset = 0;
}
}
}
}
//MatrixLED.c
#include <REGX52.H>
#include "Delay.H"
sbit SER = P3^4; //SER
sbit RCK = P3^5; //RCLK
sbit SCK = P3^6; //SRCLK
/* *
* @brief 点阵屏初始化
* @param
* @retval
*/
void MatrixLED_init()
{
SCK = 0;
RCK = 0;
}
/* *
* @brief 74HC595写入一个字节
* @param 要写入的字节
* @retval
*/
void _74HC595_WriteByte(unsigned char Byte)
{
unsigned char i;
for(i=0; i<8; i++)
{
SER = Byte & (0x80>>i);
SCK = 1;
SCK = 0;
}
RCK = 1;
RCK = 0;
}
#define MATRIX_LED_PORT P0
/* *
* @brief LED点阵屏显示一列数据
* @param column:要选择的列,范围:0~7,0在最左边
* @param Date 选择列所要显示的数据,高位在上,1为亮,0为灭
* @retval
*/
void MatrixLED_ShowColumn(unsigned char column, Date)
{
_74HC595_WriteByte(Date);
MATRIX_LED_PORT =~ (0x80>>column);
Delay(1);
MATRIX_LED_PORT = 0xFF;
}
#ifndef __MatrixLEd_H__
#define __MatrixLEd_H__
void Matrix_Init();
void MatrixLED_ShowColumn(unsigned char column, Date);
#endif