pongfactory

FreeRTOS-LCD+KEYPAD V.1 -- For Sent

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