Advertisement
pongfactory

Full LCD + Keypad STM32 V.5 (Ckeck VERY OK)

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