pongfactory

FreeRTOS-LCD+KEYPAD V.4 (OK)

Mar 16th, 2014
144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 32.52 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.         // **** First Show ****
  322.                     set_cursor(71, 0);
  323.                     lcd_print ("*D-M-Year");
  324.                     set_cursor(66, 0);
  325.                     lcd_print ("--:--");
  326.         // **** End First Show ****
  327.  
  328.         // Set test real time **************
  329.                                 sprintf (textSecond, "%02X", countSecond);
  330.                     set_cursor(13,0);                    /* set cursor    */
  331.                     lcd_print (textSecond);     /* display       */
  332.                     countSecond = countSecond + 1;
  333.                     // Cut A-F
  334.                     tempTimeSecond = countSecond&0x0F;
  335.                     if(tempTimeSecond == 0x0A){
  336.                         countSecond = countSecond + 6;
  337.                     }
  338.                     // End Cut A-F
  339.                     if(countSecond == 0x60){
  340.                         countMinute = countMinute + 1;
  341.                         // Cut A-F
  342.                         tempTimeMinute = countMinute&0x0F;
  343.                         if(tempTimeMinute == 0x0A){
  344.                            countMinute = countMinute + 6;
  345.                         }
  346.                         // End Cut A-F
  347.                         sprintf (textMinute, "%02X", countMinute);
  348.                         set_cursor(10,0);                    /* set cursor    */
  349.                         lcd_print (textMinute);     /* display       */
  350.                         set_cursor(14,0);                    /* set cursor    */
  351.                         lcd_print ("-");     /* display       */
  352.                     //  countMinute = countMinute + 1;
  353.                         countSecond = 0;
  354.                         if(countMinute == 0x60){
  355.                                 countHour = countHour + 1;
  356.                                 // Cut A-F
  357.                                 tempTimeHour = countHour&0x0F;
  358.                                 if(tempTimeHour == 0x0A){
  359.                                     countHour = countHour + 6;
  360.                                 }
  361.                                 // End Cut A-F
  362.                                 sprintf (textHour, "%02X", countHour);
  363.                                 set_cursor(7,0);                    /* set cursor    */
  364.                                 lcd_print (textHour);     /* display       */
  365.                                 set_cursor(11,0);                    /* set cursor    */
  366.                                 lcd_print ("-");     /* display       */
  367.                                 //countHour = countHour + 1;
  368.                                 countMinute = 0;
  369.                                 if(countHour == 0x24){  // 24 Hours
  370.                                         countHour = 0;
  371.                                 }
  372.                         }
  373.  
  374.                     }
  375.                     // End Set test real time ***********
  376.  
  377.                         set_cursor(64, 0);
  378.                         lcd_print ("T=");
  379.                         value = getKey();
  380.  
  381.                         set_cursor(68, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  382.                         lcd_print (":");
  383.  
  384.                         if(value !=0){
  385.                         // First For Time
  386.                         if(count == 0){
  387.                         sprintf (text, "%c", value);
  388.                         USART_puts(USART1, text);
  389.                         }
  390.                         else if(count == 1){
  391.                         sprintf (text2, "%c", value);
  392.                         USART_puts(USART1, text2);
  393.                         }
  394.                         else if(count == 2){
  395.                         sprintf (text3, "%c", value);
  396.                         USART_puts(USART1, text3);
  397.                         }
  398.                         else if(count == 3){
  399.                         sprintf (text4, "%c", value);
  400.                         USART_puts(USART1, text4);
  401.                        // count = -1;
  402.                         }
  403.                         // Last For Time
  404.  
  405.                         //First For Date  ***
  406.                         else if(count == 4){
  407.                         sprintf (Date1, "%c", value);
  408.                         USART_puts(USART1, Date1);
  409.                         }
  410.                         else if(count == 5){
  411.                         sprintf (Date2, "%c", value);
  412.                         USART_puts(USART1, Date2);
  413.                         }
  414.                         else if(count == 6){
  415.                         sprintf (Date3, "%c", value);
  416.                         USART_puts(USART1, Date3);
  417.                         }
  418.                         else if(count == 7){
  419.                         sprintf (Date4, "%c", value);
  420.                         USART_puts(USART1, Date4);
  421.                         }
  422.                         // Year
  423.                         else if(count == 8){
  424.                         sprintf (Date5, "%c", value);
  425.                         USART_puts(USART1, Date5);
  426.                         }
  427.                         else if(count == 9){
  428.                         sprintf (Date6, "%c", value);
  429.                         USART_puts(USART1, Date6);
  430.                         }
  431.                         else if(count == 10){
  432.                         sprintf (Date7, "%c", value);
  433.                         USART_puts(USART1, Date7);
  434.                         }
  435.                         else if(count == 11){
  436.                         sprintf (Date8, "%c", value);
  437.                         USART_puts(USART1, Date8);
  438.                         count = -1;
  439.                         }
  440.                         // Last For Date  ***
  441.  
  442.                         count = count + 1;
  443.                         }
  444.  
  445.                         // For Time is Hour : Minute
  446.                         strTime[0] = text;
  447.                         strTime[1] = text2;
  448.                         strTime[2] = text3;
  449.                         strTime[3] = text4;
  450.                         set_cursor(66, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  451.                         lcd_print (strTime[0]);
  452.                         set_cursor(67, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  453.                         lcd_print (strTime[1]);
  454.                         set_cursor(69, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  455.                         lcd_print (strTime[2]);
  456.                         set_cursor(70, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  457.                         lcd_print (strTime[3]);
  458.  
  459.  
  460.                         // For Date is Day : Month : Year
  461.                         strDate[0] = Date1;
  462.                         strDate[1] = Date2;
  463.                         strDate[2] = Date3;
  464.                         strDate[3] = Date4;
  465.                         strDate[4] = Date5;
  466.                         strDate[5] = Date6;
  467.                         strDate[6] = Date7;
  468.                         strDate[7] = Date8;
  469.                         set_cursor(72, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  470.                         lcd_print (strDate[0]);
  471.                         set_cursor(73, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  472.                         lcd_print (strDate[1]);
  473.                         set_cursor(74, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  474.                         lcd_print (strDate[2]);
  475.                         set_cursor(75, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  476.                         lcd_print (strDate[3]);
  477.                         set_cursor(76, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  478.                         lcd_print (strDate[4]);
  479.                         set_cursor(77, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  480.                         lcd_print (strDate[5]);
  481.                         set_cursor(78, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  482.                         lcd_print (strDate[6]);
  483.                         set_cursor(79, 0);                  // *** set_cursor(64, 0); is Column 0 in row 1 ***
  484.                         lcd_print (strDate[7]);
  485.  
  486.  
  487.                         // State Check Bit Real Time + Set Time
  488.                         checkbit1 = atoi(strTime[0]);
  489.                         checkbit2 = atoi(strTime[1]);
  490.                         checkbit3 = atoi(strTime[2]);
  491.                         checkbit4 = atoi(strTime[3]);
  492.                         //   sprintf (sepSecond, "%d", countSecond);   // is Char
  493.  
  494.                         shiftBitMinute = (checkbit1<<4)+(checkbit2);
  495.                         shiftBitSecond = (checkbit3<<4)+(checkbit4);
  496.                         if(shiftBitSecond  == countSecond && shiftBitMinute  == countMinute){
  497.                         //   if(countMinute == checkbit1 && countSecond == checkbit3  ){
  498.                             GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_SET);
  499.                         }else{
  500.                             GPIO_WriteBit(B_PORT,GPIO_Pin_10,Bit_RESET);
  501.                         }
  502.                         // End State Check Bit Real Time + Set Time
  503.  
  504.                         // For Test LED
  505.                         GPIO_WriteBit(B_PORT,GPIO_Pin_8,Bit_SET);
  506.  
  507.                         for(i=0;i<0x100000;i++); // is 1 second
  508.  
  509.                         set_cursor(9,0);
  510.                         lcd_print (":");
  511.                         set_cursor(12,0);
  512.                         lcd_print (":");
  513.                         set_cursor(0, 1);
  514.                         lcd_print ("                ");
  515.  
  516.     }
  517. }
  518.  
  519. int main(void)
  520. {
  521.                 GPIO_Config();
  522.                 init_USART1(9600); // initialize USART1 @ 115200 baud
  523.                 RCC_Configuration();
  524.                 Lcd_Init();
  525.  
  526.                 GPIO_WriteBit(B_PORT,GPIO_Pin_2,Bit_SET);
  527.                 GPIO_WriteBit(B_PORT,GPIO_Pin_5,Bit_SET);
  528.                 GPIO_WriteBit(B_PORT,GPIO_Pin_6,Bit_SET);
  529.                 GPIO_WriteBit(B_PORT,GPIO_Pin_7,Bit_SET);
  530.                 lcd_clear();                        /* clean the LCD */
  531.                 Delay(1000);
  532.  
  533.                 RCC_Configuration();
  534.                 Lcd_Init();
  535.                 lcd_clear();
  536.                 Delay(1000);
  537.                 // Main LCD do
  538.                 set_cursor(0,0);
  539.                 lcd_print ("Time =  ");
  540.  
  541.                 set_cursor(71, 0);
  542.                 lcd_print ("*D-M-Year");
  543.                 set_cursor(66, 0);
  544.                 lcd_print ("--:--");
  545.  
  546.  
  547.                 set_cursor(7,0);
  548.                 lcd_print ("0-:0-:--");
  549.                 //USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
  550.  
  551.                 GPIO_init();
  552.  
  553.                 vSemaphoreCreateBinary( xSemaphore );
  554.                 xSemaphoreTake( xSemaphore, portMAX_DELAY );
  555.                 myQueue = xQueueCreate( 10, sizeof(uint8_t) );
  556.  
  557.             //  xTaskCreate( vTaskA, ( signed portCHAR * )"Task 1", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  558.             //  xTaskCreate( vTaskB, ( signed portCHAR * )"Task 2", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  559.                 xTaskCreate( vTaskC, ( signed portCHAR * )"Task 3", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, &xHandle );
  560.             //  xTaskCreate( vTaskD, ( signed portCHAR * )"Task 4", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  561.             //  xTaskCreate( vTaskE, ( signed portCHAR * )"Task 5", configMINIMAL_STACK_SIZE, NULL, tskIDLE_PRIORITY, NULL );
  562.  
  563.                 vTaskStartScheduler();
  564.                 return 0;
  565. }
  566.  
  567.  
  568. const unsigned int SWAP_DATA[16] = { 0x0, 0x8, 0x4, 0xC, 0x2, 0xA, 0x6, 0xE,
  569.                                      0x1, 0x9, 0x5, 0xD, 0x3, 0xB, 0x7, 0xF};
  570.  
  571.  
  572. const char UserFont[8][8] = {  /* 8 user defined characters to be loaded into CGRAM (used for bargraph)*/
  573.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  574.     { 0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10 },
  575.     { 0x18,0x18,0x18,0x18,0x18,0x18,0x18,0x18 },
  576.     { 0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C,0x1C },
  577.     { 0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E,0x1E },
  578.     { 0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F,0x1F },
  579.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 },
  580.     { 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00 }
  581. };
  582.  
  583.  
  584. void GPIO_init(void)
  585. {
  586.     /* Enable the Clock */
  587.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);
  588.  
  589.     /* Initialize GPIO on STM32 board */
  590.     GPIO_InitTypeDef  GPIO_InitStructure;
  591.  
  592.     /* Configure the GPIO pin */
  593.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  594.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  595.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  596.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  597.  
  598.     GPIO_WriteBit(GPIOB,GPIO_Pin_All,Bit_RESET);
  599. }
  600.  
  601. int j;
  602. int i;
  603. int getKey(void)
  604. {
  605.          int key_pressed = 0;
  606.  
  607.           for (j=0; j < ROWS; j++) { // scan the j-th row (j=0,1,2,3)
  608.             for (i=0; i < ROWS; i++) {
  609.               // output HIGH to all rows, except the j-th row
  610.                 if(i==j){
  611.                          GPIO_WriteBit(B_PORT,output[i],Bit_RESET);
  612.                 }else{
  613.                          GPIO_WriteBit(B_PORT,output[i],Bit_SET);
  614.                 }
  615.             }
  616.             for (i=0; i < COLS; i++) {
  617.                 if(GPIO_ReadInputDataBit(GPIOA,input[i]) == 0){ // Button at (R,C)=(j,i) is pressed
  618.                  // wait until the button is released.
  619.                  while ( GPIO_ReadInputDataBit(GPIOA,input[i]) == 0 ) ; // blocking
  620.                  key_pressed = keys[j][i]; // get the associated key for that button
  621.                  break;
  622.               }
  623.             }
  624.             GPIO_WriteBit(B_PORT,output[j],Bit_SET);
  625.             if ( key_pressed != 0 ) {
  626.               return key_pressed;
  627.             }
  628.           }
  629.   return 0; // no key pressed
  630. }
  631.  
  632. void GPIO_Config(void)
  633. {
  634.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  635.             RCC_APB2Periph_GPIOA, ENABLE);
  636.     //INPUT A = COLUMN
  637.           GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4 | GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
  638.           GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  639.           GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  640.           GPIO_Init(GPIOA, &GPIO_InitStructure);
  641.  
  642.     //OUTPUT B = ROW
  643.     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 ;
  644.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  645.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  646.     GPIO_Init(GPIOB, &GPIO_InitStructure);
  647.  
  648.     //USART1 (PA9)
  649.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
  650.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  651.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  652.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  653.  
  654.  
  655. }
  656.  
  657. void init_USART1(uint32_t baudrate){
  658.     USART_InitStruct.USART_BaudRate = baudrate;  // the baudrate is set to the value we passed into this init function
  659.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;  // we want the data frame size to be 8 bits (standard)
  660.     USART_InitStruct.USART_StopBits = USART_StopBits_1;  // we want 1 stop bit (standard)
  661.     USART_InitStruct.USART_Parity = USART_Parity_No;  // we don't want a parity bit (standard)
  662.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
  663.     USART_InitStruct.USART_Mode = USART_Mode_Tx;  // we want to enable the transmitter and the receiver
  664.     USART_Init(USART1, &USART_InitStruct);  // again all the properties are passed to the USART_Init function which takes care of all the bit setting
  665.  
  666.     USART_ITConfig(USART1, USART_IT_RXNE, DISABLE);             // enable the USART1 receive interrupt
  667.     USART_Cmd(USART1, ENABLE);
  668. }
  669.  
  670. void USART_puts(USART_TypeDef* USARTx, volatile char *s){
  671.     while(*s){
  672.         // wait until data register is empty
  673.         while( !(USARTx->SR & 0x00000040) );
  674.         USART_SendData(USARTx, *s);
  675.         *s++;
  676.     }
  677. }
  678.  
  679. void GPIO_LCD1602(void)
  680. {
  681.         //valueinLCD = getKey();
  682.     RCC_Configuration();
  683.     Lcd_Init();                         /* initial       */
  684.     lcd_clear();                        /* clean the LCD */
  685.     Delay(1000);
  686.     set_cursor(0,0);                    /* set cursor    */
  687.     lcd_print (" TestTest!  ");     /* display       */
  688.     set_cursor(0, 1);
  689.     lcd_print (" SetTime  ");
  690. }
  691.  
  692. /***************************************************************************//**
  693.  * @brief System clocks configuration
  694.  ******************************************************************************/
  695. void RCC_Configuration(void)
  696. {
  697.     RCC_DeInit ();                        /* RCC system reset(for debug purpose)*/
  698.     RCC_HSEConfig (RCC_HSE_ON);           /* Enable HSE                         */
  699.  
  700.     /* Wait till HSE is ready                                                   */
  701.     while (RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
  702.  
  703.     RCC_HCLKConfig   (RCC_SYSCLK_Div1);   /* HCLK   = SYSCLK                    */
  704.     RCC_PCLK2Config  (RCC_HCLK_Div1);     /* PCLK2  = HCLK                      */
  705.     RCC_PCLK1Config  (RCC_HCLK_Div2);     /* PCLK1  = HCLK/2                    */
  706.     RCC_ADCCLKConfig (RCC_PCLK2_Div4);    /* ADCCLK = PCLK2/4                   */
  707.  
  708.     *(vu32 *)0x40022000 = 0x01;           /* Flash 2 wait state                 */
  709.  
  710.     /* PLLCLK = 8MHz * 9 = 72 MHz                                               */
  711.     RCC_PLLConfig (0x00010000, RCC_PLLMul_9);
  712.  
  713.     RCC_PLLCmd (ENABLE);                  /* Enable PLL                         */
  714.  
  715.     /* Wait till PLL is ready                                                   */
  716.     while (RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET);
  717.  
  718.     /* Select PLL as system clock source                                        */
  719.     RCC_SYSCLKConfig (RCC_SYSCLKSource_PLLCLK);
  720.  
  721.     /* Wait till PLL is used as system clock source                             */
  722.     while (RCC_GetSYSCLKSource() != 0x08);
  723. }
  724.  
  725. /***************************************************************************//**
  726.  * @brief Delay some time
  727.  ******************************************************************************/
  728. void Delay(vu32 nCount)
  729. {
  730.     for(; nCount != 0; nCount--);
  731. }
  732.  
  733. /***************************************************************************//**
  734.  * @brief  Setting all pins to output mode
  735.  ******************************************************************************/
  736. void LCD_ALL_DIR_OUT(void)
  737. {
  738.     GPIO_InitTypeDef GPIO_InitStructure;
  739.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
  740.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  741.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  742.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  743. }
  744.  
  745. /***************************************************************************//**
  746.  * @brief  Setting DATA pins to input mode
  747.  ******************************************************************************/
  748. void LCD_DATA_DIR_IN(void)
  749. {
  750.     GPIO_InitTypeDef GPIO_InitStructure;
  751.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  752.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  753.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  754.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  755. }
  756.  
  757. /***************************************************************************//**
  758.  * @brief  Setting DATA pins to output mode
  759.  ******************************************************************************/
  760. void LCD_DATA_DIR_OUT(void)
  761. {
  762.     GPIO_InitTypeDef GPIO_InitStructure;
  763.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_3;
  764.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  765.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  766.     GPIO_Init(GPIOC, &GPIO_InitStructure);
  767. }
  768. /***************************************************************************//**
  769.  * @brief  Reading DATA pins
  770.  * @return the data value.
  771.  ******************************************************************************/
  772. unsigned int LCD_DATA_IN(void)
  773. {
  774.     uint16_t u16Temp=0;
  775.     u16Temp = GPIO_ReadInputData(GPIOC)&0x000F;
  776.     return SWAP_DATA[u16Temp];
  777. }
  778.  
  779. /***************************************************************************//**
  780.  * @brief  Read status of LCD controller
  781.  * @return status : Status of LCD controller
  782.  ******************************************************************************/
  783. static unsigned char Lcd_Read_Status (void)
  784. {
  785.     unsigned char status;
  786.  
  787.     LCD_DATA_DIR_IN();
  788.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  789.     GPIO_WriteBit(GPIOC, RW, Bit_SET);
  790.     Delay(10);
  791.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  792.     Delay(10);
  793.     status  = LCD_DATA_IN() << 4;
  794.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  795.     Delay(10);
  796.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  797.     Delay(10);
  798.     status |= LCD_DATA_IN();
  799.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  800.     LCD_DATA_DIR_OUT();
  801.     return (status);
  802. }
  803.  
  804. /***************************************************************************//**
  805.  * @brief Wait while LCD is busy
  806.  * @return status : Status of LCD controller
  807.  ******************************************************************************/
  808. static unsigned char Wait_While_Busy()
  809. {
  810.     unsigned char status;
  811.     do{
  812.     status = Lcd_Read_Status();
  813.     }while(status & 0x80);
  814.  
  815.     return status;
  816. }
  817. /***************************************************************************//**
  818.  * @brief  Write 4-bits to LCD controller
  819.  ******************************************************************************/
  820. void Lcd_Write_4bits(uc8 byte)
  821. {
  822.     uint16_t u16Temp=0;
  823.     GPIO_WriteBit(GPIOC, RW, Bit_RESET);
  824.     GPIO_WriteBit(GPIOC, EN, Bit_SET);
  825.     u16Temp = GPIO_ReadOutputData(GPIOC)&0xFFF0;
  826.     u16Temp |=  SWAP_DATA[byte&0x0F];
  827.     GPIO_Write(GPIOC, u16Temp);
  828.     Delay(10);
  829.     GPIO_WriteBit(GPIOC, EN, Bit_RESET);
  830.     Delay(10);
  831. }
  832.  
  833. /***************************************************************************//**
  834.  * @brief:    Write command to LCD controller
  835.  * @param[in] command :  Command to be written
  836.  ******************************************************************************/
  837. void Lcd_Write_Command(uc8 command)
  838. {
  839.     Wait_While_Busy();
  840.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  841.     Lcd_Write_4bits(command>>4);
  842.     Lcd_Write_4bits(command);
  843. }
  844.  
  845. /***************************************************************************//**
  846.  * @brief:     Write data to LCD controller
  847.   * @param[in] data :  Data to be written
  848.  ******************************************************************************/
  849. void Lcd_Write_Data(uc8 data)
  850. {
  851.     Wait_While_Busy();
  852.     GPIO_WriteBit(GPIOC, RS, Bit_SET);
  853.     Lcd_Write_4bits(data>>4);
  854.     Lcd_Write_4bits(data);
  855. }
  856.  
  857. /*******************************************************************************
  858. * @brief : Set cursor position on LCD display
  859. * @param[in] column : Column position
  860. * @param[in] line   : Line position
  861. *******************************************************************************/
  862. void set_cursor(int column, int line)
  863. {
  864.     unsigned char address;
  865.  
  866.     address = (line * 40) + column;
  867.     address = 0x80 + (address & 0x7F);
  868.     Lcd_Write_Command(address);               /* Set DDRAM address counter to 0     */
  869. }
  870.  
  871. /***************************************************************************//**
  872.  * @brief  Initial the LCD1602
  873.  ******************************************************************************/
  874. void Lcd_Init(void)
  875. {
  876.     char const *p;
  877.     int i;
  878.  
  879.     /* Enable clock for peripheral        */
  880.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
  881.  
  882.     /* Set all pins for LCD as outputs    */
  883.     LCD_ALL_DIR_OUT();
  884.     Delay(15000);
  885.     GPIO_WriteBit(GPIOC, RS, Bit_RESET);
  886.     Lcd_Write_4bits(0x3);  /* Select 4-bit interface  */
  887.     Delay(4100);
  888.     Lcd_Write_4bits(0x3);
  889.     Delay(100);
  890.     Lcd_Write_4bits(0x3);
  891.     Lcd_Write_4bits(0x2);
  892.  
  893.     Lcd_Write_Command(0x28); /* 2 lines, 5x8 character matrix      */
  894.     Lcd_Write_Command(0x0C); /* Display ctrl:Disp=ON,Curs/Blnk=OFF */
  895.     Lcd_Write_Command(0x06); /* Entry mode: Move right, no shift   */
  896.  
  897.     /* Load user-specific characters into CGRAM                                 */
  898.     Lcd_Write_Command(0x40);                  /* Set CGRAM address counter to 0     */
  899.     p = &UserFont[0][0];
  900.     for (i = 0; i < sizeof(UserFont); i++, p++)
  901.         lcd_print (*p);
  902.     Lcd_Write_Command(0x80);                 /* Set DDRAM address counter to 0     */
  903. }
  904.  
  905. /***************************************************************************//**
  906.  * @brief   print a string on LCD1602.
  907.  * @param[in] *string : point to the string which will be printed on LCD.
  908.  ******************************************************************************/
  909. void lcd_print (char *string)
  910. {
  911.         int i;
  912.  
  913.           for (i=0;i<16 && string[i]!=0;i++)                                               // 16 Character Print
  914.           {
  915.                   Lcd_Write_Data(string[i]);                                                    // Print Byte to LCD
  916.           }
  917.         /*
  918.     while (*string)
  919.     {
  920.         Lcd_Write_Data (*string++);
  921.     }
  922.     */
  923. }
  924.  
  925. /*******************************************************************************
  926.  * @brief  Clear the LCD display                                                        *
  927. *******************************************************************************/
  928. void lcd_clear (void)
  929. {
  930.     Lcd_Write_Command(0x01);                  /* Display clear                      */
  931.     set_cursor (0, 0);
  932. }
Add Comment
Please, Sign In to add comment