#include "OB6710.h"
#include "PWM.h"

extern unsigned int PWM_DUTY0  ;      //PWM0 Duty
extern unsigned int PWM_DUTY1  ;      //PWM1 Duty
extern unsigned int PWM_DUTY2  ;      //PWM2 Duty

extern void Test_Override(void);
extern void Test_Mode(void);
unsigned char gTimer3Int;

unsigned char OVRDIS[6] = 
{
	0x01, 0x02, 0x04, 0x08, 0x10, 0x20
};


unsigned char OVRDAT[6]=
{
  0xFE, 0xFD, 0xFB,0xF7,0xEF,0xDF,
};

void TIMER3_Interrupt(void) interrupt 6
{
	P0_1 = ~P0_1;
	T3CON = T3CON | 0x20;
	if((T3CON & 0x20) != 0x00)
	  while(1);	
	T3CON = T3CON | 0x10;
	gTimer3Int++;
}

void main(void)
{

	P0M0 = 0x00;
	P0M1 = 0x00;
	P1M0 = 0x00;
	P1M1 = 0x00;
	P2M0 = 0x00;
	P2M1 = 0x00;
	P3M0 = 0x00;
	P3M1 = 0x00;

	PWM_Duty(PWM_DUTY0,PWM_DUTY1, PWM_DUTY2);
	PWM_Init();

	Test_Override();
//	Test_Mode();
	while(1);
}

void Test_Override(void)
{
	unsigned char Loop;
	PWMAUX = 0x04;	
	Loop = 0x00;
	P0M0 = 0x00;
	P0M1 = 0x00;

	OVRDAT_5 = OVRDAT[5];
	OVRDIS_5 = OVRDIS[5];
	
	OVRDAT_4 = OVRDAT[4];
	OVRDIS_4 = OVRDIS[4];
	
	OVRDAT_3 = OVRDAT[3];
	OVRDIS_3 = OVRDIS[3];
	
	OVRDAT_2 = OVRDAT[2];
	OVRDIS_2 = OVRDIS[2];
	
	OVRDAT_1 = OVRDAT[1];
	OVRDIS_1 = OVRDIS[1];
	
	OVRDAT_0 = OVRDAT[0];
	OVRDIS_0 = OVRDIS[0];
	HALLSEL = Loop;
//----------------------------
	gTimer3Int = 0x00;	

	EA = 1;	
	IEN0 = IEN0 | 0x40;	
	T3CNTL = 0x12;
	T3CNTH = 0xF4;
	T3NDIV = 0x04;
	T3CON = T3CON | 0x10;
	HALLCON = HALLCON | 0x81;
	gTimer3Int = 0x00;
//----------------
	while(1)
	{
		
		if(gTimer3Int >= 0x01)
		{
			gTimer3Int = 0x00;
			HALLSEL = Loop;
			if(Loop >= 0x05)
				Loop = 0x00;
			else
				Loop++;
		}
	}
}

void Test_Mode(void)
{
	unsigned char Loop;

	PWMAUX = 0x04;	
	Loop = 0x00;
	P0M0 = 0x00;
	P0M1 = 0x00;

	OVRDAT_5 = 0x00;
	OVRDIS_5 = 0x3f;
	
	OVRDAT_4 = 0x00;
	OVRDIS_4 = 0x3f;
	
	OVRDAT_3 = 0x00;
	OVRDIS_3 = 0x3f;
	
	OVRDAT_2 = 0x00;
	OVRDIS_2 = 0x3f;
	
	OVRDAT_1 = 0x00;
	OVRDIS_1 = 0x3f;
	
	OVRDAT_0 = 0x00;
	OVRDIS_0 = 0x3f;

	MODE_5 = 0x03;
	MODE_4 = 0x05;
	MODE_3 = 0x06;
	MODE_2 = 0x03;
	MODE_1 = 0x05;
	MODE_0 = 0x06;
	
	HALLSEL = Loop;
//----------------------------
	gTimer3Int = 0x00;	

	EA = 1;	
	IEN0 = IEN0 | 0x40;	
	T3CNTL = 0x12;
	T3CNTH = 0xF4;
	T3NDIV = 0x04;
	T3CON = T3CON | 0x10;
	HALLCON = HALLCON | 0x80;
	gTimer3Int = 0x00;
//----------------
	while(1)
	{	
		if(gTimer3Int >= 0x01)
		{
			gTimer3Int = 0x00;
			HALLSEL = Loop;
			if(Loop >= 0x05)
				Loop = 0x00;
			else
				Loop++;
		}
	}
}