#include <STC12C5A60S2.H>
#include "intrins.h"
#define FOSC 11059200L
#define BAUD 9600
typedef unsigned char BYTE;
typedef unsigned int WORD;
#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
int num = 0;
void InitUart();
void SendData(BYTE dat);
void Delay(WORD n);
void InitADC();
void main()
{
InitUart(); //Init UART, use to show ADC result
InitADC(); //Init ADC sfr
EA = 1;//打开总中断
EADC = 1;//打开ADC中断 //Enable ADC interrupt and Open master interrupt switch
//Start A/D conversion
while (1)
{
}
}
void adc_isr() interrupt 5
{
num++;
ADC_CONTR &= !ADC_FLAG; //ADC_FLAG标志位清零
SendData(ADC_RES);
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START|7;
if (num >= 250)
{
ADC_CONTR = 0;
}
}
/*----------------------------
Initial ADC sfr
----------------------------*/
void InitADC()
{
P1ASF = 0x80; //只设置7口作为ADC采集通道
ADC_RES = 0; //将结果清零
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ADC_START|7; //赋值
Delay(2); //延时4个时钟周期,保证正常进入ADC采集
}
/*----------------------------
Initial UART
----------------------------*/
void InitUart()
{
PCON |= 0x80; //使能波特率倍速位SMOD
SCON = 0x50; //8位数据,可变波特率
AUXR &= 0xBF; //定时器1时钟为Fosc/12,即12T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //清除定时器1模式位
TMOD |= 0x20; //设定定时器1为8位自动重装方式
TL1 = 0xF3; //设定定时初值
TH1 = 0xF3; //设定定时器重装值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1 //T1 start running
}
/*----------------------------
Send one byte data to PC
Input: dat (UART data)
Output:-
----------------------------*/
void SendData(BYTE dat)
{
SBUF = dat;
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
//Send current data
}
/*----------------------------
Software delay function
----------------------------*/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
上面是我直接用的例程,想问下各位大佬,我只想采集250个点, if (num >= 250)
{
ADC_CONTR = 0;
}有这个判断,连采集的数据都没法通过串口发送到电脑。 |