Advertisement
pongfactory

Full LCD + Keypad STM32 V.7 (VERY OK)

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