//;****************************************************************
//;*  Name    : duropwm.c
//;*  Author  : David Vilmos
//;*  Notice  : Copyright (c) 2008 DV Electronics
//;*          : All Rights Reserved
//;*  Date    : 29/06/2008
//;*  Version : 1.0
//;*  Notes   : PWM Temperature Controller for 12V 
//;*		  : Resistive Heating Element white 
//;*		  : Potentiometer & Botton Switch
//;*  Processor : 12F683A      INT OSC
//;*  Compiler  : CCS C
//;*****************************************************************/

#include <12F683.h>
#fuses  INTRC_IO,NOWDT,NOPUT,NOMCLR,PROTECT,NOCPD,NOBROWNOUT,NOIESO,NOFCMEN
#DEVICE ADC=8  
#use delay(clock=4000000)

#BYTE RTCC = 0x01
#BYTE GPIO = 5 
#BYTE WPU = 0x95  
#BYTE IOC = 0x96      
#bit SW_Start = GPIO.1

#rom 0x2100={2,235} 
#rom 0x2110={20} 
#rom 0x2170={__DATE__,"DVE"}

#define PIR1                0x0C
#define S_TIME              4
#define B_TIME              50

//;*****************************************************************/

int1 sw;
int selection;
int i,j;
int1 in;
int tick, b_tick;
int value;
long multi;
long duty;
int x[8];
int1 H_Run;

//;*****************************************************************/

#int_rtcc                           // This function is called every time
   									// the RTCC (timer0) overflows (255->0).
                                    // For this program this is apx 65 times
                                    // per second.

void clock_isr() 
	{               					   
		RTCC = 100;
		in=~SW_Start;
		if (in && b_tick)
		{
			if (--b_tick==0)
			sw=~sw;
		}
		if (!in)  b_tick=B_TIME;	
		if(sw)
		{	
			if (H_Run==0)
			{
				tick++;
				if (tick > S_TIME)
				H_Run=1;
			}
			else tick=0;
		}
		else
			if (H_Run==1)
			{
				tick++;
				if (tick > S_TIME)
				H_Run=0;
			}
			else tick=0;
	}

//;*****************************************************************/
	
void main() 
{
	setup_oscillator( OSC_4MHZ );
	RTCC = 100;
	setup_counters( RTCC_INTERNAL, RTCC_DIV_64 );
	enable_interrupts(INT_RTCC);
	enable_interrupts(GLOBAL);
	multi=	read_EEPROM (1);
	selection=read_EEPROM (0);								
	setup_ccp1(CCP_PWM);    // Configure CCP1 as a PWM
				          	//   The cycle time will be (1/clock)*4*t2div*(period+1)
					       	//   In this program clock=10000000 and period=127 (below)
				          	//   For the three possible selections the cycle time is:
						    //     (1/4000000)*4*1*255 =  255 us or 3.9 khz
						    //     (1/4000000)*4*4*255 = 1024 us or 980 hz
						    //     (1/4000000)*4*16*255= 4096 us or 244 hz

	switch(selection) 
	{
		case 1 : setup_timer_2(T2_DIV_BY_1, 255, 1);
		           break;
		case 2 : 	setup_timer_2(T2_DIV_BY_4, 255, 1);
		           break;
		case 3 : 	setup_timer_2(T2_DIV_BY_16, 255, 1);
		           break;
		default:	setup_timer_2(T2_DIV_BY_4, 255, 1);           
	}
	setup_port_a(sAN0);
	setup_adc(adc_clock_internal);
	set_adc_channel( 0 );
	GPIO=0;
	port_a_pullups(1);
	value=0;
	H_Run=0;
	tick=0;
	sw=0;
	b_tick=B_TIME;
	for (i=0;i<8;i++)
		{
			x[i]=0;
		}
	duty=0;
	set_pwm1_duty(duty);
	i=0;	
	while( TRUE ) 
	{
	 	if ( interrupt_active(INT_TIMER2))
		{
 	    	clear_interrupt(int_timer2);
	 		if (H_Run==1)
	 		{
		 		set_pwm1_duty(duty);          
			}
			else 
	 		{
		 		set_pwm1_duty(0);          
				for (j=0;j<8;j++)
				{
					x[j]=0;
				}
			}			
			value=read_adc();
			x[i]=value;
			i++;	
			if (i == 8) i=0;
			duty=0;
			for (j=0;j<8;j++)
			{
				duty=duty+x[j];
			}
			duty=duty/8;
//			if (duty > multi) duty = multi;
//			duty = (255*duty)/multi;
			duty = (multi*duty)/0xff;
			duty=(duty*4);
	     }  

	}
}

//;*****************************************************************/