标题:
STC12C5202AD采集AD后将数据传给UART(实验通过)
[打印本页]
作者:
wxg1988
时间:
2011-12-3 19:59
标题:
STC12C5202AD采集AD后将数据传给UART(实验通过)
#include <STC12C52.h>
#include <intrins.H>
sbit led=P1^6;
sbit key_l=P1^5;
sbit key_h=P1^2;
sbit k_out=P1^7;
sfr ADC_RES = 0xBD;
unsigned char UART_TIME_con;
unsigned char ADC_TIME_con;
unsigned char KEY_TIME_con;
bit key_ok;
bit key_down;
bit key_up;
unsigned int ADC_new_result;
unsigned char ADC_old_result;
unsigned char ADC_Val;
bit ADC_start;
unsigned char AL;
unsigned char AH;
unsigned char tab[2]={0,0};
/////////////////IAP///////////////////////////////////////////////////////
#define uchar unsigned char
#define uint unsigned int
sfr isp_data= 0xc2;
sfr isp_addrh=0xc3;
sfr isp_addrl=0xc4;
sfr isp_cmd=0xc5;
sfr isp_trig=0xc6;
sfr isp_contr=0xc7;
#define isp_byte_read 0x01
#define isp_byte_prog 0x02
#define isp_byte_eras 0x03
#define isp_wait 0x03
/////////////////IAP///////////////////////////////////////////////////////
///////////////////////////读//////////////////////////////////////////////
uint ispread(uint ispaddr)// ispaddr:地址
{
uchar sj;
EA=0;
isp_contr=0x83;
isp_cmd=isp_byte_read;
isp_addrh=(uchar)(ispaddr>>8);
isp_addrl=(uchar)(ispaddr);
isp_trig=0x5A;
isp_trig=0xA5;
_nop_();
sj= isp_data;
isp_contr=0x00;
isp_cmd=0x00;
isp_trig=0x00;
EA=1;
return sj;
}
//////////////////////////写入//////////////////////////////////////////////
void ispprog(uchar sj,uint ispaddr)// ispaddr:地址 sj:数据
{
EA=0;
isp_contr=0x83;
isp_cmd=isp_byte_prog;
isp_addrh=(uchar)(ispaddr>>8);
isp_addrl=(uchar)(ispaddr);
isp_data=sj;
isp_trig=0x5A;
isp_trig=0xA5;
_nop_();
isp_contr=0x00;
isp_cmd=0x00;
isp_trig=0x00;
EA=1;
}
//////////////////////////扇区擦除//////////////////////////////////////////////
void isperas(uint ispaddr)// ispaddr:地址
{
EA=0;
isp_contr=0x83;
isp_cmd=isp_byte_eras;
isp_addrh=(uchar)(ispaddr>>8);
isp_addrl=(uchar)(ispaddr);
isp_trig=0x5A;
isp_trig=0xA5;
_nop_();
isp_contr=0x00;
isp_cmd=0x00;
isp_trig=0x00;
EA=1;
}
void canshu_xie()
{
uchar i;
isperas(0x0200);
for(i=0;i<2;i++)
{
ispprog(tab
,0x0200+i);
}
}
void canshu_du()
{
unsigned char i;
for(i=0;i<2;i++)
{
tab
= ispread(0x0200+i);
_nop_();
}
AL = tab[0];
AH = tab[1];
}
///////////////////////////////////////////////////
void delay()
{
unsigned char i;
for(i=0;i<255;i++)
{
_nop_(); _nop_();_nop_();_nop_();
}
}
void UART_int()
{
SCON = 0X50;
ES = 1;
}
void pwm_int()
{
CMOD = 0x02; //PCA时钟选择,OSC/2 ,输出频率约21K
CL = 0x00;
CH = 0x00;
CCAP0L = 0x80; //初始值同CCAP0H
CCAP0H = 0x80; //占空比50%
CCAPM0 = 0x63; //设置PCA模块0在PWM模式,无中断
CR = 1;
}
void TIME_int()
{
TMOD = 0X21;
TH0 = 0X4C;
TL0 = 0X00;
TH1 = 0XFD;
TL1 = 0XFD;
ET0 = 1;
TR0 = 1;
TR1 = 1;
}
void ADC_int()
{
P1M0 = 0x01; //0000,0011用于A/D转换的P1.x口,先设为开漏
P1M1 = 0x81; //0000,0011P1.0--P1.1先设为开漏。断开内部上拉电阻
ADC_CONTR = ADC_CONTR|0x80; //1000,0000打开A/D转换电源
delay();
ADC_CONTR = 0xe0;
_nop_();_nop_();_nop_();_nop_();
ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,启动转换
while((ADC_CONTR & 0x10)==0x10); //0001,0000等待A/D转换结束
ADC_CONTR = ADC_CONTR&0xE7; //1110,0111清ADC_FLAG位,停止A/D转换
ADC_new_result = (ADC_RES + ADC_old_result * 1)/2;
ADC_Val = ADC_old_result = (unsigned char)ADC_new_result;
ADC_CONTR = ADC_CONTR|0x08; //0000,1000ADCS = 1,启动转换
}
void main()
{
delay();delay();delay();delay();
delay();delay();delay();delay();
canshu_du();
pwm_int();
ADC_int();
TIME_int();
UART_int();
EA = 1;
while(1)
{
if(ADC_start==1)
{
ADC_int();
ADC_start=0;
}
delay();
if((key_l==0)&&(key_h==0))
{
delay();
key_down = 1;
}
else
{ key_down = 0;KEY_TIME_con = 0;}
if((key_up==1)&&(key_l==1)&&(key_h==1))
{
delay();
key_ok = 1;
}
if(key_ok==1)
{
if(key_l==0)
{
delay();
AL = tab[0] = ADC_RES;
canshu_xie();
KEY_TIME_con=0;
key_down = 0;
key_up=0;
led =1;
key_ok=0;
}
if(key_h==0)
{
delay();
AH = tab[1] = ADC_RES;
canshu_xie();
KEY_TIME_con=0;
key_down = 0;
key_up=0;
led =1;
key_ok=0;
}
}
if(ADC_Val<=AL) k_out = 0;
if(ADC_Val>=AH) k_out = 1;
}
}
void PCA_isr() interrupt 7
{
CCF0 = 0;
}
void TIM0_isr() interrupt 1
{
TF0 = 0;
UART_TIME_con = UART_TIME_con+1;
ADC_TIME_con = ADC_TIME_con +1;
if(UART_TIME_con>19)
{
UART_TIME_con = 0;
SBUF = ADC_Val;
}
if(ADC_TIME_con>2)
{
ADC_TIME_con = 0;
ADC_start = 1;
}
if(key_down==1)
{
KEY_TIME_con = KEY_TIME_con +1;
if(KEY_TIME_con>=100)
{key_up = 1;KEY_TIME_con=0;led = 0;}
}
}
void UAET_isr() interrupt 4
{
if(TI == 1)
{
TI = 0;
}
if(RI == 1)
{
RI = 0;
}
}
欢迎光临 电子技术论坛_中国专业的电子工程师学习交流社区-中电网技术论坛 (http://bbs.eccn.com/)
Powered by Discuz! 7.0.0