#include <sh79f085.h>
#define SYSCLK 16600000L /* 系统时钟频率 */
#define ADC_CHAN 4 /* adc输入通道 */
#define ADC_REF 7 /* adc参考电压, 0-7为内部参考电压, 8为外部参考电压 */
#define ADC_CLK 100000L /* adc时钟频率 */
void serial_init(unsigned long bps)
{
SCON = 0x40;
TCON &= 0x3f;
TMOD = 0x21;
PCON |= 0x80;
TH1 = -(SYSCLK/12/bps/16);
TR1 = 1;
}
void adc_init(void)
{
ADCON &= 0x28;
ADCON |= ADC_CHAN & 0x07;
ADCON |= (ADC_REF << 1) & 0x10;
ADT = -(SYSCLK/2/ADC_CLK);
ADCH &= 0x10;
ADCH |= (1<<(ADC_CHAN&0x07)) & 0x07;
ADCH |= ADC_REF & 0x08;
ADCH |= (ADC_REF<<5) & 0xe0;
ADCON |= 0x80;
}
void adc_get(void)
{
ADCON &= 0xbf;
while (!(ADCON & 0x40))
;
SBUF = ADDH;
while (!TI)
;
TI = 0;
SBUF = ADDM;
while (!TI)
;
TI = 0;
SBUF = ADDL;
while (!TI)
;
TI = 0;
}
void main(void)
{
unsigned char i;
/*系统时钟16.6M, 不分频*/
CLKCON |= 0x08;
for (i = 0; i < 255; ++i)
;
CLKCON |= 0x04;
CLKCON &= 0x9f;
serial_init(9600);
adc_init();
while (1)
adc_get();
}
运行后在电脑上只看到串口发来
FF FF 0F B3 9C 05 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00 80 00 00
是adc哪块操作不对吗? |