![]() |
發布時間: 2016-9-25 17:25
正文摘要:用adc0832芯片和51單片機做的電壓表,里面有仿真,C源程序,可根據不同的應用場合不同,稍改程序就能改變量程,達到你的要求 源程序: #include<reg51.h> #include<intrins.h> #define uchar unsigne ... |
#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 |
點一個站 剛謝分享 |
大佬我想問一下,這個能測量外電壓嗎 |
不錯 試驗很成功 |
很不錯的帖子,贊一個 |
很不錯,很簡易的帖子。 |
范圍多大 |