Giao tiếp ds18b20 với pic16f877a hiển thị ra led 7 đoạn
Nhờ các cao thủ chỉ giáo cho, không biết code sai ở đoạn nào. Khi mô phỏng thì led không có hiện tượng gì.
//=========================================
#include <16f877a.h>
#include <def_877a.h>
#device *=16 ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, NOBROWNOUT, NOLVP,NOCPD, NOWRT
#use delay(clock=8m)
#use rs232(baud=9600,parity=N,xmit=PIN_B5,rcv=PIN_B2,bi ts=9)
#use i2c(Master,Fast,sda=PIN_B1,scl=PIN_B4)
#define DS18B20_PIN RA4
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCH_PAD 0xBE
#define led1 RB1
#define led2 RB2
#define led3 RB3
//------------------------------------------------------------------------------------
void DS18B20_RESET()
{
output_low(DS18B20_PIN);
delay_us(500);
output_float(DS18B20_PIN);
delay_us(500);
output_float(DS18B20_PIN);
}
//------------------------------------------------------------------------------------
void DS18B20_write_byte(unsigned char data)
{
unsigned char i = 8;
while(i--)
{
output_low(DS18B20_PIN);
delay_us(2);
output_bit(DS18B20_PIN, shift_right(&data, 1, 0));
delay_us( 60 );
output_float(DS18B20_PIN);
delay_us( 2 );
}
}
//------------------------------------------------------------------------------------
FLOAT DS18B20_READ_BYTE()
{
unsigned char data, i=8;
while(i--)
{
output_low(DS18B20_PIN);
delay_us(2);
output_float(DS18B20_PIN);
delay_us(8);
shift_right(&data, 1, input(DS18B20_PIN));
delay_us(120);
}
return data;
}
//------------------------------------------------------------------------------------
float ds18b20_read()
{
int8 busy, temp1, temp2;
int16 temp3;
float result;
while(1)
{
DS18B20_RESET();
DS18B20_write_byte(0xCC) ;
DS18B20_write_byte(0x44);
while(busy==0)
busy=DS18B20_READ_BYTE();
DS18B20_RESET();
DS18B20_write_byte(0xCC) ;
DS18B20_write_byte(0xBE);
temp1=DS18B20_READ_BYTE();
temp2=DS18B20_READ_BYTE();
temp3=make16(temp2,temp1);
delay_ms(200);
}
return temp3;
}
//------------------------------------------------------------------------------------
void main()
{
int16 nhiet_do;
unsigned char int_part, decimal_part, HC, HDV;
unsigned char ma7doan[10]={0xc0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x9 0};
while(1)
{
nhiet_do=ds18b20_read();
int_part=nhiet_do>>4;
HC=int_part/10;
HDV=nhiet_do%10;
decimal_part=(nhiet_do&0x000f);
decimal_part=decimal_part*10/16;
output_high(RB1);
output_C(ma7doan[HC]);
delay_ms(2);
output_low(RB1);
output_high(RB2);
output_C(ma7doan[HDV]);
delay_ms(2);
output_low(RB2);
output_high(RB3);
output_C(ma7doan[decimal_part]);
delay_ms(2);
output_low(RB3);
}
}
|