Advertisement
pongfactory

Full LCD + Keypad STM32 V.4 (Ckeck OK)

Mar 14th, 2014
135
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 23.65 KB | None | 0 0
  1. #include "stm32f10x.h"
  2. #include "stm32f10x_tim.h"
  3. #include "stm32f10x_rcc.h"
  4. #include "stm32f10x_gpio.h"
  5. #include "stm32f10x_usart.h"
  6. #include "misc.h"
  7. #include "stm32f10x_exti.h"
  8. #include <stdio.h>
  9. #include <stdlib.h>
  10. #include <string.h>
  11. #include <stdint.h>
  12. #include <time.h>
  13.  
  14. USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
  15. GPIO_InitTypeDef  GPIO_InitStructure;
  16.  
  17. void GPIO_Config(void);
  18. void init_USART1(uint32_t baudrate);
  19. void USART_puts(USART_TypeDef* USARTx, volatile char *s);
  20.  
  21. #define EN  GPIO_Pin_10
  22. #define RS  GPIO_Pin_12
  23. #define RW  GPIO_Pin_11
  24.  
  25. const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  26.                                      0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
  27.  
  28.  
  29. const char UserFont[8][8] = {  /* 8 user defined characters to be loaded into CGRAM (used for bargraph)*/
  30.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  31.     { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
  32.     { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
  33.     { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
  34.     { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
  35.     { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
  36.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  37.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
  38. };
  39.  
  40. /***************************************************************************//**
  41.  * Declare function prototypes
  42.  ******************************************************************************/
  43. void RCC_Configuration(void);
  44. void LCD_DATA_DIR_OUT(void);
  45. void LCD_DATA_DIR_IN(void);
  46. void LCD_ALL_DIR_OUT(void);
  47. unsigned int LCD_DATA_IN(void);
  48. static unsigned char Wait_While_Busy(void);
  49. static unsigned char Lcd_Read_Status (void);
  50. void Lcd_Write_4bits(uc8 byte);
  51. void Delay(vu32 nCount);
  52. void Lcd_Write_Command(uc8 command);
  53. void Lcd_Write_Data(uc8 data);
  54. void Lcd_Init(void);
  55. void Lcd_Write_Line1(void);
  56. void Lcd_Write_Line2(void);
  57. void set_cursor(int, int);
  58. void lcd_print (char *string);
  59. void lcd_clear (void);
  60.  
  61. // **** IN Time
  62. char textSecond[100] = "";
  63. char textMinute[100] = "";
  64. char textHour[100] = "";
  65. char text[100] = "";
  66. char text2[100] = "";
  67. char text3[100] = "";
  68. char text4[100] = "";
  69. // Wait Change to loop
  70. char Date1[100] = "";
  71. char Date2[100] = "";
  72. char Date3[100] = "";
  73. char Date4[100] = "";
  74. char Date5[100] = "";
  75. char Date6[100] = "";
  76. char Date7[100] = "";
  77. char Date8[100] = "";
  78. char checkOut[100] = "";
  79. int data[100];
  80. int i = 0;
  81. int j = 0;
  82. int count =0;
  83. int state = 0;
  84. uint32_t last_time = 0;
  85. uint32_t temp_time = 0;
  86. uint32_t temp = 0; //store data to check
  87. int value,value1,value2,value3,value4;
  88. char valueS[100] = "";
  89. int tempValue;
  90. int column;
  91. int row;
  92. int loop1 = 0;
  93. // constant values
  94. const int ROWS = 4;
  95. const int COLS = 4;
  96. int keys[4][4] = {{'1','2','3','A'},{'4','5','6','B'},{'7','8','9','C'},{'*','0','#','D'}};
  97. int input[4] = {((uint16_t)0x0010),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
  98. int output[4] = {((uint16_t)0x0004),((uint16_t)0x0020),((uint16_t)0x0040),((uint16_t)0x0080)};
  99. int rowPins[4] = {0,1,2,3};   // connect B0,1,2,3 to Rows 1-4 (the left four pins)
  100. int colPins[4] = {0,1,2,3};   // connect A0,1,2,3 to Column 1-4 (the right four pins)
  101. #define A_PORT GPIOA
  102. #define B_PORT GPIOB
  103. int key;
  104. int loopLCD;
  105. int loopTime;
  106. char checkLast[10];
  107. void GPIO_LCD1602(void);
  108.  
  109.  
  110. int main(void)
  111. {
  112. int tempValue;
  113. char* strCheckBit[4];
  114. char* strTime[4];
  115. char* strDate[8];
  116. strTime[2] = "Set  :  ";
  117. char* strings[60];
  118.  
  119. // *** For check keypad + time
  120. int checkbit1;
  121. int checkbit2;
  122. int checkbit3;
  123. int checkbit4;
  124. // *** End Value check
  125.  
  126.     GPIO_Config();
  127.     init_USART1(9600); // initialize USART1 @ 115200 baud
  128.     RCC_Configuration();
  129.     Lcd_Init();
  130.  
  131.     GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  132.     GPIO_WriteBit(B_PORT,GPIO_Pin_5,Bit_SET);
  133.     GPIO_WriteBit(B_PORT,GPIO_Pin_6,Bit_SET);
  134.     GPIO_WriteBit(B_PORT,GPIO_Pin_7,Bit_SET);
  135.     lcd_clear();                        /* clean the LCD */
  136.     Delay(1000);
  137.  
  138.     USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  139.     char* setTime[5];
  140.     char* keepTime[5];
  141.     int tempINT = 0;
  142.     int cLoop;
  143.     int var = 0;
  144.     int check = 0;
  145.     char keep;
  146.     char inpass[ ] = "";
  147.     int index = 0;
  148.     int iLoop;
  149.     int countSecond = 0;
  150.     int countMinute = 0;
  151.     int countHour = 0;
  152.     char checkString[10];
  153.     char b[10];
  154.     uint8_t state = 0;
  155.     // **** Time by Time.h ****
  156.  
  157.                     RCC_Configuration();
  158.                     Lcd_Init();
  159.                     lcd_clear();
  160.                     Delay(1000);
  161.                     // Main LCD do
  162.                     set_cursor(0,0);
  163.                     lcd_print ("Time =  ");
  164.  
  165. /*
  166.                    // Clock is 00 : XX
  167.                     set_cursor(7,0);
  168.                     lcd_print ("0");
  169.                     set_cursor(8,0);
  170.                     lcd_print ("0");
  171.  
  172.                     set_cursor(9,0);
  173.                     lcd_print (":");
  174. */
  175.                     set_cursor(71, 0);
  176.                     lcd_print ("*D-M-Year");
  177.                     set_cursor(66, 0);
  178.                     lcd_print ("--:--");
  179.  
  180.  
  181.                     set_cursor(7,0);
  182.                     lcd_print ("--:--:--");
  183.  
  184.  
  185.  
  186.                   //  set_cursor(7,0);
  187.                   //  lcd_print ("IC-DS1307");
  188.  
  189.  
  190.     // ****
  191.     while (1)
  192.     {
  193.  
  194.                     // Set test real time **************
  195.                     sprintf (textSecond, "%d", countSecond);
  196.                     set_cursor(13,0);                    /* set cursor    */
  197.                     lcd_print (textSecond);     /* display       */
  198.                     countSecond = countSecond + 1;
  199.                     if(countSecond == 60){
  200.                         sprintf (textMinute, "%d", countMinute);
  201.                         set_cursor(10,0);                    /* set cursor    */
  202.                         lcd_print (textMinute);     /* display       */
  203.                         set_cursor(14,0);                    /* set cursor    */
  204.                         lcd_print ("-");     /* display       */
  205.                         countMinute = countMinute + 1;
  206.                         countSecond = 0;
  207.                         if(countMinute == 60){
  208.                             sprintf (textHour, "%d", countHour);
  209.                             set_cursor(7,0);                    /* set cursor    */
  210.                             lcd_print (textMinute);     /* display       */
  211.                             set_cursor(11,0);                    /* set cursor    */
  212.                             lcd_print ("-");     /* display       */
  213.                             countHour = countHour + 1;
  214.                             countMinute = 0;
  215.                             if(countHour == 24){
  216.                                 countHour = 0;
  217.                             }
  218.                         }
  219.  
  220.                     }
  221.                     // End Set test real time ***********
  222.  
  223.  
  224.                     set_cursor(64, 0);
  225.                     lcd_print ("T=");
  226.                     value = getKey();
  227.  
  228.                     set_cursor(68, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  229.                     lcd_print (":");
  230.  
  231.                     if(value !=0){
  232.                     // First For Time
  233.                     if(count == 0){
  234.                     sprintf (text, "%c", value);
  235.                     USART_puts(USART1, text);
  236.                     strCheckBit[0] = text;
  237.                     }
  238.                     else if(count == 1){
  239.                     sprintf (text2, "%c", value);
  240.                     USART_puts(USART1, text2);
  241.                     strCheckBit[1] = text;
  242.                     }
  243.                     else if(count == 2){
  244.                     sprintf (text3, "%c", value);
  245.                     USART_puts(USART1, text3);
  246.                     }
  247.                     else if(count == 3){
  248.                     sprintf (text4, "%c", value);
  249.                     USART_puts(USART1, text4);
  250.                    // count = -1;
  251.                     }
  252.                     // Last For Time
  253.  
  254.                     //First For Date  ***
  255.                     else if(count == 4){
  256.                     sprintf (Date1, "%c", value);
  257.                     USART_puts(USART1, Date1);
  258.                     }
  259.                     else if(count == 5){
  260.                     sprintf (Date2, "%c", value);
  261.                     USART_puts(USART1, Date2);
  262.                     }
  263.                     else if(count == 6){
  264.                     sprintf (Date3, "%c", value);
  265.                     USART_puts(USART1, Date3);
  266.                     }
  267.                     else if(count == 7){
  268.                     sprintf (Date4, "%c", value);
  269.                     USART_puts(USART1, Date4);
  270.                     }
  271.                     // Year
  272.                     else if(count == 8){
  273.                     sprintf (Date5, "%c", value);
  274.                     USART_puts(USART1, Date5);
  275.                     }
  276.                     else if(count == 9){
  277.                     sprintf (Date6, "%c", value);
  278.                     USART_puts(USART1, Date6);
  279.                     }
  280.                     else if(count == 10){
  281.                     sprintf (Date7, "%c", value);
  282.                     USART_puts(USART1, Date7);
  283.                     }
  284.                     else if(count == 11){
  285.                     sprintf (Date8, "%c", value);
  286.                     USART_puts(USART1, Date8);
  287.                     count = -1;
  288.                     }
  289.                     // Last For Date  ***
  290.  
  291.                     count = count + 1;
  292.                     }
  293.  
  294.                     // For Time is Hour : Minute
  295.                     strTime[0] = text;
  296.                     strTime[1] = text2;
  297.                     strTime[2] = text3;
  298.                     strTime[3] = text4;
  299.                     set_cursor(66, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  300.                     lcd_print (strTime[0]);
  301.                     set_cursor(67, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  302.                     lcd_print (strTime[1]);
  303.                     set_cursor(69, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  304.                     lcd_print (strTime[2]);
  305.                     set_cursor(70, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  306.                     lcd_print (strTime[3]);
  307.  
  308.  
  309.                     // For Date is Day : Month : Year
  310.                     strDate[0] = Date1;
  311.                     strDate[1] = Date2;
  312.                     strDate[2] = Date3;
  313.                     strDate[3] = Date4;
  314.                     strDate[4] = Date5;
  315.                     strDate[5] = Date6;
  316.                     strDate[6] = Date7;
  317.                     strDate[7] = Date8;
  318.                     set_cursor(72, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  319.                     lcd_print (strDate[0]);
  320.                     set_cursor(73, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  321.                     lcd_print (strDate[1]);
  322.                     set_cursor(74, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  323.                     lcd_print (strDate[2]);
  324.                     set_cursor(75, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  325.                     lcd_print (strDate[3]);
  326.                     set_cursor(76, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  327.                     lcd_print (strDate[4]);
  328.                     set_cursor(77, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  329.                     lcd_print (strDate[5]);
  330.                     set_cursor(78, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  331.                     lcd_print (strDate[6]);
  332.                     set_cursor(79, 0);          // *** set_cursor(64, 0); is Column 0 in row 1 ***
  333.                     lcd_print (strDate[7]);
  334.  
  335.  
  336.                     // State Check Bit Real Time + Set Time
  337.                     checkbit1 = atoi(strTime[0]);
  338.                     checkbit2 = atoi(strTime[1]);
  339.                     checkbit3 = atoi(strTime[2]);
  340.                     checkbit4 = atoi(strTime[3]);
  341.                     if(countSecond == checkbit1){
  342.                         GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_SET);
  343.                     }else{
  344.                         GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_RESET);
  345.                     }
  346.                     // End State Check Bit Real Time + Set Time
  347.  
  348.                     // For Test LED
  349.                     GPIO_WriteBit(B_PORT,GPIO_Pin_8,Bit_SET);
  350.  
  351.                     for(i=0;i<0x100000;i++); // is 1 second
  352.  
  353.                     set_cursor(9,0);
  354.                     lcd_print (":");
  355.                     set_cursor(12,0);
  356.                     lcd_print (":");
  357.  
  358.             //      } // Close Loop is Change LoopLCD
  359.  
  360.                     set_cursor(0, 1);
  361.                     lcd_print ("        ");
  362.  
  363.     }
  364. }
  365.  
  366.  
  367. int j;
  368. int i;
  369. int getKey(void)
  370. {
  371.      int key_pressed = 0;
  372.  
  373.       for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  374.         for (i=0; i < ROWS; i++) {
  375.           // output HIGH to all rows, except the j-th row
  376.             if(i==j){
  377.                  GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
  378.             }else{
  379.                  GPIO_WriteBit(B_PORT,output[i],Bit_SET);
  380.             }
  381.         }
  382.         for (i=0; i < COLS; i++) {
  383.             if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  384.              // wait until the button is released.
  385.              while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  386.              key_pressed = keys[j][i]; // get the associated key for that button
  387.              break;
  388.           }
  389.         }
  390.         GPIO_WriteBit(B_PORT,output[j],Bit_SET);
  391.         if ( key_pressed != 0 ) {
  392.           return key_pressed;
  393.         }
  394.       }
  395.   return 0; // no key pressed
  396. }
  397.  
  398. void GPIO_Config(void)
  399. {
  400.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  401.             RCC_APB2Periph_GPIOA, ENABLE);
  402.     //INPUT A = COLUMN
  403.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  404.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  405.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  406.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  407.  
  408.     //OUTPUT B = ROW
  409.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 ;
  410.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  411.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  412.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  413.  
  414.     //USART1 (PA9)
  415.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  416.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  417.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  418.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  419.  
  420.  
  421. }
  422.  
  423. void init_USART1(uint32_t baudrate){
  424.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  425.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  426.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  427.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  428.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  429.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  430.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  431.  
  432.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  433.     USART_Cmd(USART1, ENABLE);
  434. }
  435.  
  436. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  437.     while(*s){
  438.         // wait until data register is empty
  439.         while( !(USARTx->SR & 0x00000040) );
  440.         USART_SendData(USARTx, *s);
  441.         *s++;
  442.     }
  443. }
  444.  
  445. void GPIO_LCD1602(void)
  446. {
  447.     //valueinLCD = getKey();
  448.     RCC_Configuration();
  449.     Lcd_Init();                         /* initial       */
  450.     lcd_clear();                        /* clean the LCD */
  451.     Delay(1000);
  452.     set_cursor(0,0);                    /* set cursor    */
  453.     lcd_print (" TestTest!  ");     /* display       */
  454.     set_cursor(0, 1);
  455.     lcd_print (" SetTime  ");
  456. }
  457.  
  458. /***************************************************************************//**
  459.  * @brief System clocks configuration
  460.  ******************************************************************************/
  461. void RCC_Configuration(void)
  462. {
  463.     RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  464.     RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */
  465.  
  466.     /* Wait till HSE is ready                                                   */
  467.     while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  468.  
  469.     RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  470.     RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  471.     RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  472.     RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */
  473.  
  474.     *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */
  475.  
  476.     /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  477.     RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
  478.  
  479.     RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */
  480.  
  481.     /* Wait till PLL is ready                                                   */
  482.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  483.  
  484.     /* Select PLL as system clock source                                        */
  485.     RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
  486.  
  487.     /* Wait till PLL is used as system clock source                             */
  488.     while (RCC_GetSYSCLKSource() != 0x08);
  489. }
  490.  
  491. /***************************************************************************//**
  492.  * @brief Delay some time
  493.  ******************************************************************************/
  494. void Delay(vu32 nCount)
  495. {
  496.     for(; nCount != 0; nCount--);
  497. }
  498.  
  499. /***************************************************************************//**
  500.  * @brief  Setting all pins to output mode
  501.  ******************************************************************************/
  502. void LCD_ALL_DIR_OUT(void)
  503. {
  504.     GPIO_InitTypeDef GPIO_InitStructure;
  505.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  506.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  507.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  508.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  509. }
  510.  
  511. /***************************************************************************//**
  512.  * @brief  Setting DATA pins to input mode
  513.  ******************************************************************************/
  514. void LCD_DATA_DIR_IN(void)
  515. {
  516.     GPIO_InitTypeDef GPIO_InitStructure;
  517.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  518.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  519.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  520.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  521. }
  522.  
  523. /***************************************************************************//**
  524.  * @brief  Setting DATA pins to output mode
  525.  ******************************************************************************/
  526. void LCD_DATA_DIR_OUT(void)
  527. {
  528.     GPIO_InitTypeDef GPIO_InitStructure;
  529.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  530.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  531.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  532.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  533. }
  534. /***************************************************************************//**
  535.  * @brief  Reading DATA pins
  536.  * @return the data value.
  537.  ******************************************************************************/
  538. unsigned int LCD_DATA_IN(void)
  539. {
  540.     uint16_t u16Temp=0;
  541.     u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
  542.     return SWAP_DATA[u16Temp];
  543. }
  544.  
  545. /***************************************************************************//**
  546.  * @brief  Read status of LCD controller
  547.  * @return status : Status of LCD controller
  548.  ******************************************************************************/
  549. static unsigned char Lcd_Read_Status (void)
  550. {
  551.     unsigned char status;
  552.  
  553.     LCD_DATA_DIR_IN();
  554.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  555.     GPIO_WriteBit(GPIOC, RW, Bit_SET);
  556.     Delay(10);
  557.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  558.     Delay(10);
  559.     status  = LCD_DATA_IN() << 4;
  560.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  561.     Delay(10);
  562.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  563.     Delay(10);
  564.     status |= LCD_DATA_IN();
  565.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  566.     LCD_DATA_DIR_OUT();
  567.     return (status);
  568. }
  569.  
  570. /***************************************************************************//**
  571.  * @brief Wait while LCD is busy
  572.  * @return status : Status of LCD controller
  573.  ******************************************************************************/
  574. static unsigned char Wait_While_Busy()
  575. {
  576.     unsigned char status;
  577.     do{
  578.     status = Lcd_Read_Status();
  579.     }while(status & 0x80);
  580.  
  581.     return status;
  582. }
  583. /***************************************************************************//**
  584.  * @brief  Write 4-bits to LCD controller
  585.  ******************************************************************************/
  586. void Lcd_Write_4bits(uc8 byte)
  587. {
  588.     uint16_t u16Temp=0;
  589.     GPIO_WriteBit(GPIOC, RW, Bit_RESET);
  590.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  591.     u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
  592.     u16Temp |=  SWAP_DATA[byte&0x0F];
  593.     GPIO_Write(GPIOC, u16Temp);
  594.     Delay(10);
  595.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  596.     Delay(10);
  597. }
  598.  
  599. /***************************************************************************//**
  600.  * @brief:    Write command to LCD controller
  601.  * @param[in] command :  Command to be written
  602.  ******************************************************************************/
  603. void Lcd_Write_Command(uc8 command)
  604. {
  605.     Wait_While_Busy();
  606.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  607.     Lcd_Write_4bits(command>>4);
  608.     Lcd_Write_4bits(command);
  609. }
  610.  
  611. /***************************************************************************//**
  612.  * @brief:     Write data to LCD controller
  613.   * @param[in] data :  Data to be written
  614.  ******************************************************************************/
  615. void Lcd_Write_Data(uc8 data)
  616. {
  617.     Wait_While_Busy();
  618.     GPIO_WriteBit(GPIOC, RS, Bit_SET);
  619.     Lcd_Write_4bits(data>>4);
  620.     Lcd_Write_4bits(data);
  621. }
  622.  
  623. /*******************************************************************************
  624. * @brief : Set cursor position on LCD display
  625. * @param[in] column : Column position
  626. * @param[in] line   : Line position
  627. *******************************************************************************/
  628. void set_cursor(int column, int line)
  629. {
  630.     unsigned char address;
  631.  
  632.     address = (line * 40) + column;
  633.     address = 0x80 + (address & 0x7F);
  634.     Lcd_Write_Command(address);               /* Set DDRAM address counter to 0     */
  635. }
  636.  
  637. /***************************************************************************//**
  638.  * @brief  Initial the LCD1602
  639.  ******************************************************************************/
  640. void Lcd_Init(void)
  641. {
  642.     char const *p;
  643.     int i;
  644.  
  645.     /* Enable clock for peripheral        */
  646.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  647.  
  648.     /* Set all pins for LCD as outputs    */
  649.     LCD_ALL_DIR_OUT();
  650.     Delay(15000);
  651.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  652.     Lcd_Write_4bits(0x3);  /* Select 4-bit interface  */
  653.     Delay(4100);
  654.     Lcd_Write_4bits(0x3);
  655.     Delay(100);
  656.     Lcd_Write_4bits(0x3);
  657.     Lcd_Write_4bits(0x2);
  658.  
  659.     Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix      */
  660.     Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
  661.     Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift   */
  662.  
  663.     /* Load user-specific characters into CGRAM                                 */
  664.     Lcd_Write_Command(0x40);                  /* Set CGRAM address counter to 0     */
  665.     p = &UserFont[0][0];
  666.     for (i = 0; i < sizeof(UserFont); i++, p++)
  667.         lcd_print (*p);
  668.     Lcd_Write_Command(0x80);                 /* Set DDRAM address counter to 0     */
  669. }
  670.  
  671. /***************************************************************************//**
  672.  * @brief   print a string on LCD1602.
  673.  * @param[in] *string : point to the string which will be printed on LCD.
  674.  ******************************************************************************/
  675. void lcd_print (char *string)
  676. {
  677.     int i;
  678.  
  679.       for (i=0;i<16 && string[i]!=0;i++)                                               // 16 Character Print
  680.       {
  681.           Lcd_Write_Data(string[i]);                                                    // Print Byte to LCD
  682.       }
  683.     /*
  684.     while (*string)
  685.     {
  686.         Lcd_Write_Data (*string++);
  687.     }
  688.     */
  689. }
  690.  
  691. /*******************************************************************************
  692.  * @brief  Clear the LCD display                                                        *
  693. *******************************************************************************/
  694. void lcd_clear (void)
  695. {
  696.     Lcd_Write_Command(0x01);                  /* Display clear                      */
  697.     set_cursor (0, 0);
  698. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement