#define __SRC #include "iom128v.h" #include "Init_cpu_Atmega128v.h" #include "tongxun.h" #include "macros.h" #include "delay.h" #include "i2c.h" #include "ks0108a.h" #include "ziku.h" #undef __SRC
#define LCD PORTC
/********************引脚定义********************/ #define Lcd_rs_0 PORTA&=~(1<<PA0) #define Lcd_rs_1 PORTA|=(1<<PA0) //数据指令
#define Lcd_rw_0 PORTA&=~(1<<PA1) #define Lcd_rw_1 PORTA|=(1<<PA1) //数据指令
#define Lcd_e_0 PORTA&=~(1<<PA2) #define Lcd_e_1 PORTA|=(1<<PA2) //数据指令
#define Lcd_csL_0 PORTA&=~(1<<PA5) #define Lcd_csL_1 PORTA|=(1<<PA5) //数据指令
#define Lcd_csM_0 PORTA&=~(1<<PA4) #define Lcd_csM_1 PORTA|=(1<<PA4) //数据指令
#define Lcd_csR_0 PORTA&=~(1<<PA3) #define Lcd_csR_1 PORTA|=(1<<PA3) //数据指令
unsigned char a=0;
void wtcom(void); void BusyL(void); void BusyM(void); void BusyR(void); void WriteCommandL(unsigned char CommandByte); void WriteCommandM(unsigned char CommandByte); void WriteCommandR(unsigned char CommandByte); void WriteCommand( unsigned char CommandByte );
/***************************/ /*检查Busy */ /***************************/ void BusyL(void) { Lcd_csL_0; Lcd_csM_1; Lcd_csR_1; wtcom(); }
void BusyM(void) { Lcd_csL_1; Lcd_csM_0; Lcd_csR_1; wtcom(); }
void BusyR(void) { Lcd_csL_1; Lcd_csM_1; Lcd_csR_0; wtcom(); }
void wtcom(void) { unsigned char busy; DDRC=0x00; do { Lcd_rs_0; //指令 Lcd_rw_1; Lcd_e_1; delay(2); a=PINC&0xa0; busy=PINC&0x80; Lcd_e_0; delay(2); } while(!(busy==0x00)); //Status Read Bit7 = BUSY DDRC=0xff; }
/***************************/ /*写指令 */ /***************************/ void WriteCommandL( unsigned char CommandByte ) { BusyL(); WriteCommand(CommandByte); }
void WriteCommandM( unsigned char CommandByte ) { BusyM(); WriteCommand(CommandByte); }
void WriteCommandR( unsigned char CommandByte ) { BusyR(); WriteCommand(CommandByte); }
void WriteCommand( unsigned char CommandByte ) { Lcd_rs_0; //指令 Lcd_rw_0; delay(2); LCD = CommandByte; Lcd_e_1; delay(2); Lcd_e_0; delay(2); }
unsigned char ReadData_Command(void) { unsigned char DataByte; DDRC=0x00; Lcd_rs_1; /*数据输出*/ Lcd_rw_1; /*读入 */ Lcd_e_1; /*读入到LCM*/ delay(2); DataByte = PINC; /*数据读出到数据口P1 */ Lcd_e_0; delay(2); DDRC=0xff; return DataByte; }
void WriteData_dot(unsigned char x,unsigned char y,unsigned char cls) { unsigned char temp; switch(x/64) { case 0: BusyL(); break; case 1: BusyM(); break; case 2: BusyR(); break; default: break; } WriteCommand((0xb8|((y/8)&0x07))); WriteCommand((0x40|(x&0x3f))); temp=ReadData_Command(); WriteCommand((0xb8|((y/8)&0x07))); WriteCommand((0x40|(x&0x3f))); temp=ReadData_Command(); WriteCommand((0xb8|((y/8)&0x07))); WriteCommand((0x40|(x&0x3f))); if((cls&0x01)==0) temp=temp&(~(01<<(y%8))); else temp=temp|(01<<(y%8)); Lcd_rs_1; /*数据输出*/ Lcd_rw_0; /*写输出 */ delay(2); LCD = temp; /*数据输出到数据口 */ Lcd_e_1; /*写入到LCM*/ delay(2); Lcd_e_0; delay(2); }
void WriteData_H12(unsigned char x,unsigned char y,unsigned int dat,unsigned char cls) { unsigned char i; for(i=0;i<12;i++) { if((dat&0x01)==1) { WriteData_dot(x,y,cls); } //else WriteData_dot(x,y,~cls); y++; dat>>=1; } }
/********************************************************/ /* 液晶清屏函数 */ /********************************************************/ void LcmClear(unsigned char xs,unsigned char ys,unsigned char xe,unsigned char ye,unsigned char cls) { unsigned char i,j; for(j=ys;ye>=j;j++) { for(i=xs;xe>=i;i++) { WriteData_dot(i,j,cls); } } }
void LcdClear(void) { unsigned char i,j; Lcd_csL_0; Lcd_csM_1; Lcd_csR_1; delay_us(1); delay_us(1); delay_us(1); for(j=0;j<8;j++) { WriteCommand(0xB8+j); WriteCommand(0x40); for(i=0;i<64;i++) { Lcd_rs_1; /*数据输出*/ Lcd_rw_0; /*写输出 */ delay(1); LCD = 0x00; /*数据输出到数据口 */ Lcd_e_1; /*写入到LCM*/ delay(1); Lcd_e_0; delay(2); } } }
/********************************************************/ /* 液晶初始化函数 */ /********************************************************/ void LcmInit(unsigned char cls) { Lcd_csL_0; Lcd_csM_0; Lcd_csR_0; WriteCommand(0x3E); //开显示
WriteCommand(0xc0); //设置起始地址=0 WriteCommand(0x40); //设置起始地址=0
WriteCommand(0x3f); //开显示
LcmClear(0,0,191,63,cls); //LcdClear(); }
void HZ_xianshi12x12(unsigned char x,unsigned char y,unsigned char cls,unsigned char *ptr) { unsigned char i,j,k,c1,c2,uLen; i=0; uLen=0; while (ptr[uLen]!=0) { uLen++; }; while(i<uLen) { c1 = ptr; c2 = ptr[i+1]; //ascii字符与汉字内码的区别在于128做分界,大于界线的为汉字码 if(c1 <=128) // ASCII { for(j=0;j<6;j++) { WriteData_H12(x+j,y,ASC_MSK[(c1-0x30)*6+j],cls); } x+=6; i++; } else // 中文 { for(j=0;j<sizeof(GB_12)/sizeof(struct typFNT_GB12);j++) // 查找定位 { if(c1 == GB_12[j].Index[0] && c2 == GB_12[j].Index[1]) break; } if(j < sizeof(GB_12)/sizeof(GB_12[0])) { for(k=0;k<12;k++) { WriteData_H12(x+k,y,GB_12[j].Msk[k],cls); } } x+=12; i+=2; } } }
/************************************************/ /*画线。任意方向的斜线,直线数学方程 aX+bY=1 */ /************************************************/ void Linexy(unsigned char x0,unsigned char y0,unsigned char xt,unsigned char yt,unsigned char s) { register unsigned char t; int xerr=0,yerr=0,delta_x,delta_y,distance; int incx,incy,uRow,uCol;
delta_x = xt-x0; //计算坐标增量 delta_y = yt-y0; uRow = x0; uCol = y0; if(delta_x>0) incx=1; //设置单步方向 else if( delta_x==0 ) incx=0; //垂直线 else {incx=-1;delta_x=-delta_x;}
if(delta_y>0) incy=1; else if( delta_y==0 ) incy=0; //水平线 else {incy=-1;delta_y=-delta_y;}
if( delta_x > delta_y ) distance=delta_x; //选取基本增量坐标轴 else distance=delta_y;
for( t=0;t <= distance+1; t++ ) { //画线输出 WriteData_dot(uRow,uCol,s); //画点 xerr += delta_x ; yerr += delta_y ;
if( xerr > distance ) { xerr-=distance; uRow+=incx; } if( yerr > distance ) { yerr-=distance; uCol+=incy; } } }
void Display_num6X12(unsigned char x,unsigned char y,unsigned char dat,unsigned char cls) { switch(dat&0x0f) { case 0: HZ_xianshi12x12(x,y,cls,"0"); break; case 1: HZ_xianshi12x12(x,y,cls,"1"); break; case 2: HZ_xianshi12x12(x,y,cls,"2"); break; case 3: HZ_xianshi12x12(x,y,cls,"3"); break; case 4: HZ_xianshi12x12(x,y,cls,"4"); break; case 5: HZ_xianshi12x12(x,y,cls,"5"); break; case 6: HZ_xianshi12x12(x,y,cls,"6"); break; case 7: HZ_xianshi12x12(x,y,cls,"7"); break; case 8: HZ_xianshi12x12(x,y,cls,"8"); break; case 9: HZ_xianshi12x12(x,y,cls,"9"); break; default: break; } }
/* void Display_1data(unsigned char x,unsigned char y,unsigned char dat) { Display_num8X16(x,y,((dat>>4)&0x0f)); Display_num8X16(x+8,y,((dat)&0x0f)); } */
void Display_2data(unsigned char x,unsigned char y,unsigned int dat,unsigned char cls) { Display_num6X12(x,y,((dat%10000)/1000),cls); Display_num6X12(x+6,y,((dat%1000)/100),cls); Display_num6X12(x+12,y,((dat%100)/10),cls); Display_num6X12(x+18,y,((dat%10)),cls); }
void Display_4data(unsigned char x,unsigned char y,unsigned long dat,unsigned char cls) { Display_num6X12(x,y,((dat%10000000)/1000000),cls); Display_num6X12(x+6,y,((dat%1000000)/100000),cls); Display_num6X12(x+12,y,((dat%100000)/10000),cls); Display_num6X12(x+18,y,((dat%10000)/1000),cls); Display_num6X12(x+24,y,((dat%1000)/100),cls); Display_num6X12(x+30,y,((dat%100)/10),cls); Display_num6X12(x+36,y,((dat%10)),cls); } |