有关题目
实现代码
注意:
代码实现方面 : ①注意控制温度参数temp_para范围 ②DAC输出时,注意写入的数字IIC_SendByte(temp)中temp范围在0~255; 源文件修改方面 : ①官方给的iic.h中使用的时C51的头文件"reg52.h",我们需要修改为对应的15系列头文件"STC15F2K60S2.h",这样才可以使用其中的一些特殊位寄存器 ②注意修改驱动代码时要留意一下onewire.h中单总线延时函数,是STC89C52RC ,还是15系列 的,15系列的单片机速度比51快8~12倍,需要修改对应的延时函数 。如下 //单总线内部延时函数 void Delay_OneWire(unsigned int t) { char i; while(t–) for (i = 0; i < 12; i++); }
main.c
#include "STC15F2K60S2.h"
#include "iic.h"
#include "onewire.h"
#define uchar unsigned char
#define uint unsigned int
sbit L1 = P0^0;
sbit L2 = P0^1;
sbit L3 = P0^2;
sbit L4 = P0^3;
uchar jm = 0;
code uchar tab[] = {0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90,0xff, 0xc6, 0x8c, 0x88};
bit mod_flag;
char temp_para = 25;
float curr_temp, Vdac;
void sys_init();
float rd_temperature();
void key_handle();
void dac_pcf8591(uchar da);
void led();
void dsp_smg_bit(uchar pos, val, sta);
void display();
void dsp_temp();
void dsp_para();
void dsp_dac();
void delay_k(uchar t);
void Delay1ms();
void main()
{
sys_init();
while(1)
{
curr_temp = rd_temperature();
key_handle();
display();
led();
}
}
void led()
{
if (!mod_flag)
{
P2 = (P2 & 0x1f) | 0x80;
L1 = 0;
}
if (0 == jm)
{
P2 = (P2 & 0x1f) | 0x80;
L2 = 0;
}
else if (1 == jm)
{
P2 = (P2 & 0x1f) | 0x80;
L3 = 0;
}
else if (2 == jm)
{
P2 = (P2 & 0x1f) | 0x80;
L4 = 0;
}
}
void key_handle()
{
P44 = P42 = P33 = 1;
P32 = 0;
if (!P44)
{
delay_k(15);
if (!P44)
{
while(!P44)
display();
mod_flag = !mod_flag;
if (!mod_flag)
{
if (curr_temp * 10 < temp_para * 10)
{
Vdac = 0.0;
}
else
Vdac = 5.0;
}
else
{
if (curr_temp * 10 < 200)
Vdac = 1.0;
else if (curr_temp * 10 > 400)
Vdac = 4.0;
else
{
Vdac = 0.15 * curr_temp - 2;
}
}
dac_pcf8591((uchar)(Vdac * 51));
}
}
if (!P42)
{
delay_k(15);
if (!P42)
{
while(!P42)
display();
if (1 == jm)
{
if (++temp_para > 99)
temp_para = 99;
}
}
}
P44 = P42 = P32 = 1;
P33 = 0;
if (!P44)
{
delay_k(15);
if (!P44)
{
while(!P44)
display();
if (++jm >= 3)
jm = 0;
}
}
if (!P42)
{
delay_k(15);
if (!P42)
{
while(!P42)
display();
if (1 == jm)
{
if (--temp_para < 0)
temp_para = 0;
}
}
}
}
void display()
{
if (0 == jm)
dsp_temp();
else if (1 == jm)
dsp_para();
else if (2 == jm)
dsp_dac();
}
void dsp_dac()
{
uint x = (uint) (Vdac * 100);
dsp_smg_bit(1, 13, 0);
dsp_smg_bit(6, x / 100 % 10, 1);
dsp_smg_bit(7, x / 10 % 10, 0);
dsp_smg_bit(8, x % 10, 0);
}
void dsp_para()
{
dsp_smg_bit(1, 12, 0);
dsp_smg_bit(7, temp_para / 10 % 10, 0);
dsp_smg_bit(8, temp_para % 10, 0);
}
void dsp_temp()
{
uint x = (uint) (curr_temp * 100);
dsp_smg_bit(1, 11, 0);
dsp_smg_bit(5, x / 1000 % 10, 0);
dsp_smg_bit(6, x / 100 % 10, 1);
dsp_smg_bit(7, x / 10 % 10, 0);
dsp_smg_bit(8, x % 10, 0);
}
void dsp_smg_bit(uchar pos, val, sta)
{
P2 = (P2 & 0x1f) | 0xc0;
P0 = 1 << (pos - 1);
P2 = (P2 & 0x1f) | 0xe0;
if (sta)
P0 = tab[val] & 0x7f;
else
P0 = tab[val];
Delay1ms();
P0 = 0xff;
P2 &= 0x1f;
}
void Delay1ms()
{
unsigned char i, j;
i = 12;
j = 169;
do
{
while (--j);
} while (--i);
}
void delay_k(uchar t)
{
while(t--)
display();
}
void dac_pcf8591(uchar da)
{
IIC_Start();
IIC_SendByte(0x90);
IIC_WaitAck();
IIC_SendByte(0x40);
IIC_WaitAck();
IIC_SendByte(da);
IIC_WaitAck();
IIC_Stop();
}
float rd_temperature()
{
uchar l, h;
uint t;
float temp;
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0x44);
init_ds18b20();
Write_DS18B20(0xcc);
Write_DS18B20(0xbe);
l = Read_DS18B20();
h = Read_DS18B20();
t = (h & 0x0f);
t <<= 8;
t |= l;
temp = t * 0.0625;
return temp;
}
void sys_init()
{
P2 = (P2 & 0x1f) | 0xa0;
P0 = 0xaf;
P2 = (P2 & 0x1f) | 0x80;
P0 = 0xff;
P2 &= 0x1f;
}
iic.h
#ifndef _IIC_H
#define _IIC_H
#include "STC15F2K60S2.h"
#include "intrins.h"
sbit SDA = P2^1;
sbit SCL = P2^0;
void IIC_Start(void);
void IIC_Stop(void);
bit IIC_WaitAck(void);
void IIC_SendByte(unsigned char byt);
#endif
iic.c
#include "iic.h"
#define DELAY_TIME 5
void IIC_Delay(unsigned char i)
{
do{_nop_();}
while(i--);
}
void IIC_Start(void)
{
SDA = 1;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 0;
}
void IIC_Stop(void)
{
SDA = 0;
SCL = 1;
IIC_Delay(DELAY_TIME);
SDA = 1;
IIC_Delay(DELAY_TIME);
}
bit IIC_WaitAck(void)
{
bit ackbit;
SCL = 1;
IIC_Delay(DELAY_TIME);
ackbit = SDA;
SCL = 0;
IIC_Delay(DELAY_TIME);
return ackbit;
}
void IIC_SendByte(unsigned char byt)
{
unsigned char i;
for(i=0; i<8; i++)
{
SCL = 0;
IIC_Delay(DELAY_TIME);
if(byt & 0x80) SDA = 1;
else SDA = 0;
IIC_Delay(DELAY_TIME);
SCL = 1;
byt <<= 1;
IIC_Delay(DELAY_TIME);
}
SCL = 0;
}
onewire.h
#ifndef __ONEWIRE_H
#define __ONEWIRE_H
#include "STC15F2K60S2.h"
sbit DQ = P1^4;
void Delay_OneWire(unsigned int t);
bit init_ds18b20(void);
unsigned char Read_DS18B20(void);
void Write_DS18B20(unsigned char dat);
#endif
onewire.c
#include "onewire.h"
void Delay_OneWire(unsigned int t)
{
char i;
while(t--)
for (i = 0; i < 12; i++);
}
void Write_DS18B20(unsigned char dat)
{
unsigned char i;
for(i=0;i<8;i++)
{
DQ = 0;
DQ = dat&0x01;
Delay_OneWire(5);
DQ = 1;
dat >>= 1;
}
Delay_OneWire(5);
}
unsigned char Read_DS18B20(void)
{
unsigned char i;
unsigned char dat;
for(i=0;i<8;i++)
{
DQ = 0;
dat >>= 1;
DQ = 1;
if(DQ)
{
dat |= 0x80;
}
Delay_OneWire(5);
}
return dat;
}
bit init_ds18b20(void)
{
bit initflag = 0;
DQ = 1;
Delay_OneWire(12);
DQ = 0;
Delay_OneWire(80);
DQ = 1;
Delay_OneWire(10);
initflag = DQ;
Delay_OneWire(5);
return initflag;
}
|