Advertisement
pongfactory

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

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