The given code is an example of how to display different characters on a 7-segment LED display using an 8051 microcontroller. It defines arrays for different characters ('Z', 'E', 'L', 'S', 'B') and their corresponding binary patterns. The 'display' function is used to display the characters on the 7-segment display by iterating through the segments and setting the corresponding bits on the data and control lines. The 'delay' function is used to introduce a delay between displaying different characters. The main function continuously checks the state of different keys (key1, key2, key3, key4, key5) and displays the corresponding character when a key is pressed.

#include <reg52.h>

#define uchar unsigned char #define uint unsigned int

// 字符 'Z' uchar code Z[8] = { 0x00, // 00000000 0x1F, // 00011111 0x18, // 00011000 0x10, // 00010000 0x0C, // 00001100 0x04, // 00000100 0x1F, // 00011111 0x00 // 00000000 };

// 字符 'E' uchar code E[8] = { 0x00, // 00000000 0x1F, // 00011111 0x11, // 00010001 0x1F, // 00011111 0x11, // 00010001 0x11, // 00010001 0x1F, // 00011111 0x00 // 00000000 };

// 字符 'L' uchar code L[8] = { 0x00, // 00000000 0x10, // 00010000 0x10, // 00010000 0x10, // 00010000 0x10, // 00010000 0x10, // 00010000 0x1F, // 00011111 0x00 // 00000000 };

// 字符 'S' uchar code S[8] = { 0x00, // 00000000 0x0E, // 00001110 0x11, // 00010001 0x10, // 00010000 0x0E, // 00001110 0x01, // 00000001 0x1F, // 00011111 0x00 // 00000000 };

// 字符 'B' uchar code B[8] = { 0x00, // 00000000 0x1E, // 00011110 0x11, // 00010001 0x1E, // 00011110 0x11, // 00010001 0x11, // 00010001 0x1E, // 00011110 0x00 // 00000000 };

// 显示函数 void display (uchar *data) { uint i; for(i = 0; i < 8; i++) { P0 = 0x01 << i; P2 = ~data[i]; delay(5); // 延迟 P2 = 0xFF; // 清除 } }

// 延迟函数 void delay(uint ms) { uint i, j; for(i = ms; i > 0; i--) for(j = 110; j > 0; j--); }

void main() { while(1) { // 显示字符 'Z' if(key1 == 0) { display(Z); } // 显示字符 'E' else if(key2 == 0) { display(E); } // 显示字符 'L' else if(key3 == 0) { display(L); } // 显示字符 'S' else if(key4 == 0) { display(S); } // 显示字符 'B' else if(key5 == 0) { display(B); } }

8051 微控制器 7 段 LED 显示字符示例

原文地址: https://www.cveoy.top/t/topic/pj9P 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录