après des recherches et des essais j' ai réussi a finaliser mon programmes.
A votre avis c'est correct, la lecture de la température sert 1 fois jusqu'au prochain redémarrage.
Code : Tout sélectionner
// Modifier pour un starter
// 3 lectures de la sonde, affichage pour le test.
// a inclure dans autre programme
/*
* Interfacing PIC16F887 microcontroller with DS18B20 temperature sensor.
* C Code for MPLAB XC8 compiler.
* Internal oscillator used @ 8MHz.---->( 20MHz)
* This is a free software with NO WARRANTY.
* https://simple-circuit.com/
*/
#define _XTAL_FREQ 20000000
#include "fuses.h"
#include <xc.h>
#include <stdint.h> // include stdint header
// DS18B20 data pin is connected to pin RB1
#define DS18B20_PIN RB1
#define DS18B20_PIN_Dir TRISB1
//------------------------------------------------------------------------------
// a supprimer si OK
extern char UART_Init(const long int baudrate);
extern void UART_send_string(char* st_pt);
extern void UART_send_char(char bt);
//------------------------------------------------------------------------------
uint16_t raw_temp;
char temp[] = "000.0000 C";
__bit ds18b20_start()
{
DS18B20_PIN = 0; // send reset pulse to the DS18B20 sensor
DS18B20_PIN_Dir = 0; // configure DS18B20_PIN pin as output
__delay_us(500); // wait 500 us
DS18B20_PIN_Dir = 1; // configure DS18B20_PIN pin as input
__delay_us(100); // wait 100 us to read the DS18B20 sensor response
if (!DS18B20_PIN)
{
__delay_us(400); // wait 400 us
return 1; // DS18B20 sensor is present
}
return 0; // connection error
}
void ds18b20_write_bit(uint8_t value)
{
DS18B20_PIN = 0;
DS18B20_PIN_Dir = 0; // configure DS18B20_PIN pin as output
__delay_us(2); // wait 2 us
DS18B20_PIN = (__bit)value;
__delay_us(80); // wait 80 us
DS18B20_PIN_Dir = 1; // configure DS18B20_PIN pin as input
__delay_us(2); // wait 2 us
}
void ds18b20_write_byte(uint8_t value)
{
for(uint8_t i = 0; i < 8; i++)
ds18b20_write_bit(value >> i);
}
__bit ds18b20_read_bit(void)
{
static __bit value;
DS18B20_PIN = 0;
DS18B20_PIN_Dir = 0; // configure DS18B20_PIN pin as output
__delay_us(2);
DS18B20_PIN_Dir = 1; // configure DS18B20_PIN pin as input
__delay_us(5); // wait 5 us
value = DS18B20_PIN; // read and store DS18B20 state
__delay_us(100); // wait 100 us
return value;
}
uint8_t ds18b20_read_byte(void)
{
uint8_t value = 0;
for(uint8_t i = 0; i < 8; i++)
value |= ds18b20_read_bit() << i;
return value;
}
__bit ds18b20_read(uint16_t *raw_temp_value)
{
if (!ds18b20_start()) // send start pulse
return 0; // return 0 if error
ds18b20_write_byte(0xCC); // send skip ROM command
ds18b20_write_byte(0x44); // send start conversion command
while(ds18b20_read_byte() == 0); // wait for conversion complete
if (!ds18b20_start()) // send start pulse
return 0; // return 0 if error
ds18b20_write_byte(0xCC); // send skip ROM command
ds18b20_write_byte(0xBE); // send read command
// read temperature LSB byte and store it on raw_temp_value LSB byte
*raw_temp_value = ds18b20_read_byte();
// read temperature MSB byte and store it on raw_temp_value MSB byte
*raw_temp_value |= (uint16_t)(ds18b20_read_byte() << 8);
return 1; // OK --> return 1
}
/*************************** main function *********************/
// Rajour autre programme
void main(void)
{
OSCCON = 0b01111000; // HS oscillateur
ANSEL = 0; // I/O numériques
ANSELH = 0; // configure all PORTB pins as digital
//-----------------------------------------------------------------------------
UART_Init (9600); // Initialise le module UART à 9600bps
__delay_ms(100); // 100ms
//------------------------------------------------------------------------------
int i;
for (i = 0; i < 3; i++) // Faire 3 meusures
{
temp[8] = 223; // put degree symbol ( ° )
//------------------------------------------------------------------------------
if(ds18b20_read(&raw_temp))
{
if(raw_temp & 0x8000) // if the temperature is negative
{
temp[0] = '-'; // put minus sign (-)
raw_temp = (~raw_temp) + 1; // change temperature value to positive form
}
else
{
if((raw_temp >> 4) >= 100) // if the temperature >= 100 °C
temp[0] = '1'; // put 1 of hundreds
else // otherwise
temp[0] = ' '; // put space ' '
}
// put the first two digits ( for tens and ones)
temp[1] = ( (raw_temp >> 4) / 10 ) % 10 + '0'; // put tens digit
temp[2] = (raw_temp >> 4) % 10 + '0'; // put ones digit
// put the 4 fraction digits (digits after the point)
// why 625? because we're working with 12-bit resolution (default resolution)
temp[4] = ( (raw_temp & 0x0F) * 625) / 1000 + '0'; // put thousands digit
temp[5] = (((raw_temp & 0x0F) * 625) / 100 ) % 10 + '0'; // put hundreds digit
temp[6] = (((raw_temp & 0x0F) * 625) / 10 ) % 10 + '0'; // put tens digit
temp[7] = ( (raw_temp & 0x0F) * 625) % 10 + '0'; // put ones digit
}
else
{
UART_send_string((char*)" Error! "); // Affichage
}
}
//------------------------------------------------------------------------------
while(1) // ne doit pas etre bloquante
{
UART_send_string (temp);
UART_send_char(13);
}
}
Code : Tout sélectionner
#define _XTAL_FREQ 20000000
#include <xc.h>
char UART_Init(const long int baudrate)
{
unsigned int x;
x = (_XTAL_FREQ - baudrate*64)/(baudrate*64);
if(x>255)
{
x = (_XTAL_FREQ - baudrate*16)/(baudrate*16);
BRGH = 1;
}
if(x<256)
{
SPBRG = x;
SYNC = 0;
SPEN = 1;
TRISC7 = 1;
TRISC6 = 1;
CREN = 1;
TXEN = 1;
return 1;
}
return 0;
}
char UART_get_char()
{
if(OERR) // check for Error
{
CREN = 0; //If error -> Reset
CREN = 1; //If error -> Reset
}
while(!RCIF); // hold the program till RX buffer is free
return RCREG; //receive the value and send it to main function
}
void UART_send_char(char bt)
{
while(!TXIF); // hold the program till TX buffer is free
TXREG = bt; //Load the transmitter buffer with the received value
}
//**Fonction convertion string en byte**//
void UART_send_string(char* st_pt)
{
while(*st_pt) // s'il y a un char
UART_send_char(*st_pt++); // le traiter comme un octet de données
}
Code : Tout sélectionner
#define _XTAL_FREQ 20000000
#include <xc.h>
#include <stdlib.h>
#include <ctype.h>
// CONFIG1
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator: High-speed crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
//------------------------------------------------------------------------------
Cdt.
Marcus.