File "KLCD-D.C"

Full Path: /home/analogde/www/private/Projet/Example/HC11/KLCD-D.C
File size: 4.71 KB
MIME-type: text/x-c
Charset: utf-8


// ============================================================================
//														  Motorola 68HC11
//   Example Keypad and LCD display drivers
//
//   For use with the Axiom CMD11 development board
//   Compiled with Imagecraft C Compiler
//
//	To execute under buffalo, type load t, upload the s19 file then type
//	go 2440.  This is the beginning of the main() function for this program
//  as compiled.  If you re-compile it, look in the .mp file to locate main().
//
// ============================================================================

#include <hc11.h>

#define LINE_1      0x80                // beginning position of LCD line 1
#define LINE_2      0xC0                // beginning position of LCD line 2
#define LINE_3      0x94                // beginning position of LCD line 3
#define LINE_4      0xD4                // beginning position of LCD line 4

#define DELAY1MS	73		// number of loops = 1ms

// I/O Port Addresses
#define LCD_CMD  *(unsigned char *)(_IO_BASE + 0xA5F0)
#define LCD_DAT  *(unsigned char *)(_IO_BASE + 0xA5F1)
#define AUX_A    *(unsigned char *)(_IO_BASE + 0xA5F4)
#define AUX_C    *(unsigned char *)(_IO_BASE + 0xA5F6)
#define AUX_CON  *(unsigned char *)(_IO_BASE + 0xA5F7)

// Function Prototypes
void InitKey();
char kbhit();
char getkey();
void LCD_Command(unsigned char cval);
void LCD_busy(void);
void cprint(char dval);
void LCDprint(char *sptr);
void delay100ms(unsigned int secs);
void delay1ms(unsigned int msecs);

// Global Variables
unsigned char	NewKey, LastKey, testval;	// global variables used by keypad
unsigned char	colcount, colval, rowval, keybuf;

main(){
	DDRD = 0x3F;	// set keypad I/O data direction register

	NewKey = 0; 	// reset key variables
	LastKey = 0xFF;

	// Initialize the LCD
    LCD_Command(0x3C);                 // initialize command
    LCD_Command(0x0C);                 // display on, cursor off
    LCD_Command(0x06);
    LCD_Command(0x01);

    // Display Header Information on LCD
    LCD_Command(LINE_1);               // goto lcd line 1
    LCDprint("Keypad Test");
    LCD_Command(LINE_2);               // goto lcd line 2
    LCDprint("Press Keys");
    LCD_Command(LINE_3);               // goto lcd line 3

	// main loop .. endless
	for( ; ;){

		if(kbhit()){				// if new key pressed
			cprint(NewKey);			// display it
		}

	}
}


// Wait for the LCD busy pin to clear
void LCD_busy(){
    while ((LCD_CMD & 0x80)) ;
}

void LCD_Command(unsigned char cval){
//unsigned char cval;
    LCD_busy();                         // wait for busy to clear
    LCD_CMD = cval;                     // ouptut command
}

//* LCD Display Character
void cprint(char dval){
    LCD_busy();                         // wait for busy to clear
    LCD_DAT = dval;                     // ouptut data
}

// LCD Display String
void LCDprint(char *sptr){
	while( *sptr ){
		cprint(*sptr);
		++sptr;
	}
}

//  Busy wait loop to generate a delay multiple of 1ms
void delay1ms(unsigned int msecs) {
  unsigned int i,j;

  if (msecs > 0) {
     for (j=0; j<=msecs; j++)
        for (i=0; i<DELAY1MS; i++);
  }
  return;
}

//  Busy wait loop to generate a delay multiple of 100ms.
void delay100ms(unsigned int secs){
  unsigned int i,j;

  if (secs > 0) {
     for (j=0; j<=secs; j++)
        for (i=0; i<100; i++)	delay1ms(1);
  }
}

// initialize keyports, prepare for keyscan
void InitKey(){
	AUX_CON=0x8B;	// cycle port
	AUX_A=0xFF;		// force A high
	AUX_CON=0x93;	// AUX PortC 4-7 = Out, AUX PortA = IN
	AUX_C=0xFF;		// Key columns High on Port C
}

// returns 1 if a key is pressed.
// the key value/index is stored in the global variable NewKey.
char kbhit(){
	unsigned char KeyDATA[] =
	 {'D','#','0','*','C','9','8','7','B','6','5','4','A','3','2','1'};

	InitKey();			/* INITIALIZE KEYSCAN*/
	delay1ms(10);		/* wait 10ms*/

	/* do quick keyscan of all rows */
	AUX_C = 0x0F;
	testval = AUX_A;
	if((testval ^ 0xFF) == 0){
		LastKey = 0xFF;
		return 0;
	}

	if(LastKey != 0xFF){	// if same key still pressed
		return 0;
	}

	keybuf = 0;
	for(colcount=0x80; colcount > 0x0F; colcount = colcount >> 1){
		colval = colcount ^ 0xFF;
		AUX_C = colval;
		rowval = AUX_A;
		rowval = rowval ^ 0xFF;
		if(rowval != 0) break;
		keybuf += 4;
	}
	if(rowval == 0) return 0;

	testval = AUX_A;

	colcount=0x80;
	for( ; ;){
		if((testval & colcount) == 0){		/* if this row pressed*/
			InitKey();			/* INITIALIZE KEYSCAN*/
			NewKey = KeyDATA[keybuf];
			LastKey = NewKey;
			return(1);
		}
		++keybuf;
		colcount >>= 1; 	/* rotate to check next row */
		if(colcount == 0x08)	break;
	}

	InitKey();			/* INITIALIZE KEYSCAN*/
	return 0;		    /* return no key pressed*/

}


char dosomething(){
	return 0;
}
// waits for a key press and returns it
char getkey(){
	while( !kbhit()){		// wait for key press
		dosomething();
	}
	return(NewKey);
}