main.h:
/*--------------------------------------------------------------------------------
采集ADC口(P1.3)的数据发送到串口显示
MCU: STC15W408AS
开发板: STC15W408AS开发板
晶振: 内部时钟:11.0592MHz 波特率9600
-------------------------------------------------------------------------------------*/
/************************包含头文件***************************************************/
#include
#include
#include
//定义要采集AD通道
#define ch 3 //即P1.3端口
//sfr P1M1 = 0x91; //PxM1.n,PxM0.n =00--->Standard, 01--->push-pull
//sfr P1M0 = 0x92; // =10--->pure input, 11--->open drain
/************************主函数******************************************************/
void main()
{
unsigned int res=0;
unsigned char result[4]={0};
P1M1=0;
P1M0=0;
InitSerialPort(); //初始化串口,波特率9600,8bit数据位,1停止位,无校验
OpenADC_CHx(ch);
while(1)
{
//发送数据采集结果
//SendByte('\t');
SendString("AD_Value = ");
GetADCResult_Char(ch,result);
SendString(result);
//SendByte(GetADCResult_High(ch));
//SendByte(GetADCResult_Low(ch));
//换行
SendByte('\n');
//延时
Delay(30);
}
}
/*********************************The End****************************************/
ADC.c:
-
-
- #include
- #include
- #include "intrins.h"
-
- code unsigned char decode_ch[8]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80};
-
- /*----------------------------------------------------------------------
- 函数:打开采集通道ch
- 参数:ch取值:0~7
- ----------------------------------------------------------------------*/
- void OpenADC_CHx(unsigned char ch)
- {
- P1ASF |= decode_ch[ch]; //打开采集通道
- ADC_RES = 0; //Clear previous result
- ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
- Delay(2); //ADC power-on and delay
- }
- /*----------------------------------------------------------------------
- 函数:关闭采集通道ch
- 参数:ch取值:0~7
- ----------------------------------------------------------------------*/
- void CloseADC_CHx(unsigned char ch)
- {
- P1ASF &= ~decode_ch[ch]; //关闭采集通道
- }
-
- /*----------------------------------------------------------------------
- 函数:获取采集数据
- 参数:ch取值0~7,代表0-7通道号
- 返回值:10bit返回值
- -----------------------------------------------------------------------*/
- unsigned int GetADCResult(unsigned char ch)
- {
- unsigned int res = 0;
-
- 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
-
- res = ADC_RES;
- res = res << 2;
- res = res + (0x03&ADC_RESL);
- return res; //Return ADC result
- }
- /*----------------------------------------------------------------------
- 函数:获取采集数据,结果以4个数字格式存储
- 参数:ch取值0~7,代表0-7通道号
- 返回值:返回采集结果,并分解为4个数字存储到数组中,方便数显使用
- -----------------------------------------------------------------------*/
- void GetADCResult_Number(unsigned char ch,unsigned char* ptr)
- {
- unsigned int res = 0;
-
- 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
-
- res = ADC_RES;
- res = res << 2;
- res = res + (0x03&ADC_RESL);
-
- *ptr = res/1000;
- *(ptr+1) = (res % 1000)/100;
- *(ptr+2) = ((res % 1000)%100)/10;
- *(ptr+3) = ((res % 1000)%100)%10;
- }
- /*----------------------------------------------------------------------
- 函数:获取采集数据,结果以4个字符格式存储
- 参数:ch取值0~7,代表0-7通道号
- 返回值:返回采集结果,并分解为4个字符存储到数组中,方便串口发送使用
- -----------------------------------------------------------------------*/
- void GetADCResult_Char(unsigned char ch,unsigned char* ptr)
- {
- unsigned int res = 0;
-
- 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
-
- res = ADC_RES;
- res = res << 2;
- res = res + (0x03&ADC_RESL);
-
- *ptr = res/1000+0x30;
- *(ptr+1) = (res % 1000)/100+0x30;
- *(ptr+2) = ((res % 1000)%100)/10+0x30;
- *(ptr+3) = ((res % 1000)%100)%10+0x30;
- }
- /*----------------------------------------------------------------------
- 函数:获取采集数据,返回结果的高字节
- 参数:ch取值0~7,代表0-7通道号
- 返回值:返回采集结果高字节
- -----------------------------------------------------------------------*/
- unsigned char GetADCResult_High(unsigned char 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;
- }
- /*----------------------------------------------------------------------
- 函数:获取采集数据,返回结果的低字节
- 参数:ch取值0~7,代表0-7通道号
- 返回值:返回采集结果低字节
- -----------------------------------------------------------------------*/
- unsigned char GetADCResult_Low(unsigned char 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_RESL;
- }
-
- /*----------------------------------------------------------------------
- 函数:延时
- -----------------------------------------------------------------------*/
- void Delay(unsigned int n)
- {
- unsigned int x;
-
- while (n--)
- {
- x = 5000;
- while (x--);
- }
- }
ADC.h:
-
- #ifndef _ADC_H_
- #define _ADC_H_
-
- /*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 OpenADC_CHx(unsigned char ch);
- void CloseADC_CHx(unsigned char ch);
- unsigned int GetADCResult(unsigned char ch);
- void GetADCResult_Number(unsigned char ch,unsigned char* ptr);
- void GetADCResult_Char(unsigned char ch,unsigned char* ptr);
- unsigned char GetADCResult_High(unsigned char ch);
- unsigned char GetADCResult_Low(unsigned char ch);
- void Delay(unsigned int n);
-
- #endif
-
USART.c:
- #include
- #include
-
- unsigned char *pchar; //定义一个全局指针
-
- //----------------------------------------------------------
- // 函数名称:void IniSerialPort(void)
- // 函数功能:串口初始化
- //----------------------------------------------------------
- void InitSerialPort(void)
- {
- SCON = 0x50; //8位数据,可变波特率
- AUXR |= 0x01;
- AUXR |= 0x04; //定时器时钟为Fosc,1T模式
- T2L = 0xE0; //定时器初值,内部时钟11.0592M,波特率9600
- T2H = 0xFE; //定时器初值
- AUXR |= 0x10; //启动定时器
- //ES=1; //串口中断开关,采用查询法时不用打开中断
- REN=1; //串口为工作方式1,允许接收数据
- SM0=0; //SM0 SM1串口工作方式选择,01:8位异步收发,波特率由定时器决定
- SM1=1;
- }
- //----------------------------------------------------------
- // 函数名称:unsigned char ReceiveByte(void)
- // 函数功能:查询法接收一个字节
- //----------------------------------------------------------
- unsigned char ReceiveByte(void)
- {
- unsigned char rbyte;
- while(!RI); //查询接收标志位,是否有数据到达缓冲区
- RI=0; //清零接收标志位
- rbyte=SBUF; //从缓冲区读取数据
- return rbyte;
- }
- //----------------------------------------------------------
- // 函数名称:void SendByte(unsigned char sbyte)
- // 函数功能:串口发送一个字节
- //----------------------------------------------------------
- void SendByte(unsigned char sbyte)
- {
- SBUF=sbyte; //发送数据
- while(!TI); //等待发送完成
- TI=0; //清零发送标志位
- }
- //----------------------------------------------------------
- // 函数名称:void SendString(unsigned char *pstr)
- // 函数功能:串口发送一个字符串
- //----------------------------------------------------------
- void SendString(unsigned char *pstr)
- {
- while(*pstr!='\0') //字符串是否发完
- {
- SendByte(*pstr); //发送字符串数据
- pstr++; //指向下一个字符
- }
- }
- //----------------------------------------------------------
- // 函数名称:void SerialPortInte(void)
- // 函数功能:串口中断方式接收一个字符
- //----------------------------------------------------------
- void SerialPortInte(void) interrupt 4
- {
- RI=0; //清零接收标志位
- *pchar=SBUF; //读取缓冲区的数据
- }
- /**********************************THE END**********************************/
-
USART.h:
- #ifndef _USART_H_
- #define _USART_H_
-
- extern unsigned char *pchar;
-
- void InitSerialPort(void); //串口初始化设置
- unsigned char ReceiveByte(void); //串口查询法接收一个字节
- void SendByte(unsigned char sbyte); //串口发送一个字节
- void SendString(unsigned char *pstr); //串口发送一个字符串
-
- #endif
完整资料打包: