📄 ds18b20.i
字号:
/* Multipoint thermometer with LCD display
using the Maxim DS18B20
1 Wire bus temperature sensors
CodeVisionAVR C Compiler
(C) 2000-2005 HP InfoTech S.R.L.
www.hpinfotech.ro
Chip: ATmega8515
Memory Model: SMALL
Data Stack Size: 128 bytes
PLEASE MAKE SURE THAT THE CKSEL0..3 FUSE
BITS ARE PROGRAMMED TO USE THE EXTERNAL
3.6864MHz CLOCK SOURCE OF THE STK500 AND NOT
THE INTERNAL 1MHz OSCILLATOR.
The ATmega8515 chip comes from the factory
with CKSEL0..3 fuse bits set to use the
internal 1 MHz oscillator.
The DS18B20 sensors are connected to
bit 6 of PORTA of the ATmega8515 as follows:
[DS18B20] [STK500 PORTA HEADER]
1 GND - 9 GND
2 DQ - 7 PA6
3 VDD - 10 +5V
All the temperature sensors must be connected
in parallel
AN 4.7k PULLUP RESISTOR MUST BE CONNECTED
BETWEEN DQ (PA6) AND +5V !
*/
#asm
.equ __w1_port=0x1b
.equ __w1_bit=6
#endasm
/* Use an 2x16 alphanumeric LCD connected
to PORTC as follows:
[LCD] [STK500 PORTC HEADER]
1 GND- 9 GND
2 +5V- 10 VCC
3 VLC- LCD contrast control voltage 0..1V
4 RS - 1 PC0
5 RD - 2 PC1
6 EN - 3 PC2
11 D4 - 5 PC4
12 D5 - 6 PC5
13 D6 - 7 PC6
14 D7 - 8 PC7
*/
#asm
.equ __lcd_port=0x15
#endasm
/* LCD driver routines
CodeVisionAVR C Compiler
(C) 1998-2003 Pavel Haiduc, HP InfoTech S.R.L.
BEFORE #include -ING THIS FILE YOU
MUST DECLARE THE I/O ADDRESS OF THE
DATA REGISTER OF THE PORT AT WHICH
THE LCD IS CONNECTED!
EXAMPLE FOR PORTB:
#asm
.equ __lcd_port=0x18
#endasm
#include <lcd.h>
*/
#pragma used+
void _lcd_ready(void);
void _lcd_write_data(unsigned char data);
// write a byte to the LCD character generator or display RAM
void lcd_write_byte(unsigned char addr, unsigned char data);
// read a byte from the LCD character generator or display RAM
unsigned char lcd_read_byte(unsigned char addr);
// set the LCD display position x=0..39 y=0..3
void lcd_gotoxy(unsigned char x, unsigned char y);
// clear the LCD
void lcd_clear(void);
void lcd_putchar(char c);
// write the string str located in SRAM to the LCD
void lcd_puts(char *str);
// write the string str located in FLASH to the LCD
void lcd_putsf(char flash *str);
// initialize the LCD controller
unsigned char lcd_init(unsigned char lcd_columns);
#pragma used-
#pragma library lcd.lib
/*
CodeVisionAVR C Compiler
(C) 1998-2005 Pavel Haiduc, HP InfoTech S.R.L.
Prototypes for Dallas Semiconductor
DS18B20 1 Wire bus temperature sensor
functions
BEFORE #include -ING THIS FILE YOU
MUST DECLARE THE I/O ADDRESS OF THE
DATA REGISTER OF THE PORT AT WHICH
THE 1 WIRE BUS IS CONNECTED AND
THE DATA BIT USED
EXAMPLE FOR PORTB:
#asm
.equ __w1_port=0x18
.equ __w1_bit=3
#endasm
#include <ds1820.h>
*/
/*
CodeVisionAVR C Compiler
(C) 1998-2000 Pavel Haiduc, HP InfoTech S.R.L.
Prototypes for Dallas Semiconductor
1 Wire protocol functions
BEFORE #include -ING THIS FILE YOU
MUST DECLARE THE I/O ADDRESS OF THE
DATA REGISTER OF THE PORT AT WHICH
THE 1 WIRE BUS IS CONNECTED AND
THE DATA BIT USED
EXAMPLE FOR PORTB:
#asm
.equ __w1_port=0x18
.equ __w1_bit=3
#endasm
#include <1wire.h>
*/
#pragma used+
unsigned char w1_init(void);
unsigned char w1_read(void);
unsigned char w1_write(unsigned char data);
unsigned char w1_search(unsigned char cmd,void *p);
unsigned char w1_dow_crc8(void *p,unsigned char n);
#pragma used-
#pragma used+
extern struct __ds18b20_scratch_pad_struct
{
unsigned char temp_lsb,temp_msb,
temp_high,temp_low,
conf_register,
res1,
res2,
res3,
crc;
} __ds18b20_scratch_pad;
unsigned char ds18b20_select(unsigned char *addr);
unsigned char ds18b20_read_spd(unsigned char *addr);
float ds18b20_temperature(unsigned char *addr);
unsigned char ds18b20_init(unsigned char *addr,signed char temp_low,signed char temp_high,
unsigned char resolution);
#pragma used-
#pragma library ds18b20.lib
// CodeVisionAVR C Compiler
// (C) 1998-2000 Pavel Haiduc, HP InfoTech S.R.L.
#pragma used+
void delay_us(unsigned int n);
void delay_ms(unsigned int n);
#pragma used-
// CodeVisionAVR C Compiler
// (C) 1998-2006 Pavel Haiduc, HP InfoTech S.R.L.
// Prototypes for standard I/O functions
// CodeVisionAVR C Compiler
// (C) 1998-2002 Pavel Haiduc, HP InfoTech S.R.L.
// Variable length argument list macros
typedef char *va_list;
#pragma used+
char getchar(void);
void putchar(char c);
void puts(char *str);
void putsf(char flash *str);
char *gets(char *str,unsigned int len);
void printf(char flash *fmtstr,...);
void sprintf(char *str, char flash *fmtstr,...);
void snprintf(char *str, unsigned int size, char flash *fmtstr,...);
void vprintf (char flash * fmtstr, va_list argptr);
void vsprintf (char *str, char flash * fmtstr, va_list argptr);
void vsnprintf (char *str, unsigned int size, char flash * fmtstr, va_list argptr);
signed char scanf(char flash *fmtstr,...);
signed char sscanf(char *str, char flash *fmtstr,...);
#pragma used-
#pragma library stdio.lib
char lcd_buffer[33];
/* maximum number of DS18B20 connected to the 1 Wire bus */
/* DS18B20 devices ROM code storage area */
unsigned char rom_code[8][9];
main()
{
unsigned char i,j,devices;
float tmp;
lcd_init(16);
lcd_putsf("CodeVisionAVR\n1 Wire Bus Demo");
delay_ms(200);
lcd_clear();
/* detect how many DS18B20 devices
are connected to the 1 Wire bus */
devices=w1_search(0xf0,rom_code);
sprintf(lcd_buffer,"%u DS18B20\nDevice detected",devices);
lcd_puts(lcd_buffer);
delay_ms(800);
/* display the ROM codes for each device */
if (devices)
{
for (i=0;i<devices;i++)
{
sprintf(lcd_buffer,"Device #%u ROM\nCode is:",i+1);
lcd_clear();
lcd_puts(lcd_buffer);
delay_ms(800);
lcd_clear();
for (j=0;j<8;j++)
{
sprintf(lcd_buffer,"%02X ",rom_code[i][j]);
lcd_puts(lcd_buffer);
if (j==3) lcd_gotoxy(0,1);
};
delay_ms(800);
};
}
else
while (1); /* stop here if no devices were found */
/* configure each DS18B20 device for 12 bit temperature
measurement resolution */
for (i=0;i<devices;)
if (!ds18b20_init(&rom_code[i++][0],20,30,3 ))
{
sprintf(lcd_buffer,"Init error for\ndevice #%u",i);
lcd_clear();
lcd_puts(lcd_buffer);
while (1); /* stop here if init error */
};
/* measure and display the temperature(s) */
while (1)
{
//j=1;
for (i=0;i<devices;i++)
{
tmp=ds18b20_temperature(&rom_code[i][0]);
if(tmp>125)
sprintf(lcd_buffer,"t%u=%+.3f\xdfC",i+1,tmp-4096);
else
sprintf(lcd_buffer,"t%u=%+.3f\xdfC",i+1,tmp);
lcd_clear();
lcd_puts(lcd_buffer);
delay_ms(1000);
};
};
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -