Advertisement
pongfactory

Full LCD + Keypad STM32 V.1(S)

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