51单片机开发板外加16X16点阵模块,显示汉字动态显示通过按键控制动态显示方式代码
时间: 2024-03-22 16:42:10 浏览: 62
好的,以下是一个简单的51单片机开发板外加16X16点阵模块,显示汉字动态显示通过按键控制动态显示方式的代码,供您参考:
```c
#include <reg52.h>
//定义按键引脚
sbit KEY1 = P1^0;
sbit KEY2 = P1^1;
sbit KEY3 = P1^2;
sbit KEY4 = P1^3;
//定义点阵引脚
sbit CS = P2^0;
sbit CLK = P2^1;
sbit DIN = P2^2;
//定义汉字点阵数据
unsigned char code chinese[][32]={
{0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10}, // "中"
{0x04,0x04,0x04,0x04,0x1C,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x24,0x1C,0x04,0x04,0x04,0x04,0x04,0x04}, // "国"
{0x00,0x00,0x10,0x20,0x40,0x80,0xFE,0x00,0x10,0x20,0x40,0x80,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}, // "器"
};
//定义动态显示方式
unsigned char mode = 0;
//延时函数
void delay(int i) {
while(i--);
}
//写入数据到点阵模块
void writeData(unsigned char dat) {
unsigned char i;
for(i=0;i<8;i++) {
CLK = 0;
if(dat & 0x80) {
DIN = 1;
} else {
DIN = 0;
}
delay(1);
CLK = 1;
dat <<= 1;
delay(1);
}
}
//写入命令到点阵模块
void writeCmd(unsigned char cmd) {
unsigned char i;
CS = 0;
CLK = 0;
delay(1);
for(i=0;i<8;i++) {
CLK = 0;
if(cmd & 0x80) {
DIN = 1;
} else {
DIN = 0;
}
delay(1);
CLK = 1;
cmd <<= 1;
delay(1);
}
}
//初始化点阵模块
void initMatrix() {
writeCmd(0x0C); //关闭显示
writeCmd(0x01); //清空显示
writeCmd(0x09); //译码方式:译码模式
writeCmd(0x0F); //亮度控制:最亮
}
//清空点阵屏幕
void clearMatrix() {
unsigned char i, j;
for(i=1;i<=16;i++) {
writeCmd(i);
for(j=1;j<=16;j++) {
writeData(0);
}
}
}
//显示汉字
void showChinese(unsigned char* chineseData) {
unsigned char i, j;
for(i=0;i<16;i++) {
writeCmd(i+1);
for(j=0;j<16;j++) {
writeData(chineseData[i*2+j]);
}
}
}
//左移动态显示
void showLeft() {
unsigned char i, j;
for(i=0;i<16;i++) {
for(j=0;j<16;j++) {
writeCmd(j+1);
writeData(chinese[i][j+mode]);
}
delay(10000);
}
}
//右移动态显示
void showRight() {
unsigned char i, j;
for(i=0;i<16;i++) {
for(j=0;j<16;j++) {
writeCmd(j+1);
writeData(chinese[i][31-j-mode]);
}
delay(10000);
}
}
//上移动态显示
void showUp() {
unsigned char i, j;
for(j=0;j<16;j++) {
for(i=0;i<16;i++) {
writeCmd(j+1);
writeData(chinese[i+mode][j]);
}
delay(10000);
}
}
//下移动态显示
void showDown() {
unsigned char i, j;
for(j=0;j<16;j++) {
for(i=0;i<16;i++) {
writeCmd(j+1);
writeData(chinese[31-i-mode][j]);
}
delay(10000);
}
}
//闪烁动态显示
void showFlash() {
unsigned char i, j, k;
for(k=0;k<5;k++) {
for(i=0;i<16;i++) {
for(j=0;j<16;j++) {
writeCmd(j+1);
if((i+j)%2==0) {
writeData(chinese[i][j]);
} else {
writeData(0);
}
}
}
delay(50000);
clearMatrix();
delay(50000);
}
}
//按键扫描
void keyScan() {
if(KEY1==0) {
mode++;
if(mode>=16) {
mode = 0;
}
delay(5000);
}
if(KEY2==0) {
showLeft();
}
if(KEY3==0) {
showRight();
}
if(KEY4==0) {
showUp();
}
}
//主函数
void main() {
initMatrix();
clearMatrix();
while(1) {
showChinese(chinese[0]);
keyScan();
switch(mode) {
case 0: showChinese(chinese[0]); break;
case 1: showChinese(chinese[1]); break;
case 2: showChinese(chinese[2]); break;
case 3: showLeft(); break;
case 4: showRight(); break;
case 5: showUp(); break;
case 6: showDown(); break;
case 7: showFlash(); break;
default: break;
}
}
}
```
代码中使用了MAX7219点阵模块,按下不同按键可以切换不同的动态显示效果,包括左移、右移、上移、下移、闪烁等。其中,汉字点阵数据存储在数组中,可以根据需要进行修改。
阅读全文