|
本帖最后由 憶1709 于 2021-6-3 16:37 編輯
程序要實(shí)現(xiàn)的是使用STC15L408AD的ADC輸入來控制LED燈亮滅,根據(jù)ADC輸入的不同值,來控制不同的LED燈亮。
目前使用串口工具測(cè)試過,ADC轉(zhuǎn)換值沒有問題,ADC的輸入端是10K的搖桿電位器,接到STC15的一路ADC;
為什么我用if語句判斷沒有效果,這里程序該怎么寫?謝謝。
如下是我的程序:
#include <STC15W.h>
#include <intrins.h>
#define ADC_POWER 0x80
#define ADC_FLAG 0x10
#define ADC_SPEEDHH 0x00
#define ADC_START 0x08
typedef unsigned char uchar;
typedef unsigned int uint;
void GetADC_Result(uchar ch);
void ADC_INIT();
uint ADC_DATA;
void delay_1ms(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
//*****ADC初始化函數(shù)*****//
void ADC_INIT()
{
P1ASF =0xff;
ADC_RES=0;ADC_RESL=0; //清除ADC結(jié)果寄存器
ADC_CONTR|=ADC_POWER; //開啟ADC電源
delay_1ms(20); //延時(shí)20ms
}
//*****ADC轉(zhuǎn)換函數(shù)*****//
void GetADC_Result(uchar ch)
{
ADC_DATA=0;
ADC_CONTR = ADC_POWER| ADC_SPEEDHH| ADC_START|ch; //電源開/90個(gè)CLK轉(zhuǎn)換頻率/開轉(zhuǎn)換/設(shè)定通道
_nop_();_nop_();_nop_();_nop_();
while(ADC_CONTR&ADC_FLAG==0); //等待轉(zhuǎn)換完成
ADC_CONTR &= ~ADC_FLAG; //清FLAG
ADC_DATA=ADC_RES;
ADC_DATA=ADC_RES<<2;
ADC_DATA|=ADC_RESL;
}
/*************************************************************************************
功能:采集ADC的變化來控制P1口輸出控制8個(gè)LED燈的變化
實(shí)現(xiàn)過程:采樣ADC值===>比較ADC值===>根據(jù)ADC值所處范圍不同,點(diǎn)亮不同的LED燈
*************************************************************************************/
void main()
{
ADC_INIT();
P10=0;P26=0;P27=0;P32=0;P34=0;P35=0;P36=0;P20=0;
while(1)
{
uchar i;
GetADC_Result(1); //1通道的ADC轉(zhuǎn)換 ,搖桿電位器在量程內(nèi)擺動(dòng),ADC值的范圍是0到850
if(ADC_DATA<100) {P10=1;P26=0;P27=0;P32=0;P34=0;P35=0;P36=0;P20=0;}
if(120<ADC_DATA<220) {P10=1;P26=1;P27=0;P32=0;P34=0;P35=0;P36=0;P20=0;}
if(240<ADC_DATA<340) {P10=1;P26=1;P27=1;P32=0;P34=0;P35=0;P36=0;P20=0;}
if(360<ADC_DATA<460) {P10=1;P26=1;P27=1;P32=1;P34=0;P35=0;P36=0;P20=0;}
if(480<ADC_DATA<580) {P10=1;P26=1;P27=1;P32=1;P34=1;P35=0;P36=0;P20=0;}
if(600<ADC_DATA<700) {P10=1;P26=1;P27=1;P32=1;P34=1;P35=1;P36=0;P20=0;}
if(720<ADC_DATA<820) {P10=1;P26=1;P27=1;P32=1;P34=1;P35=1;P36=1;P20=0;}
if(800<ADC_DAT) {P10=1;P26=1;P27=1;P32=1;P34=1;P35=1;P36=1;P20=1;}
}
}
|
|