// Thermo.c
// 04/02/2005
#include <p18f452.h>
#include "Platform.h"

#include "Wait.h"
#include "Temperature.h"
#include "DS1822.h"
#include "DS1822OD.h"
#include "NokiaLCD.h"
#include "Sample.h"


void main (void);

#if 0
void InterruptHandlerHigh (void);
void TimerOneSecInit ();

void TimerOneSecInit () {
	// Init timer1 to fire every 1 sec
 	RCONbits.IPEN = 1; //enable high level inter
	T1CON = 0x0A; // 0-:2 ops, 00:1:1, 1:osc enabled, 0:sync ext clk, 1:ext clk, 0:stopped
	TMR1H = 0x80;
	TMR1L = 0x00;
	PIR1bits.TMR1IF = 0; // Clear overflow
	IPR1bits.TMR1IP = 1; // High priority
	PIE1bits.TMR1IE = 1; // Enables TMR1 overflow int
	INTCONbits.GIEH = 1; // Enable all high int
	T1CONbits.TMR1ON = 1; // Starts counting
}

#pragma code InterruptVectorHigh = 0x08
void
InterruptVectorHigh (void)
{
  _asm
    goto InterruptHandlerHigh //jump to interrupt routine
  _endasm
}

#pragma code
#pragma interrupt InterruptHandlerHigh

void InterruptHandlerHigh () {
	if (PIR1bits.TMR1IF) {
		TMR1H = 0x80; // 32768 cycles @32768 Hz => 1sec
		TMR1L = 0x00;
    	PIR1bits.TMR1IF = 0;  //clear interrupt flag
	}
}

void main () {
	TimerOneSecInit ();
	TRISCbits.TRISC7=0;

	while (1) {
	PORTCbits.RC7 = 0;
	Sleep ();
	PORTCbits.RC7 = 1;
	Sleep ();
	}
}
#endif






#if 1
void main () {
	UInt16 sec;
	UInt8 lsb, msb; // DS registers
	UInt8 sampleA, sampleB; // y coordinate for the samples
	UInt8 bcdvalue [6]; // to convert ds temperature in BCD
	UInt8 sign, tvalue, treste;
	  
	// Dallas DS1822 sensor init
    ADCON1=0x07; // DS1822 <-> RA Digital in/out
	DS1822_Init ((UInt8) DS_MODE_9BITS); 
	DS1822OD_Init ((UInt8) DS_MODE_9BITS); 
 
	 // Starts first conversions
	DS1822_StartConversion ();
	DS1822OD_StartConversion ();
	
	// Nokia LCD Init
	LcdInit ();
	LcdGotoXY (0, 0);
	LcdChr (FONT_1X, '#'); // 30
	LcdChr (FONT_1X, '&');
	LcdGotoXY (0, 1);
	LcdChr (FONT_1X, '$'); // 20
	LcdChr (FONT_1X, '&');
	LcdGotoXY (0, 2);
	LcdChr (FONT_1X, '%'); // 10
	LcdChr (FONT_1X, '&');
	LcdGotoXY (1, 3);
	LcdChr (FONT_1X, '&'); // 0
	LcdGotoXY (0, 4);
	LcdChr (FONT_1X, '\''); // -10
	LcdChr (FONT_1X, '&');
	LcdGotoXY (0, 5);
	LcdChr (FONT_1X, '!'); // Home
	LcdGotoXY (8, 5);
	LcdChr (FONT_1X, '\"'); // Tree
	LcdUpdate ();

	// Samples init
	InitSamples (); // Used to display samples
	sec = 0;

	while (1) {
		// We can read the first sample now. There is 230 ms (@7.2MHz)
		// between the start and the read. 
		// In this mode, only 100ms are necessary to finish the conversion
		Wait_1s();

  		// Read & display inside temperature
		DS1822_ReadTemperature (&lsb, &msb);
		DSToTemperature(lsb, msb, &sign, &tvalue, &treste);
		sampleA = TemperatureToSample (sign, tvalue, treste);
		TemperatureToBCD (sign, tvalue, treste, bcdvalue);
		
		LcdGotoXY (1, 5); // Inside temperature
		LcdStr ( FONT_1X, bcdvalue );

		// Read & display outside temperature
  		DS1822OD_ReadTemperature (&lsb, &msb);
		DSToTemperature(lsb, msb, &sign, &tvalue, &treste);
		sampleB = TemperatureToSample (sign, tvalue, treste);
		TemperatureToBCD (sign, tvalue, treste, bcdvalue);
		
		LcdGotoXY (9, 5); // Outside temperature
		LcdStr ( FONT_1X, bcdvalue );

		// Display new samples every 300 sec (5 minutes)
		if (sec == 0) {
			AddSample (sampleA, sampleB);
			DrawSamples ();
			sec = 300; // 1 sample every 5 minutes => 6 hours
		}
		sec--;
		LcdUpdate (); // Refresh LCD

		// Start a new conversion
		DS1822_StartConversion ();
		DS1822OD_StartConversion ();
	}
}
#endif

