Пример 1.
#include<p18cxxx.h>
#include<delays.h>
#include<string.h>
#pragma config OSC=RC
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF
#pragma config MCLRE=OFF
#define LCD PORTB
#define SDATA PORTBbits.RB5
#define CLOCK PORTBbits.RB4
#define E PORTBbits.RB3
#define KEYPORT PORTA
#define DELAY 15
rom far char lookupKey[]=
{
1,4,7,10,
2,5,8,0,
3,6,9,11
};
unsigned char k;
#pragma code
void Switch(char sw)
{
do{
while((KEYPORT&sw)!=sw);
Delay1KTCYx(DELAY);
}while((KEYPORT&sw)!=sw);
do{
while((KEYPORT&sw)==sw);
Delay1KTCYx(DELAY);
}while((KEYPORT&sw)==sw);
}
unsigned char Key(void)
{
#define MASK 0x0F
#define ROWS 4
char a;
char keyCode;
PORTB=keyCode=0;
Switch(MASK);
PORTB=0xfe;
while((PORTA&MASK)==MASK)
{
PORTB=(PORTB<<1)|1;
keyCode+=ROWS;
}
for(a=1;a!=0;a<<=1)
{
if((PORTA&a)==0){
break;
}
keyCode++;
}
return lookupKey[keyCode];
}
void clockShiftReg(void)
{
CLOCK=1;
CLOCK=0;
}
void clearShiftReg(void)
{
char a;
SDATA=0;
for(a=0;a<6;a++)
clockShiftReg();
}
void sendNibble(char nib,char RS)
{
char a;
clearShiftReg();
SDATA=RS;
clockShiftReg();
for(a=0;a<4;a++)
{
SDATA=nib&1;
clockShiftReg();
nib>>=1;
}
E=1;
E=0;
}
void SendLCDdata(char data,char RS)
{
sendNibble(data>>4,RS);
sendNibble(data,RS);
}
void InitLCD(void)
{
Delay1KTCYx(20);
sendNibble(3,0);
Delay1KTCYx(4);
sendNibble(3,0);
Delay1KTCYx(12);
SendLCDdata(0x32,0);
SendLCDdata(0x28,0);
SendLCDdata(0x08,0);
SendLCDdata(0x01,0);
Delay1KTCYx(2);
SendLCDdata(0x06,0);
SendLCDdata(0x0c,0);
clearShiftReg();
}
void clrLCD(void)
{
SendLCDdata(0x01,0);
Delay1KTCYx(2);
}
void main(void)
{
ADCON1=0x7f;
TRISB=0;
TRISA=0x0f;
PORTB=0;
PORTA=0;
InitLCD();
k=0;
while(1)
{
k+='0';
SendLCDdata(0x80,0);
SendLCDdata(k,1);
k=Key();
clrLCD();
}
}
Пример 2.
#include<p18cxxx.h>
#include<delays.h>
#include<string.h>
#pragma config OSC=RC
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF
#pragma config MCLRE=OFF
#define LCD PORTB
#define SDATA PORTBbits.RB5
#define CLOCK PORTBbits.RB4
#define E PORTBbits.RB3
#define KEYPORT PORTA
#define DELAY 15
rom far char lookupKey[]=
{
1,4,7,10,
2,5,8,0,
3,6,9,11
};
#pragma code
unsigned char key2asc(unsigned char keyNum)
{
unsigned char k;
k=keyNum;
if(k==11)
k=-0x0d;
if(k==10)
k=-6;
k+='0';
return k;
}
void Switch(char sw)
{
do{
while((KEYPORT&sw)!=sw);
Delay1KTCYx(DELAY);
}while((KEYPORT&sw)!=sw);
do{
while((KEYPORT&sw)==sw);
Delay1KTCYx(DELAY);
}while((KEYPORT&sw)==sw);
}
unsigned char Key(void)
{
#define MASK 0x0F
#define ROWS 4
char a;
char keyCode;
PORTB=keyCode=0;
Switch(MASK);
PORTB=0x0e;
while((PORTA&MASK)==MASK)
{
PORTB=(PORTB<<1)|1;
keyCode+=ROWS;
}
for(a=1;a!=0;a<<=1)
{
if((PORTA&a)==0){
break;
}
keyCode++;
}
return key2asc(lookupKey[keyCode]);
}
void clockShiftReg(void)
{
CLOCK=1;
CLOCK=0;
}
void clearShiftReg(void)
{
char a;
SDATA=0;
for(a=0;a<6;a++)
clockShiftReg();
}
void sendNibble(char nib,char RS)
{
char a;
clearShiftReg();
SDATA=RS;
clockShiftReg();
for(a=0;a<4;a++)
{
SDATA=nib&1;
clockShiftReg();
nib>>=1;
}
E=1;
E=0;
}
void SendLCDdata(char data,char RS)
{
sendNibble(data>>4,RS);
sendNibble(data,RS);
}
void InitLCD(void)
{
Delay1KTCYx(20);
sendNibble(3,0);
Delay1KTCYx(4);
sendNibble(3,0);
Delay1KTCYx(12);
SendLCDdata(0x32,0);
SendLCDdata(0x28,0);
SendLCDdata(0x08,0);
SendLCDdata(0x01,0);
Delay1KTCYx(2);
SendLCDdata(0x06,0);
SendLCDdata(0x0c,0);
clearShiftReg();
}
void clrLCD(void)
{
SendLCDdata(0x01,0);
Delay1KTCYx(2);
}
void DisplayNum(unsigned char key,char i)
{
if(i<16){
SendLCDdata(0x80+i,0);
SendLCDdata(key,1);
}
else if(i>=16&&i<32){
SendLCDdata(0xc0+i-16,0);
SendLCDdata(key,1);
}
}
void main(void)
{
ADCON1=0x7f;
TRISB=0;
TRISA=0x0f;
PORTB=0;
PORTA=0;
InitLCD();
SendLCDdata(0x80,0);
SendLCDdata('0',1);
while(1)
{
char i;
for(i=0;i<=32;i++)
DisplayNum(Key(),i);
clrLCD();
SendLCDdata(0x80,0);
SendLCDdata('0',1);
}
}
Пример 3.
#include<p18cxxx.h>
#include<delays.h>
#pragma config OSC=RC
#pragma config WDT=OFF
#pragma config LVP=OFF
#pragma config BOR=OFF
#pragma config MCLRE=OFF
#define LCD PORTB
#define SDATA PORTBbits.RB6
#define CLOCK PORTBbits.RB5
#define E PORTBbits.RB4
#define KEYPORT PORTA
#define DELAY 15
rom far char lookupKey[]=
{
7,4,1,-0x30,
8,5,2,0,
9,6,3,0x0d,
0x0a,-0x06,-0x03,-0x05
};
#pragma code
unsigned char key2asc(unsigned char keyNum)
{
unsigned char k;
k=keyNum;
k+='0';
return k;
}
void Switch(char sw)
{
do{
while((KEYPORT&sw)!=sw);
Delay1KTCYx(DELAY);
}while((KEYPORT&sw)!=sw);
do{
while((KEYPORT&sw)==sw);
Delay1KTCYx(DELAY);
}while((KEYPORT&sw)==sw);
}
unsigned char Key(void)
{
#define MASK 0x0F
#define ROWS 4
char a;
char keyCode;
PORTB=keyCode=0;
Switch(MASK);
PORTB=0x0e;
while((PORTA&MASK)==MASK)
{
PORTB=(PORTB<<1)|1;
keyCode+=ROWS;
}
for(a=1;a!=0;a<<=1)
{
if((PORTA&a)==0){
break;
}
keyCode++;
}
return key2asc(lookupKey[keyCode]);
}
void clockShiftReg(void)
{
CLOCK=1;
CLOCK=0;
}
void clearShiftReg(void)
{
char a;
SDATA=0;
for(a=0;a<6;a++)
clockShiftReg();
}
void sendNibble(char nib,char RS)
{
char a;
clearShiftReg();
SDATA=RS;
clockShiftReg();
for(a=0;a<4;a++)
{
SDATA=nib&1;
clockShiftReg();
nib>>=1;
}
E=1;
E=0;
}
void SendLCDdata(char data,char RS)
{
sendNibble(data>>4,RS);
sendNibble(data,RS);
}
void InitLCD(void)
{
Delay1KTCYx(20);
sendNibble(3,0);
Delay1KTCYx(4);
sendNibble(3,0);
Delay1KTCYx(12);
SendLCDdata(0x32,0);
SendLCDdata(0x28,0);
SendLCDdata(0x08,0);
SendLCDdata(0x01,0);
Delay1KTCYx(2);
SendLCDdata(0x06,0);
SendLCDdata(0x0c,0);
clearShiftReg();
}
void clrLCD(void)
{
SendLCDdata(0x01,0);
Delay1KTCYx(2);
}
char DisplayNum(unsigned char key,char i)
{
if(key!=0){
if(i<16){
SendLCDdata(0x80+i,0);
SendLCDdata(key,1);
}
else if(i>=16&&i<32){
SendLCDdata(0xc0+i-16,0);
SendLCDdata(key,1);
}
return 1;
}
else
return 0;
}
void main(void)
{
ADCON1=0x7f;
TRISB=0;
TRISA=0x0f;
PORTB=0;
PORTA=0;
InitLCD();
SendLCDdata(0x80,0);
SendLCDdata('0',1);
while(1)
{
char i;
for(i=0;i<=32;i++)
if(!DisplayNum(Key(),i))
break;
clrLCD();
SendLCDdata(0x80,0);
SendLCDdata('0',1);
}
}
НАЗАД ДАЛЕЕ