Advertisement
pongfactory

Full LCD + Keypad STM32 V.2

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