版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、include mclude mclude mclude math.h”typedef unsigned chai BYTE;typedef unsigned mt WORD;typedef bit BOOL;/*DS1620管脚腿定义*/#define DQ P2_4#define CLK P2_3#define RST P2_5/*LCD1602 define*/#define uchai unsigned char#define umt unsigned mt#define LCD_DB PO/*/sbit rs=P2A0;/LCD 1602 RS 定义sbit rw=P2人 1;/LC
2、D 1602 RW 定义sbit ep=P2A2;/LCD 1602 EP 定义sbit DQ=P2A4;sbit CLK=P2A3;sbit RST=P2A5;sbit p27=P2A7; /*test lamp*/sbit p26=P2,6;控制继电器开断/sbit RST = 0 xA2; /* DS1620 reset line */sbit CLK = 0 xA3; /* DS 1620 clock line */sbit DQ = 0 xA4; /* DS1620 data line */unsigned char disl =”lichunming”;/unsigned clw
3、show =(M0123456789ABCDEFH);unsigned char show!0=(H0123456789M);unsigned char shownum=(H000n);unsigned char temp =(ntemp:H;stmct PID (unsigned mt SetPomt; / 设定目标 Desired Valueunsigned mt Piopoition; / 比例常数 Proportional Constunsigned mt Integral; / 枳分常数 Integral Constunsigned mt Derivative; / 微分常数 Der
4、ivative Constunsigned mt LastEnor; / Enor-1unsigned mt PievEiror; / Error-2unsigned mt SuniError; / Sums of Errors;stmct PID spid; / PID Contiol Stinctureunsigned mt rout; / PID Response (Output)unsigned mt rin; / PID Feedback (Input)全局变量unsigned mt SetTemp ;/*/* Function prototypes */*函数声明*/*unsign
5、ed mt Getl620byte( void );unsigned mt Readl620byte( void );unsigned mt DS 1620StaitConv(void);unsigned mt DS 1620ReadCoiif(void);unsigned mt DS 16201ead(void);unsigned mt set_tempQ;void InitDS 1620(void);void Display_TempQ;void Put1620byte(unsigned char m);void Write 1620byte(unsigned char m);*/*/读取
6、DS 1620数值读取DS162。数值(10位的)/ZDS1620转换温度读取DS1620控制字读取DS1620温度值设置预期温度值初始化DS1620/显刁 温度值向DS1620写数值向DS1620写数值(10位的)unsigned mt DS 1620SetCoiif(unsigned chai val); 设置 DS 1620 控制字void DS 1620SetTemp(unsigned TH,unsigned TL);设置 TH、TLvoid PIDIiut (stmct PID *pp);*/*.-LCD 1602显示声明函数初始化函数/LCD 1602写指令函数/LCD 1602写
7、数据函数void LCD_init(void);void LCD_vrite_conmiaiid(uchai dat);void LCD_write_data(uchar dat);void LCD_disp_char(uchai x.uchar y,uchar dat);/LCD1602 显示字符函数延时10ms程序检查忙函数/位置显示函数/显示 tem:显示读取的数字灯p27闪烁程序void delay 10ms(void);BOOL LCD_check_busy(void);void LCD_pos (BYTE pos);void LCD_disp_temp();void LCD_dis
8、p_num(unsigned num);void p27Twin();void compaie_temper();,*/*延时程序/void delay (mt m) /m=20 约为 lOnisunsigned mt i,j;for (i=O;im;i+)for (j=0;j253;j+);void delay 1 (int m)unsigned mt i,j;fbr (i=0;ivm;i+)for(j=0jO;i)fbi(k=25O;kO;k-); TOC o 1-5 h z /*函数定义*/*Get temp from DS1620*/*/*从DS1620读字节函数*/unsigned
9、mt Getl620byte( void ) unsigned int j,k,b;k=0;/7k=valveb=l;位移变量RST=1;fbr(j=O;jv8;j+)DQ=1;CLK=0;if (DQ) k|=b; /* Read bit and or if = 1 */CLK=1;b=(bl); /* Setup for next read and or */delaylOnisQ;RST=0;return k;/*/* Put temp to DS1620 */*对DS1620写字节函数*/*void Put1620byte(unsigned int m)b=l;高电平写RST=1;fb
10、r (k=0; k8; k+)CLK=O;上升沿有效DQ = (m & b); /* Send bit to 1620 */CLK=1;b=l; /* Setup to send next bit */delay lOinsQ;RST=0;return;/*/* read temp fiom DS1620 */* DS1620读字节函数*/10O oooooooooooooooooooooooooooooooo/*unsigned mt Readl620byte( void )unsigned mt j,k,b; 临时变量k=0;b=l;移位变量赋初值RST=1;血(=0;jviO;j+)CL
11、K=0;/CLK 低if(DQ) k|=b;输出数据CLK=1;CLK 高b=(bl);移位 b=ldelaylOnisQ;RST=0;return k;/*/* write temp from DS1620 */* DS1620 函数*/写 10 4立。oooooooooooooooooooooooooooooooo/*void Write 1620byte(unsigned int m)RST=1;力RST高电平才能传输数据fbr (k=0; k10; k+) CLK=O; DQ = (m & b); /* Send bit to 1620 */ CLK=1; b=(bl); /* Set
12、up to send next bit */ delaylOnisQ; RST=0;return;/*/*转换温度函数*/* start conversion */*/unsigned mt DS 1620StaitConv(void)RST=1;Put1620byte(0 xEE);delay 10ms();RST=0;return 0 x00;/KST 高写命令字/KST 低返回值*unsigned mt temp; RST=0;RST=1;Piitl620byte(0 xAC); temp=Get!620byteQ; delay 10ms();RST=0;return temp;/*读取
13、DS1620控制字*/* Read Config Register of DS1620 */*unsigned mt DS 1620ReadCoiif(void)返回值临时变量/RST 高写命令控制字 OxAC为Read Config读取控制寄存器值/RST 低返回值*/*设置DS1620控制字*/* Setting Coilfig Register of DS 1620 */*RST=O;delay 10ms();RST=1;Putl620byte(0 x0C);写命令字delay 10ms();Put1620byte(val);写控制寄存器参数delay 10ms();RST=O;retu
14、rn 1;/*/*从DS 1620读取温度值*/* Read Temp from DS1620 */*/unsigned mt DS 16201ead(void)unsigned mt temp_and JialfLbit; /* temp byte and half bit */ unsigned mt sign_bit; /* sign bit 温度正负位 */ unsigned char k;double tenip_c; /* temperature in deg. C */double tenip.f; /* temperature in deg. F */double tenip_
15、read; /* */unsigned mt count_reniain; /* used for getting .1 deg.*/unsigned mt count_per_c; /* used fbr getting .1 deg.*/ int tempi;DS 1620StaitConvQ;fbr(tempi = 0; tempi 100; tempi+)delay 10ms();RST=1;fbr(k=0;k100;k+)delay 10ms();Putl620byte(0 xAA);写命令字temp_and_halfLbit=Get 162 Obyte。; 读取温度的第一个字节 s
16、ign_bit=Get 1620byteQ;读取温度的符号位,即正负位RST=0;/*Get count remain & count per C for . 1 resolution*/RST=1;Put1620byte(0 xA0);/read count reniaiiiingcount_remam=Get1620byte();/read 1st bytecount_iemam+=Get 1620byte()*256; /read 2nd byteRST=0;RST=1;Piitl620byte(0 x41);RST=0;RST=1;Put1620byte(0 xA0);count_pe
17、r_c=Get 1620byteQ;/load slope mto counter/*read slope as count/c 0 xA0=Read Counter */ /read 1st byte/read 2nd bytecount_pei_c+=Get 162Obyte()*256;RST=0;/*/* Calculate ?C and ?F */*iRc ount_pei_c=0)count_per_c=l;temp_read=(tenip_andJialCbit 1);if (sign_bit!=O)temp_read=(tenip_read-128);temp_c=(float
18、)temp_read-0.25+(counCper_c-count_remam)/(float) count_pei_c;/temp_c为真实摄氏温度值,上式为芯片手册给的转换公式temp_f=temp_c *9/5+32;转换为华氏度return temp_c;/*/* 初始化 DS1620 */* Initial DS1620 */*/*/*/*/void InitDS1620(void) DS1620SetConf(0 x02);设置初始化值位 0 x00/DS1620SetTemp(0 x50,0 xl4);设置初始温度为 TH=+40C, TL=+10C TOC o 1-5 h z
19、/*/*设置 TH,TL*/*/void DS 1620SetTemp(TH JL) RST=1; Putl620byte(0 x01); delay lOmsQ; Write 1620byte(TH): delay lOmsQ; RST=0; delay(20);RST=1;Putl620byte(0 x02);delay lOmsQ;Write 1620byte(TL);delay lOmsQ;RST=O;return;/*/*显示温度函数*/* Display_Temp */*/void Display_Temp ()unsigned mt Read_Temp:/unsigned mt
20、a.bjj;/unsigned show_data;Read_Temp=OxOO:DS1620StartConvQ;转换温度delay(5O);Read_Temp=Ox 12;LCD_disp_num(Read_Temp);p27TwinQ;/*if(Read_Temp=O)(J=o;wliile(disl。!=0)LCD_pos(0 x4(Hj);LCD-write_data(dis 1 j);J+;)else1=0;while(tempi!-0*)显示字符 “temp:LCD_pos(i);LCD_wnte_data(tempi);i+;*/ TOC o 1-5 h z /*/*LCD 1
21、602 函数*/*/*初始化函数*/void LCD_uiit(void)LCD_write_conmiand(0 x3 8);delay ;LCD_write_command(0 x0c);delay ;LCD_write_command(0 x06);delay ;LCD_write_conmiand(OxO 1);delay ;/*LCD1602 写指令函数*/void LCD_write_conmiaiid(uchar dat)wliile (LCD_check_busyQ);is=0;nv=0;ep=0;PO=dat;ep=l;_】op_0;_】op_0;_】op_0;ep=0;/*
22、LCD1602 写数据函数*/void LCD_write_data(uchar dat)wliile (LCD_check_busy();is=l;nv=0;ep=0;_nop_0;_nop_0;PO=dat;_nop_0;_nop_0;_nop_0;_nop_0;ep=l;_nop_0;WP_0;_nop_0;_nop_0;ep=0;/*LCD1602显示字符函数*/void LCD_disp_char(uchai x.uchar y,uchar dat)/x(0-16),第几个字符开始显示;y(l-2),第几行显示 uchar address;while(dat!=,O,)】f(y=i)
23、addiess=0 x80+x;elseaddiess=OxcO+x;LCD_wnte_coininand(addiess);LCD_write_data(dat);/*检查忙函数 */BOOL LCD_check_busyO unsigned hit i;fbr(i=0;i10;i+)BOOL result;/读忙信号is=O;rw=l;ep=l;result = (BOOL)(PO&Ox80);ep=O;leturn result;leturn 0;件*位置显示函数*/void LCD_pos (BYTE pos)LCD_write_conmiand(pos|0 x80);/*显示 “te
24、mp: */void LCD_disp_temp()unsigned chai itemp;itemp=0;while(tempitemp !=r0,)显示字符temp:LCD_wnte_data(tempitemp);itemp+;)/*显示读取的数值*void LCD_disp_num(unsigned hit num)unsigned hit a,b,numl;unsigned hit x,y,z;a=num;OxlO;b=num%OxlO;numl=a*16+b;化为/10进制的数x=numl/100;/x为百位数y=(numl%100)/10;y 为十位数z=numl%10;/z为个
25、位数shownum0=show 10 x;/*值给数组 shownum(*/ shownum 1 =showl 0y;shownum2=showl 0z;LCD_pos(0 x06);LCD-write_data(shownum0);LCD_pos(0 x07);LCD-write_data(shownum 1 );LCD_pos(0 x08);LCD-write_data(shownum2);/*灯p27闪烁程序(无限闪烁)*/void p27TwinQ funsigned int i,j,k;for(k=0;k10;k+)(fbr(i=0;i10;i-H-)p27=0;delay(lOO)
26、;for(j=0jSetPoiiit - NextPomt; / 偏差 pp-SuinEnor += Error; / 每次偏差的累加 pp-PrevErroi-=Enor; 偏差赋给前一次 dEnor = pp-LastEnor - pp-PrevEnor; / 偏差的变化量 pp-LastEnor = Enor;记录当前偏差用于下次比较retuin (pp-Proportion * Error/ 比例 + pp-hitegral * pp-SuniEnor 积分项 + pp-Derivative * dEnor); / 微分项 ,*温度比较处理子程序*/void compaie_tempe
27、r()unsigned mt p,q;unsigned mt receivedTenip =DS1620readQ;if(set_temp()receivedTemp)(if(set_temp()-receivedTemp=1) /如果设定值比读取值高1度以上,启动 加热(p26=l;else rout = PIDCalc( &spidjiii);while (ioutp)(riii =DS 1620readQ ;/ Read Inputrout = PIDCalc ( &spid,iiii);/ Perform PID Iiiteiation)p26=l; /PID算法后的输出值大于等于?
28、? ?度, 启动加热else if(set_tempO= 1) 如果读取值比设置值高1度以上,停止 加热(p26=0;else rout = PIDCalc(&spiddiii);while (routq)riii =DS 1620readQ ;/ Read Inputrout = PIDCalc (&spid,iiii);/ Perform PID Iiiteration)p26=0; 仰ID算法后的输出值大于等于? ? ? ? ?度, 停止加热从PC上得到要设置的预期温度值 */*/,*左*左* MAIN *左* *=*=*/inam ()*/* Local variables */*/u
29、nsigned int i,j;/unsigned char c; /* dummy variable */unsigned char Select_Type; /* Function variable */ /unsigned int k; /* counter */unsigned int q; /* used for getting 9 bits */unsigned int Config_Data; /* Coilfig. Reg. Data */ unsigned char Temp_High; /* Temp. High Data */ unsigned mt Temp_Low; /* Temp_Low Data */ /unsigned clw temp_string10; /* */ hit tempi;
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 中国调压阀弹簧项目投资可行性研究报告
- 中国通话系统项目投资可行性研究报告
- 承重粉煤灰多孔砖行业深度研究报告
- 掩门矮柜行业深度研究报告
- 奔驰涨紧轮行业深度研究报告
- 团队沟通与协作高效工具指南
- 卧式自粘机行业深度研究报告
- 黄松木木纹软片行业深度研究报告
- 中国标准电感组项目投资可行性研究报告
- 警察攻楼考试题目及答案
- 2025至2030年中国山东省建筑行业发展运行现状及投资战略规划报告
- 初中生关于友情的作文范例集锦
- 生物抑尘技术应用-洞察及研究
- 眼科手术给药
- 2025年肺动脉高压指南要点解读
- 石家庄市社区工作者招聘笔试真题2024
- 海尔oec管理办法
- 产品防锈蚀管理办法
- 2025年电气技术员技能培训考试题及答案
- 红色物业培训课件
- DG-TJ08-2134-2024 建筑装饰工程石材应用技术标准
评论
0/150
提交评论