Advertisement
pongfactory

Full LCD + Keypad STM32 V.8 (Last OK)

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