Advertisement
pongfactory

Full LCD + Keypad STM32 V.1

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