|
本帖最后由 fpgaw 于 2010-11-25 14:11 编辑
#include "xparameters.h"
#include "stdio.h"
#include "xutil.h"
#include "xbasic_types.h"
#include "xstatus.h"
#include "xgpio.h"
#define CHANNEL 1
XGpio Lcd_Data,Lcd_Ctr;
Xuint8 table[]="timthorpe";
unsigned char number[]="0123456789";
/*******************************************************************/
/*延时time us*/
void delay(Xuint16 time)
{
Xuint16 i,j;
for(i=0;i<time;i++)
for(j=0;j<26;j++);
}
void delay_ms(Xuint16 time)
{
Xuint16 i,j;
for(i=0;i<time;i++)
for(j=0;j<3785;j++);
}
/*短延时time */
void delay_s(time)
{ Xuint8 i;
for(i=0;i<time;i++);
}
/*******************************************************************/
/*液?2864进行操作的一系列子函数 没什么可说的 按照时序图写褪橇?
/*******************************************************************/
Xint8 lcd_busy()
{
Xuint16 nTimeOut=0;
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x1);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x2);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x4);
delay(3);
XGpio_SetDataDirection(&Lcd_Data,CHANNEL,0xffffffff);
while((XGpio_DiscreteRead(&Lcd_Data,CHANNEL)&0x80));
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
delay(1);
// xil_printf("the value of result is %x\r\n",result);
}
/*******************************************************************/
/* */
/*写指令数据到LCD */
/*RS=L,RW=L,E=高脉冲,D0-D7=指令码。 */
/*******************************************************************/
void lcd_wcmd(Xuint8 cmd)
{
lcd_busy();
// delay_ms(20);
//delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x1);
//delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x2);
//delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
//delay(1);
// XGpio_SetDataDirection(&Lcd_Data,CHANNEL,0x0);
XGpio_DiscreteWrite(&Lcd_Data,CHANNEL,cmd);
delay_ms(5);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x4);
delay_ms(5);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
delay(1);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x2);
// delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x1);
//delay(1);
xil_printf("command %x\r\n",cmd);
}
/*******************************************************************/
/* */
/*写显示数据到LCD */
/*RS=H,RW=L,E=高脉冲,D0-D7=数据。 */
/* */
/*******************************************************************/
void lcd_wdat(Xuint8 dat)
{
lcd_busy();
/* delay_ms(20);
//delay(1);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x2);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
//delay(100);
//delay(1);
XGpio_DiscreteWrite(&Lcd_Data,CHANNEL,dat);
delay(5);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x4);
delay(5);
// XGpio_SetDataDirection(&Lcd_Data,CHANNEL,0x0);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
// delay(1);
//XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x2);
// delay(1);
//XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x1);
// delay(1);
xil_printf("data %x\r\n",dat);
delay_ms(1);*/
// delay_ms(20);
//delay(1);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x1);
//delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x2);
//delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
//delay(1);
// XGpio_SetDataDirection(&Lcd_Data,CHANNEL,0x0);
XGpio_DiscreteWrite(&Lcd_Data,CHANNEL,dat);
delay_ms(5);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x4);
delay_ms(5);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x4);
delay(1);
XGpio_DiscreteSet(&Lcd_Ctr,CHANNEL,0x2);
// delay(1);
XGpio_DiscreteClear(&Lcd_Ctr,CHANNEL,0x1);
//delay(1);
xil_printf("data %x\r\n",dat);
}
/******************************************************************
设置在液晶中显疚?
********************************************************************/
void lcd_pos(Xuint8 x,Xuint8 y)
{
Xuint8 pos;
if (x==0)
{x=0x80;}
else if (x==1)
{x=0x90;}
else if (x==2)
{x=0x88;}
else if (x==3)
{x=0x98;}
pos = x+y;
lcd_wcmd(pos); //显示地址
}
/*******************************************************************
在液晶中输出一串字?
********************************************************************
*/
void print_string(Xuint8 x,Xuint8 y,Xuint8 * str)
{
Xuint8 lcd_temp;
lcd_pos(x,y);
lcd_temp=*str;
while(lcd_temp != '\0')
{
lcd_wdat(lcd_temp);
lcd_temp=*(++str);
}
}
/*******************************************************************/
/* */
/* LCD初始化设定 */
/* */
/*******************************************************************/
void lcd_init()
{ delay_ms(50);
lcd_wcmd(0x30); //基本指令
delay(1500);
lcd_wcmd(0x30); //基本指令操作
delay(400);
lcd_wcmd(0x0F); //显示开,关?
delay(1500);
lcd_wcmd(0x01); //清除LCD的允灸谌?
delay_ms(15);
xil_printf("lcd initialization done!\n\r");
}
//********************************************上述为对液晶的操作
int main(void){
Xuint8 result;
xil_printf("now begin\r\n");
XGpio_Initialize(&Lcd_Data,XPAR_LCD_DATA_DEVICE_ID);
XGpio_SetDataDirection(&Lcd_Data,CHANNEL,0x0);
//XGpio_SetDataDirection(&Lcd_Data,CHANNEL,0x0);
XGpio_Initialize(&Lcd_Ctr,XPAR_LCD_CTR_DEVICE_ID);
XGpio_SetDataDirection(&Lcd_Ctr,CHANNEL,0x0);
lcd_init();
lcd_pos(1,3);
lcd_wdat(0x35);
/*delay_ms(10000);
lcd_wcmd(0x87);
lcd_wdat(0x2F);
delay_ms(10000);
print_string(2,0,number);
delay_ms(10000);
lcd_wcmd(0x01);*/
while(1);
return 0;
} |
|