按键调节18b20(数码管显示)最高温度控制继电器的程序

按键调节18b20(数码管显示)最高温度控制继电器的程序,第1张

是使用按钮选择最高温读,温度达到时继电器闭合吗?我曾在12年做过一个,两位数码显示的温控器,附上原理图与程序,供参考、修改。

/13-1-1 在10-6基础上修改成30-35度的温控器。

//使用节拍器线路板,LED显示温度,

//30度继电器吸和,35度继电器释放。

#include<pic.h>

__CONFIG(0x3954)

#define uchar unsigned char

#define uint  unsigned int

#define DQ RB5    //18B20读写口。

#define DQ_HIGH() TRISB5=1  //读写口设定读

#define DQ_LOW() TRISB5=0DQ=0 //设定写。

#define clk RC7    //74HC164时钟口。

#define ds  RC5    //74HC164数据口。

#define BZ RC2    //蜂鸣器输出口。

const unsigned char LED[12]=

{

0X40,  //0

0X79,  //1

0X24,  //2

0X30,  //3

0X19,  //4

0X12,  //5

0X02,  //6

0X78,  //7

0X00,  //8

0X10,  //9

0X46,   //C  10

0X7F,  //不显示 11

}

uint temper

uchar tem1     //温度读数寄存器。

uchar tem2

uchar b1,b2     //显示2位

void delayus1(uint)   //微秒延时子函数

void delay(uint x)   //延时子函数

void delayus(uint,uchar)  //微秒延时子函数

void init()     //初始化子函数

void dispw(uchar wnum1,uchar wnum2,uchar wnum3,uchar wnum4) //温度显示子函数。

void write_byte(uchar date) //写2068数据子函数。

uchar read_byte()   //读2068数据。

void get_tem()    //温度计子函数。

uchar M1     //上电鸣叫次数。

uchar M3     //调试闪动长短。

uchar M4

uchar Lsb

uchar Msb

uchar temp1     //温度计算寄存器

uchar temp2     //同上。

//-------为调分设定------

uchar q0  //秒暂存

uchar q1  //分暂存

uchar q2  //时暂存

uchar q3  //日暂存

uchar q4  //周暂存

uchar q5  //月暂存

uchar q6  //年暂存

uchar t1  //调整中时十位

uchar t2  //调整中时个位

uchar t3  //调整中分十位

uchar t4  //调整中分个位

bit ff_ui  //调分、调时标志,0调分,1调时。

bit UI_UJ  //时闪动标志

bit FF_UJ  //分闪动标志

bit bcvi  //中断标志

void tcui()  //调时子函数

void tcff()  //调分子函数

#define TU RA0 //调时定义

#define JW RA1 //加数定义

#define JIAN RA2 //减数定义

void didi()  //滴声

//----------------------

void reset()      //18B20起始子函数

{

 uchar st=1

 DQ_HIGH()     //数据线拉高

 NOP()NOP()     //延时

 while(st)     //st=1时循环

 {

  GIE=0

  DQ_LOW()    //拉低

  delayus(170,100)   //750uS

  DQ_HIGH()    //释放

  delayus1(13)    //延时47uS

  if(DQ==1)    //数据线上高?

   st=1

  else

   st=0    //数据线低st=0,跳出循环

  delayus(130,40)   //500US

  GIE=1 

 }

}

void write_byte(uchar date)  //18B20写一字节。

