标题:
请问一下单片机程序这样为什么不能正确读取温度?
[打印本页]
作者:
dadsaa
时间:
2024-11-1 17:09
标题:
请问一下单片机程序这样为什么不能正确读取温度?
用的普中的stc89c52
主函数:
#include <REGX52.H>#include "Delay.h"
#include "LCD1602.h"
#include "Matrix.h"
#include "Timer0.h"
#include "Key.h"
#include <INTRINS.H>
#include "DS1302.h"
#include "AT24C02.h"
#include "I2C.h"
#include "DS18B20.h"
#include "Buzzer.h"
#include "OneWire.h"
float TF,TE,temp,e;
char TH=30,TL;
unsigned char Keynum,n;
unsigned char mode=1;
unsigned char date[2];
unsigned int intT;
void change()
{
if(n==1)
{
mode++;
if(mode>3)
mode=3;
}
if(n==2)
{
mode--;
if(mode<1)
{
mode=1;
}
}
}
void main()
{
unsigned char rom1[8],rom2[8];
TH=AT24C02_RecieveByte(0);
TL=AT24C02_RecieveByte(1);
DS18B20_ConvertT();
Delay(1000);
LCD_Init();
LCD_ShowNum(2,4,TH,2);
LCD_ShowNum(2,10,TL,2);
DS1302_Init();
settime();
while(1)
{
readtime();
// DS18B20_ConvertT();
// TF=DS18B20_ReadT();
OneWire_Init();
if (OneWire_RecieveBit() == 0) {
OneWireSearchROM(rom1);
DS18B20_ConvertT();
Delay(750);
TF=DS18B20_ReadT(rom1);
}
OneWire_Init();
if (OneWire_RecieveBit() == 0) {
OneWireSearchROM(rom2);
DS18B20_ConvertT();
Delay(750);
TE = DS18B20_ReadT(rom2);
}
change();
n=MatrixKey();
if(mode==1)
{
LCD_ShowNum(1,12,DS1302_Time[4],2);
LCD_ShowChar(1,14,':');
LCD_ShowNum(1,15,DS1302_Time[5],2);
LCD_ShowNum(2,4,TH,2);
LCD_ShowNum(2,10,TL,2);
if(TH>99||TL<0||TH<=TL)
{
TH=30;
TL=10;
}
if(TF<0)
{
TF=0;
}
else
{
LCD_ShowChar(1,1,'+');
if(TF>=100)
{
TF=100;
}
}
LCD_ShowNum(1,2,TF,3);
LCD_ShowChar(1,5,'.');
LCD_ShowNum(1,6,(unsigned long)(TF*100)%100,2);
LCD_ShowString(2,1,"TH:");
LCD_ShowString(2,7,"TL:");
Keynum=Key();
if(Keynum)
{
if(Keynum==1)
{
TH++;
if(TH>100)
TH=100;
}
if(Keynum==2)
{
TH--;
if(TH<=TL)
TH++;
}
if(Keynum==3)
{
TL++;
if(TL>=TH)
TL--;
}
if(Keynum==4)
{
TL--;
if(TL<0)
TL=0;
}
LCD_ShowNum(2,4,TH,2);
LCD_ShowNum(2,10,TL,2);
AT24C02_WriteByte(0,TH);
Delay(5);
AT24C02_WriteByte(1,TL);
Delay(5);
}
if(TF>TH)
{
Buzzer_Time(10);
LCD_ShowString(2,13,"OVER");
P2=~P2;
}
else
{
if(TF<TL)
{
Buzzer_Time(10);
LCD_ShowString(2,13,"LOW");
P2=~P2;
}
else
{
LCD_ShowString(2,13," ");
}
}
}
}
}
DS18B20:#include <REGX52.H>
#include "OneWire.h"
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCHPAD 0xBE
void DS18B20_ConvertT()
{
OneWire_Init();
OneWire_SendByte(DS18B20_SKIP_ROM);
OneWire_SendByte(DS18B20_CONVERT_T);
}
void OneWireMatchROM(unsigned char rom[8]) {
OneWire_SendByte(0x55);
OneWire_SendByte(rom[0]);
OneWire_SendByte(rom[1]);
OneWire_SendByte(rom[2]);
OneWire_SendByte(rom[3]);
OneWire_SendByte(rom[4]);
OneWire_SendByte(rom[5]);
OneWire_SendByte(rom[6]);
OneWire_SendByte(rom[7]);
}
void OneWireSearchROM(unsigned char rom[8]) {
unsigned char i, j;
unsigned char id_bit, cmp_id_bit;
unsigned char rom_bit;
for (i = 0; i < 8; i++) {
rom[ i] = 0;
}
OneWire_Init();
OneWire_SendByte(0xF0);
for (i = 0; i < 64; i++) {
id_bit = OneWire_RecieveBit();
cmp_id_bit = OneWire_RecieveBit();
if (id_bit && cmp_id_bit) {
return;
}
rom_bit = (id_bit << 1) | cmp_id_bit;
for (j = 0; j < 8; j++) {
if (rom_bit & 0x01) {
OneWire_SendBit(1);
rom[j] |= (1 << i);
} else {
OneWire_SendBit(0);
}
rom_bit >>= 1;
}
}
}
float DS18B20_ReadT(unsigned char rom[8])
{
int Temp;
float T;
unsigned char TLSB,TMSB;
OneWire_Init();
OneWireMatchROM(rom);
OneWire_SendByte(DS18B20_SKIP_ROM);
OneWire_SendByte(DS18B20_READ_SCRATCHPAD);
TLSB=OneWire_RecieveByte();
TMSB=OneWire_RecieveByte();
Temp=(TMSB<<8)|TLSB;
T=Temp/16.0;
return T;
}
Onewire:#include <REGX52.H>
sbit OneWire_DQ=P3^7;
unsigned char OneWire_Init()
{
unsigned char i;
unsigned char AckBit;
OneWire_DQ=1;
OneWire_DQ=0;
i = 247;while (--i); //Delay 500us
OneWire_DQ=1;
i = 32;while (--i); //Delay 70us
AckBit=OneWire_DQ;
i = 247;while (--i); //Delay 500us
return AckBit;
}
void OneWire_SendBit(unsigned char Bit)
{
unsigned char i;
OneWire_DQ=0;
i = 4;while (--i); //Delay 10us
OneWire_DQ=Bit;
i = 24;while (--i); //Delay 50us
OneWire_DQ=1;
}
unsigned char OneWire_RecieveBit()
{
unsigned char i,Bit;
OneWire_DQ=0;
i = 2;while (--i); //Delay 5us
OneWire_DQ=1;
i = 2;while (--i); //Delay 5us
Bit=OneWire_DQ;
i = 24;while (--i); //Delay 50us
return Bit;
}
void OneWire_SendByte(unsigned char Byte)
{
unsigned char i;
for(i=0;i<8;i++)
{
OneWire_SendBit(Byte&(0x01<<i));
}
}
unsigned char OneWire_RecieveByte()
{
unsigned char i,Byte=0x00;
for(i=0;i<8;i++)
{
if(OneWire_RecieveBit()){Byte|=(0x01<<i);}
}
return Byte;
}
作者:
WL0123
时间:
2024-11-1 22:20
DS18B20对时序非常敏感,发送转换命令后也要等待一段时间才能正确读取温度值。
作者:
STC庄伟
时间:
2024-11-2 10:59
普中开发板 直接换成AI8051U 就可以USB直接下载了
/************* 功能说明 **************
本例程基于AI8051U为主控芯片的实验箱进行编写测试。
使用Keil C251编译器,Memory Model推荐设置XSmall模式,默认定义变量在edata,单时钟存取访问速度快。
edata建议保留1K给堆栈使用,空间不够时可将大数组、不常用变量加xdata关键字定义到xdata空间。
读ADC和测温度.
用STC的MCU的IO方式控制74HC595驱动8位数码管。
使用Timer0的16位自动重装来产生1ms节拍,程序运行于这个节拍下,用户修改MCU主时钟频率时,自动定时于1ms.
右边4位数码管显示温度值, 分辨率0.1度.
NTC使用1%精度的MF52 10K@25度.
测温度时, 使用12位的ADC值, 使用对分查找表格来计算, 小数点后一位数是用线性插补来计算的.
下载时, 选择时钟 24MHZ (用户可自行修改频率).
******************************************/
#include "..\..\comm\AI8051U.h"
#include "stdio.h"
#include "intrins.h"
typedef unsigned char u8;
typedef unsigned int u16;
typedef unsigned long u32;
/****************************** 用户定义宏 ***********************************/
#define MAIN_Fosc 24000000UL
#define Timer0_Reload (65536UL -(MAIN_Fosc / 1000)) //Timer 0 中断频率, 1000次/秒
/*****************************************************************************/
#define DIS_DOT 0x20
#define DIS_BLACK 0x10
#define DIS_ 0x11
/************* IO口定义 **************/
sbit P_HC595_SER = P3^4; //pin 14 SER data input
sbit P_HC595_RCLK = P3^5; //pin 12 RCLk store (latch) clock
sbit P_HC595_SRCLK = P3^2; //pin 11 SRCLK Shift data clock
/************* 本地常量声明 **************/
u8 code t_display[]={ //标准字库
// 0 1 2 3 4 5 6 7 8 9 A B C D E F
0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71,
//black - H J K L N o P U t G Q r M y
0x00,0x40,0x76,0x1E,0x70,0x38,0x37,0x5C,0x73,0x3E,0x78,0x3d,0x67,0x50,0x37,0x6e,
0xBF,0x86,0xDB,0xCF,0xE6,0xED,0xFD,0x87,0xFF,0xEF,0x46}; //0. 1. 2. 3. 4. 5. 6. 7. 8. 9. -1
u8 code T_COM[]={0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; //位码
/************* 本地变量声明 **************/
u8 LED8[8]; //显示缓冲
u8 display_index; //显示位索引
bit B_1ms; //1ms标志
u16 msecond;
/************* 本地函数声明 **************/
u16 get_temperature(u16 adc);
u16 Get_ADC12bitResult(u8 channel); //channel = 0~15
/**********************************************/
void main(void)
{
u8 i;
u16 j;
WTST = 0; //设置程序指令延时参数,赋值为0可将CPU执行指令的速度设置为最快
EAXFR = 1; //扩展寄存器(XFR)访问使能
CKCON = 0; //提高访问XRAM速度
P0M1 = 0x00; P0M0 = 0x00; //设置为准双向口
P1M1 = 0x08; P1M0 = 0x00; //设置为准双向口, P1.3高阻输入
P2M1 = 0x00; P2M0 = 0x00; //设置为准双向口
P3M1 = 0x00; P3M0 = 0x00; //设置为准双向口
P4M1 = 0x00; P4M0 = 0x00; //设置为准双向口
P5M1 = 0x00; P5M0 = 0x02; //设置为准双向口, P5.1推挽输出
P6M1 = 0x00; P6M0 = 0x00; //设置为准双向口
P7M1 = 0x00; P7M0 = 0x00; //设置为准双向口
display_index = 0;
ADCTIM = 0x3f; //设置 ADC 内部时序,ADC采样时间建议设最大值
ADCCFG = 0x2f; //设置 ADC 时钟为系统时钟/2/16/16
ADC_CONTR = 0x80; //使能 ADC 模块
AUXR = 0x80; //Timer0 set as 1T, 16 bits timer auto-reload,
TH0 = (u8)(Timer0_Reload / 256);
TL0 = (u8)(Timer0_Reload % 256);
ET0 = 1; //Timer0 interrupt enable
TR0 = 1; //Tiner0 run
EA = 1; //打开总中断
P51 = 1; //给NTC供电
for(i=0; i<8; i++) LED8[i] = 0x10; //上电消隐
while(1)
{
if(B_1ms) //1ms到
{
B_1ms = 0;
if(++msecond >= 300) //300ms到
{
msecond = 0;
j = Get_ADC12bitResult(3); //参数0~15,查询方式做一次ADC, 返回值就是结果, == 4096 为错误
if(j < 4096)
{
/*
LED8[0] = j / 1000; //显示ADC值
LED8[1] = (j % 1000) / 100;
LED8[2] = (j % 100) / 10;
LED8[3] = j % 10;
if(LED8[0] == 0) LED8[0] = DIS_BLACK;
*/
j = get_temperature(j); //计算温度值
if(j >= 400) F0 = 0, j -= 400; //温度 >= 0度
else F0 = 1, j = 400 - j; //温度 < 0度
LED8[4] = j / 1000; //显示温度值
LED8[5] = (j % 1000) / 100;
LED8[6] = (j % 100) / 10 + DIS_DOT;
LED8[7] = j % 10;
if(LED8[4] == 0) LED8[4] = DIS_BLACK;
if(F0) LED8[4] = DIS_; //显示-
}
else //错误
{
for(i=0; i<8; i++) LED8[i] = DIS_;
}
}
}
}
}
/**********************************************/
//========================================================================
// 函数: u16 Get_ADC12bitResult(u8 channel)
// 描述: 查询法读一次ADC结果.
// 参数: channel: 选择要转换的ADC.
// 返回: 12位ADC结果.
// 版本: V1.0, 2012-10-22
//========================================================================
u16 Get_ADC12bitResult(u8 channel) //channel = 0~15
{
ADC_RES = 0;
ADC_RESL = 0;
ADC_CONTR = (ADC_CONTR & 0xf0) | channel; //设置ADC转换通道
ADC_START = 1;//启动ADC转换
_nop_();
_nop_();
_nop_();
_nop_();
while(ADC_FLAG == 0); //wait for ADC finish
ADC_FLAG = 0; //清除ADC结束标志
return (((u16)ADC_RES << 8) | ADC_RESL);
}
// MF52E 10K at 25, B = 3950, ADC = 12 bits
u16 code temp_table[]={
140, //;-40 0
149, //;-39 1
159, //;-38 2
168, //;-37 3
178, //;-36 4
188, //;-35 5
199, //;-34 6
210, //;-33 7
222, //;-32 8
233, //;-31 9
246, //;-30 10
259, //;-29 11
272, //;-28 12
286, //;-27 13
301, //;-26 14
317, //;-25 15
333, //;-24 16
349, //;-23 17
367, //;-22 18
385, //;-21 19
403, //;-20 20
423, //;-19 21
443, //;-18 22
464, //;-17 23
486, //;-16 24
509, //;-15 25
533, //;-14 26
558, //;-13 27
583, //;-12 28
610, //;-11 29
638, //;-10 30
667, //;-9 31
696, //;-8 32
727, //;-7 33
758, //;-6 34
791, //;-5 35
824, //;-4 36
858, //;-3 37
893, //;-2 38
929, //;-1 39
965, //;0 40
1003, //;1 41
1041, //;2 42
1080, //;3 43
1119, //;4 44
1160, //;5 45
1201, //;6 46
1243, //;7 47
1285, //;8 48
1328, //;9 49
1371, //;10 50
1414, //;11 51
1459, //;12 52
1503, //;13 53
1548, //;14 54
1593, //;15 55
1638, //;16 56
1684, //;17 57
1730, //;18 58
1775, //;19 59
1821, //;20 60
1867, //;21 61
1912, //;22 62
1958, //;23 63
2003, //;24 64
2048, //;25 65
2093, //;26 66
2137, //;27 67
2182, //;28 68
2225, //;29 69
2269, //;30 70
2312, //;31 71
2354, //;32 72
2397, //;33 73
2438, //;34 74
2479, //;35 75
2519, //;36 76
2559, //;37 77
2598, //;38 78
2637, //;39 79
2675, //;40 80
2712, //;41 81
2748, //;42 82
2784, //;43 83
2819, //;44 84
2853, //;45 85
2887, //;46 86
2920, //;47 87
2952, //;48 88
2984, //;49 89
3014, //;50 90
3044, //;51 91
3073, //;52 92
3102, //;53 93
3130, //;54 94
3157, //;55 95
3183, //;56 96
3209, //;57 97
3234, //;58 98
3259, //;59 99
3283, //;60 100
3306, //;61 101
3328, //;62 102
3351, //;63 103
3372, //;64 104
3393, //;65 105
3413, //;66 106
3432, //;67 107
3452, //;68 108
3470, //;69 109
3488, //;70 110
3506, //;71 111
3523, //;72 112
3539, //;73 113
3555, //;74 114
3571, //;75 115
3586, //;76 116
3601, //;77 117
3615, //;78 118
3628, //;79 119
3642, //;80 120
3655, //;81 121
3667, //;82 122
3679, //;83 123
3691, //;84 124
3702, //;85 125
3714, //;86 126
3724, //;87 127
3735, //;88 128
3745, //;89 129
3754, //;90 130
3764, //;91 131
3773, //;92 132
3782, //;93 133
3791, //;94 134
3799, //;95 135
3807, //;96 136
3815, //;97 137
3822, //;98 138
3830, //;99 139
3837, //;100 140
3844, //;101 141
3850, //;102 142
3857, //;103 143
3863, //;104 144
3869, //;105 145
3875, //;106 146
3881, //;107 147
3887, //;108 148
3892, //;109 149
3897, //;110 150
3902, //;111 151
3907, //;112 152
3912, //;113 153
3917, //;114 154
3921, //;115 155
3926, //;116 156
3930, //;117 157
3934, //;118 158
3938, //;119 159
3942 //;120 160
};
/******************** 计算温度 ***********************************************/
// 计算结果: 0对应-40.0度, 400对应0度, 625对应25.0度, 最大1600对应120.0度.
// 为了通用, ADC输入为12bit的ADC值.
// 电路和软件算法设计: Coody
/**********************************************/
#define D_SCALE 10 //结果放大倍数, 放大10倍就是保留一位小数
u16 get_temperature(u16 adc)
{
u16 code *p;
u16 i;
u8 j,k,min,max;
adc = 4096 - adc; //Rt接地
p = temp_table;
if(adc < p[0]) return (0xfffe);
if(adc > p[160]) return (0xffff);
min = 0; //-40度
max = 160; //120度
for(j=0; j<5; j++) //对分查表
{
k = min / 2 + max / 2;
if(adc <= p[k]) max = k;
else min = k;
}
if(adc == p[min]) i = min * D_SCALE;
else if(adc == p[max]) i = max * D_SCALE;
else // min < temp < max
{
while(min <= max)
{
min++;
if(adc == p[min]) {i = min * D_SCALE; break;}
else if(adc < p[min])
{
min--;
i = p[min]; //min
j = (adc - i) * D_SCALE / (p[min+1] - i);
i = min;
i *= D_SCALE;
i += j;
break;
}
}
}
return i;
}
/**************** 向HC595发送一个字节函数 ******************/
void Send_595(u8 dat)
{
u8 i;
for(i=0; i<8; i++)
{
dat <<= 1;
P_HC595_SER = CY;
P_HC595_SRCLK = 1;
P_HC595_SRCLK = 0;
}
}
/********************** 显示扫描函数 ************************/
void DisplayScan(void)
{
Send_595(t_display[LED8[display_index]]); //输出段码
Send_595(~T_COM[display_index]); //输出位码
P_HC595_RCLK = 1;
P_HC595_RCLK = 0;
if(++display_index >= 8) display_index = 0; //8位结束回0
}
/********************** Timer0 1ms中断函数 ************************/
void timer0 (void) interrupt 1
{
DisplayScan(); //1ms扫描显示一位
B_1ms = 1; //1ms标志
}
复制代码
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1