File "KEYLCD-E.C"

Full Path: /home/analogde/www/private/Projet/Example/HC11/KEYLCD-E.C
File size: 3.49 KB
MIME-type: application/octet-stream
Charset: utf-8

/* Simple Small-C program to test CME11A or CMM11A LCD and keypad 
/* using HC11 port D and E (SS:KEYPAD). Key's pressed are displayed on the LCD.
/* To compile:  type at the dos prompt - sc keylcd-e

/* Define where things should reside... */
#define REG_BASE 0x1000
#data    0x2000
#code    0x2100

#include <68hc11.h>
#include <stdio.h>

/* I/O Port definitions */
#define LCD_DATA 0xB5F1      /* LCD Data Register */
#define LCD_CMD  0xB5F0      /* LCD Command Register */

char keybuf;	/* index variable for key scan */
char keychar;
unsigned char colnum, rownum, ColCnt, keylast, keyflag;

/* main loop */
void main(){

	pokeb(REG_BASE+DDRD,0x3E);	/* set data direction registers */
	pokeb(REG_BASE+DDRA,0xF0);

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

	LCDprint("Press any key: ");
	for( ; ;){
		keychar = getkey();
		cprint(keychar);
	}
}


/* returns 1 if a key is pressed.
   the key value/index is stored in the global variable keybuf.
*/
kphit(){
 	/* test rows high while column is high, if none high return */
	for(ColCnt = 0, colnum = 0x04; colnum < 0x40; ColCnt = ColCnt +4){
		pokeb(REG_BASE+PORTD,colnum);	/* set column value */
		#asm		/* delay for lines to settle */
			NOP
			NOP
		#endasm
		rownum = peekb(REG_BASE+PORTE);	/* get row value */
		rownum = rownum & 0x0f;			/* mask all but rows */
		if(rownum){	/* if a row in this column high */
			if(rownum == 0x04) rownum = 3;
			if(rownum == 0x08) rownum = 4;	/* figure row number */
			rownum = rownum - 1;
			keybuf = ColCnt + rownum;	/* calculate key index */
			if(keybuf == keylast)	return 0; /* ignore if same key pressed */
			keylast = keybuf;	/* save this key for repeat */
			return 1;			/* return YES  */
	   }
		/* colnum = colnum << 1; */ /* c code rotates the carry flag, which we don't want */
		#asm
			LSL	_colnum		; shift column left
		#endasm
	}
	keylast = 0xff;	/* initialize key repeat compare value*/
	return 0;	/* no key pressed, return 0 */
}

/* return key pressed value.  this table returns ASCII representations*/
/* variable keybuf contains the index */
keyval(){
	char KeyDATA[16];
	KeyDATA[0] = 	'1';
	KeyDATA[1] = 	'2';
	KeyDATA[2] = 	'3';
	KeyDATA[3] = 	'A';

	KeyDATA[4] = 	'4';
	KeyDATA[5] = 	'5';
	KeyDATA[6] = 	'6';
	KeyDATA[7] = 	'B';

	KeyDATA[8] = 	'7';
	KeyDATA[9] = 	'8';
	KeyDATA[10] = 	'9';
	KeyDATA[11] = 	'C';

	KeyDATA[12] = 	'*';
	KeyDATA[13] = 	'0';
	KeyDATA[14] = 	'#';
	KeyDATA[15] = 	'D';

	return(KeyDATA[keybuf]);
}

/* waits for a key press and returns it */
getkey(){
	while(kphit() == 0)	; /* wait for key press */
	return(keyval());     /* return it */
}


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

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

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

/* LCD Display String                                                 */

void LCDprint(sptr)
unsigned char *sptr;
{
	while( *sptr ){
		cprint(*sptr);
		++sptr;
	}
}