pongfactory

FreeRTOS-LCD+KEYPAD V.6 -- Pass

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