经多次测试,在YL-KL26Z 开发板上要实现QC12864B的显示功能,可采用串行模式来实现,方法是将VDD、Vo、CS、LEDA连在一起接+5V电源,将VSS、PSB、LEDK连在一起接GND,将SID接PD4,SCK接PD5。 在QC12864B上以串行模式显示汉字的代码如下: - /***********************************************
- 标题: LCD12864程序
- 效果: 基于YL-KL26Z 开发板的QC12864B液晶屏
- 串行模式测试程序 (已经验证 OK)
- ***************************************************/
- #include "includes.h"
- //SID PD4
- //SCLK PD5
- #define BIT(x) (1<<(x))
- #define uchar unsigned char
- #define uint unsigned int
- #define nop() delay_us()
- #define xtal 10
- #define Set_CS() GPIO_SET(MKL_PORTD,3)
- #define Set_SID() GPIO_SET(MKL_PORTD,4)
- #define Set_SCLK() GPIO_SET(MKL_PORTD,5)
- #define Clr_CS() GPIO_CLR(MKL_PORTD,3)
- #define Clr_SID() GPIO_CLR(MKL_PORTD,4)
- #define Clr_SCLK() GPIO_CLR(MKL_PORTD,5)
- void delay_us(void);
- void Delay(uint32_t a);
- void W_1byte(uchar RW, uchar RS, uchar W_data);
- void Write_8bits(uint W_bits);
- void LCD_Init(void);
- void Char_Set_XY(uchar x, uchar y, const uchar *p);
- void Set_XY(uchar x, uchar y, uchar *p);
- void Set_White(uchar x,uchar y,uchar end_x,uchar clear);
- void Setxy(uchar x, uchar y);
- void myDelay (INT32U ulTime)
- {
- INT32U i;
- i = 0;
- while (ulTime--) {
- for (i = 0; i < 5000; i++);
- }
- }
- int main (void)
- {
- SystemCoreClockUpdate();
- SIM_SCGC5 |= (SIM_SCGC5_PORTA_MASK
- | SIM_SCGC5_PORTB_MASK
- | SIM_SCGC5_PORTC_MASK
- | SIM_SCGC5_PORTD_MASK
- | SIM_SCGC5_PORTE_MASK );
- PORT_ENABLE_CLK(MKL_PORTD);
- IO_FUN_SEL(MKL_PORTD,3,1);
- IO_FUN_SEL(MKL_PORTD,4,1);
- IO_FUN_SEL(MKL_PORTD,5,1);
- GPIO_DDR_OUTPUT(MKL_PORTD,3);
- GPIO_DDR_OUTPUT(MKL_PORTD,4);
- GPIO_DDR_OUTPUT(MKL_PORTD,5);
- Clr_CS();
- Clr_SID();
- Clr_SCLK();
- LCD_Init();
- Delay(50);
- LCD_Init();
- nop();
- nop();
- Delay(500);
- Char_Set_XY(0,0, "秦时明月汉时关");
- Char_Set_XY(0,1,"万里长征人未还");
- Char_Set_XY(0,2, "但使龙城飞将在");
- Char_Set_XY(0,3, "不教胡马度阴山");
- while(1);
- }
- void LCD_Init(void)
- {
- uchar cmd;
- cmd=0x30;
- W_1byte(0,0,cmd);
- Delay(2);
- cmd=0x0C;
- W_1byte(0,0,cmd);
- Delay(2);
- cmd=0x01;
- W_1byte(0,0,cmd);
- Delay(2);
- cmd=0x02;
- W_1byte(0,0,cmd);
- Delay(2);
- cmd=0x80;
- W_1byte(0,0,cmd);
- Delay(2);
- }
- void W_1byte(uchar RW, uchar RS,uchar W_data)
- { uint H_data,L_data,S_ID = 0xf8;
- if(RW == 0)
- {
- S_ID &=~ 0x04;
- }
- else
- {
- S_ID |= 0X04;
- }
- if(RS == 0)
- {
- S_ID &=~ 0x02;
- }
- else
- {
- S_ID |= 0X02;
- }
- H_data = W_data;
- H_data &= 0xf0;
- L_data = W_data;
- L_data &= 0x0f;
- L_data <<= 4;
- Set_CS();
- Write_8bits(S_ID);
- Write_8bits(H_data);
- Write_8bits(L_data);
- Clr_CS();
- }
- void Write_8bits(uint W_bits)
- { uint i,Temp_data;
- for(i=0; i<8; i++)
- { Temp_data = W_bits;
- Temp_data <<= i;
- if((Temp_data&0x80)==0)
- {
- Clr_SID();
- }
- else
- {
- Set_SID();
- }
- nop();
- Set_SCLK();
- nop();
- nop();
- Clr_SCLK();
- nop();
- Clr_SID();
- }
- }
- void delay_us()
- { unsigned char i;
- for(i=1;i<10;i++);
- }
- void Delay(uint32_t a) //1ms????
- { uint32_t i;
- while( --a != 0)
- {
- for(i = 0; i<5200; i++);
- }
- }
- void Char_Set_XY(uchar x, uchar y,const uchar *p)
- { if(y == 0)
- {
- W_1byte(0,0,(0x80+x));
- }
- if(y == 1)
- {
- W_1byte(0,0,(0x90+x));
- }
- if(y == 2)
- {
- W_1byte(0,0,(0x88+x));
- }
- if(y == 3)
- {
- W_1byte(0,0,(0x98+x));
- }
- while(*p != 0)
- {
- W_1byte(0,1,*p++);
- }
- }
- void Set_XY(uchar x, uchar y, uchar *p)
- { if(y == 0)
- {
- W_1byte(0,0,(0x80+x));
- }
- if(y == 1)
- {
- W_1byte(0,0,(0x90+x));
- }
- if(y == 2)
- {
- W_1byte(0,0,(0x88+x));
- }
- if(y == 3)
- {
- W_1byte(0,0,(0x98+x));
- }
- while(*p != 0)
- {
- W_1byte(0,1,*p++);
- }
- }
- void Setxy(uchar x, uchar y)
- { if(y == 0)
- {
- W_1byte(0,0,(0x80+x));
- }
- if(y == 1)
- {
- W_1byte(0,0,(0x90+x));
- }
- if(y == 2)
- {
- W_1byte(0,0,(0x88+x));
- }
- if(y == 3)
- {
- W_1byte(0,0,(0x98+x));
- }
- }
- void Set_White(uchar x,uchar y,uchar end_x,uchar clear)
- { uchar i, j, white_x, white_y,white_end_x,clr_x,clr_y;
- white_end_x = (end_x-x+1);
- white_end_x <<= 1;
- if(y==1)
- { white_x = (0x80+x-1);
- white_y = 0x80;
- clr_x = 0x80;
- clr_y = 0x80;
- }
- else if(y==2)
- { white_x = (0x80+x-1);
- white_y = 0x90;
- clr_x = 0x80;
- clr_y = 0x90;
- }
- else if(y==3)
- { white_x = (0x88+x-1);
- white_y = 0x80;
- clr_x = 0x88;
- clr_y = 0x80;
- }
- else if(y==4)
- { white_x = (0x88+x-1);
- white_y = 0x90;
- clr_x = 0x88;
- clr_y = 0x90;
- }
- if(clear==0)
- { for(i=0;i<16;i++ )
- { W_1byte(0,0,clr_y++);
- W_1byte(0,0,clr_x);
- for(j=0;j<16;j++)
- { W_1byte(0,1,0x00);
- nop();
- }
- }
- }
- nop();
- for(i=0;i<16;i++ )
- { W_1byte(0,0,white_y++);
- W_1byte(0,0,white_x);
- for(j=0;j<white_end_x;j++)
- { if(clear==1)
- {
- W_1byte(0,1,0x00);
- }
- else
- {
- W_1byte(0,1,0xff);
- }
- nop();
- }
- }
- }
复制代码经实际验证,说明程序的移植是成功的,可以在YL-KL26Z 开发板上由QC12864B实现常规的显示功能。
|