Advertisement
pongfactory

Full LCD + Keypad STM32 V.9 Beta

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