123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127 |
- /*
- * ds18b20.c
- *
- * Copyright 2022 dh33ex <dh33ex@riseup.net>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 3 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
- * MA 02110-1301, USA or see <http://www.gnu.org/licenses/>.
- *
- *
- */
- #ifndef __msp430_h_
- #include <msp430.h>
- #endif
- #include "ds18b20.h"
- void reset_18B20(unsigned char pin) {
- P1DIR |= pin;
- P1OUT &= ~pin;
- __delay_cycles(500);
- P1OUT |= pin;
- P1DIR &= ~pin;
- __delay_cycles(500);
- }
- void send_18B20(unsigned char pin, char data) {
- char i;
- for (i = 8; i > 0; i--) {
- P1DIR |= pin;
- P1OUT &= ~pin;
- __delay_cycles(2);
- if (data & 0x01){
- P1OUT |= pin;
- }
- __delay_cycles(60);
- P1OUT |= pin;
- P1DIR &= ~pin;
- data >>= 1;
- }
- }
- unsigned int read_18B20(unsigned char pin) {
- unsigned int data=0;
- char i;
- /* read only first 2 bytes (temperature) */
- for (i = 16; i > 0; i--) {
- P1DIR |= pin;
- P1OUT &= ~pin;
- __delay_cycles(2);
- P1OUT |= pin;
- P1DIR &= ~pin;
- __delay_cycles(8);
- if (P1IN & pin) {
- data |= 0x8000;
- }
- data >>= 1;
- __delay_cycles(120);
- }
- return(data);
- }
- void set_resolution(unsigned char pin, unsigned char resolution) {
- unsigned char package;
- switch (resolution) {
- case 9:
- package = 0b00011111;
- break;
- case 10:
- package = 0b00111111;
- break;
- case 11:
- package = 0b01011111;
- break;
- case 12:
- package = 0b01111111;
- break;
- default:
- package = 0b00011111; /* wrong resolution argument, set default */
- break;
- }
- reset_18B20(pin);
- send_18B20(pin, 0xcc); /* send skip ROM command */
- send_18B20(pin, 0x4e); /* send write scratchpad command */
- send_18B20(pin, 0x00); /* send TH byte (unused) */
- send_18B20(pin, 0x00); /* send TL byte (unused) */
- send_18B20(pin, package); /* send configure byte */
- reset_18B20(pin);
- __delay_cycles(120);
- }
- float get_temp(unsigned char pin) {
- unsigned int temp;
- reset_18B20(pin);
- send_18B20(pin, 0xcc); /* send skip ROM command */
- send_18B20(pin, 0x44); /* send convert temperature command */
- __delay_cycles(1000000);
- reset_18B20(pin);
- send_18B20(pin, 0xcc); /* send skip ROM command */
- send_18B20(pin, 0xbe); /* send read scratchpad command */
- temp = read_18B20(pin); /* read raw data */
- return((float) temp / 8.0); /* calculate temperature */
- }
|