http://blog.csdn.net/deyusun/article/details/5701598 2010
#include <reg51.h> sbit P00 = P0 ^ 0; sbit P01 = P0 ^ 1; sbit P02 = P0 ^ 2; sbit P03 = P0 ^ 3; int DATA_LED[11] = { //0~9的数码管段码 A0H,BBH,62H,2AH,39H,2CH,24H,BAH,20H,28H 根据具体电而定,不通用 0xA0, //0 0xBB, //1 0x62, //2 0x2A, //3 0x39, //4 0x2C, //5 0x24, //6 0xBA, //7 0x20, //8 0x28, //9 0xFF }; void delay(int m) //延时程序,延时m*0.5毫秒 { unsigned int i; unsigned int j; for (i = 0; i < m; i++) { for (j = 0; j < 500; j++){} } } void led(int n) //数码管显示及数据处理程序 { P1 = 0xFF; P1 = DATA_LED[n % 10]; //个位 P00 = 0; delay(1); P00 = 1; P1 = 0xFF; P1 = DATA_LED[n / 10 % 10]; //十位 P01 = 0; delay(1); P01 = 1; P1 = 0xFF; P1 = DATA_LED[n / 100 % 10]; //百位 P02 = 0; delay(1); P02 = 1; P1 = 0xFF; P1 = DATA_LED[n / 1000 % 10]; //千位 P03 = 0; delay(1); P03 = 1; } void main(void) { while (1) { led(404); //显示数字404,实际显示的是0404,需要在led()函数中判断,没有千位的情况,不是让显示成0 } } |
|