Advertisement
pongfactory

Full LCD + Keypad STM32 V.10 A

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