welcom ! Handel home

2015年1月23日 星期五

I2C 1602 LCM Drive DEMO Code by C ( 20X4 )

I2C LCM  Drive DEMO Code  by C

===========================================
[i2c_drv.h]

#define Byte unsigned char
#define Word unsigned long

struct st{
Byte yy;
Byte xx;
char *mess;
};

extern Byte ASCIICODE[16];
extern unsigned char LCDBUFF[80];
extern struct st mess_array[];

extern void i2clcm_WriteCommand(unsigned char nData);
extern void i2clcm_WriteData(unsigned char nData);

extern void i2clcm_init(void);
extern void i2clcm_setxy(Byte posy,Byte posx);
extern void i2clcm_show_char(Byte posy,Byte posx,Byte chardata);
extern void i2clcm_Show_String(Byte posy,Byte posx,char *strdata);


extern void i2clcm_Show_byte(Byte posy,Byte posx,Byte chardata);
extern void i2clcm_Show_word(Byte posy,Byte posx,Word worddata);

// show value for 5 number Dec and point for set (0 to 4)
//Rang 65535 .. 0 or 6.5535 .. 0.0001
// ext: show 12345 => i2clcm_Show_number(1,1,12345,0)
// ext: show 1.2345 => i2clcm_Show_number(1,1,12345,1)
// ext: show 12.345 => i2clcm_Show_number(1,1,12345,2)
extern void i2clcm_Show_number(Byte posy,Byte posx,Word value,Byte pt);

extern void i2clcm_buffer_fill(Byte fillcode);
extern void i2clcm_Show_buffer(Byte count);


============================================

============================================
[i2c_drv.c]

//================================================
//*** i2clcm Drive V 1.0 ****
//20150121    Handel    edit v1.0
//================================================

extern int PCF8547_WriteData( unsigned char Data  );
extern int PCF8547_ReadData( unsigned char *pData );

#define Byte unsigned char
#define Word unsigned long

//***  I2C LCM Parameter
#define lcmrow 20
#define lcmcol 4
#define line1 0x80
#define line2 0xc0
#define line3 0x94
#define line4 0xd4

Byte ASCIICODE[16] = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
Byte LCDBUFF[80];

struct st{
Byte yy;
Byte xx;
char *mess;
};

struct st mess_array[] = {
{ 1, 10,"mess1 "}, //0
{ 2, 10,"mess2 "}, //1
{ 3, 10,"mess3 "}, //2
{ 4, 10,"mess4 "}, //3
{ 1, 10,"ok "} , //4
{ 1,  1,"Handel  V1.0"},//5
{ 1,  1,"Hello !"}, //6
{ 1,  1,"OK    !"}, //7
{ 1,  1,"ERR   !"}, //8
{ 1,  1,"Test ..."} //9
};



