Advertisement
pongfactory

Full LCD + Keypad STM32 V.8 (Already)

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