#include<reg51.h> #include<intrins.h> #define uchar unsigned char #define uint unsigned int sbit cs=P3^5; sbit clk=P3^3; sbit dati=P3^4; sbit dato=P3^4; sbit P20=P2^4; uchar dat=0x00; uchar count=0x00; uchar CH; uchar dis[]={0x01,0x02,0x03,0x04}; uchar code tab[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; uchar code tablewe[]={0xfd,0xfb,0xf7,0xf0,0xdf,0xfe}; uchar adc0832(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=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;} void delay(void)//延时函数 {uint r; for(r=10;r<500;r++) ; } void convdata(uchar i) { dis[0]=i/51; dis[1]=(i%51)*10/51; dis[2]=((i%51)*10%51)*10/51; } void display(void) {P2=0x02; P0=tab[dis[0]]&0x7f; delay(); P2=0x04; P0=tab[dis[1]]; delay(); P2=0x08; P0=tab[dis[2]]; delay(); } void main(void) { P2=0xff; P0=0xff; delay(); delay(); CH=0x00; TMOD=0x01; TH0=15535/256; TL0=15536%256; IE=0x82; TR0=1; while(1) {dat=adc0832(CH); convdata(dat); display(); }} void timero(void) interrupt 1 { TMOD=0x01; TMOD=0x01; TH0=15535/256; TL0=15536%256; IE=0x82; TR0=1; count++; if(count==0x01) {count=0x00;dat=adc0832(CH);}} |
怎么检测是AC还是DC |
点一个站 刚谢分享 |
大佬我想问一下,这个能测量外电压吗 |
不错 试验很成功 |
很不错的帖子,赞一个 |
很不错,很简易的帖子。 |
范围多大 |