void i2clcm_WriteCommand(unsigned char nData){
unsigned char  WR_Comm;

//__delay_cycles(500); // 10us * 10 = 100us

// d7,d6,d5,d4, x,en,rw,rs
// d7,d6,d5,d4, 1, 1, 0, 0
WR_Comm = (0x0c |(nData & 0xf0));
PCF8547_WriteData( WR_Comm );
// d7,d6,d5,d4, 1, 0, 0, 0
WR_Comm = (0x08 |(nData & 0xf0));
PCF8547_WriteData( WR_Comm );
// __delay_cycles(500); // 10us * 10 = 100us

// d3,d2,d1,d0, 1, 1, 0, 0
WR_Comm =0x0c |((nData & 0x0f)<<4 p=""> PCF8547_WriteData( WR_Comm );
// d3,d2,d1,d0, 1, 0, 0, 0
WR_Comm =0x08 |((nData & 0x0f)<<4 p=""> PCF8547_WriteData( WR_Comm );

//__delay_cycles(10000);//600us

}
void i2clcm_WriteData(unsigned char nData){
unsigned char  WR_Data;

// d7,d6,d5,d4, x,en,rw,rs
// d7,d6,d5,d4, 1, 1, 0, 1
WR_Data =0x0d |(nData & 0xf0);
PCF8547_WriteData( WR_Data );
// __delay_cycles(500); // 10us * 10 = 100us
// d7,d6,d5,d4, 1, 0, 0, 1
WR_Data =0x09 |(nData & 0xf0);
PCF8547_WriteData( WR_Data );
// __delay_cycles(500); // 10us * 10 = 100us

// d3,d2,d1,d0, 1, 1, 0, 1
WR_Data =0x0d |((nData & 0x0f)<<4 p=""> PCF8547_WriteData( WR_Data );
// __delay_cycles(500); // 10us * 10 = 100us
// d3,d2,d1,d0, 1, 0, 0, 1
WR_Data =0x09 |((nData & 0x0f)<<4 p=""> PCF8547_WriteData( WR_Data );

//__delay_cycles(500);//600us
}

void i2clcm_init(void){
i2clcm_WriteCommand( 0x33 );
i2clcm_WriteCommand( 0x32 );
//    WriteCommand( 0x38 );
//    WriteCommand( 0x28 );

i2clcm_WriteCommand( 0x28 );
//    __delay_cycles(1000000);  //delay ~60ns
i2clcm_WriteCommand( 0x08 );
    //__delay_cycles(5000);  //delay ~5ms
i2clcm_WriteCommand( 0x01 );
    //__delay_cycles(5000);  //delay ~5ms
i2clcm_WriteCommand( 0x06 );
    //__delay_cycles(5000);  //delay ~5ms
i2clcm_WriteCommand( 0x0C );
    //__delay_cycles(5000);  //delay ~5ms
    //WriteCommand( 0x0f );
}


void i2clcm_setxy(Byte posy,Byte posx){
Byte lcm_memadd;

if(posx > lcmrow )posx=lcmrow;
if(posy > lcmcol )posy=lcmcol;
if(posy==1)lcm_memadd = line1+posx-1;
if(posy==2)lcm_memadd = line2+posx-1;
if(posy==3)lcm_memadd = line3+posx-1;
if(posy==4)lcm_memadd = line4+posx-1;

i2clcm_WriteCommand( lcm_memadd );
}

void i2clcm_show_char(Byte posy,Byte posx,Byte chardata){
i2clcm_setxy(posy,posx);
i2clcm_WriteData(chardata);
}

void i2clcm_Show_String(Byte posy,Byte posx,char *strdata){
char *pp;

i2clcm_setxy(posy,posx);
    pp=strdata;
while(*pp !='\0'){
i2clcm_WriteData(*pp);
pp++;
}
}

void i2clcm_Show_byte(Byte posy,Byte posx,Byte chardata){

i2clcm_setxy(posy,posx);
i2clcm_WriteData(ASCIICODE[(chardata&0xf0)>>4]);
//i2clcm_setxy(posy,posx+1);
i2clcm_WriteData(ASCIICODE[(chardata&0x0f)]);
}


void i2clcm_Show_word(Byte posy,Byte posx,Word worddata){
i2clcm_Show_byte(posy,posx,(Byte )((worddata&0xff00)>>8));
i2clcm_Show_byte(posy,posx+2,(Byte )(worddata&0x00ff));
}

// show value for 5 number Dec and point for set (0 to 4)
//Rang 65535 .. 0 or 6.5535 .. 0.0001
// ext: show 12345 => i2clcm_Show_number(1,1,12345,0)
// ext: show 1.2345 => i2clcm_Show_number(1,1,12345,1)
// ext: show 12.345 => i2clcm_Show_number(1,1,12345,2)
void i2clcm_Show_number(Byte posy,Byte posx,Word value,Byte pt){

Byte n[5]={0,0,0,0,0};
    Byte i;

n[4]=  value %10;
n[3]= (value/10)%10;
n[2]= (value/100)%10;
n[1]= (value/1000)%10;
n[0]= (value/10000)%10;


i2clcm_setxy(posy,posx);
for(i = 0; i<=4 ; i++ ){
//i2clcm_setxy(posy,posx+i);
i2clcm_WriteData(ASCIICODE[n[i]]);
if(i==(pt-1)){
posx++;
i2clcm_setxy(posy,posx+i);
i2clcm_WriteData('.');
}
}

}

void i2clcm_buffer_fill(Byte fillcode){
Byte i;
for(i=0;i<80 fillcode="" i="" p="">}

//buffer show 8 bank , bank0..7
void i2clcm_Show_buffer(Byte count){
Byte i,loopadd;

loopadd= (count%8)*10;

i2clcm_setxy(count/20,count%20);

for(i=0;i<10 i="" p=""> i2clcm_WriteData(LCDBUFF[loopadd+i]);
}

}


===========================================

沒有留言: