pongfactory

FreeRTOS-LCD+KEYPAD V.5 (OK)

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