Main.c

Fra Hougaard

Skift til: Navigation, Søgning
// Akvarie Computer
 
// PIC
#include <pic18.h>
#include <pic18f6x2x.h>
 
// Standard C
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
 
// TCP/IP
#define THIS_IS_STACK_APPLICATION
#include "stacktsk.h"
#include "tick.h"
#include "mac.h"
#include "dhcp.h"
 
// Akvarie
#include "lcd.h"
#include "delay.h"
#include "akvarie.h"
#include "menu.h"
#include "xeeprom.h"
 
/* this is the maximum value of an A2D conversion. */
#define MAXVOLTAGE 5
 
AkvarieInfo AI;
Automation  CA;
int					CurrentAutomationNo;
//Trigger 		CurrentTrigger;
//int					CurrentTriggerNo;
 
char ErrorMsg(char *Message,char No)
{
	char Text[20];
	char power;
	for (power = 0; power < 4; power++) // turn off everything
	 	SetPower(power,0);
 
	lcd_clear();
	lcdtext(1,1,"Error:");
	lcdtext(1,2,Message);
	sprintf(Text,"Code = %d",No);
	lcdtext(1,3,Text);
	lcdtext(1,4,"Press Enter..");
 
	while(1)
	{
		DelayMs(100);
		if (KeyEnter)
		{
			while(KeyEnter);
			return 0;
		}
	}
	return 0;
}
 
void FatalError(char i,char i2)
{
	char Text[20];
	char power;
	for (power = 0; power < 4; power++) // turn off everything
	 	SetPower(power,0);
 
	lcd_clear();
 
	// Module 2 = RTC
 
	lcdtext(1,1,"TankCommander halted");
	sprintf(Text,"Fatal Error %u %u",i,i2);
	lcdtext(1,2,Text);
	//while(1);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
}
 
void init(void)
{
	char	*Data = &AI;
	unsigned int   i;
 
	PORTF 		= 0;
	LATF 			= 0;
	ADCON1 		= 0x0F;
	CMCON 		= 0x07;
	lcd_init(FOURBIT_MODE);
	TRISA			= 0b11111111;	/* Some PORTA pins are analog input channels */
	ADON			= 1;	/* enable A2D converter */
	ADIE			=	0;	/* not interrupt driven */
 
	// Keyboard
	TRISB 		= 0b00011111;
	RBPU 			= 0;
 
	// outputswitch
	TRISC7 		= 0;
	PORTB 		= 0;
	RC7 			= 0;
 
  // I2C
  XEEInit(EE_BAUD(CLOCK_FREQ, 100000));
 
	// Setup from EEPROM (Internal in chip)	
	for (i = 0; i < sizeof(AkvarieInfo); i++)
	{
		*Data = eeprom_read(i);
		*Data++;
	}
 
	if (AI.MagicChar != MAGICCHAR)
	{
		// Default setup
		// -------------
		memset(&AI,0,sizeof(AkvarieInfo));
		for (i = 0; i < 4; i++)
  		strcpy(AI.ProbeSetup[i],"Off");
 
		strcpy(AI.PowerPanel,"Off");
		strcpy(AI.Input1,"Off");
		strcpy(AI.Input2,"Off");
 
		for (i = 0; i < 4; i++)
 			strcpy(AI.Power[i],"Off");
 
		lcdtext(1,4,"No saved setup");
	}
	CurrentAutomationNo = 0;
	if (ReadAutomation(CurrentAutomationNo))
	{
		if (CA.MagicChar != MAGICCHAR)
  		InitAutomation(CurrentAutomationNo);
	}
	else
		ErrorMsg("EEPROM Error",100);
}
 
void Idle()
{
	char buffer[20];
	char Probe = 0;
	char Print = 1;
 
	ReadRTC(); // Update clock
 
 	sprintf(buffer,"%02u:%02u:%02u %02u/%02u/%02u %d%d",AI.Clock[2],AI.Clock[1],AI.Clock[0],AI.Clock[4],AI.Clock[5],AI.Clock[6],RA4,RA5);
  lcdtext(1,Print,buffer);
  Print++;
 
  //sprintf(buffer,"%s %s %s %s  ",AI.Power[0],AI.Power[1],AI.Power[2],AI.Power[3]);
  //lcdtext(2,Print,buffer);
  //Print++;
 
  for (Probe = 0; Probe < 3; Probe++)
  {
	  if (GetProbeType(Probe) != PROBE_OFF)
	  {
	  	AI.Probe[Probe] = (ReadProbe(Probe) * AI.Probe_A[Probe]) + AI.Probe_B[Probe];
			sprintf(buffer,"%5s : %.2f %s  ",AI.ProbeSetup[Probe],AI.Probe[Probe],AI.ProbeSetup[Probe]);
			lcdtext(1,Print,buffer);
			Print++;
		}
	}
	PerformAutomation();
}
 
void main(void)
{
	int i;
	char *Data = &AI;
 
	init();
	lcdtext(1,1,"Crazy Ivan");
	lcdtext(1,2,"TankCommander 1.00");
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	DelayMs(250);
	lcd_clear();
 
	// TCP
    TickInit();
 
 
 
	for (;;)
	{
		// TCP/IP
        MACTask();
        StackTask();
 
 
		Idle();	
 
	  if (KeyEnter) // Menu knap
	  {
		  while(KeyEnter);
		  MenuHandler();
 
		  // Save Setup into EEPROM
		  {
			  char *Data = &AI;
			  AI.MagicChar = MAGICCHAR;
				for (i = 0; i < sizeof(AkvarieInfo); i++)
					eeprom_write(i,*Data++);
			}
		}
	}
}
Personlige værktøjer