|
100黑幣
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit PWM=P1^0;
sbit DSPORT=P3^7;
sbit LSA=P2^2;
sbit LSB=P2^3;
sbit LSC=P2^4;
void Ds18b20ReadTemp();
void UsartConfiguration();
void DigDisplay();
void delay(unsigned int i);
unsigned int temp=0;
unsigned char zhuanshu=20;
unsigned int cnt=0;
unsigned char receiveData;
unsigned int dingshi=0;
unsigned char min=1;
unsigned char code smgduan[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char DisplayData[8];
void main(void)
{
unsigned int i=3000;
UsartConfiguration();
while(1)
{
if(cnt>=3000)
{
cnt=0;
Ds18b20ReadTemp(); //3s檢測一次溫度
DigDisplay();
}
if(receiveData==0x11)
{
ET0=1;zhuanshu=20;receiveData=0;
}
if(receiveData==0x01)
{
zhuanshu=0;while(i--);ET0=0;receiveData=0;
}
if(receiveData==0x10)
{
zhuanshu=50;
}
if(receiveData==0x12)
{
zhuanshu=70;
}
}
}
void UsartConfiguration()
{
SCON=0X50; //設置為工作方式1
TMOD=0X21; //設置計數器工作方式2
PCON=0X80; //波特率加倍
TH0 = 0xFC;
TL0 = 0x66;
TH1=0XF9; //計數器初始值設置,注意波特率是9600的
TL1=0XF9;
ET0=1;
ES=1; //打開接收中斷
EA=1; //打開總中斷
TR1=1;
TR0=1; //打開計數器
}
void Usart() interrupt 4
{
receiveData=SBUF; //出去接收到的數據
RI = 0; //清除接收中斷標志位
}
void Time1(void) interrupt 1 //3 為定時器1的中斷號 1 定時器0的中斷號 0 外部中斷1 2 外部中斷2 4 串口中斷
{
static unsigned char timer1=0;
TH0 = 0xF6; //重新賦初值
TL0 = 0x66;
timer1++;
cnt++;
dingshi++;
if(dingshi>=60050)
{
dingshi=0;
}
if(timer1>100) //PWM周期為100*0.5ms
{
timer1=0;
}
if(timer1 <zhuanshu)//改變zhunashu這個值可以改變直流電機的速度這個值越大轉的越快
{
PWM=1;
}
else
{
PWM=0;
}
}
void Delay1ms(uint y) //延時程序
{
uint x;
for( ; y>0; y--)
{
for(x=110; x>0; x--);
}
}
uchar Ds18b20Init() //溫度的子函數
{
uchar i;
DSPORT = 0; //將總線拉低480us~960us
i = 70;
while(i--);//延時642us
DSPORT = 1; //然后拉高總線,如果DS18B20做出反應會將在15us~60us后總線拉低
i = 0;
while(DSPORT) //等待DS18B20拉低總線
{
i++;
if(i>5)//等待>5MS
{
return 0;//初始化失敗
}
Delay1ms(1);
}
return 1;//初始化成功
}
void Ds18b20WriteByte(uchar dat)
{
uint i, j;
for(j=0; j<8; j++)
{
DSPORT = 0; //每寫入一位數據之前先把總線拉低1us
i++;
DSPORT = dat & 0x01; //然后寫入一個數據,從最低位開始
i=6;
while(i--); //延時68us,持續時間最少60us
DSPORT = 1; //然后釋放總線,至少1us給總線恢復時間才能接著寫入第二個數值
dat >>= 1;
}
}
uchar Ds18b20ReadByte()
{
uchar byte, bi;
uint i, j;
for(j=8; j>0; j--)
{
DSPORT = 0;//先將總線拉低1us
i++;
DSPORT = 1;//然后釋放總線
i++;
i++;//延時6us等待數據穩定
bi = DSPORT; //讀取數據,從最低位開始讀取
/*將byte左移一位,然后與上右移7位后的bi,注意移動之后移掉那位補0。*/
byte = (byte >> 1) | (bi << 7);
i = 4; //讀取完之后等待48us再接著讀取下一個數
while(i--);
}
return byte;
}
void Ds18b20ChangTemp()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);//跳過ROM操作命令
Ds18b20WriteByte(0x44); //溫度轉換命令
// Delay1ms(100);//等待轉換成功,而如果你是一直刷著的話,就不用這個延時了
}
void Ds18b20ReadTempCom()
{
Ds18b20Init();
Delay1ms(1);
Ds18b20WriteByte(0xcc);//跳過ROM操作命令
Ds18b20WriteByte(0xbe);//發送讀取溫度命令
}
void Ds18b20ReadTemp()
{
float tp;
int temp1 = 0;
uchar tmh, tml;
Ds18b20ChangTemp();//先寫入轉換命令
Ds18b20ReadTempCom();//然后等待轉換完后發送讀取溫度命令
tml = Ds18b20ReadByte();//讀取溫度值共16位,先讀低字節
tmh = Ds18b20ReadByte();//再讀高字節
temp1 = tmh;
temp1 <<= 8;
temp1 |= tml;
if(temp1< 0)
{
DisplayData[0] = 0x40;
temp1=temp1-1;
temp1=~temp1;
tp=temp1;
temp1=tp*0.0625*100+0.5;
}
else
{
DisplayData[0] = 0x00;
tp=temp1;
temp1=tp*0.0625*100+0.5;
}
temp=temp1%10000;
DisplayData[1] = smgduan[temp1 / 10000];
DisplayData[2] = smgduan[temp1 % 10000 / 1000];
DisplayData[3] = smgduan[temp1 % 1000 / 100] | 0x80;
DisplayData[4] = smgduan[temp1 % 100 / 10];
DisplayData[5] = smgduan[temp1 % 10];
}
/*******************************************************************************
* 函數名 :DigDisplay()
* 函數功能 :數碼管顯示函數
* 輸入 : 無
* 輸出 : 無
*******************************************************************************/
void DigDisplay()
{
unsigned char i;
for(i=0;i<6;i++)
{
switch(i) //位選,選擇點亮的數碼管,
{
case(0):
LSA=0;LSB=0;LSC=0; break;//顯示第0位
case(1):
LSA=1;LSB=0;LSC=0; break;//顯示第1位
case(2):
LSA=0;LSB=1;LSC=0; break;//顯示第2位
case(3):
LSA=1;LSB=1;LSC=0; break;//顯示第3位
case(4):
LSA=0;LSB=0;LSC=1; break;//顯示第4位
case(5):
LSA=1;LSB=0;LSC=1; break;//顯示第5位
}
P0=DisplayData[5-i];//發送數據
//Delay1ms(100); //間隔一段時間掃描
//P0=0x00;//消隱
}
}
代碼是這樣的 下面的是DS18B20測溫 數碼管顯示 但是顯示的時候 只能從數碼管最后一位循環依次顯示前面的 不能顯示整個溫度
|
|