单片机8x8led点阵屏程序
时间: 2023-11-15 22:06:37 浏览: 163
以下是一个基于8051单片机的8x8 LED点阵屏程序示例,可以通过串口输入字符并在点阵屏上显示。
```c
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit SRCLK = P2^0; // shift register clock pin
sbit RCLK = P2^1; // register clock pin
sbit SER = P2^2; // serial data pin
void delay(uint t) // delay function
{
uint i, j;
for (i = 0; i < t; i++) {
for (j = 0; j < 125; j++);
}
}
void writeByte(uchar dat) // write a byte to shift register
{
uchar i;
for (i = 0; i < 8; i++) {
SRCLK = 0;
SER = dat & 0x80;
dat <<= 1;
SRCLK = 1;
}
}
void latchData() // latch data to register
{
RCLK = 0;
delay(1);
RCLK = 1;
}
void showChar(uchar c) // show a character on the LED matrix
{
uchar i, j;
for (i = 0; i < 8; i++) {
writeByte(1 << i);
for (j = 0; j < 8; j++) {
writeByte((c & (1 << j)) ? 0 : 1);
}
latchData();
delay(1);
}
}
void main()
{
TMOD = 0x20; // use timer 1 as serial port
TH1 = 0xfd; // set baud rate to 9600
TL1 = 0xfd;
SCON = 0x50; // enable serial port
TR1 = 1; // start timer 1
while (1) {
while (RI == 0); // wait for input
showChar(SBUF); // show the input character
RI = 0; // clear receive interrupt flag
}
}
```
该程序使用了一个移位寄存器来控制点阵屏,通过串口输入字符来在点阵屏上显示。你可以根据自己的需要修改程序,例如更改显示方式、加入其他功能等。
阅读全文