** **功能说明:show1:数码管显示
show2:光柱显示
show3:报警上限值设定
show4:报警下限值设定
****************************************************************/
#include<reg52.h>
#include<stdio.h>
sbit D1=P1^0;//3202模式控制端
sbit D0=P1^1;//3202数据输出端
sbit CLK=P1^2;//3202时钟控制端
sbit X5045=P1^3;//5045片选端(低电平有效)
sbit CS=P1^4;//3202片选端(低电平有效)
sbit key=P1^5;//按键检测端
sbit A0=P1^6;//4052通道选择端
sbit A1=P1^7;//4052通道选择端
sbit clk1=P2^4;//光柱阴极锁存端
sbit LATCH1=P2^5;//数码管段锁存
sbit clk3=P2^6;
sbit CLK4=P2^7;//单片机动作信号指示灯
/********************报警声明*************************/
sbit AL_L=P3^6;//报警下限控制端
sbit AL_H=P3^7;//报警上限控制端
#define DataPort P0//定义数据输出端口
#define saomiao P2//定义按键端口
#define uchar unsigned char
#define uint unsigned int
unsigned int code number[]={
2,3, 5, 10,
20, 40, 80, 160,
320, 625, 1250, 2500,5000};//将12位数据转换成千位数
unsigned char code dofly_DuanMa[]={0xC0, 0xF9, 0xA4, 0xB0,
0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83,
0xC6, 0xA1, 0x86, 0x8E,
0x89,0xBF};// 显示段码值0~f和,h和-
unsigned char code dofly_WeiMa[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87};//分别对应相应的数码管点亮,即位码
unsigned char code gzdata[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c};//对应点亮相应光柱
unsigned char code gzdataj[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//光柱精确显示
unsigned int limit_l[4];//储存报警值下限
unsigned int limit_h[4];//储存报警值上限
unsigned char TempData[8]; //存储显示值的全局变量
uchar gz[3];//储存光柱显示值变量
uint dispnum,qiehuan,channel,limit,xkey,dat,top,topj;
uchar cos;
uchar limith,limitl;//报警延时去抖
void show1();
void show2();
void show3();
void show4();
uint MCP();
void shujuzhuanhuan(uint dat);
void fenxi(uint c0);
void fenxi1(uint c0);
void Display();
void delay(unsigned char t);
uchar getkey();
void jiexikey(uchar xkey);
void channel_sel(uchar channel);
void jia();
void jia1();
void jian();
void jian1();
void alarming();
uchar top1();
uchar jingque();
void dispgz();
/******************************************************************
** 延时函数,延时多少毫秒
*******************************************************************/
void delayms(unsigned char t)
{
while(t--)
{
//大致延时1mS
delay(245);
delay(245);
}
}
//微延时
void delay(unsigned char t)
{
while(--t);
}
/***********************初始化函数*********************/
void init(void)
{
//初始化配置
AL_L=1;
AL_H=1;
channel = 0;
dispnum = 0;
limit_h[4]=(5000,5000,5000,5000);
limit_l[4]=(200,200,200,200);
qiehuan=0;;
channel_sel(channel);
}
/***************************************************/
void main()
{ init();
while(1)
{
xkey=getkey();
jiexikey(xkey);
if(qiehuan==0)
show1();
else if(qiehuan==1)
show2();
else if(qiehuan==2)
show3();
else if(qiehuan==3)
show4();
}
}
/*********************数码管显示*************/
void show1()
{ dispnum=0;
channel_sel(channel);
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
dat=MCP();
shujuzhuanhuan(dat);
alarming();
fenxi1(dispnum);
Display();
}
/**********************光柱显示**************/
void show2()
{ dispnum=0;
channel_sel(channel);
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
dat=MCP();
shujuzhuanhuan(dat);
alarming();
fenxi(dispnum);
top=top1();
topj=jingque();
dispgz();
}
/****************上限值设定*******************/
void show3()
{
xkey=getkey();
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
jiexikey(xkey);
fenxi1(limit_h[channel]);
Display();
}
/**********************下限值设定**************/
void show4()
{xkey=getkey();
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
jiexikey(xkey);
fenxi1(limit_l[channel]);
Display();
}
/**************************************************
MCP3202数模转换程序********/
uint MCP()
{
uint dat=0,i;
CS=1; //片选关闭
CLK=0; //clk拉低
CS=0; //片选打开
delay(1);
D1=1; //启动转换
delay(1);
CLK=1; //第一个脉冲上升沿
delay(1);
D1=0;
delay(1);
CLK=0; //clk拉低
D1=1; //配置1
delay(1);
CLK=1; //第二个脉冲 上升沿
delay(1);
D1=0;
CLK=0; //clk拉低
D1=0; //配置0 :配置1和配置0共同决定选择CH0通道
delay(1);
CLK=1; //第三个脉冲上升沿
delay(1);
CLK=0;
delay(1);
CLK=1; //第四个脉冲上升沿
delay(1);
CLK=0;
D1=1; /////////////////////
delay(1);
CLK=1; //第五个脉冲上升沿
delay(1);
for(i=0;i<12;i++)//读出12位数据
{
CLK=0;
delay(1);
dat=dat<<1|D0;
delay(1);
CLK=1;
delay(1);
}
return dat;
}
/*************************************
12位MCP3202数据转换为十进制数字*******/
void shujuzhuanhuan(uint dat)
{
uchar i,temp;
for(i=0;i<13;i++)
{
temp = (dat>>i) & 0x01;
if(temp == 1)
dispnum += number[i];
}
}
/*********************************
数据显示转换程序****************/
void fenxi(uint c0)
{uint c1,c2,c3,c4,c5,c6;
c1=c0/1000;
c2=c0-c1*1000;
c3=c2/100;
c4=c2-c3*100;
c5=c4/10;
c6=c4-c5*10;
gz[0]=c1;
gz[1]=c3;
gz[2]=c5;
}
/*****************************上四位数据显示下四位显示通道
****************/
void fenxi1(uint c0)
{uint c1,c2,c3,c4,c5,c6;
c1=c0/1000;
c2=c0-c1*1000;
c3=c2/100;
c4=c2-c3*100;
c5=c4/10;
c6=c4-c5*10;
TempData[0]=dofly_DuanMa[c1]&0x7f;
TempData[1]=dofly_DuanMa[c3];
TempData[2]=dofly_DuanMa[c5];
TempData[3]=dofly_DuanMa[c6];
TempData[4]=dofly_DuanMa[12];
TempData[5]=dofly_DuanMa[16];
TempData[6]=dofly_DuanMa[17];
TempData[7]=dofly_DuanMa[channel];
}
show2:光柱显示
show3:报警上限值设定
show4:报警下限值设定
****************************************************************/
#include<reg52.h>
#include<stdio.h>
sbit D1=P1^0;//3202模式控制端
sbit D0=P1^1;//3202数据输出端
sbit CLK=P1^2;//3202时钟控制端
sbit X5045=P1^3;//5045片选端(低电平有效)
sbit CS=P1^4;//3202片选端(低电平有效)
sbit key=P1^5;//按键检测端
sbit A0=P1^6;//4052通道选择端
sbit A1=P1^7;//4052通道选择端
sbit clk1=P2^4;//光柱阴极锁存端
sbit LATCH1=P2^5;//数码管段锁存
sbit clk3=P2^6;
sbit CLK4=P2^7;//单片机动作信号指示灯
/********************报警声明*************************/
sbit AL_L=P3^6;//报警下限控制端
sbit AL_H=P3^7;//报警上限控制端
#define DataPort P0//定义数据输出端口
#define saomiao P2//定义按键端口
#define uchar unsigned char
#define uint unsigned int
unsigned int code number[]={
2,3, 5, 10,
20, 40, 80, 160,
320, 625, 1250, 2500,5000};//将12位数据转换成千位数
unsigned char code dofly_DuanMa[]={0xC0, 0xF9, 0xA4, 0xB0,
0x99, 0x92, 0x82, 0xF8,
0x80, 0x90, 0x88, 0x83,
0xC6, 0xA1, 0x86, 0x8E,
0x89,0xBF};// 显示段码值0~f和,h和-
unsigned char code dofly_WeiMa[]={0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87};//分别对应相应的数码管点亮,即位码
unsigned char code gzdata[]={0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0a,0x0b,0x0c};//对应点亮相应光柱
unsigned char code gzdataj[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};//光柱精确显示
unsigned int limit_l[4];//储存报警值下限
unsigned int limit_h[4];//储存报警值上限
unsigned char TempData[8]; //存储显示值的全局变量
uchar gz[3];//储存光柱显示值变量
uint dispnum,qiehuan,channel,limit,xkey,dat,top,topj;
uchar cos;
uchar limith,limitl;//报警延时去抖
void show1();
void show2();
void show3();
void show4();
uint MCP();
void shujuzhuanhuan(uint dat);
void fenxi(uint c0);
void fenxi1(uint c0);
void Display();
void delay(unsigned char t);
uchar getkey();
void jiexikey(uchar xkey);
void channel_sel(uchar channel);
void jia();
void jia1();
void jian();
void jian1();
void alarming();
uchar top1();
uchar jingque();
void dispgz();
/******************************************************************
** 延时函数,延时多少毫秒
*******************************************************************/
void delayms(unsigned char t)
{
while(t--)
{
//大致延时1mS
delay(245);
delay(245);
}
}
//微延时
void delay(unsigned char t)
{
while(--t);
}
/***********************初始化函数*********************/
void init(void)
{
//初始化配置
AL_L=1;
AL_H=1;
channel = 0;
dispnum = 0;
limit_h[4]=(5000,5000,5000,5000);
limit_l[4]=(200,200,200,200);
qiehuan=0;;
channel_sel(channel);
}
/***************************************************/
void main()
{ init();
while(1)
{
xkey=getkey();
jiexikey(xkey);
if(qiehuan==0)
show1();
else if(qiehuan==1)
show2();
else if(qiehuan==2)
show3();
else if(qiehuan==3)
show4();
}
}
/*********************数码管显示*************/
void show1()
{ dispnum=0;
channel_sel(channel);
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
dat=MCP();
shujuzhuanhuan(dat);
alarming();
fenxi1(dispnum);
Display();
}
/**********************光柱显示**************/
void show2()
{ dispnum=0;
channel_sel(channel);
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
dat=MCP();
shujuzhuanhuan(dat);
alarming();
fenxi(dispnum);
top=top1();
topj=jingque();
dispgz();
}
/****************上限值设定*******************/
void show3()
{
xkey=getkey();
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
jiexikey(xkey);
fenxi1(limit_h[channel]);
Display();
}
/**********************下限值设定**************/
void show4()
{xkey=getkey();
if(channel==(-1))
channel=3;
if(channel==4)
channel=0;
jiexikey(xkey);
fenxi1(limit_l[channel]);
Display();
}
/**************************************************
MCP3202数模转换程序********/
uint MCP()
{
uint dat=0,i;
CS=1; //片选关闭
CLK=0; //clk拉低
CS=0; //片选打开
delay(1);
D1=1; //启动转换
delay(1);
CLK=1; //第一个脉冲上升沿
delay(1);
D1=0;
delay(1);
CLK=0; //clk拉低
D1=1; //配置1
delay(1);
CLK=1; //第二个脉冲 上升沿
delay(1);
D1=0;
CLK=0; //clk拉低
D1=0; //配置0 :配置1和配置0共同决定选择CH0通道
delay(1);
CLK=1; //第三个脉冲上升沿
delay(1);
CLK=0;
delay(1);
CLK=1; //第四个脉冲上升沿
delay(1);
CLK=0;
D1=1; /////////////////////
delay(1);
CLK=1; //第五个脉冲上升沿
delay(1);
for(i=0;i<12;i++)//读出12位数据
{
CLK=0;
delay(1);
dat=dat<<1|D0;
delay(1);
CLK=1;
delay(1);
}
return dat;
}
/*************************************
12位MCP3202数据转换为十进制数字*******/
void shujuzhuanhuan(uint dat)
{
uchar i,temp;
for(i=0;i<13;i++)
{
temp = (dat>>i) & 0x01;
if(temp == 1)
dispnum += number[i];
}
}
/*********************************
数据显示转换程序****************/
void fenxi(uint c0)
{uint c1,c2,c3,c4,c5,c6;
c1=c0/1000;
c2=c0-c1*1000;
c3=c2/100;
c4=c2-c3*100;
c5=c4/10;
c6=c4-c5*10;
gz[0]=c1;
gz[1]=c3;
gz[2]=c5;
}
/*****************************上四位数据显示下四位显示通道
****************/
void fenxi1(uint c0)
{uint c1,c2,c3,c4,c5,c6;
c1=c0/1000;
c2=c0-c1*1000;
c3=c2/100;
c4=c2-c3*100;
c5=c4/10;
c6=c4-c5*10;
TempData[0]=dofly_DuanMa[c1]&0x7f;
TempData[1]=dofly_DuanMa[c3];
TempData[2]=dofly_DuanMa[c5];
TempData[3]=dofly_DuanMa[c6];
TempData[4]=dofly_DuanMa[12];
TempData[5]=dofly_DuanMa[16];
TempData[6]=dofly_DuanMa[17];
TempData[7]=dofly_DuanMa[channel];
}