壓力校驗(yàn)儀開發(fā)工程師依照demo編寫了如下代碼,卻打不開AD轉(zhuǎn)換(顯示的轉(zhuǎn)換結(jié)果亂七八糟,沒有任何規(guī)律),其中displaystring()是用LCD顯示字符串的函數(shù),已經(jīng)調(diào)試成功。編寫程序如下: #Include <REG1210.H> // The header file with the MSC register definitions #Include <stdio.h> // Standard I/O so we can use the printf function
extern signed long bipolar(void); #define LSB 298e-9 #Ifndef XTAL // if no XTAL compiler variable defined use: #define XTAL 11059200 // XTAL frequency 11.0592 MHz #Endif
Main(void) { char i,j; float result;
PDCON = 0x75; // Turn on the A/D ACLK = XTAL/1000000; // ACLK freq. = XTAL Freq./(ACLK +1) = 0.9216 MHz // 0.9216 Mhz/64 = 14,400 Hz DECIMATION = 1440; // Data Rate = 14,400/1,440 = 10 Hz ADMUX = 0x08; // AINP = AIN0, AINN = AINCOM ADCON0 = 0x30; // Vref On, 2.5V, Buffer Off, PGA=1 ADCON1 = 0x01; // bipolar, auto, self calibration, offset, gain //wait for the calibration to take place for (i=0;i<3;i++){ // dump 3 conversions while(!(AIE&0x20)) {} j=ADRESL; }
j=1; while(1){ while(!(AIE&0x20)) {} // Waiting for conversion result=bipolar(); // Save Results sprintf (szBuffer, "%10.8f", result*LSB); DisplayString(0x80, szBuffer); // Display ADResult on the first line of LCD } }