pongfactory

FreeRTOS-LCD+KEYPAD V.3 (OK)

Mar 16th, 2014
140
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 26.18 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 = 0 ,countMinute = 0,countHour = 0;
  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.  
  121.     }
  122. }
  123.  
  124. void vTaskD (void* pvParameters){
  125.     for (;;) {
  126.         // **** First Show ****
  127.                     set_cursor(71, 0);
  128.                     lcd_print ("*D-M-Year");
  129.                     set_cursor(66, 0);
  130.                     lcd_print ("--:--");
  131.         // **** End First Show ****
  132.  
  133.         // Set test real time **************
  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){
  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  == countSecond && shiftBitMinute  == countMinute){
  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. int main(void)
  325. {
  326.                 GPIO_Config();
  327.                 init_USART1(9600); // initialize USART1 @ 115200 baud
  328.                 RCC_Configuration();
  329.                 Lcd_Init();
  330.  
  331.                 GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  332.                 GPIO_WriteBit(B_PORT,GPIO_Pin_5,Bit_SET);
  333.                 GPIO_WriteBit(B_PORT,GPIO_Pin_6,Bit_SET);
  334.                 GPIO_WriteBit(B_PORT,GPIO_Pin_7,Bit_SET);
  335.                 lcd_clear();                        /* clean the LCD */
  336.                 Delay(1000);
  337.  
  338.                 RCC_Configuration();
  339.                 Lcd_Init();
  340.                 lcd_clear();
  341.                 Delay(1000);
  342.                 // Main LCD do
  343.                 set_cursor(0,0);
  344.                 lcd_print ("Time =  ");
  345.  
  346.                 set_cursor(71, 0);
  347.                 lcd_print ("*D-M-Year");
  348.                 set_cursor(66, 0);
  349.                 lcd_print ("--:--");
  350.  
  351.  
  352.                 set_cursor(7,0);
  353.                 lcd_print ("0-:0-:--");
  354.                 //USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  355.  
  356.                 GPIO_init();
  357.  
  358.                 vSemaphoreCreateBinary( xSemaphore );
  359.                 xSemaphoreTake( xSemaphore, portMAX_DELAY );
  360.                 myQueue = xQueueCreate( 10, sizeof(uint8_t) );
  361.  
  362.             //  xTaskCreate( vTaskA, ( signed portCHAR * )"Task 1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  363.             //  xTaskCreate( vTaskB, ( signed portCHAR * )"Task 2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  364.             //  xTaskCreate( vTaskC, ( signed portCHAR * )"Task 3", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
  365.                 xTaskCreate( vTaskD, ( signed portCHAR * )"Task 4", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  366.             //  xTaskCreate( vTaskE, ( signed portCHAR * )"Task 5", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  367.  
  368.                 vTaskStartScheduler();
  369.                 return 0;
  370. }
  371.  
  372.  
  373. const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  374.                                      0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
  375.  
  376.  
  377. const char UserFont[8][8] = {  /* 8 user defined characters to be loaded into CGRAM (used for bargraph)*/
  378.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  379.     { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
  380.     { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
  381.     { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
  382.     { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
  383.     { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
  384.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  385.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
  386. };
  387.  
  388.  
  389. void GPIO_init(void)
  390. {
  391.     /* Enable the Clock */
  392.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  393.  
  394.     /* Initialize GPIO on STM32 board */
  395.     GPIO_InitTypeDef  GPIO_InitStructure;
  396.  
  397.     /* Configure the GPIO pin */
  398.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  399.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  400.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  401.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  402.  
  403.     GPIO_WriteBit(GPIOB,GPIO_Pin_All,Bit_RESET);
  404. }
  405.  
  406. int j;
  407. int i;
  408. int getKey(void)
  409. {
  410.          int key_pressed = 0;
  411.  
  412.           for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  413.             for (i=0; i < ROWS; i++) {
  414.               // output HIGH to all rows, except the j-th row
  415.                 if(i==j){
  416.                          GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
  417.                 }else{
  418.                          GPIO_WriteBit(B_PORT,output[i],Bit_SET);
  419.                 }
  420.             }
  421.             for (i=0; i < COLS; i++) {
  422.                 if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  423.                  // wait until the button is released.
  424.                  while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  425.                  key_pressed = keys[j][i]; // get the associated key for that button
  426.                  break;
  427.               }
  428.             }
  429.             GPIO_WriteBit(B_PORT,output[j],Bit_SET);
  430.             if ( key_pressed != 0 ) {
  431.               return key_pressed;
  432.             }
  433.           }
  434.   return 0; // no key pressed
  435. }
  436.  
  437. void GPIO_Config(void)
  438. {
  439.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  440.             RCC_APB2Periph_GPIOA, ENABLE);
  441.     //INPUT A = COLUMN
  442.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  443.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  444.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  445.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  446.  
  447.     //OUTPUT B = ROW
  448.     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 ;
  449.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  450.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  451.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  452.  
  453.     //USART1 (PA9)
  454.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  455.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  456.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  457.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  458.  
  459.  
  460. }
  461.  
  462. void init_USART1(uint32_t baudrate){
  463.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  464.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  465.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  466.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  467.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  468.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  469.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  470.  
  471.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  472.     USART_Cmd(USART1, ENABLE);
  473. }
  474.  
  475. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  476.     while(*s){
  477.         // wait until data register is empty
  478.         while( !(USARTx->SR & 0x00000040) );
  479.         USART_SendData(USARTx, *s);
  480.         *s++;
  481.     }
  482. }
  483.  
  484. void GPIO_LCD1602(void)
  485. {
  486.         //valueinLCD = getKey();
  487.     RCC_Configuration();
  488.     Lcd_Init();                         /* initial       */
  489.     lcd_clear();                        /* clean the LCD */
  490.     Delay(1000);
  491.     set_cursor(0,0);                    /* set cursor    */
  492.     lcd_print (" TestTest!  ");     /* display       */
  493.     set_cursor(0, 1);
  494.     lcd_print (" SetTime  ");
  495. }
  496.  
  497. /***************************************************************************//**
  498.  * @brief System clocks configuration
  499.  ******************************************************************************/
  500. void RCC_Configuration(void)
  501. {
  502.     RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  503.     RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */
  504.  
  505.     /* Wait till HSE is ready                                                   */
  506.     while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  507.  
  508.     RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  509.     RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  510.     RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  511.     RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */
  512.  
  513.     *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */
  514.  
  515.     /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  516.     RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
  517.  
  518.     RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */
  519.  
  520.     /* Wait till PLL is ready                                                   */
  521.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  522.  
  523.     /* Select PLL as system clock source                                        */
  524.     RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
  525.  
  526.     /* Wait till PLL is used as system clock source                             */
  527.     while (RCC_GetSYSCLKSource() != 0x08);
  528. }
  529.  
  530. /***************************************************************************//**
  531.  * @brief Delay some time
  532.  ******************************************************************************/
  533. void Delay(vu32 nCount)
  534. {
  535.     for(; nCount != 0; nCount--);
  536. }
  537.  
  538. /***************************************************************************//**
  539.  * @brief  Setting all pins to output mode
  540.  ******************************************************************************/
  541. void LCD_ALL_DIR_OUT(void)
  542. {
  543.     GPIO_InitTypeDef GPIO_InitStructure;
  544.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  545.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  546.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  547.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  548. }
  549.  
  550. /***************************************************************************//**
  551.  * @brief  Setting DATA pins to input mode
  552.  ******************************************************************************/
  553. void LCD_DATA_DIR_IN(void)
  554. {
  555.     GPIO_InitTypeDef GPIO_InitStructure;
  556.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  557.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  558.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  559.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  560. }
  561.  
  562. /***************************************************************************//**
  563.  * @brief  Setting DATA pins to output mode
  564.  ******************************************************************************/
  565. void LCD_DATA_DIR_OUT(void)
  566. {
  567.     GPIO_InitTypeDef GPIO_InitStructure;
  568.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  569.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  570.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  571.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  572. }
  573. /***************************************************************************//**
  574.  * @brief  Reading DATA pins
  575.  * @return the data value.
  576.  ******************************************************************************/
  577. unsigned int LCD_DATA_IN(void)
  578. {
  579.     uint16_t u16Temp=0;
  580.     u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
  581.     return SWAP_DATA[u16Temp];
  582. }
  583.  
  584. /***************************************************************************//**
  585.  * @brief  Read status of LCD controller
  586.  * @return status : Status of LCD controller
  587.  ******************************************************************************/
  588. static unsigned char Lcd_Read_Status (void)
  589. {
  590.     unsigned char status;
  591.  
  592.     LCD_DATA_DIR_IN();
  593.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  594.     GPIO_WriteBit(GPIOC, RW, Bit_SET);
  595.     Delay(10);
  596.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  597.     Delay(10);
  598.     status  = LCD_DATA_IN() << 4;
  599.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  600.     Delay(10);
  601.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  602.     Delay(10);
  603.     status |= LCD_DATA_IN();
  604.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  605.     LCD_DATA_DIR_OUT();
  606.     return (status);
  607. }
  608.  
  609. /***************************************************************************//**
  610.  * @brief Wait while LCD is busy
  611.  * @return status : Status of LCD controller
  612.  ******************************************************************************/
  613. static unsigned char Wait_While_Busy()
  614. {
  615.     unsigned char status;
  616.     do{
  617.     status = Lcd_Read_Status();
  618.     }while(status & 0x80);
  619.  
  620.     return status;
  621. }
  622. /***************************************************************************//**
  623.  * @brief  Write 4-bits to LCD controller
  624.  ******************************************************************************/
  625. void Lcd_Write_4bits(uc8 byte)
  626. {
  627.     uint16_t u16Temp=0;
  628.     GPIO_WriteBit(GPIOC, RW, Bit_RESET);
  629.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  630.     u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
  631.     u16Temp |=  SWAP_DATA[byte&0x0F];
  632.     GPIO_Write(GPIOC, u16Temp);
  633.     Delay(10);
  634.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  635.     Delay(10);
  636. }
  637.  
  638. /***************************************************************************//**
  639.  * @brief:    Write command to LCD controller
  640.  * @param[in] command :  Command to be written
  641.  ******************************************************************************/
  642. void Lcd_Write_Command(uc8 command)
  643. {
  644.     Wait_While_Busy();
  645.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  646.     Lcd_Write_4bits(command>>4);
  647.     Lcd_Write_4bits(command);
  648. }
  649.  
  650. /***************************************************************************//**
  651.  * @brief:     Write data to LCD controller
  652.   * @param[in] data :  Data to be written
  653.  ******************************************************************************/
  654. void Lcd_Write_Data(uc8 data)
  655. {
  656.     Wait_While_Busy();
  657.     GPIO_WriteBit(GPIOC, RS, Bit_SET);
  658.     Lcd_Write_4bits(data>>4);
  659.     Lcd_Write_4bits(data);
  660. }
  661.  
  662. /*******************************************************************************
  663. * @brief : Set cursor position on LCD display
  664. * @param[in] column : Column position
  665. * @param[in] line   : Line position
  666. *******************************************************************************/
  667. void set_cursor(int column, int line)
  668. {
  669.     unsigned char address;
  670.  
  671.     address = (line * 40) + column;
  672.     address = 0x80 + (address & 0x7F);
  673.     Lcd_Write_Command(address);               /* Set DDRAM address counter to 0     */
  674. }
  675.  
  676. /***************************************************************************//**
  677.  * @brief  Initial the LCD1602
  678.  ******************************************************************************/
  679. void Lcd_Init(void)
  680. {
  681.     char const *p;
  682.     int i;
  683.  
  684.     /* Enable clock for peripheral        */
  685.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  686.  
  687.     /* Set all pins for LCD as outputs    */
  688.     LCD_ALL_DIR_OUT();
  689.     Delay(15000);
  690.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  691.     Lcd_Write_4bits(0x3);  /* Select 4-bit interface  */
  692.     Delay(4100);
  693.     Lcd_Write_4bits(0x3);
  694.     Delay(100);
  695.     Lcd_Write_4bits(0x3);
  696.     Lcd_Write_4bits(0x2);
  697.  
  698.     Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix      */
  699.     Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
  700.     Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift   */
  701.  
  702.     /* Load user-specific characters into CGRAM                                 */
  703.     Lcd_Write_Command(0x40);                  /* Set CGRAM address counter to 0     */
  704.     p = &UserFont[0][0];
  705.     for (i = 0; i < sizeof(UserFont); i++, p++)
  706.         lcd_print (*p);
  707.     Lcd_Write_Command(0x80);                 /* Set DDRAM address counter to 0     */
  708. }
  709.  
  710. /***************************************************************************//**
  711.  * @brief   print a string on LCD1602.
  712.  * @param[in] *string : point to the string which will be printed on LCD.
  713.  ******************************************************************************/
  714. void lcd_print (char *string)
  715. {
  716.         int i;
  717.  
  718.           for (i=0;i<16 && string[i]!=0;i++)                                               // 16 Character Print
  719.           {
  720.                   Lcd_Write_Data(string[i]);                                                    // Print Byte to LCD
  721.           }
  722.         /*
  723.     while (*string)
  724.     {
  725.         Lcd_Write_Data (*string++);
  726.     }
  727.     */
  728. }
  729.  
  730. /*******************************************************************************
  731.  * @brief  Clear the LCD display                                                        *
  732. *******************************************************************************/
  733. void lcd_clear (void)
  734. {
  735.     Lcd_Write_Command(0x01);                  /* Display clear                      */
  736.     set_cursor (0, 0);
  737. }
Add Comment
Please, Sign In to add comment