全新论坛MCU智学网上线,欢迎访问新论坛!稀缺资源、技术干货、参考设计、原厂资料尽在MCU智学网
更新自动建库工具PCB Footprint Expert 2023.13 Pro / Library Expert 破解版

WINAVR下液晶3310的驱动

[复制链接]
5741 0

本文包含原理图、PCB、源代码、封装库、中英文PDF等资源

您需要 登录 才可以下载或查看,没有帐号?注册会员

x
从网址邮购处买到3310的液晶显示。试了一下,感觉很好,屏幕很清晰,很适合自己做点小项目。参考网站上的例子,用WINAVR(2005/02/14) 重新写了一个驱动,贴出来与大家共享。

网站上的例子, 初始化当中有一点DUG,提出来与大家讨论:
1. PB4当作RES的控制脚不妥当,因为在SPI下,PB4只能是输入,作输出控制RES不妥。
8c070ce21f55383092e2d6935ef8e0a2.jpg
2. 屏幕的最上面一行,显示不停滚动的中文:“欢迎光临本网站!” 好像后面还跟一些乱码(不知是否WINAVR的原因)。我在程序中改变了一下,现在正常了。
//NokiaLCD.h


  1. /***********************************************************************************
  2. Name         :  NokiaLCD.h
  3. Description  :  Header file for Nokia 84x48 graphic LCD driver.
  4. Author       :  2005-06-24 - TeSTCode
  5. Compiler     :  WINAVR Version: 20050214
  6. ************************************************************************************/
  7. #ifndef NOKIALCD_INCLUDED
  8. #define NOKIALCD_INCLUDED

  9. #include <avr/pgmspace.h>  

  10. #define LCD_X_RES        (84)
  11. #define LCD_Y_RES        (48)

  12. #define LCD_Array_SIZE   ((LCD_X_RES * LCD_Y_RES) / 8)

  13. #define LCD_DC_PORT     PORTB //  LCD&micro;&Uacute;4&frac12;&Aring;&pound;&not; Mega32&pound;&ordm;PB1  &micro;&Uacute;2&frac12;&Aring;
  14. #define LCD_DC_DDR     DDRB
  15. #define LCD_DC_BIT_NUM (1)

  16. #define LCD_CE_PORT     PORTB //  LCD&micro;&Uacute;5&frac12;&Aring;&pound;&not; Mega32&pound;&ordm;PB0  &micro;&Uacute;1&frac12;&Aring;
  17. #define LCD_CE_DDR     DDRB
  18. #define LCD_CE_BIT_NUM (0)

  19. #define LCD_RST_PORT   PORTA  //  LCD&micro;&Uacute;8&frac12;&Aring;&pound;&not; Mega32&pound;&ordm;PA2 &micro;&Uacute;3&frac12;&Aring;
  20. #define LCD_RST_DDR     DDRA
  21. #define LCD_RST_BIT_NUM (2)

  22. /*****************************************************************************
  23. *            SPI Definitions
  24. */
  25. #define SPI_PORT PORTB
  26. #define SPI_DDR       DDRB //Data direction register
  27. #define SPI_PIN       PINB //Port used for SPI
  28. #define SPI_SS_NUM (4) //SPI Slave select, must be set as output
  29. #define SPI_MOSI_NUM (5) //SPI CPU master output
  30. #define SPI_MISO_NUM (6) //SPI CPU master input
  31. #define SPI_SCK_NUM  (7) //SPI clock, CPU master


  32. #define delay_1us()       _delay_us(1)
  33. #define delay_1ms()       _delay_ms(1)

  34. #ifndef BIT
  35. #define BIT(x) (1 << (x))
  36. #endif

  37. enum {LCD_CMD  = 0, LCD_DATA = 1};

  38. class NokiaLcd{
  39. private:
  40. unsigned char LcdRow,LcdCol;  
  41. void InitLCDSPI(void);  

  42. public:
  43. NokiaLcd(unsigned char mRow = LCD_Y_RES,
  44.    unsigned char mCol= LCD_X_RES);
  45. void LCD_init(void);
  46. void LCD_clear(void);
  47. void LCD_set_XY(unsigned char X, unsigned char Y);
  48. void LCD_write_string(unsigned char X,unsigned char Y,char *s);
  49. void LCD_write_chinese_string(unsigned char X, unsigned char Y,
  50.                    unsigned char ch_with,unsigned char num,
  51.                    unsigned char line,unsigned char row);
  52. void LCD_move_chinese_string(unsigned char X, unsigned char Y, unsigned char T);                  
  53. void LCD_write_char(unsigned char c);
  54. void LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,PGM_P map,
  55.                   unsigned char Pix_x,unsigned char Pix_y);                  
  56. void LCD_write_byte(unsigned char data, unsigned char dc);   
  57. void delay_nus(unsigned int n);
  58. void delay_nms(unsigned int n);              
  59. };

  60. #endif
  61. /////////////////////////////////////////////////////////////////////////////////

  62. //NokiaLCD.c
  63. /****************************************************************************
  64. Name         :  NokiaLCD.c
  65. Description  :  This is a driver for the Nokia 84x48 graphic LCD.
  66. Author       :  2005-06-24 - TeSTCode
  67. Compiler     :  WINAVR Version: 20050214
  68. *****************************************************************************/
  69. #include <avr/io.h>
  70. #include <avr/delay.h>
  71. #include "NokiaLCD.h"
  72. #include "english_6x8_pixel.h"
  73. #include "move_chinese_string_pixel.h"
  74. #include "write_chinese_string_pixel.h"

  75. NokiaLcd::NokiaLcd(unsigned char mRow, unsigned char mCol)
  76. : LcdRow(mRow),
  77. LcdCol(mCol)
  78. {  
  79.   InitLCDSPI();  
  80.   LCD_init();
  81. }  

  82. /*****************************************************************************
  83. * WriteReadSPI: Send character to SPI and return character read back
  84. */
  85. void NokiaLcd::InitLCDSPI(void)
  86. {
  87. //Set SPI ports as output
  88.    SPI_DDR |= SPI_DDR|(1<<SPI_SCK_NUM)|(1<<SPI_MOSI_NUM)|(1<<SPI_SS_NUM);
  89.    SPI_PORT = SPI_PORT & (~(1<<SPI_MISO_NUM)); //Turn off pull-up
  90. /* Enable SPI, Master, set clock rate fck/16 */
  91. SPSR |= (1<<SPI2X);                 // &Eacute;è&Ouml;&Atilde;SPI&Ecirc;±&Ouml;&Oacute;±&para;&Euml;&Ugrave;
  92.     SPCR |= (1<<SPE)|(1<<MSTR); // &Ecirc;&sup1;&Auml;&Uuml;SPI&frac12;&Oacute;&iquest;&Uacute;&pound;&not;&Ouml;÷&raquo;ú&Auml;&pound;&Ecirc;&frac12;&pound;&not;4M&Ecirc;±&Ouml;&Oacute;
  93. }

  94. /*-----------------------------------------------------------------------
  95. Name         :  LcdInit
  96. Description  :  Performs MCU SPI & LCD controller initialization.
  97. -----------------------------------------------------------------------*/
  98. void NokiaLcd::LCD_init(void)
  99. {
  100.    _delay_ms(30);  //30ms power on delay
  101.    LCD_DC_DDR |= (1<<LCD_DC_BIT_NUM); //Set DC pin as output
  102.    LCD_CE_DDR |= (1<<LCD_CE_BIT_NUM); //Seet Ce pin as output
  103.    LCD_RST_DDR |= (1<<LCD_RST_BIT_NUM);//Set reset pin as output  
  104.    
  105.     //Toggle display reset pin.
  106.     LCD_RST_PORT &= ~(1<<LCD_RST_BIT_NUM);//LCD reset low
  107.     delay_1us(); //delay 1us
  108.     LCD_RST_PORT |= (1<<LCD_RST_BIT_NUM);   
  109.       
  110.     LCD_CE_PORT &= ~(1<<LCD_CE_BIT_NUM);
  111.     delay_1us();
  112.     LCD_CE_PORT |= (1<<LCD_CE_BIT_NUM);
  113.     delay_1us();

  114.     LCD_write_byte(0x21, LCD_CMD); // LCD Extended Commands.
  115.     LCD_write_byte(0xc8, LCD_CMD); // Set LCD Vop (Contrast).
  116.     LCD_write_byte(0x06, LCD_CMD); // Set Temp coefficent.
  117.     LCD_write_byte(0x13, LCD_CMD); // LCD bias mode 1:48.
  118.     LCD_write_byte(0x20, LCD_CMD); // LCD Standard Commands, Horizontal addressing mode
  119.     LCD_clear();              // Lcd clear screen
  120.     LCD_write_byte(0x0c, LCD_CMD); // LCD Standard Commands, Horizontal addressing mode     
  121.     LCD_CE_PORT &= ~(1<<LCD_CE_BIT_NUM);  // Deselect Lcd
  122. }

  123. /*-----------------------------------------------------------------------
  124. Name    : LCD Clear Screen
  125. Description  : LcdClear Screen  
  126. -----------------------------------------------------------------------*/
  127. void NokiaLcd::LCD_clear(void)
  128.   {
  129.     unsigned int i;

  130.     LCD_write_byte(0x0c, LCD_CMD);
  131.     LCD_write_byte(0x80, LCD_CMD);

  132.     for (i=0; i<504; i++)
  133.       LCD_write_byte(0, LCD_DATA);
  134.   }

  135. /*-----------------------------------------------------------------------
  136. Name         :  LCD_set_XY
  137. Description  : Set Lcd X(Page) Y(Column) position  
  138. -----------------------------------------------------------------------*/
  139. void NokiaLcd::LCD_set_XY(unsigned char X, unsigned char Y)
  140. {
  141.     LCD_write_byte(0x40 | Y, LCD_CMD); // column
  142.     LCD_write_byte(0x80 | X, LCD_CMD);    // row
  143. }

  144. /*-----------------------------------------------------------------------
  145. Name         :  LCD_write_char
  146. Description  :  Display one ASCII character
  147. -----------------------------------------------------------------------*/
  148. void NokiaLcd::LCD_write_char(unsigned char c)
  149.   {
  150.     unsigned char line;

  151.     c -= 32;
  152. LCD_write_byte(0x00,LCD_DATA);
  153.     for (line=0; line<5; line++)
  154.       LCD_write_byte(pgm_read_byte(&font6x8[c][line]), LCD_DATA);
  155.   }

  156. /*-----------------------------------------------------------------------
  157. Name         :  LCD_writeString
  158. Description  :  Write English string to Lcd Screen
  159. -----------------------------------------------------------------------*/
  160. void NokiaLcd::LCD_write_string(unsigned char X,unsigned char Y,char *s)
  161.   {
  162.     LCD_set_XY(X,Y);
  163.     while (*s)  
  164.     {
  165.   LCD_write_char(*s);
  166.   ++s;
  167.     }
  168.   }
  169. /*-----------------------------------------------------------------------
  170. Name         :  LCD_write_chinese_string
  171. Description  :  Write chinese character string to LCD Screen
  172. Argument(s)  :  X, Y -> Coordinate for new cursor position.
  173.           ch_with -> Chinese Character width
  174.           num  -> number of characters to display
  175.           line -> start line of the chinese character
  176.           row   ->Space lines between each char
  177. -----------------------------------------------------------------------*/                        
  178. void NokiaLcd::LCD_write_chinese_string(unsigned char X, unsigned char Y,  
  179.                    unsigned char ch_with,unsigned char num,
  180.                    unsigned char line,unsigned char row)
  181.   {
  182.     unsigned char i,n;
  183.      
  184.     LCD_set_XY(X,Y);                             //&Eacute;è&Ouml;&Atilde;&sup3;&otilde;&Ecirc;&frac14;&Icirc;&raquo;&Ouml;&Atilde;     
  185.     for (i=0;i<num;)
  186.       {
  187.        for (n=0; n<ch_with*2; n++)              //&ETH;&acute;&Ograve;&raquo;&cedil;&ouml;&ordm;&ordm;×&Ouml;
  188.          {  
  189.            if (n==ch_with)                      //&ETH;&acute;&ordm;&ordm;×&Ouml;&micro;&Auml;&Iuml;&Acirc;°&euml;&sup2;&iquest;·&Ouml;
  190.              {
  191.                LCD_set_XY((X+(ch_with+row)*i),Y+1);
  192.               }
  193.         LCD_write_byte(pgm_read_byte(&write_chinese_string[line+i][n]),LCD_DATA);
  194.          }
  195.        i++;
  196.        LCD_set_XY((X+(ch_with+row)*i),Y);
  197.       }
  198.   }
  199.    
  200. /*-----------------------------------------------------------------------
  201. Name         :  LCD_move_chinese_string
  202. Description  :  move the chinese string line on the screen;
  203. Argument(s)  :  X, Y -> Coordinate for new cursor position.
  204.               T -> moving speed
  205. -----------------------------------------------------------------------*/                        
  206. void NokiaLcd::LCD_move_chinese_string (unsigned char X, unsigned char Y, unsigned char T)
  207.   {
  208.     unsigned char i,n,j=0;
  209.     unsigned char buffer_h[84]={0};
  210.     unsigned char buffer_l[84]={0};
  211.       
  212.     for (i=0; i<96; i++)
  213.       {
  214.         buffer_h[83] = pgm_read_byte(&move_chinese_string[i/12][j]);
  215.         buffer_l[83] = pgm_read_byte(&move_chinese_string[i/12][j+12]);
  216.         j++;
  217.         if (j==12) j=0;
  218.          
  219.         for (n=0; n<83; n++)
  220.           {  
  221.             buffer_h[n]=buffer_h[n+1];
  222.             buffer_l[n]=buffer_l[n+1];
  223.           }  
  224.          
  225.         LCD_set_XY(X,Y);
  226.         for (n=0; n<83; n++)
  227.           {  
  228.             LCD_write_byte(buffer_h[n],LCD_DATA);
  229.           }  
  230.          
  231.         LCD_set_XY(X,Y+1);  
  232.         for (n=0; n<83; n++)
  233.           {  
  234.             LCD_write_byte(buffer_l[n],LCD_DATA);
  235.           }  
  236.            
  237.        delay_nms(T);
  238.       }
  239.   }

  240. /*-----------------------------------------------------------------------
  241. Name         :  LCD_draw_bmp_pixel
  242. Description  :  draw a picture on the Lcd screen
  243. Argument(s)  :  X, Y -> start position on thelcd screen
  244.                 map  ->picture array on the flash
  245.                 Pix_x   ->picture height (pixes)
  246.                 Pix_y   ->picture width (pixes)
  247. -----------------------------------------------------------------------*/
  248. void NokiaLcd::LCD_draw_bmp_pixel(unsigned char X,unsigned char Y,PGM_P map,
  249.                   unsigned char Pix_x,unsigned char Pix_y)            
  250.   {
  251.     unsigned int i,n;
  252.     unsigned char row;
  253.      
  254.     if (Pix_y%8==0) row=Pix_y/8;  //Cal. the total page numbers
  255.       else
  256.         row=Pix_y/8+1;
  257.      
  258.     for (n=0;n<row;n++)
  259.       {
  260.        LCD_set_XY(X,Y);
  261.         for(i=0; i<Pix_x; i++)
  262.           {
  263.             LCD_write_byte(pgm_read_byte( &map[i+n*Pix_x]), LCD_DATA);
  264.           }
  265.         Y++;                         //go to next page
  266.       }      
  267.   }

  268. /*-----------------------------------------------------------------------
  269. Name         :  LCD_write_byte
  270. Description  :  Sends data to display controller.
  271. Argument(s)  :  data -> Data to be sent
  272.                 cd   -> Command or data (see/use enum)
  273. -----------------------------------------------------------------------*/
  274. void NokiaLcd::LCD_write_byte(unsigned char data, unsigned char command)
  275. {
  276.     LCD_CE_PORT &= ~(1<<LCD_CE_BIT_NUM); // Enable SPI  
  277.     if (command == LCD_CMD)  // Send command
  278.       LCD_DC_PORT &= ~(1<<LCD_DC_BIT_NUM);              
  279.     else
  280.       LCD_DC_PORT |= (1<<LCD_DC_BIT_NUM);         // Send data

  281.     SPDR = data; // Load data to SPDR
  282.     while ((SPSR & 0x80) == 0);         // Wait until data sent
  283.     LCD_CE_PORT |= (1<<LCD_CE_BIT_NUM); // &sup1;&Oslash;±&Otilde;LCD
  284. }

  285. void NokiaLcd::delay_nus(unsigned int n)       //delay n us
  286. {
  287.    unsigned int i=0;
  288.    for (i=0;i<n;i++)
  289.    _delay_us(1);
  290. }
  291.    
  292. void NokiaLcd::delay_nms(unsigned int n)       //Delay n ms
  293. {
  294.    unsigned int i=0;
  295.    for (i=0;i<n;i++)
  296.    _delay_ms(1);
  297. }

复制代码


52f5d2c323e1db285bc9137402701cd8.jpg

举报

回复
*滑块验证:
您需要登录后才可以回帖 登录 | 注册会员

本版积分规则

打开支付宝扫一扫,最高立得1212元红包
搜索

图文热点

更多

社区学堂

更多

客服中心

QQ:187196467 服务时间:周一至周日 8:30-20:30

关注我们

关于我们
关于我们
友情链接
联系我们
帮助中心
网友中心
购买须知
支付方式
服务支持
资源下载
售后服务
定制流程
关注我们
官方微博
官方空间
官方微信
快速回复 返回顶部 返回列表