pongfactory

FreeRTOS-LCD+KEYPAD V.2 (OK)

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