/*
*********************************************************************************************************
*								KEYPAD AND UART DEBUG DEMONSTRATION PROGRAM
*
* File name		: main.c
* Programmer 	: John Leung, TechToys. Hong Kong
* Web presence  : www.TechToys.com.hk
* Note			: 
* Language		: Keil C 7.09 2K limited version under uVision2 IDE
* Hardware		: AT89S52-gLCD-STK1
* Date			: Version 1.0 (7th June 2006)
*********************************************************************************************************
*											DESCRIPTION
*
* This program checks for any key press on S1 (5-way navigator switch).
* Key debounce achieved by simple software delay function DelayMs(xKEY_SCAN_TASK_DLY).
* If key UP or DOWN pressed, the corresponding text message is transmitted via UART for debug purpose. 
* Auto-repeat feature is enabled with UP/DOWN, so holding these keys would repeat sending text messages.
* Similarly, text messages debug feature included for keys RIGHT, LEFT, and CENTRE, with auto-repeat
* disabled. At the same time, an LED is blinked at a rate of 1/50ms (20Hz) to show the software loop
* is running under the extreme conditions of a key hold.
* Baud rate is 9600bps, Timer 1 is used as the baud rate generator
* Standard library stdio.h is also included for printf function.
* Code size is 1664 bytes is printf is used, else, the code is around 494 bytes
*********************************************************************************************************
*/


#include <REGX52.h>
#include "xKey.h"
#include "delay.h"
#include <stdio.h>

sbit LED = P2^0;

/*
*********************************************************************************************************
*                         					UART INITIALIZATION
*
* Description : This function initialize the UART port of 89S52. Timer 1 is used as the baud rate
*				generator with an external crystal frequency of 11.0592MHz onboard.
*
* Arguments   : none
*			
* Returns     : none
*********************************************************************************************************
*/
void uartInit(void)
{
	SCON  = 0x52;    	//UART in 8 bit mode
 	TMOD  = TMOD|0x20;  //Timer 1 configured for 8-bit auto-reload timer mode, Timer 0 unchanged
 	TH1   = 0xfd;    	//TH1 value for a baud rate 9600bps
	TR1   = 1;			//Timer 1 is turned ON
}

/*
*********************************************************************************************************
*													MAIN
*********************************************************************************************************
*/
void main (void){
	
		unsigned char keyCode;

		xKeyInit();
		uartInit();

		for(;;){
		DelayMs(xKEY_SCAN_TASK_DLY);
		xKeyScanTask();
		if(xKeyHit())
		{
			keyCode = xKeyGetKey();
			switch (keyCode)
			{
				case xKEY_UP_FLAG: 	xKeyRptEn=TRUE; 
									printf("Key Up Pressed \n");
									break;
				case xKEY_DN_FLAG: 	xKeyRptEn=TRUE; 
									printf("Key Down Pressed \n");
									break;
				case xKEY_RT_FLAG:	xKeyRptEn=FALSE; 
									printf("Key Right Pressed \n");
									break;
				case xKEY_LT_FLAG:	xKeyRptEn=FALSE; 
									printf("Key Left Pressed \n");
									break;
				case xKEY_CTR_FLAG: xKeyRptEn=FALSE; 
									printf("Key Center Pressed \n");
									break;
				default:			;//inform user of an error, optional
			}
		}
		LED = ~LED;
		}
}
