基于單片機的三位半數字電壓表?#include <intrins.h>
#include <reg52.h>
#include <stdio.h>
#include "dis.h"
#define ulong unsigned long
#define uint unsigned int
#define uchar unsigned char
uchar k,i,j;
float l;
sbit sw=P3^0; //159選通開關
sbit ADCS =P3^1; //ADC0832 chip seclect
sbit ADDI =P3^3; //ADC0832 k in
sbit ADDO =P3^3; //ADC0832 k out
sbit ADCLK =P3^2; //ADC0832 clock signal
uchar dsw[20]; //存放sprintf轉換字符
uchar idata value[149]; //點陣字符儲存
void delay(int c) //延時
{
int i,j;
for(i=0;i<c;i )
for(j=0;j<10;j )
;
}
//采集并返回
unsigned int Adc0832(uchar channel) //AD轉換,返回結果
{
uchar i=0;
uchar j;
uint dat=0;
uchar ndat=0;
if(channel==0)channel=2;
if(channel==1)channel=3;
ADDI=1;
_nop_();
_nop_();
ADCS=0;//拉低CS端
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿1
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
ADDI=channel&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿2
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
ADDI=(channel>>1)&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿3
ADDI=1;//控制命令結束
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i )
{
dat|=ADDO;//收數據
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//形成一次時鐘脈沖
_nop_();
_nop_();
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i )
{
j=0;
j=j|ADDO;//收數據
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//形成一次時鐘脈沖
_nop_();
_nop_();
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1;//拉低CS端
ADCLK=0;//拉低CLK端
ADDO=1;//拉高數據端,回到初始狀态
dat<<=8;
dat|=ndat;
return(dat); //return ad k
}
/*************************************************
點陣字符采集函數
**************************************************/
void tra(uchar *p) //數組dsp[]裡面的字符查詢後,再儲儲存到另一個顯示數組value[]
{ for(i=0;i<5;i )
{
if(i<4) //i=2時查的是句号,其他3個是電壓值
{
for(j=0;j<16;j )
{
value[(i 1)*16 j]=table[(*p-0x2e)*16 j];
}
}
else
{ for(j=0;j<16;j ) //電壓符号V
value[(i 1)*16 j]=table[12*16 j];
}
p ;
}
for(i=0;i<16;i ) //前16個和後21個消隐作用,不顯示
value[i]=0;
for(i=128;i<149;i )
value[i]=0;
}
void main()
{
unsigned int b=0,i,j;
unsigned char a;
while(1)
{
k=Adc0832(0); //AD轉換結果
l=k/255.0*5.0;
sprintf(dsw,"%2.3f",l); //電壓值轉換成字符方式儲存,不明白的人上百度查一下sprintf函數的運用就清楚的,俺也是查的
tra(dsw);
j=0;
if(a>2) //移動間隔時間;越大移動越慢
{
a=0;
b =2;
if(b>=85) //顯示到最後一個字,回頭顯示
{
b=0;
}
}
sw=1; //顯示字符,順序是從左掃到右,有16列,數據電平是從左1到左16,由低到高
for(i=0;i<16;i ) //sw是154選通開關
{
P1=i;
P0=value[j b];
P2=value[j b 1];
delay(5);
P0=0x00;
P2=0x00;
j =2;
}
sw=0;
for(i=0;i<16;i )
{
P1=i;
P0=value[j b];
P2=value[j b 1];
delay(5);
P0=0x00;
P2=0x00;
j =2;
}
a ;
}
}
#ifndef _dis_H
#define _dis_H
unsigned char code table[]={ //字符查表
0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*".",0*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,/*"0",1*/
0x00,0x00,0x10,0x20,0x10,0x20,0xF8,0x3F,0x00,0x20,0x00,0x20,0x00,0x00,0x00,0x00,/*"1",2*/
0x00,0x00,0x70,0x30,0x08,0x28,0x08,0x24,0x08,0x22,0x88,0x21,0x70,0x30,0x00,0x00,/*"2",3*/
0x00,0x00,0x30,0x18,0x08,0x20,0x88,0x20,0x88,0x20,0x48,0x11,0x30,0x0E,0x00,0x00,/*"3",4*/
0x00,0x00,0x00,0x07,0xC0,0x04,0x20,0x24,0x10,0x24,0xF8,0x3F,0x00,0x24,0x00,0x00,/*"4",5*/
0x00,0x00,0xF8,0x19,0x08,0x21,0x88,0x20,0x88,0x20,0x08,0x11,0x08,0x0E,0x00,0x00,/*"5",6*/
0x00,0x00,0xE0,0x0F,0x10,0x11,0x88,0x20,0x88,0x20,0x18,0x11,0x00,0x0E,0x00,0x00,/*"6",7*/
0x00,0x00,0x38,0x00,0x08,0x00,0x08,0x3F,0xC8,0x00,0x38,0x00,0x08,0x00,0x00,0x00,/*"7",8*/
0x00,0x00,0x70,0x1C,0x88,0x22,0x08,0x21,0x08,0x21,0x88,0x22,0x70,0x1C,0x00,0x00,/*"8",9*/
0x00,0x00,0xE0,0x00,0x10,0x31,0x08,0x22,0x08,0x22,0x10,0x11,0xE0,0x0F,0x00,0x00,/*"9",10*/
0x08,0x00,0x78,0x00,0x88,0x07,0x00,0x38,0x00,0x0E,0xC8,0x01,0x38,0x00,0x08,0x00,/*"V",11*/
};
#endif
,今天小編就來聊一聊關于基于單片機的三位半數字電壓表?接下來我們就一起去研究一下吧!
#include <intrins.h>
#include <reg52.h>
#include <stdio.h>
#include "dis.h"
#define ulong unsigned long
#define uint unsigned int
#define uchar unsigned char
uchar k,i,j;
float l;
sbit sw=P3^0; //159選通開關
sbit ADCS =P3^1; //ADC0832 chip seclect
sbit ADDI =P3^3; //ADC0832 k in
sbit ADDO =P3^3; //ADC0832 k out
sbit ADCLK =P3^2; //ADC0832 clock signal
uchar dsw[20]; //存放sprintf轉換字符
uchar idata value[149]; //點陣字符儲存
void delay(int c) //延時
{
int i,j;
for(i=0;i<c;i )
for(j=0;j<10;j )
;
}
//采集并返回
unsigned int Adc0832(uchar channel) //AD轉換,返回結果
{
uchar i=0;
uchar j;
uint dat=0;
uchar ndat=0;
if(channel==0)channel=2;
if(channel==1)channel=3;
ADDI=1;
_nop_();
_nop_();
ADCS=0;//拉低CS端
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿1
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
ADDI=channel&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿2
_nop_();
_nop_();
ADCLK=1;//拉高CLK端
ADDI=(channel>>1)&0x1;
_nop_();
_nop_();
ADCLK=0;//拉低CLK端,形成下降沿3
ADDI=1;//控制命令結束
_nop_();
_nop_();
dat=0;
for(i=0;i<8;i )
{
dat|=ADDO;//收數據
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//形成一次時鐘脈沖
_nop_();
_nop_();
dat<<=1;
if(i==7)dat|=ADDO;
}
for(i=0;i<8;i )
{
j=0;
j=j|ADDO;//收數據
ADCLK=1;
_nop_();
_nop_();
ADCLK=0;//形成一次時鐘脈沖
_nop_();
_nop_();
j=j<<7;
ndat=ndat|j;
if(i<7)ndat>>=1;
}
ADCS=1;//拉低CS端
ADCLK=0;//拉低CLK端
ADDO=1;//拉高數據端,回到初始狀态
dat<<=8;
dat|=ndat;
return(dat); //return ad k
}
/*************************************************
點陣字符采集函數
**************************************************/
void tra(uchar *p) //數組dsp[]裡面的字符查詢後,再儲儲存到另一個顯示數組value[]
{ for(i=0;i<5;i )
{
if(i<4) //i=2時查的是句号,其他3個是電壓值
{
for(j=0;j<16;j )
{
value[(i 1)*16 j]=table[(*p-0x2e)*16 j];
}
}
else
{ for(j=0;j<16;j ) //電壓符号V
value[(i 1)*16 j]=table[12*16 j];
}
p ;
}
for(i=0;i<16;i ) //前16個和後21個消隐作用,不顯示
value[i]=0;
for(i=128;i<149;i )
value[i]=0;
}
void main()
{
unsigned int b=0,i,j;
unsigned char a;
while(1)
{
k=Adc0832(0); //AD轉換結果
l=k/255.0*5.0;
sprintf(dsw,"%2.3f",l); //電壓值轉換成字符方式儲存,不明白的人上百度查一下sprintf函數的運用就清楚的,俺也是查的
tra(dsw);
j=0;
if(a>2) //移動間隔時間;越大移動越慢
{
a=0;
b =2;
if(b>=85) //顯示到最後一個字,回頭顯示
{
b=0;
}
}
sw=1; //顯示字符,順序是從左掃到右,有16列,數據電平是從左1到左16,由低到高
for(i=0;i<16;i ) //sw是154選通開關。
{
P1=i;
P0=value[j b];
P2=value[j b 1];
delay(5);
P0=0x00;
P2=0x00;
j =2;
}
sw=0;
for(i=0;i<16;i )
{
P1=i;
P0=value[j b];
P2=value[j b 1];
delay(5);
P0=0x00;
P2=0x00;
j =2;
}
a ;
}
}
#ifndef _dis_H
#define _dis_H
unsigned char code table[]={ //字符查表
0x00,0x00,0x00,0x30,0x00,0x30,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,/*".",0*/
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0xE0,0x0F,0x10,0x10,0x08,0x20,0x08,0x20,0x10,0x10,0xE0,0x0F,0x00,0x00,/*"0",1*/
0x00,0x00,0x10,0x20,0x10,0x20,0xF8,0x3F,0x00,0x20,0x00,0x20,0x00,0x00,0x00,0x00,/*"1",2*/
0x00,0x00,0x70,0x30,0x08,0x28,0x08,0x24,0x08,0x22,0x88,0x21,0x70,0x30,0x00,0x00,/*"2",3*/
0x00,0x00,0x30,0x18,0x08,0x20,0x88,0x20,0x88,0x20,0x48,0x11,0x30,0x0E,0x00,0x00,/*"3",4*/
0x00,0x00,0x00,0x07,0xC0,0x04,0x20,0x24,0x10,0x24,0xF8,0x3F,0x00,0x24,0x00,0x00,/*"4",5*/
0x00,0x00,0xF8,0x19,0x08,0x21,0x88,0x20,0x88,0x20,0x08,0x11,0x08,0x0E,0x00,0x00,/*"5",6*/
0x00,0x00,0xE0,0x0F,0x10,0x11,0x88,0x20,0x88,0x20,0x18,0x11,0x00,0x0E,0x00,0x00,/*"6",7*/
0x00,0x00,0x38,0x00,0x08,0x00,0x08,0x3F,0xC8,0x00,0x38,0x00,0x08,0x00,0x00,0x00,/*"7",8*/
0x00,0x00,0x70,0x1C,0x88,0x22,0x08,0x21,0x08,0x21,0x88,0x22,0x70,0x1C,0x00,0x00,/*"8",9*/
0x00,0x00,0xE0,0x00,0x10,0x31,0x08,0x22,0x08,0x22,0x10,0x11,0xE0,0x0F,0x00,0x00,/*"9",10*/
0x08,0x00,0x78,0x00,0x88,0x07,0x00,0x38,0x00,0x0E,0xC8,0x01,0x38,0x00,0x08,0x00,/*"V",11*/
};
#endif
視頻講解:第204期
,更多精彩资讯请关注tft每日頭條,我们将持续为您更新最新资讯!