|
arduino mega256 1602 扩展库
[复制链接]
1.myLCD1602.cpp
//#include "WProgram.h"
#include "Arduino.h"
#include
#define uchar unsigned char
uchar RS = 22;
uchar RW = 23;
uchar E =24;
uchar D0 = 25;
uchar D1 = 26;
uchar D2 = 27;
uchar D3 = 28;
uchar D4 = 29;
uchar D5 = 30;
uchar D6 = 31;
uchar D7 = 32;
myLCD::myLCD()
{}
void myLCD::lcdWriteCmd(uchar cmd)
{
uchar i;
digitalWrite(RS,LOW);
digitalWrite(RW,LOW);
for(i=0;i<8;i++)
{
digitalWrite(25+i,cmd & 0x1);
cmd >>=1;
}
digitalWrite(E,LOW);
delayMicroseconds(1);
digitalWrite(E,HIGH);
delayMicroseconds(1);
digitalWrite(E,LOW);
delayMicroseconds(1);
}
void myLCD::lcdWriteData(uchar data)
{
uchar i;
digitalWrite(RS,HIGH);
digitalWrite(RW,LOW);
for(i=0;i<8;i++)
{
digitalWrite(25+i,data & 0x1);
data>>=1;
}
digitalWrite(E,LOW);
delayMicroseconds(1);
digitalWrite(E,HIGH);
delayMicroseconds(1);
digitalWrite(E,LOW);
delayMicroseconds(1);
}
void myLCD::lcdInit()
{
char i;
//set use pin output
for(i=0;i<11;i++)
pinMode(22+i,OUTPUT);
delay(100);
lcdWriteCmd(0x38);
delay(64);
lcdWriteCmd(0x38);
delay(50);
lcdWriteCmd(0x38);
delay(20);
lcdWriteCmd(0x06);
delay(20);
lcdWriteCmd(0x0e);
delay(20);
lcdWriteCmd(0x01);
delay(100);
lcdWriteCmd(0x80);
delay(20);
}
//x==0 or 1, 0<=y<16
void myLCD::lcdSetXY(uchar x,uchar y)
{
lcdWriteCmd(0x0c);
delay(10);
if(x==0)
{
lcdWriteCmd(0x80+y);
}
else if(x==1)
{
lcdWriteCmd(0xc0+y);
}
else
{
}
delay(10);
}
void myLCD::lcdClear()
{
lcdWriteCmd(0x01);
delay(10);
}
void myLCD::lcdWrite(unsigned char *data,unsigned char size)
{
uchar i;
for(i=0;i
{
lcdWriteData(*data++);
}
}
2.myLCD1602.h
#ifndef myLCD1602
#define myLCD1602
class myLCD{
public:
myLCD();
void lcdInit();
void lcdSetXY(uchar x,uchar y);
void lcdWrite(uchar *data,uchar size);
private:
void lcdWriteCmd(uchar cmd);
void lcdWriteData(uchar data);
};
#endif
3.keywords.txt
myLCD KEYWORD1
lcdInit KEYWORD2
lcdSetXY KEYWORD2
lcdWrite KEYWORD2
4.testlib.ino
#include
myLCD mylcd1;
char ledpin=38;
void setup()
{
mylcd1.lcdInit();
}
void loop()
{
unsigned char test[]={'A','r','d','u','i','n','o'};
unsigned char test1[]={'e','r','a','i','n','_','3','0'};
mylcd1.lcdSetXY(0,4);
mylcd1.lcdWrite(test,7);
mylcd1.lcdSetXY(1,4);
mylcd1.lcdWrite(test1,8);
pinMode(ledpin,OUTPUT);
while(1)
{
digitalWrite(ledpin,LOW);
delay(500);
digitalWrite(ledpin,HIGH);
delay(500);
}
}
|
|