1、txy_smg_ca.c(注意添加ADD)
#include "absacc.h"
void SMG_Bit(unsigned pos, unsigned char dat)//数码管位置、数据
{
XBYTE[0xe000] = 0xff;//为了效果好先熄灭,一定的消影作用
XBYTE[0xc000] = 0x01 << pos;//MM模式
XBYTE[0xe000] = dat;
}
void SMG_All(unsigned char dat)//操作所有数码管
{
XBYTE[0xc000] = 0xff;//数码管所有位置
XBYTE[0xe000] = dat;
}
void DelaySMG(unsigned int t)
{
while(t--);
}
2、txy_smg_ca.h
#ifndef TXY_SMG_CA_H
#define TXY_SMG_CA_H
unsigned char code SMG_duanma[18] = {
// 0 1 2 3 4 5 6 7
0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,
// 8 9 A10 B11 C12 D13 E14 F15
0x80,0x90,0x88,0x80,0xc6,0xc0,0x86,0x8e,
// -16 .17
0xbf,0x7f}; //共阳数码管
unsigned char code SMG_duanmaDot[10] = {
// 0. 1. 2. 3. 4. 5. 6. 7.
0x40,0x79,0x24,0x30,0x19,0x12,0x02,0x78,
// 8. 9.
0x00,0x10}; //数码管(小数点)
void SMG_Bit(unsigned pos, unsigned char dat);//数码管位置、数据
void SMG_All(unsigned char dat);//操作所有数码管
void DelaySMG(unsigned int t);
#endif
3、DS18B20.c
#include "reg52.h"
#include "absacc.h"
#include "onewire.h"
#include "txy_smg_ca.h"
unsigned int temp = 0;//温度
void ShowSMG()//显示温度
{
//567
//百位:567 / 100 = 5.67 = 5
//十位:567 % 100 = 5.67 = 67 67 / 10 = 6.7 = 6
//个位:567 % 10 = 56.7 = 7
/*显示1位小数
SMG_Bit(7, SMG_duanma[temp%10]);
DelaySMG(100);
SMG_Bit(6, SMG_duanmaDot[(temp%100)/10]);
DelaySMG(100);
SMG_Bit(5, SMG_duanma[temp/100]);
DelaySMG(100);
*/
SMG_Bit(7, SMG_duanma[temp%10]);//显示整数
DelaySMG(100);
SMG_Bit(6, SMG_duanma[temp/10]);
DelaySMG(100);
SMG_Bit(5, 0xff);
DelaySMG(100);
SMG_Bit(4, 0xff);//不显示值,一定的消影作用
DelaySMG(100);
SMG_Bit(3, 0xff);
DelaySMG(100);
SMG_Bit(2, 0xff);
DelaySMG(100);
SMG_Bit(1, 0xff);
DelaySMG(100);
SMG_Bit(0, 0xff);
DelaySMG(100);
//最后一个数码管特别亮?
SMG_All(0xff);//关掉所有数码管
}
void Delay(unsigned int t)
{
while(t--)//数码管动态显示本质:不断轮流点亮每一个数码管,不可以间断
{
ShowSMG();//一旦动态扫描间断,由多个数码管变成只显示一个数码管
} //因此在等待时间消化的过程中,仍然要保持数码管动态扫描,轮流显示没有间断
}
void Read_DS18B20_temp()//读温度
{
unsigned char LSB, MSB;//定义8字节变量接收数据
init_ds18b20();//复位
Write_DS18B20(0xcc);//写入0xcc 跳过ROM指令
Write_DS18B20(0x44);//写入0x44 开始温度转化
Delay(1000);//延时,等待温度转化完成(注意延时的时候数码管不可以间断:延时里要动态扫描数码管)
init_ds18b20();//复位,开始新一轮操作
Write_DS18B20(0xcc);//写入0xcc 跳过ROM指令
Write_DS18B20(0xbe);//写入0xbe 读取16位数据
LSB = Read_DS18B20();//读低8位数据
MSB = Read_DS18B20();//读高8位数据
init_ds18b20();//复位,停止数据读取
temp = MSB;//高8位放到temp里
temp <<= 8;//左移8位
temp = temp | LSB;//或(加)上低8位=>将LSB、MSB整合成为一个16位数据
temp >>= 4;//右移4位,移走小数(整数不需要换算)//显示整数
/*显示1位小数
if ((temp & 0xf800) == 0x0000)//正数(16位 高5位为符号位)
{
temp >>= 4;//右移四位,移走小数
temp = temp * 10;//放大10倍
temp = temp + (LSB & 0x0f) * 0.625;//温度(*0.625是换算)
}
*/
}
void main()
{
XBYTE[0x8000] = 0xff;//关闭LED
XBYTE[0xa000] = 0x00;//关闭蜂鸣器与继电器
while(1)
{
Read_DS18B20_temp();
ShowSMG();
}
}