//----------------------------------------------------------------------------
//
//	SPKOSCTBG.h: Control de Base de Tiempos para Osciloscopio
//	
//	Start:				07/02/2009
//	Last Modification:	07/02/2009
//	Comercial Version:	none
//
//	By: Fabian Traczuk
//	Chivilcoy - Buenos Aires - Argentina
//
//	Hardware Model		(FINAL)
//	Hardware Version	1.0
//	Hardware Revision	1
//	Firmware Version	1.0
//	Firmware Revision	1 
//
//	Code for Atmel AVR microcontrollers (ATMEGA16)
//
//----------------------------------------------------------------------------
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include <avr\io.h>
#include <avr\pgmspace.h>
#include <util\delay.h>
#include <avr\interrupt.h>
#include <avr\eeprom.h>

//Version
#define MAYOR_FIRMWARE_VERSION 1
#define MINOR_FIRMWARE_VERSION 0
#define REV_FIRMWARE_VERSION 0
#define presentacion  "\n===============================================================\nSimpletek Electronica\nSPK01\nControl de desplazamiento para balancines\n\n(c)Fabian Traczuk\n2007 - Chivilcoy - Buenos Aires - Argentina\n\nVersion 1.0\nRevision 1\n===============================================================\n"
const char Simpletek[] PROGMEM = {"Simpletek.com.ar"};

//Crystal Frequency and BaudRate
#ifndef FOSC
	#define FOSC 1000000UL
#endif

//---------------------------------------------------------------------
//Macros
#define CHECKBIT(ADDRESS,BIT) (ADDRESS & (1<<BIT))


//RELAYS Control-----------------------------------------
#define RELAY_1_PORT	PORTB
#define RELAY_1_CONF	DDRB
#define RELAY_2_PORT	PORTC
#define RELAY_2_CONF	DDRC
#define RELAY_3_PORT	PORTD
#define RELAY_3_CONF	DDRD

#define TIMER_SAMPLE	TIMER1_OVF_vect
#define ADC_COMPLETE	ADC_vect
#define ANALOG_INPUT	PA0

#define TEST_LED		PB0

//Baud Rate Values for 8MHZ Int. Osc.
#define BAUD4800	0x0C
#define BAUD9600	0x0C
#define BAUD19200	0x19
#define BAUD38400	0x0C
#define BAUD57600	0x08
#define BAUD115200	0x03

//MACROS-----------------------------------------
#define CRLF()  putchar(13); putchar(10)
#define M_ENABLE_TIMER() (TIMSK |= (1<<TOIE1))
#define M_DISABLE_TIMER() (TIMSK &= ~(1<<TOIE1))
#define M_ENABLE_ADC() (ADCSRA |= (1<<ADEN))
#define M_DISABLE_ADC() (ADCSRA &= ~(1<<ADEN))
#define M_START_ADC() (ADCSRA |= (1<<ADSC))

enum {YES, NO};

//Reload timer value. default 0.5s with prescaler 256 8MHZ CPU CLK
volatile unsigned int	TIMER_1_RELOAD = 0xFD5E;	//0xF85E 1MHZ; 0xE424 8MHZ; 0xF0BD 2MHZ
volatile unsigned char PreviousSwitchPos = 0x00;

//--------------------------------------------------------
#define TOP_SWITCH 0x16

typedef struct ADC_SWITCH_FIELDS{
	unsigned char Position;
	unsigned char hValue;
	unsigned char lValueMin;
	unsigned char lValue;
	unsigned char lValueMax;
	unsigned char PortBValue;
	unsigned char PortCValue;
	unsigned char PortDValue;
} ADC_SWITCH_FIELDS;

const struct ADC_SWITCH_FIELDS AdcSwitchPosition[23] PROGMEM = {
	{1,  0x00, 0x00, 0x0B, 0x25, 0x40, 0x40, 0x90},
	{2,  0x00, 0x2A, 0x3A, 0x4E, 0x20, 0x20, 0x90},
	{3,  0x00, 0x55, 0x69, 0x7F, 0x10, 0x10, 0x90},
	{4,  0x00, 0x8A, 0x98, 0xA8, 0x08, 0x08, 0x90},
	{5,  0x00, 0xB0, 0xC5, 0xDA, 0x04, 0x04, 0x90},
	{6,  0x00, 0xE0, 0xF7, 0xFF, 0x80, 0x80, 0x48},
	{7,  0x01, 0x12, 0x21, 0x39, 0x40, 0x40, 0x48},
	{8,  0x01, 0x40, 0x50, 0x63, 0x20, 0x20, 0x48},
	{9,  0x01, 0x6C, 0x7C, 0x94, 0x10, 0x10, 0x48},
	{10, 0x01, 0x9C, 0xA9, 0xBC, 0x08, 0x08, 0x48},
	{11, 0x01, 0xC5, 0xD7, 0xED, 0x04, 0x04, 0x48},
	{12, 0x02, 0x00, 0x05, 0x1E, 0x80, 0x80, 0x24},
	{13, 0x02, 0x24, 0x32, 0x48, 0x40, 0x40, 0x24},	//*
	{14, 0x02, 0x4A, 0x5E, 0x6F, 0x20, 0x20, 0x24},	//*
	{15, 0x02, 0x75, 0x89, 0x9A, 0x10, 0x10, 0x24},	//*
	{16, 0x02, 0xA3, 0xB5, 0xC7, 0x08, 0x08, 0x24},
	{17, 0x02, 0xD8, 0xE0, 0xFA, 0x04, 0x04, 0x24},
	{18, 0x03, 0x00, 0x0D, 0x1E, 0x80, 0x80, 0x00},
	{19, 0x03, 0x20, 0x3A, 0x4B, 0x40, 0x40, 0x00},
	{20, 0x03, 0x50, 0x68, 0x7A, 0x20, 0x20, 0x00},
	{21, 0x03, 0x80, 0x96, 0xAF, 0x10, 0x10, 0x00},
	{22, 0x03, 0xB0, 0xC5, 0xD2, 0x08, 0x08, 0x00},
	{23, 0x03, 0xE9, 0xF1, 0xFF, 0x04, 0x04, 0x00},
};

//----------------------------------------------------------------------------
//EEPROM Parameters


//----------------------------------------------------------------------------
//	Control Functions
void StartUp(void);
void init_uart(void);
void SendData(unsigned char Value);
void ADC_Init(void);
void LookUpSwichtTable(unsigned char hValue, unsigned char ADCValue);
