今天写了个1824内部ADC的程序,通过串口到上位机,但是不掉电的话数据不更新,只有断电后上电才回更新数据,还有就是在保持上电的情况下插拔ICD3连接到下载口的情况下,串口发出的数据也会随电位器的调节而有所改变。求教啊。
#include<pic.h>
__CONFIG(FOSC_INTOSC & WDTE_OFF & PWRTE_OFF & MCLRE_ON & CP_OFF & BOREN_OFF & CLKOUTEN_OFF);
__CONFIG(WRT_OFF & STVREN_OFF & BORV_LO & BOREN_OFF & LVP_OFF);
__IDLOC7(0x20,0x12,0x06,0x01);
void Init(void);
unsigned char receive,xdata_m[50],address;
unsigned int ithrob,byte_cnt,i,adnum;
unsigned char a1,a2,a3,a4;
void delay(unsigned int x)
{
unsigned int a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
unsigned int get_ad()
{
unsigned int adval;
float advalf;
delay(100);
ADGO=1;
while(ADGO);
adval=ADRESH;//00000000 00000011
adval=adval<<8|ADRESL;//00000011 11111111
advalf=adval/1023.0*5.0;//==2.3843
adval=advalf*1000;
return (adval);
}
void main()
{
Init();
while(1)
{
adnum=get_ad();
a1=adnum/1000;
a2=adnum%1000/100;
a3=adnum%100/10;
a4=adnum%10;
}
}
void Init(void)
{
OSCCON = 0b01101000; //4MHZ
TRISC = 0b00101111;
TRISA = 0b00010111;
ANSELA = 0x17;
ANSELC = 0x0f;
// Flag_1=0;
OPTION_REG = 0x05; //1:64
T1CON=0x31;//1:8
TMR1IE=1;
PEIE=1;
GIE=1;
RCIE = 1;
// ADIE = 1;
RCIF = 0;
// ADIF = 0;
TXEN = 1;
CREN = 1;
SYNC = 0;
SPEN = 1;
SPBRG = 25; //9600bps
BRGH = 1;
BRG16 = 0;
ADCON0=0x09;
ADCON1=0x90;
delay(10);
}
void interrupt isr(void)
{
if(RCIE&&RCIF==1)
{
receive=RCREG;
TXREG=a1+0x30;while(!TRMT);
TXREG=0x2e;while(!TRMT);
TXREG=a2+0x30;while(!TRMT);
TXREG=a3+0x30;while(!TRMT);
TXREG=a4+0x30;while(!TRMT);
TXREG=0x5f;while(!TRMT);
}
}
|