#include "1602.h"
#include "18b20.h"
#include "main.h"
__CONFIG(0X0B31);
main()
{
char msg[2][16] = {" Temperature: "," .C"};
delay_us2 (40, 40);
delay_us2 (100, 200);
initial();
while(1) {
start_convert();
change_to_ascii (get_temp (), &msg[1][3]);
display2(&msg[0][0],&msg[1][0]);
}
}
//------------------------us级延时----------------------------//
void delay_us2 (char x, char y)
{
char z;
do { z=y;
do {;} while(--z);
} while(--x);
}
//------------------------1秒延时--------------------------------//
void delay_1s(unsigned int x)
{
unsigned a,b;
for(a=x;a>0;a--)
for(b=110;b>0;b--);
}
//-----------------------------------------------------------------------//
void delay_us1 (char d)
{
while(--d) ;
}
#include "18b20.h"
#include "main.h"
//--------------产生复位脉冲--------------------//
void tx_reset()
{
ADCON1=0X06;
TRISA5=0;
DQ=1;
delay_us2(10,5); //65us
while(!DQ); //判断总线是否忙
DQ=0;
delay_us2(10,23); //730us
}
//----------------等待应答脉冲----------------//
void tx_wait ()
{
TRISA5=1; //设为输入方式后,DQ被外部上拉电阻自动拉高
asm("nop");
while (DQ);
while (!DQ);
delay_us2(10,13); //430us
}
//-------------------读一个字节---------------------//
char read_byte()
{
char byte,count;
for (count=8;count>0;count--)
{
byte = byte>>1;
TRISA5=0;
DQ=0;
asm("nop");
asm("nop");
TRISA5=1;asm("nop");
if(DQ)
byte=byte|0x80;
else
byte =byte|0x00;
delay_us2(3,5); //65us
}
return byte;
}
//-------------------写一个字节---------------------//
void write_byte (char btmp)
{
char i;
TRISA5=0;
for (i=8;i>0;i--)
{
DQ=0;
asm("nop");
asm("nop");
if (btmp&0x01) {
DQ=1;
delay_us2(3,6); //65us
}
else{
DQ=0;
delay_us2(3,6); //65us
}
DQ=1;
btmp=btmp>>1;delay_us2(3,2);
}
}
//---------------------启动温度转换---------------------//
void start_convert()
{
tx_reset();
tx_wait();
write_byte(0xCC);
write_byte(0x44);
delay_1s(1000);
}
//----------读取暂存寄存器中的温度值-----------//
int get_temp ()
{
char high,low;
volatile int temp;
tx_reset();
tx_wait();
write_byte(0xcc);
write_byte(0xbe);
low=read_byte();
high=read_byte();
temp=high;
temp=temp<<8;
temp=temp|low;
return temp;
}
//-----------------温度值转换成ASCII码---------------//
void change_to_ascii (int data, char *ascii)
{
char left,right;
int middle;
left = data >> 4;
right = data & 0x000f;
middle = 10000 * 0.0625 * right;
ascii[0] = left&0x80? ’-’: ’+’; //判断温度最高位符号
left = left&0x80? ~left&0x7f:left;
ascii[1] = left/100 + 48;
ascii[2] = left%100/10 +48;
ascii[3] = left%10 + 48;
ascii[4] = ’.’;
本文关键字:单片机 PIC单片机,单片机-工控设备 - PIC单片机
上一篇:PIC16F72定时器C程序