//以下代码均是复制引用STC官方案例,还望多多包涵,祝你学习愉快
/------------------------------------------------------------------/
/ --- STC MCU Limited ---------------------------------------------/
/ --- STC89-90xx Series MCU A/D Conversion Demo -------------------/
/ If you want to use the program or the program referenced in the /
/ article, please specify in which data and procedures from STC /
/------------------------------------------------------------------/
#include "reg51h"
#include "intrinsh"
#define FOSC 18432000L
#define BAUD 9600
typedef unsigned char BYTE;
typedef unsigned int WORD;
/Declare SFR associated with the ADC /
sfr ADC_CONTR = 0xC5; //ADC control register
sfr ADC_RES = 0xC6; //ADC high 8-bit result register
sfr ADC_LOW2 = 0xC7; //ADC low 2-bit result register
sfr P1ASF = 0x97; //P1 secondary function control register
/Define ADC operation const for ADC_CONTR/
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDHH 0x00 //89 clocks
#define ADC_SPEEDH 0x20 //178 clocks
#define ADC_SPEEDL 0x40 //356 clocks
#define ADC_SPEEDLL 0x60 //534 clocks
void InitUart();
void InitADC();
void SendData(BYTE dat);
BYTE GetADCResult(BYTE ch);
void Delay(WORD n);
void ShowResult(BYTE ch);
void main()
{
InitUart(); //Init UART, use to show ADC result
InitADC(); //Init ADC sfr
while (1)
{
ShowResult(0); //Show Channel0
ShowResult(1); //Show Channel1
ShowResult(2); //Show Channel2
ShowResult(3); //Show Channel3
ShowResult(4); //Show Channel4
ShowResult(5); //Show Channel5
ShowResult(6); //Show Channel6
ShowResult(7); //Show Channel7
}
}
/----------------------------
Send ADC result to UART
----------------------------/
void ShowResult(BYTE ch)
{
SendData(ch); //Show Channel NO
SendData(GetADCResult(ch)); //Show ADC high 8-bit result
//if you want show 10-bit result, uncomment next line
// SendData(ADC_LOW2); //Show ADC low 2-bit result
}
/----------------------------
Get ADC result
----------------------------/
BYTE GetADCResult(BYTE ch)
{
ADC_CONTR = 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 UART
----------------------------/
void InitUart()
{
SCON = 0x5a; //8 bit data ,no parity bit
TMOD = 0x20; //T1 as 8-bit auto reload
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
TR1 = 1; //T1 start running
}
/----------------------------
Initial ADC sfr
----------------------------/
void InitADC()
{
P1ASF = 0xff; //Open 8 channels ADC function
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_SPEEDLL;
Delay(2); //ADC power-on and delay
}
/----------------------------
Send one byte data to PC
Input: dat (UART data)
Output:-
----------------------------/
void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}
/----------------------------
Software delay function
----------------------------/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
仔细检查下载软件上选定的器件型号与你实际使用的芯片型号是否完全一致,注意一定要完全一致,以前我这边就出现过设置为STC89C52给STC89C52RC下程序无法连接的情况。如果不是这个问题,建议你换个版本的STCISP软件试试
以上就是关于单片机型号为STC90C516AD,怎么用自带的AD转换读取数据麻烦大家写一下相应的子程序,稍详全部的内容,包括:单片机型号为STC90C516AD,怎么用自带的AD转换读取数据麻烦大家写一下相应的子程序,稍详、stc12c4052ad烧写问题。 STC单片机的ISP用的是uart串口下载程序。 本人手头上有、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)