数码管显示0~99,以下是我在网上看到的代码,有不懂得地方发上来问问
- #include<reg52.h>
- <font color="#ff00ff">sbit WEI = P1^6; //位选 为什么要设这两个,要设位选的话也应该如下图那样设P0^2、P0^3吧?
- sbit DU = P1^7; //段选 </font>
- unsigned char DISP_smg[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
- #define uchar unsigned char // 8bit, 0 ~ 255
- #define uint unsigned int // 16bit, 0 ~ 65,535
- void delayms(uint m) // 11.0592MHz 晶振ms延时,偏差小于1%
- {
- uint i, j;
- for(i=m; i>0; i--)
- for(j=114; j>0; j--);
- }
- void main()
- {
- unsigned char C0 = 0;
- unsigned char i = 0;
- DU = 0;
- P0 = 0x00;
- DU = 1;
-
- while(1)
- {
- for(i = 0; i < 10; i++) //循环10次
- {
- WEI = 1;
- <font color="#ff00ff"> P0 = 0x7f; </font><span style="color: rgb(255, 0, 255);"> 这两个不是应该一个P0、一个P2吗</span>
- WEI = 0;
- DU = 1;
- <font color="#ff00ff"> P0 = DISP_smg[ i]; </font>
- delayms(400);
- WEI = 1;
- <font color="#ff00ff"> P0 = 0xbf; 还有这两个。</font>
- WEI = 0;
- DU = 1;
- <font color="#ff00ff"> P0 = DISP_smg[C0];</font>
- delayms(400);
- if(i==8)
- {
- C0 = C0+1;
- }
-
- }
-
- }
- }
上面那个代码发的有点小错误,我重新发
#include<reg52.h>
sbit WEI = P1^6; //位选
sbit DU = P1^7; //段选 为什么要设这两个,要设位选的话也应该如下图那样设P0^2、P0^3吧?
unsigned char DISP_smg[10] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
#define uchar unsigned char // 8bit, 0 ~ 255
#define uint unsigned int // 16bit, 0 ~ 65,535
void delayms(uint m) // 11.0592MHz 晶振ms延时,偏差小于1%
{
uint i, j;
for(i=m; i>0; i--)
for(j=114; j>0; j--);
}
void main()
{
unsigned char C0 = 0;
unsigned char i = 0;
DU = 0;
P0 = 0x00;
DU = 1;
while(1)
{
for(i = 0; i < 10; i++) //循环10次
{
WEI = 1;
P0 = 0x7f; 这个和下面那个P0不是应该一个P0,一个P2吗
WEI = 0;
DU = 1;
P0 = DISP_smg[ i];
delayms(400);
WEI = 1;
P0 = 0xbf; 还有这个P0下面那个P0
WEI = 0;
DU = 1;
P0 = DISP_smg[C0];
delayms(400);
if(i==8)
{
C0 = C0+1;
}
}
}
}