欢迎来到天天文库
浏览记录
ID:37707992
大小:59.00 KB
页数:6页
时间:2019-05-29
《ad da测试程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、AD#ifndef_PCF8591_H#define_PCF8591_H#include"reg52.h"#include"intrins.h"#definesomenop{_nop_();_nop_();_nop_();_nop_();_nop_();}sbitSDA=P2^1;sbitSCL=P2^0;#defineCHANNEL_00x00#defineCHANNEL_10x01#defineCHANNEL_20x02#defineCHANNEL_30x03voidiic_start(void);voidiic_stop(void);voi
2、diic_ack(bitackbit);voidiic_sendbyte(unsignedcharbyt);voiddelay(unsignedchart);unsignedcharadc_pcf8591(void);bitiic_waitack(void);unsignedchari2c_recbyte(void);#endif#include"pcf8591.h"voidiic_start(void){SDA=1;_nop_();SCL=1;somenop;SDA=0;somenop;SCL=0;}voidiic_stop(void){SDA
3、=0;_nop_();SCL=1;somenop;SDA=1;}voidiic_ack(bitackbit){if(ackbit)SDA=0;elseSDA=1;somenop;SCL=1;somenop;SCL=0;SDA=1;somenop;}bitiic_waitack(void){SDA=1;somenop;SCL=1;somenop;if(SDA){SCL=0;iic_stop();return0;}else{SCL=0;return1;}}6voidiic_sendbyte(unsignedcharbyt){unsignedchari
4、;for(i=0;i<8;i++){if(byt&0x80)SDA=1;elseSDA=0;somenop;SCL=1;byt<<=1;somenop;SCL=0;}}unsignedchariic_recbyte(void){unsignedcharda;unsignedchari;for(i=0;i<8;i++){SCL=1;somenop;da<<=1;if(SDA)da
5、=0x01;SCL=0;somenop;}returnda;}voiddelay(unsignedchart){unsignedchari;while(t--){for(
6、i=0;i<112;i++);}}unsignedcharadc_pcf8591(void){unsignedchartemp;iic_start();iic_sendbyte(0x91);iic_waitack();temp=iic_recbyte();iic_ack(0);iic_stop();returntemp;}#include"reg52.h"#include"pcf8591.h"voidinit_t0(void);voidinit_pcf8591(void);voiddisplay(unsignedchard);bitadcflag
7、=0;unsignedchardspflag;unsignedchartflag1;unsignedchartflag2;unsignedcharcodedsp_code_ca[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};voidmain(void){unsignedcharadcvalue;init_pcf8591();init_t0();while(1){display(adcvalue);if(adcflag==1){adcvalue=adc_pcf8591();adcflag
8、=0;}}}6voidisr_t0(void)interrupt1{tflag1++;tflag2++;if(tflag1==16)//4ms{tflag1=0;dspflag++;if(dspflag==3)dspflag=0;}if(tflag2==80)//20ms{tflag2=0;adcflag=1;}}voidinit_t0(void){TMOD=0x02;TH0=0x06;TL0=0x06;ET0=1;EA=1;TR0=1;}voidinit_pcf8591(void){iic_start();iic_sendbyte(0x90);
9、iic_waitack();iic_sendbyte(CHANNEL_3);iic_waitack();iic_stop();delay
此文档下载收益归作者所有