1.实验目的 掌握SPI总线的ADC0832模数转换的使用方法。 2.实验内容 利用ADC0832采集直流0~5V的电压,并在LCD1602上显示结果。
C语程序 #include <reg52.H> #include"LCD1602.h" #include"ADC0832.h" #define uchar unsigned char #define uint unsigned int uchar Voltage = 0; /*主函数*/ void main() { LCD1602_init(); LCD1602_string(1,1," ADC0832 Test"); LCD1602_string(2,1," Voltsge: V"); while(1) { Voltage = adc0832(1); Display_2num_dot(2,10,Voltage*50/255); } } #include<reg52.h> #include<intrins.h> #define uchar unsigned char #define unit unsigned int sbit ADCS =P1^5; sbit ADDI =P3^2; sbit ADDO =P1^7; sbit ADCLK=P1^6; uchar adc0832(uchar channel) { uchar i=0; uchar dat=0; if(channel==0)channel=2; if(channel==1)channel=3; ADDI=0;ADCS=1;ADCLK=0; ADDI=1;ADCS=0;ADCLK=1; ADCLK=0;ADCLK=1; ADDI=channel&0x1; ADCLK=0;ADCLK=1; ADDI=(channel>>1)&0x1; ADCLK=0;ADDI=1; dat=0; for(i=0;i<8;i++) { dat|=ADDO; ADCLK=1; ADCLK=0; dat<<=1; if(i==7)dat|=ADDO; } ADCS=1;ADCLK=0;ADDO=1; return(dat); } #ifndef __ADC0832_H__ #define __ADC0832_H__ unsigned char adc0832(unsigned char channel); #endif
|