#include <mega16.h>
#include <1wire.h>
#include <alcd.h>
#include <stdio.h>
#include <delay.h>
#include <ds18b20.h>
#define MAX_DS1820 4
enum e_type{
menu,temp,set
}st;
unsigned char digit [10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0xff,0x6f};
unsigned char n1,n2,n3;
unsigned char ds1820_devices;
unsigned char rom_codes[MAX_DS1820][9];
unsigned char d[20],l,h;
float t1,t2,t3,t4;
float s,m;
void f_menu(void);
void f_temp(void);
void f_set(void);
void f_top(void);
void main(void)
{
// Declare your local variables here
// Input/Output Ports initialization
// Port A initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTA=0x00;
DDRA=0x00;
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=T State1=T State0=T
PORTB=0x00;
DDRB=0x00;
// Port C initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=T State2=P State1=P State0=P
PORTC=0x07;
DDRC=0xf0;
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=Out Func1=In Func0=Out
// State7=T State6=T State5=T State4=T State3=T State2=0 State1=T State0=0
PORTD=0x00;
DDRD=0x05;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=0xFF
// OC0 output: Disconnected
TCCR0=0x00;
TCNT0=0x00;
OCR0=0x00;
// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: Timer1 Stopped
// Mode: Normal top=0xFFFF
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 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: Timer2 Stopped
// Mode: Normal top=0xFF
// OC2 output: Disconnected
ASSR=0x00;
TCCR2=0x00;
TCNT2=0x00;
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;
// USART initialization
// USART disabled
UCSRB=0x00;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;
// ADC initialization
// ADC disabled
ADCSRA=0x00;
// SPI initialization
// SPI disabled
SPCR=0x00;
// TWI initialization
// TWI disabled
TWCR=0x00;
// 1 Wire Bus initialization
// 1 Wire Data port: PORTA
// 1 Wire Data bit: 0
// Note: 1 Wire port settings must be specified in the
// Project|Configure|C Compiler|Libraries|1 Wire IDE menu.
w1_init();
// Determine the number of DS1820 devices
// connected to the 1 Wire bus
ds1820_devices=w1_search(0xf0,rom_codes);
// Alphanumeric LCD initialization
// Connections specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTB Bit 1
// RD - PORTB Bit 2
// EN - PORTB Bit 3
// D4 - PORTB Bit 4
// D5 - PORTB Bit 5
// D6 - PORTB Bit 6
// D7 - PORTB Bit 7
// Characters/line: 16
lcd_init(16);
while (1)
{
f_top();
}
}
void f_menu(void){
lcd_gotoxy(0,0);
lcd_putsf("1.TEMP");
lcd_gotoxy(0,1);
lcd_putsf("2.SET");
if(PINC.0==0){
delay_ms(200);
while(PINC.0==0);
st=temp;
lcd_clear();
}
if(PINC.1==0){
delay_ms(200);
while(PINC.1==0);
st=set;
lcd_clear();
}
}
void f_temp(void){
t1=ds18b20_temperature(&rom_codes[0][0]);
t2=ds18b20_temperature(&rom_codes[1][0]);
t3=ds18b20_temperature(&rom_codes[2][0]);
t4=ds18b20_temperature(&rom_codes[3][0]);
s=t1+t2+t3+t4;
m=(s/4);
n1=(unsigned char)(m/100);
n2=(unsigned char)((m/10)-(n1*10));
n3=(unsigned char)(m-((n1*100)+(n2*10)));
PORTD=digit[n3];
PORTC.5=1;
PORTC.6=0;
PORTC.7=0;
delay_ms(5);
PORTD=digit[n2];
PORTC.5=0;
PORTC.6=1;
PORTC.7=0;
delay_ms(5);
PORTD=digit[n1];
PORTC.5=0;
PORTC.6=0;
PORTC.7=1;
delay_ms(5);
sprintf(d,"Temp=%2.2fc",m);
lcd_clear();
lcd_gotoxy(0,0);
lcd_puts(d);
sprintf(d,"L:%2.2d H:%2.2d",l,h);
lcd_gotoxy(0,1);
lcd_puts(d);
if(m>h){
PORTD.0=1;
PORTD.2=0;}
if((m>l) && (m<h)){
PORTD.0=0;
PORTD.2=0;}
if(m<l){
PORTD.0=0;
PORTD.2=1;}
if(PINC.2==0){
delay_ms(200);
while(PINC.2==0);
st=menu;
lcd_clear();}
}
void f_set(void){
sprintf(d,"L:%2.2d H:%2.2d",l,h);
lcd_gotoxy(0,0);
lcd_puts(d);
if(PINC.0==0){
delay_ms(100);
h++;
if(h>99)
h=0;
}
if(PINC.1==0){
delay_ms(100);
l++;
if(l>99)
l=0;
}
if(PINC.2==0){
delay_ms(200);
while(PINC.2==0);
st=menu;
lcd_clear();}
}
void f_top (void)
{
switch(st)
{
case menu:
f_menu();
break;
case temp:
f_temp();
break;
case set:
f_set();
}//switch
}