1.硬件
STC89C52/90C516RD/89C516RD等 晶振:11.0592M 内存配置:
Memory Model为small,选择其他可能无法正常运行。
2.代码
adc0832.h
#ifndef __ADC0832_H__
#define __ADC0832_H__
#include <reg52.h>
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
sbit CS = P1^0;
sbit Clk = P1^1;
sbit DATI = P1^2;
sbit DATO = P1^2;
uchar get_adc0832_data(uchar CH);
#endif
adc0832.c
#include "adc0832.h"
#include <stdlib.h>
#include <intrins.h>
uint dat = 0x00;
uchar CH;
uchar data dis[] = {0x00, 0x00,0x00,0x00};
uchar get_adc0832_data(uchar CH)
{
uchar i,test,adval;
adval = 0x00;
test = 0x00;
Clk = 0;
DATI = 1;
_nop_();
CS = 0;
_nop_();
Clk = 1;
_nop_();
if ( CH == 0x00 )
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 0;
_nop_();
Clk = 1;
_nop_();
}
else
{
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
DATI = 1;
_nop_();
Clk = 1;
_nop_();
}
Clk = 0;
DATI = 1;
for( i = 0;i < 8;i++ )
{
_nop_();
adval <<= 1;
Clk = 1;
_nop_();
Clk = 0;
if (DATO)
adval |= 0x01;
else
adval |= 0x00;
}
for (i = 0; i < 8; i++)
{
test >>= 1;
if (DATO)
test |= 0x80;
else
test |= 0x00;
_nop_();
Clk = 1;
_nop_();
Clk = 0;
}
if (adval == test)
dat = test;
nop_();
CS = 1;
DATO = 1;
Clk = 1;
return dat;
}
|