stc12c5a60s2 单片机ad转换到数码管显示哪里错了求大神指导
#include"reg51.h"#include"intrins.h"typedefunsignedcharBYTE;typedefunsignedintWORD;sb...
#include "reg51.h"
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
sbit D2=P3^0;
BYTE code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90};//共阳数字编码 0.1.2.3.4....9
/*Declare SFR associated with the ADC */
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC high 8-bit result register
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
/*Define ADC operation const for ADC_CONTR*/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //420 clocks
#define ADC_SPEEDL 0x20 //280 clocks
#define ADC_SPEEDH 0x40 //140 clocks
#define ADC_SPEEDHH 0x60 //70 clocks
void InitADC();
BYTE GetADCResult(BYTE ch);
void Delay(WORD n);
void ShowResult(BYTE ch);
BYTE SUE;
float SUE1;
void delay1(BYTE i) //延时函数
{
BYTE j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void main()
{ D2=1;
InitADC(); //Init ADC sfr
while (1)
{
ShowResult(1); //Show Channel1
}
}
void ShowResult(BYTE ch)//显示函数
{
SUE=GetADCResult(ch);
SUE1=SUE/256*5000;
Delay(2);
P0=table[(WORD)SUE1/1000];
P2_0 = 0;
delay1(5);
P2_0 = 1;
P0=table[(WORD)SUE1%1000/100];
P2_1 = 0;
delay1(5);
P2_1 = 1;
P0=table[(WORD)SUE1%100/10];
P2_2 = 0;
delay1(5);
P2_2 = 1;
P0=table[(WORD)SUE1%10];
P2_3 = 0;
delay1(5);
P2_3 = 1;
}
/*----------------------------
Get ADC result
----------------------------*/
BYTE GetADCResult(BYTE ch)
{
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
_nop_(); //Must wait before inquiry
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
return ADC_RES; //Return ADC result
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
P1ASF = 0xFF; //Open 1 channels ADC function P1.1
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDL;
Delay(2); //ADC power-on and delay
}
/*----------------------------
Software delay function
----------------------------*/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
} 展开
#include "intrins.h"
typedef unsigned char BYTE;
typedef unsigned int WORD;
sbit P2_0 = P2^0;
sbit P2_1 = P2^1;
sbit P2_2 = P2^2;
sbit P2_3 = P2^3;
sbit D2=P3^0;
BYTE code table[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,
0xf8,0x80,0x90};//共阳数字编码 0.1.2.3.4....9
/*Declare SFR associated with the ADC */
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC high 8-bit result register
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
/*Define ADC operation const for ADC_CONTR*/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //420 clocks
#define ADC_SPEEDL 0x20 //280 clocks
#define ADC_SPEEDH 0x40 //140 clocks
#define ADC_SPEEDHH 0x60 //70 clocks
void InitADC();
BYTE GetADCResult(BYTE ch);
void Delay(WORD n);
void ShowResult(BYTE ch);
BYTE SUE;
float SUE1;
void delay1(BYTE i) //延时函数
{
BYTE j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void main()
{ D2=1;
InitADC(); //Init ADC sfr
while (1)
{
ShowResult(1); //Show Channel1
}
}
void ShowResult(BYTE ch)//显示函数
{
SUE=GetADCResult(ch);
SUE1=SUE/256*5000;
Delay(2);
P0=table[(WORD)SUE1/1000];
P2_0 = 0;
delay1(5);
P2_0 = 1;
P0=table[(WORD)SUE1%1000/100];
P2_1 = 0;
delay1(5);
P2_1 = 1;
P0=table[(WORD)SUE1%100/10];
P2_2 = 0;
delay1(5);
P2_2 = 1;
P0=table[(WORD)SUE1%10];
P2_3 = 0;
delay1(5);
P2_3 = 1;
}
/*----------------------------
Get ADC result
----------------------------*/
BYTE GetADCResult(BYTE ch)
{
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
_nop_(); //Must wait before inquiry
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
return ADC_RES; //Return ADC result
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
P1ASF = 0xFF; //Open 1 channels ADC function P1.1
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDL;
Delay(2); //ADC power-on and delay
}
/*----------------------------
Software delay function
----------------------------*/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
} 展开
2个回答
展开全部
求个位 P0=table[(WORD)SUE1%10]; 正确
其余错误:应该是
求十位 P0=table[(WORD)SUE1/10%10];
求百位 P0=table[(WORD)SUE1/100%10];
求千位 P0=table[(WORD)SUE1/1000%10];
求万位 P0=table[(WORD)SUE1/10000%10];
如果确保最大值不超过10万,则不对10求余也正确
求万位 P0=table[(WORD)SUE1/10000];
其余错误:应该是
求十位 P0=table[(WORD)SUE1/10%10];
求百位 P0=table[(WORD)SUE1/100%10];
求千位 P0=table[(WORD)SUE1/1000%10];
求万位 P0=table[(WORD)SUE1/10000%10];
如果确保最大值不超过10万,则不对10求余也正确
求万位 P0=table[(WORD)SUE1/10000];
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |