#define MAIN_Fosc 12000000L //定义主时钟
//#define Main_Fosc_KHZ (MAIN_Fosc / 1000)
#include "15W4KxxS4.h"
#include "intrins.h"
#include <string.h>
#define Buf_Max 5
#define uint8 unsigned char
#define uint16 unsigned int
uint8 data Rec_Buf[Buf_Max];
uint8 i = 0;
//uint8 data temp[100];
uint8 ti = 0;
sbit CLK=P3^0;
sbit SI=P0^0;
void Timer0Init(void) //1毫秒@12.000MHz
{
AUXR &= 0x7F; //定时器时钟12T模式
TMOD &= 0xF0; //设置定时器模式
TL0 = 0x18; //设置定时初值
TH0 = 0xFC; //设置定时初值
TF0 = 0; //清除TF0标志
ET0=1;
EA=1;
TR0 = 1; //定时器0开始计时
}
void Delay500us() //@12.000MHz
{
unsigned char i, j;
i = 6;
j = 211;
do
{
while (--j);
} while (--i);
}
void Delay1ms() //@12.000MHz
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void Delay399500us() //@12.000MHz
{
unsigned char i, j, k;
_nop_();
_nop_();
i = 19;
j = 56;
k = 85;
do
{
do
{
while (--k);
} while (--j);
} while (--i);
}
void delay_ms(unsigned char ms)
{
unsigned int i;
do
{
i = MAIN_Fosc /12000;
while(--i) ; //14T per loop
}
while(--ms);
}
void UartInit(void) //9600bps@12.000MHz
{
AUXR1 |=0x40; //选择串口3.6 3.7
SCON = 0x50; //8位数据,可变波特率
AUXR |= 0x40; //定时器1时钟为Fosc,即1T
AUXR &= 0xFE; //串口1选择定时器1为波特率发生器
TMOD &= 0x0F; //设定定时器1为16位自动重装方式
TL1 = 0xC7; //设定定时初值
TH1 = 0xFE; //设定定时初值
ET1 = 0; //禁止定时器1中断
TR1 = 1; //启动定时器1
}
void U1SendData(uint8 ch)
{
SBUF = ch; //写数据到UART数据寄存器
while(TI == 0); //在停止位没有发送时,TI为0即一直等待
TI = 0; //清除TI位(该位必须软件清零)
}
void U1SendString(uint8 *s)
{
while (*s) //检测字符串结束标志
{
U1SendData(*s++); //发送当前字符
}
}
void Uart1() interrupt UART1_VECTOR using 1
{
ES = 0; // 串口1中断关闭
if (RI) //串行接收到停止位的中间时刻时,该位置1
{
RI = 0; //清除RI位 (该位必须软件清零)
Rec_Buf[i] = SBUF; //把串口1缓存SBUF寄存器数据依次存放到数组Rec_Buf中
i++;
if(i>Buf_Max) //接收数大于定义接收数组最大个数时,覆盖接收数组之前值
{
i = 0;
}
}
if (TI) //在停止位开始发送时,该位置1
{
TI = 0; //清除TI位(该位必须软件清零)
}
ES = 1; // 串口1中断打开
}
void ADC_config(void)
{
ADC_CONTR|=0x80; //开AD转换电源
delay_ms(10); //适当延时等待AD转换供电稳定
P1ASF|=0x80; //选择P1.7作为模拟功能AD使用
ADC_CONTR|=0x07; //选择P1.7作为AD转换通道输入使用
ADC_CONTR|=0x60; //AD转换速度为90个时钟周期转换一次
ADC_CONTR&=0xEF; //清AD转换完成标志
EADC=0; //禁止ADC转换中断
CLK_DIV|=0x20; //ADC转换结果ADC_RES存高2位,ADC_RESL存低8位
ADC_CONTR|=0x08; //启动AD转换,ADC_START=1
}
uint16 Get_ADC10bitResult(void)
{
uint16 AD_Dat=0;
ADC_CONTR&=0xE7; // 将ADC_FLAG清0
//10位AD结果的高2位放ADC_RES的低2位,低8位在ADC_RESL
AD_Dat = ADC_RES; //将ADC_RES低2位移到应在的第9位和第10位
AD_Dat <<= 8;
AD_Dat|= ADC_RESL; //将ADC_RESL的8位移到应在的低8位
ADC_CONTR|=0x08; //重新启动AD转换,ADC_START=1。
return AD_Dat;
}
int main(void)
{
uint16 TempPhoto,Temp;
uint8 strPhoto[6];
P1M1 &= 0x3F; P1M0 &= 0x3F; //设置P1.6~P1.7为准双向口
P3M1 &= 0x3F; P3M0 &= 0x3F; //设置P3.6~P3.7为准双向口
ADC_config(); //ADC初始化
UartInit(); //串口1初始化
EA = 1; //打开总中断
delay_ms(10); //初始化后延时
Timer0Init();
ti=0;
while (1)
{
Delay500us();
SI=1;
Delay1ms();
SI=0;
Delay399500us();
memset(strPhoto, 0, sizeof(strPhoto)); //strTemp数组清零
TempPhoto = Get_ADC10bitResult(); //实时读取P1.7通道的AD转换结果
delay_ms(5);
if(TempPhoto==Temp) //如果ADC检测结果没有变化,则不更新屏显示
{
;
}
else //如果ADC检测结果发生变化,则更新屏显示内容
{
Temp=TempPhoto;
strPhoto[0] = TempPhoto/100+48; //光强度百位
strPhoto[1] = (TempPhoto%100)/10+48; //光强度十位
strPhoto[2] = (TempPhoto%100)%10+48; //光强度个位
U1SendString(strPhoto);
U1SendString("\r\n"); //输出回车换行符,方便观察数据
}
delay_ms(5);
}
}
void wave() interrupt 1
{
CLK=!CLK;
}