{

 GIE=0      //关中断。

 uchar i,temp

 DQ_HIGH()

 NOP()NOP()

 for(i=8i>0i--)    //8位设定

 {

  temp=date&0x01   //1“与”最低位。

  DQ_LOW()

  delayus1(3)    //15uS

  if(temp==1)    //如是1

  DQ_HIGH()    //置高

  delayus1(8)    //延时32uS

  DQ_HIGH()

  date=date>>1    //右移一位。

 }

 GIE=1      //开中断。

uchar read_byte()     //读一个字节。

{

 GIE=0      //关中断。

 uchar i,date

 static bit j

 for(i=8i>0i--)    //8位循环

 {

  date=date>>1    //右移

  DQ_LOW()

  delayus1(0)    //6uS

  DQ_HIGH()    //拉高

  delayus1(0)    //6uS

  j=DQ     //读

  if(j==1)     //如读1

  date=date|0x80   //1"或"最高位

  delayus1(8)    //32uS

 }

 return (date)    //数据存入read_byte()

 GIE=1      //开中断。

}

void get_tem()

{

 

 uchar tem1,tem2,num

 float aaa     //32位浮点数

 reset()       //复位

 write_byte(0xCC)    //跳过RAM

 write_byte(0x44)    //温度转换

 dispw(b1,b2,b3,b4)   //插入显示等待转换

 reset()      //再次复位

 write_byte(0xCC)    //跳过RAM

 write_byte(0xBE)    //读取数据

 Lsb=read_byte()    // 低字节

 Msb=read_byte()    // 高字节

 temp2=Lsb&0x0f    //LSB的低4位为小数部分

 temp1=(Lsb>>4)|(Msb<<4)  //LSB的高4位和MSB拼成整数部分

 b1=temp1/10 

 b2=temp1%10

 if(temp2>15) temp2=0   //小数部分超出15,归0.

        //

}

void delayus(uint x,uchar y)

{

 uint i      //微秒级延时

 uchar j

 for(i=xi>0i--)

 for(j=yj>0j--)

}

void delay(uint x)    //毫秒级延时

{

 uint a,b

 for(a=xa>0a--)

 for(b=305b>0b--)

}

//----------初始化--------

void init()

{

 TRISC=0X02     //RC1输入

 TRISB=0X21     //RB4-5 RB0 输入

 TRISA=0X07     //A0-2按钮

 PORTA=0XF0

 PORTC=0X40

 OSCCON=0X34     //16MHz内部RC.

 ANSELB=0X00     //A,B口数字。

 ANSELA=0X00 

 T1CON=0X31     //8预分频

 TMR1IF=0     //清除TMR1中断标志

 TMR1IE=1     //TMR1中断允许 

 TMR1H=0xEC     //赋初值,以便TMR1每10MS中断一次

 TMR1L=0x7A

 PIR1=0      //TRM1能使。

 PEIE=1      //开中断。

 GIE=1

}

void dispw(uchar wnum1,uchar wnum2,uchar wnum3,uchar wnum4)

{

 RC6=1      //温度显示子函数

 

 PORTB=0X1E     //关闭全部共阳

 SPIW164(table[wnum1])   //串行送入第一个数码管

 PORTB=0x6f     //开放第一位共阳ef

 delay(3)     //延时2毫秒。

 PORTB=0X1E     //关闭全部共阳

 SPIW164(tabled[wnum2])  //串行送入第二个数码管

 PORTB=0x77     //开放第二位共阳f7

 delay(3)     //延时2毫秒。

 PORTB=0X1E     //关闭全部共阳

 SPIW164(table[wnum3])   //串行送入第三个数码管

 PORTB=0x7b     //开放第三位共阳fb

 delay(3)     //延时2毫秒。

 PORTB=0X1E     //关闭全部共阳

 SPIW164(tabled[wnum4])  //显示第四个数码管C

 PORTB=0x7d     //开放第四位共阳fd

 delay(3)     //延时2毫秒。

 PORTB=0X1E

}

void SPIW164(uchar demand)

{

 uchar j=8     //8位数据 

 while(j--)

 {

  clk=0

  NOP()  

  if((demand&0x80)==0) ds=0

  else ds=1

  demand<<=1

  NOP()

  clk=1

  NOP()  

 }

 clk=0

}

void delayus1(uint x)

{

 uint i

 for(i=xi>0i--)

}

//---中断子函数-------

 void interrupt  T1(void)

{

 if(TMR1IF==1)     //10mS

 {

  M2++

  M3++

  bcvi=1

    TMR1H=0XEC    //输入初值。

  TMR1L=0x7A  

  TMR1IF=0    //清标志。

  if(++ycon>49)     //计数50次后,为0.5秒

  {

   ycon=0

   MC=!MC          //将MC取反

  }            

 }

}

void disp(uchar num1,uchar num2,uchar num3,uchar num4)

{

 RC6=1      //时钟显示子函数。

 

 PORTB=0X1E     //关闭全部共阳

 SPI164(table[num1])   //串行送入第一个数码管

 PORTB=0x6f     //开放第一位共阳ef

 delay(3)     //延时3毫秒。

 PORTB=0X1E     //关闭全部共阳

 if(UI_UJ==1) SPI164(0x00)      

 else SPI164(table[num2])  //串行送入第二个数码管

 PORTB=0x77     //开放第二位共阳f7

 delay(3)     //延时3毫秒。

 PORTB=0X1E     //关闭全部共阳

 SPI164(table[num3])   //串行送入第三个数码管

 PORTB=0x7b     //开放第三位共阳fb

 delay(3)     //延时3毫秒。

 PORTB=0X1E     //关闭全部共阳

 if(FF_UJ==1) SPI164(0x00) 

 else SPI164(table[num4])  //显示第四个数码管C

 PORTB=0x7d     //开放第四位共阳fd

 delay(3)     //延时3毫秒。

 PORTB=0X1E

}

void SPI164(uchar demand)

{

 uchar j=8     //8位数据 

 while(j--)

 {

  clk=0

  NOP()  

  if((demand&0x80)==0) ds=0

  else ds=1

  demand<<=1

  NOP()

  clk=1

  NOP()  

 }

 clk=0

}

//---------主程序-------

void main()

{  

 init()

 M1=3      //上电3次鸣叫。

 while(M1--)

 {

  BZ=1

  MC=1

  delay(150)    //间隔150毫秒。 

  BZ=0

  MC=0

  delay(150)

 }     

 if(RC1==1)     //如果RC1接高电平,直接读数据。

 goto  XPHR    //已设置跳至循环。

XPHR:       //循环。

 while(1)

 {  

  get_tem()   //读取温度。 

 } 

}

//--------------------------------------------

//----------

void didi()     //鸣叫子函数。

{

 BZ=1

 delay(150)

 BZ=0

}

温度扰扒传感器(temperature transducer)是指能感受温度并转换成可用输出信号的传感器。温度传感器是温度测量仪表的核心部分,品种繁缓裂昌多。按测量方式可分为接触式和非接触式两大类,按照传感器源亏材料及电子元件特性分为热电阻和热电偶两类。

/***********************************************************************

PID温度控制程序

程序说明:

系统上电后显示 “--温度”

表示需要先设定温度才开始进行温度检测

温度设定完毕后程序才开始进行PID温控

***********************************************************************/

#include <reg52.h>

#include <absacc.h>

#include"DS18B20.H"

#include"PID.H"

#define uchar unsigned char

#define uint unsigned int

unsigned char code tab[]=

{

0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xBF

}

/*个位0~9的数码管段码*/

unsigned char code sao[]=

{

0x7f,0xbf,0xdf,0xef

}

//扫描码

uchar set=30,keyflag=1 //set初谈腊段始化为30° keyflag为进入温度设定的标志位

//4个按键使用说明

sbit key_out=P1^0 //用于温度设定后的退出

sbit key_up=P1^1 //设定温度含誉加

sbit key_down=P1^2 //设定温度减

sbit key_in=P1^3 //在程序的运行中如需要重新设定温度 按下此键才能进入设置模式并且此时是停在温度控制的,按下key_out键后才表示设定完毕

void Show_key()

/***********************************************************/

void delays(unsigned char k)

{

unsigned char i,j

for(i=0i<ki++)

for(j=0j<50j++)

}

/*********************************************************

//数码管显示函数

P0口 作为数据口

P2口的低四位作为扫描口

变量 x表示扫描

d表示是否要加小数点 为1是 为0不加

y表示传递的数值

*********************************************************/

LCD_disp_char(uchar x,bit d,uchar y)

{

P2=0XFF

P0=0xFF

if(d==0)

P0=tab[y]

else

P0=tab[y]&0x7f //与上0x7f表示是否要加小数点

P2=sao[x]//打开扫描端号

}

/*********************************************************

按键扫描

*********************************************************/

void keyscan(void)

{

if(key_in==0) //按键进入函数

{

delays(10) //延时消抖 (以下同)

if(key_in==0)

{

while(key_in==0)

{

Show_key()//如果局缺一直按着键不放 就一直显示在当前状态 (以下同)

}

keyflag=1 //按键标志位

}

}

/***********************/

if(key_out==0) //按键退出

{

delays(10)

if(key_out==0)

{

while(key_out==0)

{

Show_key()

}

keyflag=0

set_temper=set

}

}

/*************************/

if(key_up==0) //设定温度的加

{

delays(10)

if(key_up==0)

{

while(key_up==0)

{

Show_key()

}

if(keyflag==1)

{

set++

if(set>90) //如果大于90°就不在加

set=90

}

}

}

/*************************/

if(key_down==0) //温度设定的减

{

delays(10)

if(key_down==0)

{

while(key_down==0)

{

Show_key()

}

if(keyflag==1)

{

set--

if(set<30) //温度减到30°时不在往下减

set=30

}

}

}

}

/*********************************************************************

按键按下时的显示函数

***********************************************************************/

void Show_key()

{

output=1

LCD_disp_char(3,0,10)//显示 -

delays(3)

LCD_disp_char(2,0,10)//显示- (表示温度设定 )

delays(3)

LCD_disp_char(1,0,set/10)//显示温度十位

delays(3)

LCD_disp_char(0,0,set%10)//显示温度个位

delays(3)

}

/*****************************************************************/

void main()

{

unsigned int tmp //声明温度中间变量

unsigned char counter=0

PIDBEGIN()//PID参数的初始化

output=1 //关闭继电器输出

while(1)

{

keyscan()

if(keyflag)

{

Show_key()//显示温度设定

}

else

{

if(counter--==0)

{

tmp=ReadTemperature()//每隔一段时间读取温度值

counter=20

}

LCD_disp_char(3,0,tmp/1000) //显示温度十位

delays(3)

LCD_disp_char(2,1,tmp/100%10)//显示温度个位

//显示小数点

delays(3)

LCD_disp_char(1,0,tmp/10%10)//显示温度小数后一位

delays(3)

LCD_disp_char(0,0,tmp%10)//显示温度小数后二位

delays(3)

P2=0XFF

P0=0xff

compare_temper()//比较温度

}

}

}

/**********************************************************************************************************************************************/

//PID算法温控C语言2008-08-17 18:58

#ifndef _PID_H__

#define _PID_H__

#include<intrins.h>

#include<math.h>

#include<string.h>

struct PID

{

unsigned int SetPoint

// 设定目标 Desired Value

unsigned int Proportion

// 比例常数 Proportional Const

unsigned int Integral

// 积分常数 Integral Const

unsigned int Derivative

// 微分常数 Derivative Const

unsigned int LastError

// Error[-1]

unsigned int PrevError

// Error[-2]

unsigned int SumError

// Sums of Errors

}

struct PID spid

// PID Control Structure

unsigned int rout

// PID Response (Output)

unsigned int rin

// PID Feedback (Input)

sbit output=P1^4

unsigned char high_time,low_time,count=0

//占空比调节参数

unsigned char set_temper

void PIDInit(struct PID*pp)

{

memset(pp,0,sizeof(struct PID))//PID参数初始化全部设置为0

}

unsigned int PIDCalc(struct PID*pp,unsigned int NextPoint)

{

unsigned int dError,Error

Error=pp->SetPoint-NextPoint

// 偏差

pp->SumError+=Error

// 积分

dError=pp->LastError-pp->PrevError

// 当前微分

pp->PrevError=pp->LastError

pp->LastError=Error

//比例

//积分项

return(pp->Proportion*Error+pp->Integral*pp->SumError+pp->Derivative*dError)

// 微分项

}

/***********************************************************

温度比较处理子程序

***********************************************************/

void compare_temper()

{

unsigned char i

//EA=0

if(set_temper>temper)

{

if(set_temper-temper>1)

{

high_time=100 //大于1°不进行PID运算

low_time=0

}

else

{ //在1°范围内进行PID运算

for(i=0i<10i++)

{

//get_temper()

rin=s

// Read Input

rout=PIDCalc(&spid,rin)//执行PID运算

// Perform PID Interation

}

if(high_time<=100) //限制最大值

high_time=(unsigned char)(rout/800)

else

high_time=100

low_time=(100-high_time)

}

}

/****************************************/

else if(set_temper<=temper) //当实际温度大于设置温度时

{

if(temper-set_temper>0)//如果实际温度大于设定温度

{

high_time=0

low_time=100

}

else

{

for(i=0i<10i++)

{

//get_temper()

rin=s

// Read Input

rout=PIDCalc(&spid,rin)

// Perform PID Interation

}

if(high_time<100) //此变量是无符号字符型

high_time=(unsigned char)(rout/10000)

else

high_time=0 //限制不输出负值

low_time=(100-high_time)

//EA=1

}

}

}

/*****************************************************

T0中断服务子程序,用于控制电平的翻转 ,40us*100=4ms周期

******************************************************/

void serve_T0()interrupt 1 using 1

{

if(++count<=(high_time))

output=0

else if(count<=100)

{

output=1

}

else

count=0

TH0=0x2f

TL0=0xe0

}

void PIDBEGIN()

{

TMOD=0x01

TH0=0x2f

TL0=0x40

EA=1

ET0=1

TR0=1

high_time=50

low_time=50

PIDInit(&spid)

// Initialize Structure

spid.Proportion=10

// Set PID Coefficients

spid.Integral=8

spid.Derivative=6

spid.SetPoint=100

// Set PID Setpoint

}

#endif

转自他人程序。


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/yw/12352166.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2023-05-22
下一篇2023-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存