Advertisement
pongfactory

Full LCD + Keypad STM32 V.3 (Ckeck OK)

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