我的程序引脚定义与单片机不符合,改后数码管显示还是乱码,求助,要怎么改
时间:10-02
整理:3721RD
点击:
#include <REGX52.H>
void Timer0Init(void);//函数声明
int button_read(void);
int T_RED = 7;//红色LED所占比例
sbit LED_RED = P2^0;
sbit LED_BLUE = P2^1;
sbit BUTTON_1 = P3^0;
sbit BUTTON_2 = P3^1;
sbit BUTTON_3 = P3^2;
sbit BUTTON_4 = P3^3;
#define SEG_PORT_D P0
sbit SEG_PORT_W1 = P0^4 ;
sbit SEG_PORT_W2 = P3^5 ;
sbit SEG_PORT_W3 = P3^6;
sbit SEG_PORT_W4 = P3^7 ;
void SEG_Display(int DAT , int NUM) ;
void SEG_Display_4(int *BUFF) ;
const int LED_RED_RATE[4] = {7,6,5,4};//LED中红色的比例
#define ON 0
#define OFF 1
void main(void)
{
int BUFF[4]={7,11,11,1};
int ButtonValue = 0;//按键值变量
Timer0Init();
while(1)
{
ButtonValue = button_read();//读取按键值
if(ButtonValue != 0)//说明有按键按下
{
T_RED = LED_RED_RATE[ButtonValue - 1];//将值送给按键存储起来
BUFF[0] = T_RED;
BUFF[1] = 11;//表示-
BUFF[2] = 11;//表示-
BUFF[3] = 8-BUFF[0];
}
SEG_Display_4(BUFF);
}
}
void Timer0Init(void)//定时器初始化 初始化为250us @ 12MHZ晶振
{
TMOD = 0x01;
TL0 = 0x06;
TH0 = 0xFF;
TF0 = 0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
int T_C = 0;//计时变量
void tm0_isr() interrupt 1 using 1//定时器0初始化
{
TL0 = 0x06;
TH0 = 0xFF;
T_C ++;//每次进入中断 计时变量自增
if(T_C >= 8)//8次后变量清零
{
T_C = 0;
}
if(T_C < T_RED)
{
LED_RED = ON;
LED_BLUE = OFF;
}
else if(T_C < 8)
{
LED_RED = OFF;
LED_BLUE = ON;
}
}
int button_read(void)//读取按键值
{
int i = 0;
if(!BUTTON_1)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_1)
{
while(!BUTTON_1);
return 0x01;
}
return 0x00;
}
else if(!BUTTON_2)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_2)
{
while(!BUTTON_2);
return 0x02;
}
return 0x00;
}
else if(!BUTTON_3)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_3)
{
while(!BUTTON_3);
return 0x03;
}
return 0x00;
}
else if(!BUTTON_4)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_4)
{
while(!BUTTON_4);
return 0x04;
}
return 0x00;
}
return 0x00;
}
const int SEG_CODE[12 + 10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10} ;//11?-
int SEG_SWITCH_NUM = 0 ;
int SEG_SWITCH_DELAY = 0 ;
void SEG_Display(int DAT , int NUM)//?
{
int i = 0x01 , j = 0;
SEG_PORT_D = SEG_CODE[10] ;//?
switch(NUM)//?
{
case 0:{SEG_PORT_W1 = 0 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 1 ;break;}
case 1:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 0 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 1 ;break;}
case 2:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 0 ; SEG_PORT_W4 = 1 ;break;}
case 3:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 0 ;break;}
default:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 1 ;break;}
}
SEG_PORT_D = SEG_CODE[DAT] ;
}
void SEG_Display_4(int *BUFF)//?4?
{
SEG_SWITCH_DELAY ++ ;
if(SEG_SWITCH_DELAY >= 2)//?
{
SEG_Display(*(BUFF + SEG_SWITCH_NUM) , SEG_SWITCH_NUM) ;
SEG_SWITCH_NUM ++ ;
if(SEG_SWITCH_NUM >= 4) SEG_SWITCH_NUM = 0 ;
SEG_SWITCH_DELAY = 0 ;
}
}
void Timer0Init(void);//函数声明
int button_read(void);
int T_RED = 7;//红色LED所占比例
sbit LED_RED = P2^0;
sbit LED_BLUE = P2^1;
sbit BUTTON_1 = P3^0;
sbit BUTTON_2 = P3^1;
sbit BUTTON_3 = P3^2;
sbit BUTTON_4 = P3^3;
#define SEG_PORT_D P0
sbit SEG_PORT_W1 = P0^4 ;
sbit SEG_PORT_W2 = P3^5 ;
sbit SEG_PORT_W3 = P3^6;
sbit SEG_PORT_W4 = P3^7 ;
void SEG_Display(int DAT , int NUM) ;
void SEG_Display_4(int *BUFF) ;
const int LED_RED_RATE[4] = {7,6,5,4};//LED中红色的比例
#define ON 0
#define OFF 1
void main(void)
{
int BUFF[4]={7,11,11,1};
int ButtonValue = 0;//按键值变量
Timer0Init();
while(1)
{
ButtonValue = button_read();//读取按键值
if(ButtonValue != 0)//说明有按键按下
{
T_RED = LED_RED_RATE[ButtonValue - 1];//将值送给按键存储起来
BUFF[0] = T_RED;
BUFF[1] = 11;//表示-
BUFF[2] = 11;//表示-
BUFF[3] = 8-BUFF[0];
}
SEG_Display_4(BUFF);
}
}
void Timer0Init(void)//定时器初始化 初始化为250us @ 12MHZ晶振
{
TMOD = 0x01;
TL0 = 0x06;
TH0 = 0xFF;
TF0 = 0;
EA = 1;
ET0 = 1;
TR0 = 1;
}
int T_C = 0;//计时变量
void tm0_isr() interrupt 1 using 1//定时器0初始化
{
TL0 = 0x06;
TH0 = 0xFF;
T_C ++;//每次进入中断 计时变量自增
if(T_C >= 8)//8次后变量清零
{
T_C = 0;
}
if(T_C < T_RED)
{
LED_RED = ON;
LED_BLUE = OFF;
}
else if(T_C < 8)
{
LED_RED = OFF;
LED_BLUE = ON;
}
}
int button_read(void)//读取按键值
{
int i = 0;
if(!BUTTON_1)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_1)
{
while(!BUTTON_1);
return 0x01;
}
return 0x00;
}
else if(!BUTTON_2)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_2)
{
while(!BUTTON_2);
return 0x02;
}
return 0x00;
}
else if(!BUTTON_3)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_3)
{
while(!BUTTON_3);
return 0x03;
}
return 0x00;
}
else if(!BUTTON_4)
{
for(i = 0 ; i < 100 ; i++);
if(!BUTTON_4)
{
while(!BUTTON_4);
return 0x04;
}
return 0x00;
}
return 0x00;
}
const int SEG_CODE[12 + 10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10} ;//11?-
int SEG_SWITCH_NUM = 0 ;
int SEG_SWITCH_DELAY = 0 ;
void SEG_Display(int DAT , int NUM)//?
{
int i = 0x01 , j = 0;
SEG_PORT_D = SEG_CODE[10] ;//?
switch(NUM)//?
{
case 0:{SEG_PORT_W1 = 0 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 1 ;break;}
case 1:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 0 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 1 ;break;}
case 2:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 0 ; SEG_PORT_W4 = 1 ;break;}
case 3:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 0 ;break;}
default:{SEG_PORT_W1 = 1 ; SEG_PORT_W2 = 1 ; SEG_PORT_W3 = 1 ; SEG_PORT_W4 = 1 ;break;}
}
SEG_PORT_D = SEG_CODE[DAT] ;
}
void SEG_Display_4(int *BUFF)//?4?
{
SEG_SWITCH_DELAY ++ ;
if(SEG_SWITCH_DELAY >= 2)//?
{
SEG_Display(*(BUFF + SEG_SWITCH_NUM) , SEG_SWITCH_NUM) ;
SEG_SWITCH_NUM ++ ;
if(SEG_SWITCH_NUM >= 4) SEG_SWITCH_NUM = 0 ;
SEG_SWITCH_DELAY = 0 ;
}
}
这应该是你的数字编码不对,看电路图自己调一下
const int SEG_CODE[12 + 10] = {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0xFF,0xBF,
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10} ;//11?
修改数组