标签:define 引脚 使用 get one 图片 span += col
测量电压,四位数码管显示
#include <reg52.h> #include <absacc.h> #define uint unsigned int #define SEG1 XBYTE[0xffec] #define SEG2 XBYTE[0xffed] #define SEG3 XBYTE[0xffee] #define SEG4 XBYTE[0xffef] #define SUM 50 //多次测量求平均值次数 #define VREF 4999//放大1000倍 code unsigned char ledmap[]= //共阴LED数码管译码表 { 0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,//0-9不带小数点 0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,0x00,0x10 //0-9带小数点 }; //接口定义 sbit CS = P1^5; sbit DA = P1^6; sbit CK = P1^7; //读取ADC结果函数 unsigned int get_data(void) { unsigned char i; unsigned int temp = 0; CS = 0; //开启控制电路,使能DA和CK IO引脚; for(i=0;i<10;i++) //采集10次 ,即10bit { CK = 0; temp <<= 1; if(DA) temp++; CK = 1; } CS = 1; return(temp); } void delay(unsigned char i) //延时子程序 { while(--i); } main() { unsigned long int temp; unsigned char i; CS = 1; //禁止I/O CLOCK i = SUM; temp = 0; while(1) { temp += get_data(); i--; if(i==0) { temp = temp / SUM; // 取平均值 temp = temp * VREF; // 转换成电压值 temp = temp / 1023; // 十进制译码送数码管显示 SEG1 = ledmap[temp/1000%10 + 10];//+10表示该位带小数点 SEG2 = ledmap[temp/100%10]; SEG3 = ledmap[temp/10%10]; SEG4 = ledmap[temp%10]; i = SUM; temp = 0; } delay(10); //不使用延时,测量程序延时为28us } }
标签:define 引脚 使用 get one 图片 span += col
原文地址:https://www.cnblogs.com/bymeet/p/14358367.html