pongfactory

FreeRTOS-LCD+KEYPAD V.1

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