/*
Рабочая програмка для ATmega16 и 2 датчиков 18В20
для использования переменных float в настройках проекта изменить:
в закладке Code Generation - (s)printf Features int на float.
Ещё:
lcd_gotoxy(0,1); 0- позиция символа, а 1 - 2-я строка
\n - перевод на новую строку всего что после этой команды.
Chip type : ATmega16
Program type : Application
Clock frequency : 8,000000 MHz
Memory model : Small
External SRAM size : 0
Data Stack size : 256
*****************************************************/
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
// 1 Wire Bus functions
#asm
.equ __w1_port=0x1B ;PORTA
.equ __w1_bit=0
#endasm
#include <1wire.h>
// DS1820 Temperature Sensor functions
#include <ds18B20.h>
#define MAX_DEVICES 9 // до 3-х устройств на линии
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x12 ;PORTD
#endasm
#include <lcd.h>
// Declare your global variables here
unsigned char lcd_buffer[33];
unsigned char rom_code[MAX_DEVICES][9];
int devices;
float t1, t2;
void main(void)
{
// Declare your local variables here
PORTA=0x00;
DDRA=0x00;
PORTB=0x00;
DDRB=0x00;
PORTC=0x00;
DDRC=0x00;
PORTD=0x00;
DDRD=0x00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer 1 Stopped
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: Off
// Compare B Match Interrupt: Off
TCCR1A=0x00;
TCCR1B=0x00;
TCNT1H=0x00;
TCNT1L=0x00;
ICR1H=0x00;
ICR1L=0x00;
OCR1AH=0x00;
OCR1AL=0x00;
OCR1BH=0x00;
OCR1BL=0x00;
// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=34285;
OCR2=0x00;
// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
// INT2: Off
MCUCR=0x00;
MCUCSR=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x00;
ACSR=0x80;
SFIOR=0x00;
OCR2=31250;
TCCR2=0x40; //инициализация таймера счётчика
OCR2=5650;
// 1 Wire Bus initialization
w1_init();
// LCD module initialization
lcd_init(16);
lcd_putsf("Tepmometer ver.1");
delay_ms(400);
lcd_clear();
/* DS18B20 devices ROM code storage area */
devices=w1_search(0xf0,rom_code);
sprintf(lcd_buffer,"Devices - %u\nType-DS18B20",devices);
lcd_puts(lcd_buffer);
delay_ms(400);
lcd_clear();
//================================================================================= ===
while (1)
{
t1=ds18b20_temperature(&rom_code[0/*nomer ustroystva*/][0]);
t2=ds18b20_temperature(&rom_code[1/*nomer ustroystva*/][0]); // от 2-го датчика температуры
lcd_clear();
lcd_gotoxy(0,0);
sprintf(lcd_buffer,"T1 = %+4.1f \xEFC\nT2 = %+4.1f \xEFC",t1,t2); // + означает всегда показывать знак
lcd_puts(lcd_buffer);
delay_ms(200);
};
}
do {
#asm("wdr")
delay_ms(200);
}
while (!ds18b20_init(0,20,30,DS18B20_12BIT_RES));
/*
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>
*/
#ifndef _W1_INCLUDED_
#define _W1_INCLUDED_
#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-
#endif
/*
CodeVisionAVR C Compiler
(C) 1998-2009 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 <ds18b20.h>
*/
#ifndef _DS18B20_INCLUDED_
#define _DS18B20_INCLUDED_
#include <1wire.h>
#define DS18B20_9BIT_RES 0 // 9 bit thermometer resolution
#define DS18B20_10BIT_RES 1 // 10 bit thermometer resolution
#define DS18B20_11BIT_RES 2 // 11 bit thermometer resolution
#define DS18B20_12BIT_RES 3 // 12 bit thermometer resolution
#define DS18B20_FAMILY_CODE 0x28
#define DS18B20_SEARCH_ROM_CMD 0xf0
#define DS18B20_ALARM_SEARCH_CMD 0xec
#pragma used+
extern struct __ds18b20_scratch_pad_struct
{
unsigned char temp_lsb,temp_msb;
signed char temp_high,temp_low;
unsigned char 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
#endif
Если Вы уже зарегистрированы на Портале - войдите в систему, если Вы еще не регистрировались - пройдите простую процедуру регистрации.