Nokia3310LCD显示模块不仅小巧、省电、驱动容易,而且价钱也便宜。因此,最近网上常见到不少朋友都在用单片机来驱动该模块,制做出很多作品。在此,我要特别感谢神舟兄,是在他的热情帮助下,我才购得这款3310的液晶显示模块。如果有朋友需要的话也可以和他联系(神舟emal:conglp@beelink.com)。实验目标本实验我们就采用Atmega8或者Atmega48来驱动3310的LCD。在LCD上显示和移动汉字;在LCD的最上一行显示“欢迎光临电子爱好者俱乐部!”,并且让这串汉字在LCD上从左至右不停地移动。在LCD上显示英文字符串;在LCD的最底一行固定显示“”。在LCD上显示位图。在LCD的中间位置显示“AVR”和“实验室”两个位图。本站商城提供该LCD模块邮购:实验器材Atmega8或者Atmega48芯片一只、3.3V稳压二极管一只、3310LCD点阵液晶显示屏。Atmega8和Atmega48的管脚顺序完全相同,硬件可以直接替用。实验电路图及工作原理由于3310LCD的工作电压范围是2.7-3.3V,因此实验中使用一个3.3V的稳压二极管D1将5V电源稳压为3.3V左右,为MCU和LCD供电。MCU和LCD之间使用SPI接口传输数据。LCD模块的引脚排列:从模块的背面看,管脚排列顺序从左至右分别为1至8脚。如下图所示:注意:LCD模块的引脚即使用导线直接焊接上,也要用东西将金属引脚压下去,否则还是不能接触到内部电路实验源程序主程序:文件名是3310LCDTest.c#includeiom48v.h#includemacros.h#includelcd.h#includebmp.h/*******************************************/voidmain(void){OSCCAL=0x99;//8M系统内部时钟校准//设置MCU的I/O口DDRB|=LCD_RST|LCD_DC|LCD_CE|SPI_MOSI|SPI_CLK|0X02;SPSR|=BIT(SPI2X);//设置SPI时钟倍速SPCR|=BIT(SPE)|BIT(MSTR);//使能SPI接口,主机模式,4M时钟LCD_init();//初始化LCD模块LCD_write_String(1,5,);//显示英文字符串”“LCD_draw_map(0,2,AVR_bmp,40,24);//显示“AVR”位图LCD_draw_map(44,3,china_bmp,36,15);//显示”实验室“位图PORTB|=0x02;//LED亮while(1){LCD_move_chi(0,0,25);//汉字移动显示}}LCD3310驱动程序:文件名3310LCD.c#includeiom48v.h#includemacros.h#includelcd.h#include6x8.h#includechinese.h/*---------------------------------------LCD_init:3310LCD初始化编写日期:2004-8-10最后修改日期:2004-8-10----------------------------*/voidLCD_init(void){PORTB&=~LCD_RST;//产生一个让LCD复位的低电平脉冲delay_1us();PORTB|=LCD_RST;PORTB&=~LCD_CE;//关闭LCDdelay_1us();PORTB|=LCD_CE;//使能LCDdelay_1us();LCD_write_byte(0x21,0);//使用扩展命令设置LCD模式LCD_write_byte(0xc8,0);//设置偏置电压LCD_write_byte(0x06,0);//温度校正LCD_write_byte(0x13,0);//1:48LCD_write_byte(0x20,0);//使用基本命令LCD_clear();//清屏LCD_write_byte(0x0c,0);//设定显示模式,正常显示PORTB&=~LCD_CE;//关闭LCD}/*-----------------------------------------------------------------------LCD_clear:LCD清屏函数编写日期:2004-8-10最后修改日期:2004-8-10-----------------------------------------------------------------------*/voidLCD_clear(void){unsignedinti;LCD_write_byte(0x0c,0);LCD_write_byte(0x80,0);for(i=0;i504;i++)LCD_write_byte(0,1);}/*-----------------------------------------------------------------------LCD_set_XY:设置LCD坐标函数输入参数:X:0-83Y:0-5编写日期:2004-8-10最后修改日期:2004-8-10-----------------------------------------------------------------------*/voidLCD_set_XY(unsignedcharX,unsignedcharY){LCD_write_byte(0x40|Y,0);//columnLCD_write_byte(0x80|X,0);//row}/*-----------------------------------------------------------------------LCD_write_char:显示英文字符输入参数:c:显示的字符;编写日期:2004-8-10最后修改日期:2004-8-10-----------------------------------------------------------------------*/voidLCD_write_char(unsignedcharc){unsignedcharline;c-=32;for(line=0;line6;line++)LCD_write_byte(font6x8[c][line],1);}/*-----------------------------------------------------------------------LCD_write_char:英文字符串显示函数输入参数:*s:英文字符串指针;X、Y:显示字符串的位置编写日期:2004-8-10最后修改日期:2004-8-10-----------------------------------------------------------------------*/voidLCD_write_String(unsignedcharX,unsignedcharY,char*s){LCD_set_XY(X,Y);while(*s){LCD_write_char(*s);s++;}}/*-----------------------------------------------------------------------LCD_write_chi:在LCD上显示汉字输入参数:X、Y:显示汉字的起始X、Y坐标;ch_with:汉字点阵的宽度num:显示汉字的个数;line:汉字点阵数组中的起始行数row:汉字显示的行间距编写日期:2004-8-11最后修改日期:2004-8-12-----------------------------------------------------------------------*/voidLCD_write_chi(unsignedcharX,unsignedcharY,unsignedcharch_with,unsignedcharnum,unsignedcharline,unsignedcharrow){unsignedchari,n;LCD_set_XY(X,Y);//设置初始位置for(i=0;inum;){for(n=0;nch_with*2;n++)//写一个汉字{if(n==ch_with)//写汉字的下半部分{if(i==0)LCD_set_XY(X,Y+1);elseLCD_set_XY((X+(ch_with+row)*i),Y+1);}LCD_write_byte(china_char[line+i][n],1);}i++;LCD_set_XY((X+(ch_with+row)*i),Y);}}/*-----------------------------------------------------------------------LCD_write_chi:汉字移动输入参数:X、Y:显示汉字的起始X、Y坐标;T:移动速度;编写日期:2004-8-13最后修改日期:2004-8-13-----------------------------------------------------------------------*/voidLCD_move_chi(unsignedcharX,unsignedcharY,unsignedcharT){unsignedchari,n,j=0;unsignedcharbuffer_h[84]={0};unsignedcharbuffer_l[84]={0};for(i=0;i156;i++){buffer_h[83]=china_char[i/12][j];buffer_l[83]=china_char[i/12][j+12];j++;if(j==12)j=0;for(n=0;n83;n++){buffer_h[n]=buffer_h[n+1];buffer_l[n]=buffer_l[n+1];}LCD_set_XY(X,Y);for(n=0;n83;n++){LCD_write_byte(buffer_h[n],1);}LCD_set_XY(X,Y+1);for(n=0;n83;n++){LCD_write_byte(buffer_l[n],1);}delay_nms(T);}}/*-----------------------------------------------------------------------LCD_draw_map:位图绘制函数输入参数:X、Y:位图绘制的起始X、Y坐标;*map:位图点阵数据;Pix_x:位图像素(长)Pix_y:位图像素(宽)编写日期:2004-8-13最后修改日期:2004-8-13-----------------------------------------------------------------------*/voidLCD_draw_map(unsignedcharX,unsignedcharY,unsignedchar*map,unsignedcharPix_x,unsignedcharPix_y){unsignedinti,n;unsignedcharrow;if(Pix_y%8==0)row=Pix_y/8;//计算位图所占行数elserow=Pix_y/8+1;for(n=0;nrow;n++){LCD_set_XY(X,Y);for(i=0;iPix_x;i++){LCD_write_byte(map[i+n*Pix_x