LED点阵屏
介绍
显示原理
原理图
74HC595
C51的sfr,sbit
选择列来显示矩阵LED
main.c
#include <REGX52.H>
#inlcude "Delay.h"
sbit mRCLK=P3^5; //RCLK
sbit mSRCLK=P3^6; //SRCLK
sbit mSER=P3^4; //SER
void _74HC595_WriteByte(unsigned char Byte)
{
unsigned char i;
mSRCLK=0;
mRCLK=0;
for(i=0;i<8;i++)
{
mSER=Byte&(0x80>>i); //sbit非0置1
mSRCLK=1;
mSRCLK=0;
}
mRCLK=1;
}
void MatrixLED_ShowColumn(unsigned char Column, Data)
{
_74HC595_WriteByte(Data);
P0=~(0x80>>Column);
}
void main()
{
MatrixLED_ShowColumn(7,0xF0);
while(1)
{
}
}
运行效果
矩阵LED显示笑脸
main.c
#include <REGX52.H>
#include "Delay.h"
sbit mRCLK=P3^5; //RCLK
sbit mSRCLK=P3^6; //SRCLK
sbit mSER=P3^4; //SER
#define MATRIX_LED_PORT P0
/**
* @brief 74HC595写入一个字节
* @param 要写入的字节
* @retval 无
*/
void _74HC595_WriteByte(unsigned char Byte)
{
unsigned char i;
mSRCLK=0;
mRCLK=0;
for(i=0;i<8;i++)
{
mSER=Byte&(0x80>>i); //sbit非0置1
mSRCLK=1;
mSRCLK=0;
}
mRCLK=1;
}
/**
* @brief LED点阵屏显示一列数据
* @param Column 要选择的列, 范围: 0~7, 0在最左边
* @param Data 选择列显示的数据, 高位在上, 1为亮,0为灭
* @retval 无
*/
void MatrixLED_ShowColumn(unsigned char Column, Data)
{
_74HC595_WriteByte(Data);
MATRIX_LED_PORT=~(0x80>>Column);
Delay(1);
MATRIX_LED_PORT=0xFF;
}
void main()
{
while(1)
{
MatrixLED_ShowColumn(0,0x3C);
MatrixLED_ShowColumn(1,0x42);
MatrixLED_ShowColumn(2,0xA9);
MatrixLED_ShowColumn(3,0x85);
MatrixLED_ShowColumn(4,0x85);
MatrixLED_ShowColumn(5,0xA9);
MatrixLED_ShowColumn(6,0x42);
MatrixLED_ShowColumn(7,0x3C);
}
}
运行效果
矩阵LED显示流动字幕
MatrixLED.c
#include <REGX52.H>
#include "Delay.h"
sbit mRCLK=P3^5; //RCLK
sbit mSRCLK=P3^6; //SRCLK
sbit mSER=P3^4; //SER
#define MATRIX_LED_PORT P0
/**
* @brief 74HC595写入一个字节
* @param 要写入的字节
* @retval 无
*/
void _74HC595_WriteByte(unsigned char Byte)
{
unsigned char i;
mSRCLK=0;
mRCLK=0;
for(i=0;i<8;i++)
{
mSER=Byte&(0x80>>i); //sbit非0置1
mSRCLK=1;
mSRCLK=0;
}
mRCLK=1;
}
/**
* @brief LED点阵屏显示一列数据
* @param Column 要选择的列, 范围: 0~7, 0在最左边
* @param Data 选择列显示的数据, 高位在上, 1为亮,0为灭
* @retval 无
*/
void MatrixLED_ShowColumn(unsigned char Column, Data)
{
_74HC595_WriteByte(Data);
MATRIX_LED_PORT=~(0x80>>Column);
Delay(1);
MATRIX_LED_PORT=0xFF;
}
MatrixLED.h
#ifndef __MATRIX_LED_H__
#define __MATRIX_LED_H__
void _74HC595_WriteByte(unsigned char Byte);
void MatrixLED_ShowColumn(unsigned char Column, Data);
#endif
main.c
#include <REGX52.H>
#include "MatrixLED.h"
unsigned char code Animation[]={ //放入Flash不占用内存,但是只读
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0xFF,0x08,0x08,0xFF,0x00,0x0E,0x15,0x15,0x0D,0x00,0xFE,0x01,0x02,0x00,0xFE,0x01,
0x02,0x00,0x0E,0x11,0x11,0x0E,0x00,0x7D,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
};
void main()
{
unsigned char i,Offset=0,Count=0;
while(1)
{
for(i=0;i<8;i++)
{
MatrixLED_ShowColumn(i,Animation[i+Offset]);
}
Count++;
if(Count>10)
{
Count=0;
Offset++;
if(Offset>32)
{
Offset=0;
}
}
}
}