Advertisement
pongfactory

LCD Demo V.1 OK

Feb 28th, 2014
172
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 11.17 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f10x_gpio.h"
  3. #include "stm32f10x_rcc.h"
  4. #include <stdint.h>
  5. #include <stdio.h>
  6.  
  7. /***************************************************************************//**
  8.  * Global variables, private define and typedef
  9.  ******************************************************************************/
  10. #define EN  GPIO_Pin_10
  11. #define RS  GPIO_Pin_12
  12. #define RW  GPIO_Pin_11
  13.  
  14. const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  15.                                      0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
  16.  
  17.  
  18. const char UserFont[8][8] = {  /* 8 user defined characters to be loaded into CGRAM (used for bargraph)*/
  19.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  20.     { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
  21.     { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
  22.     { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
  23.     { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
  24.     { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
  25.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  26.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
  27. };
  28.  
  29. /***************************************************************************//**
  30.  * Declare function prototypes
  31.  ******************************************************************************/
  32. void RCC_Configuration(void);
  33. void LCD_DATA_DIR_OUT(void);
  34. void LCD_DATA_DIR_IN(void);
  35. void LCD_ALL_DIR_OUT(void);
  36. unsigned int LCD_DATA_IN(void);
  37. static unsigned char Wait_While_Busy(void);
  38. static unsigned char Lcd_Read_Status (void);
  39. void Lcd_Write_4bits(uc8 byte);
  40. void Delay(vu32 nCount);
  41. void Lcd_Write_Command(uc8 command);
  42. void Lcd_Write_Data(uc8 data);
  43. void Lcd_Init(void);
  44. void Lcd_Write_Line1(void);
  45. void Lcd_Write_Line2(void);
  46. void set_cursor(int, int);
  47. void lcd_print (char *string);
  48. void lcd_clear (void);
  49.  
  50. /***************************************************************************//**
  51.  * @brief First set the RCC clock, then initial the LCD, display characters.
  52.  ******************************************************************************/
  53. void GPIO_LCD1602(void)
  54. {
  55.     RCC_Configuration();
  56.     Lcd_Init();                         /* initial       */
  57.     lcd_clear();                        /* clean the LCD */
  58.     Delay(1000);
  59.     set_cursor(0,0);                    /* set cursor    */
  60.     lcd_print (" ESL Lab!  ");     /* display       */
  61.     set_cursor(0, 1);
  62.     lcd_print (" Embedded 2014 ");
  63. }
  64.  
  65. /***************************************************************************//**
  66.  * @brief System clocks configuration
  67.  ******************************************************************************/
  68. void RCC_Configuration(void)
  69. {
  70.     RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  71.     RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */
  72.  
  73.     /* Wait till HSE is ready                                                   */
  74.     while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  75.  
  76.     RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  77.     RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  78.     RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  79.     RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */
  80.  
  81.     *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */
  82.  
  83.     /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  84.     RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
  85.  
  86.     RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */
  87.  
  88.     /* Wait till PLL is ready                                                   */
  89.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  90.  
  91.     /* Select PLL as system clock source                                        */
  92.     RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
  93.  
  94.     /* Wait till PLL is used as system clock source                             */
  95.     while (RCC_GetSYSCLKSource() != 0x08);
  96. }
  97.  
  98. /***************************************************************************//**
  99.  * @brief Delay some time
  100.  ******************************************************************************/
  101. void Delay(vu32 nCount)
  102. {
  103.     for(; nCount != 0; nCount--);
  104. }
  105.  
  106. /***************************************************************************//**
  107.  * @brief  Setting all pins to output mode
  108.  ******************************************************************************/
  109. void LCD_ALL_DIR_OUT(void)
  110. {
  111.     GPIO_InitTypeDef GPIO_InitStructure;
  112.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  113.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  114.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  115.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  116. }
  117.  
  118. /***************************************************************************//**
  119.  * @brief  Setting DATA pins to input mode
  120.  ******************************************************************************/
  121. void LCD_DATA_DIR_IN(void)
  122. {
  123.     GPIO_InitTypeDef GPIO_InitStructure;
  124.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  125.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  126.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  127.     GPIO_Init(GPIOC, &GPIO_InitStructure);  
  128. }
  129.  
  130. /***************************************************************************//**
  131.  * @brief  Setting DATA pins to output mode
  132.  ******************************************************************************/
  133. void LCD_DATA_DIR_OUT(void)
  134. {
  135.     GPIO_InitTypeDef GPIO_InitStructure;
  136.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  137.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  138.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  139.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  140. }
  141. /***************************************************************************//**
  142.  * @brief  Reading DATA pins
  143.  * @return the data value.
  144.  ******************************************************************************/
  145. unsigned int LCD_DATA_IN(void)
  146. {
  147.     uint16_t u16Temp=0;
  148.     u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
  149.     return SWAP_DATA[u16Temp];
  150. }
  151.  
  152. /***************************************************************************//**
  153.  * @brief  Read status of LCD controller
  154.  * @return status : Status of LCD controller
  155.  ******************************************************************************/
  156. static unsigned char Lcd_Read_Status (void)
  157. {
  158.     unsigned char status;
  159.  
  160.     LCD_DATA_DIR_IN();
  161.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  162.     GPIO_WriteBit(GPIOC, RW, Bit_SET);
  163.     Delay(10);
  164.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  165.     Delay(10);
  166.     status  = LCD_DATA_IN() << 4;
  167.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  168.     Delay(10);
  169.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  170.     Delay(10);
  171.     status |= LCD_DATA_IN();
  172.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  173.     LCD_DATA_DIR_OUT();
  174.     return (status);
  175. }
  176.  
  177. /***************************************************************************//**
  178.  * @brief Wait while LCD is busy
  179.  * @return status : Status of LCD controller
  180.  ******************************************************************************/
  181. static unsigned char Wait_While_Busy()
  182. {  
  183.     unsigned char status;
  184.     do{
  185.     status = Lcd_Read_Status();
  186.     }while(status & 0x80);
  187.  
  188.     return status;
  189. }
  190. /***************************************************************************//**
  191.  * @brief  Write 4-bits to LCD controller
  192.  ******************************************************************************/
  193. void Lcd_Write_4bits(uc8 byte)
  194. {
  195.     uint16_t u16Temp=0;
  196.     GPIO_WriteBit(GPIOC, RW, Bit_RESET);
  197.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  198.     u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
  199.     u16Temp |=  SWAP_DATA[byte&0x0F];
  200.     GPIO_Write(GPIOC, u16Temp);
  201.     Delay(10);
  202.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  203.     Delay(10);
  204. }
  205.  
  206. /***************************************************************************//**
  207.  * @brief:    Write command to LCD controller
  208.  * @param[in] command :  Command to be written
  209.  ******************************************************************************/
  210. void Lcd_Write_Command(uc8 command)
  211. {
  212.     Wait_While_Busy();
  213.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  214.     Lcd_Write_4bits(command>>4);
  215.     Lcd_Write_4bits(command);
  216. }
  217.  
  218. /***************************************************************************//**
  219.  * @brief:     Write data to LCD controller
  220.   * @param[in] data :  Data to be written
  221.  ******************************************************************************/
  222. void Lcd_Write_Data(uc8 data)
  223. {
  224.     Wait_While_Busy();
  225.     GPIO_WriteBit(GPIOC, RS, Bit_SET);
  226.     Lcd_Write_4bits(data>>4);
  227.     Lcd_Write_4bits(data);
  228. }
  229.  
  230. /*******************************************************************************
  231. * @brief : Set cursor position on LCD display
  232. * @param[in] column : Column position
  233. * @param[in] line   : Line position
  234. *******************************************************************************/
  235. void set_cursor(int column, int line)
  236. {
  237.     unsigned char address;
  238.  
  239.     address = (line * 40) + column;
  240.     address = 0x80 + (address & 0x7F);
  241.     Lcd_Write_Command(address);               /* Set DDRAM address counter to 0     */
  242. }
  243.  
  244. /***************************************************************************//**
  245.  * @brief  Initial the LCD1602
  246.  ******************************************************************************/
  247. void Lcd_Init(void)
  248. {
  249.     char const *p;
  250.     int i;
  251.  
  252.     /* Enable clock for peripheral        */
  253.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  254.    
  255.     /* Set all pins for LCD as outputs    */
  256.     LCD_ALL_DIR_OUT();
  257.     Delay(15000);
  258.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  259.     Lcd_Write_4bits(0x3);  /* Select 4-bit interface  */
  260.     Delay(4100);
  261.     Lcd_Write_4bits(0x3);
  262.     Delay(100);
  263.     Lcd_Write_4bits(0x3);
  264.     Lcd_Write_4bits(0x2);
  265.    
  266.     Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix      */
  267.     Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
  268.     Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift   */
  269.  
  270.     /* Load user-specific characters into CGRAM                                 */
  271.     Lcd_Write_Command(0x40);                  /* Set CGRAM address counter to 0     */
  272.     p = &UserFont[0][0];
  273.     for (i = 0; i < sizeof(UserFont); i++, p++)
  274.         lcd_print (*p);
  275.     Lcd_Write_Command(0x80);                 /* Set DDRAM address counter to 0     */
  276. }
  277.  
  278. /***************************************************************************//**
  279.  * @brief   print a string on LCD1602.
  280.  * @param[in] *string : point to the string which will be printed on LCD.
  281.  ******************************************************************************/
  282. void lcd_print (char *string)
  283. {
  284.     while (*string)
  285.     {
  286.         Lcd_Write_Data (*string++);
  287.     }
  288. }
  289.  
  290. /*******************************************************************************
  291.  * @brief  Clear the LCD display                                                        *
  292. *******************************************************************************/
  293. void lcd_clear (void)
  294. {
  295.     Lcd_Write_Command(0x01);                  /* Display clear                      */
  296.     set_cursor (0, 0);
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement