pongfactory

Lab 4-3 STM v.3 OK

Jan 27th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.28 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include "stm32f10x.h"
  6. #include "stm32f10x_rcc.h"
  7. #include "stm32f10x_tim.h"
  8. #include "stm32f10x_gpio.h"
  9. #include "stm32f10x_usart.h"
  10.  
  11. /* Declare function prototype ------------------------------------------------*/
  12. void GPIO_init(void);
  13. void Delay(__IO uint32_t nCount);
  14. void USART_puts(USART_TypeDef* USARTx, volatile char *c);
  15. void USART_init(uint32_t baudrate);
  16. void Timer_init(void);
  17. uint32_t micro(void);
  18. uint8_t buf[1024];
  19.  
  20.  
  21. int main(void)
  22. {
  23.     /* variable for string buffer uart */
  24.     char str_value[15];
  25.  
  26.     /* variable for store signal sequence */
  27.     uint32_t data[100];
  28.     uint8_t index = 0;
  29.  
  30.     /* variable for calculate time */
  31.     uint32_t lastTime;
  32.     uint32_t temp;
  33.  
  34.     /* variable for signal state */
  35.     uint8_t state = 0;
  36.  
  37.     /* Configure General-Purpose Input/Output */
  38.     GPIO_init();
  39.     /* Configure Timer 100Hz 1-tick is 10ms*/
  40.     Timer_init();
  41.     /* Configure UART2 Baudrate 115200, */
  42.     USART_init(115200);
  43.  
  44.     USART_puts(USART1,"Program started\r\n");
  45.     while(1)
  46.     {
  47.         if( state == 0 )
  48.         {
  49.             /* wait until signal reset */
  50.             if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0 )
  51.             {
  52.                 /* store current time when reset signal */
  53.                 lastTime = micro();
  54.                 /* next state */
  55.                 state = 1;
  56.             }
  57.         }
  58.         else if( state == 1)
  59.         {
  60.             /* wait until signal set */
  61.             if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0 )
  62.             {
  63.                 /* store length of time into temp and store curent time */
  64.                 temp = micro() - lastTime;
  65.                 lastTime = micro();
  66.                 /*
  67.                  * if signal reset = 9ms is first start bit and next state
  68.                  * else back first state
  69.                  */
  70.                 if( temp > 800)
  71.                     state = 2;
  72.                 else
  73.                     state = 0;
  74.             }
  75.         }
  76.         else if( state == 2 )
  77.         {
  78.             /* wait until signal reset */
  79.             if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0 )
  80.             {
  81.                 /* store length of time into temp and store curent time */
  82.                 temp = micro() - lastTime;
  83.                 lastTime = micro();
  84.                 /*
  85.                  * if signal set = 4.4ms is second start bit and next state
  86.                  * else back first state
  87.                  */
  88.                 if( temp > 400)
  89.                 {
  90.                     state = 3;
  91.                 }
  92.                 else {
  93.                     state = 0;
  94.                     /* some delay */
  95.                     Delay(0xFFF);
  96.                 }
  97.             }
  98.         }
  99.         else if( state == 3 )
  100.         {
  101.             /* wait until signal set */
  102.             if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0 )
  103.             {
  104.                 /* store curent time and next state */
  105.                 lastTime = micro();
  106.                 state = 4;
  107.             }
  108.         }
  109.         else if( state == 4 )
  110.         {
  111.             /* wait until signal reset */
  112.             if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0 )
  113.             {
  114.                 /* store length of time into temp */
  115.                 temp = micro() - lastTime;
  116.                 /*
  117.                  * consider time, if signal set < 35ms is not end
  118.                  * let store length of time and back state 3
  119.                  * and else not store length of time and next state 5
  120.                  */
  121.                 if( temp < 3500 )   // 35ms
  122.                 {
  123.                     data[index++] = temp;
  124.                     state = 3;
  125.                 }
  126.                 else
  127.                 {
  128.                     state = 5;
  129.                 }
  130.                 /* store curent time */
  131.                 lastTime = micro();
  132.             }
  133.         }
  134.         else if( state == 5 )
  135.         {
  136.             /*
  137.              * consider length of time of signal set,
  138.              * condition ( time < 1ms ).
  139.              * if length of time = 1.6ms set = '1'
  140.              * else if length of time = 0.4ms set = '0'
  141.              */
  142.             int i;
  143.             for( i = 15; i < 21; i++ ){
  144.                 if(data[i] < 100){
  145.                     data[i] = 0;
  146.                 }
  147.                 else{
  148.                     data[i] = 1;
  149.                 }
  150.             }
  151.             /* clear variable str_value */
  152.             strcpy(str_value, "");
  153.             /* consider only bit 15-20 is specifically bit */
  154.             for( i = 15; i < 21; i++ ){
  155.                 sprintf(str_value,"%s%d",str_value,(int)data[i] );
  156.                 data[i] = 0;
  157.             }
  158.             /* convert string to int */
  159.             temp = (u32)atoi(str_value);
  160.  
  161.             sentButton( temp);
  162.  
  163.  
  164.             index = 0;
  165.             state = 0;
  166.         }
  167.     }
  168. }
  169.  
  170. void Delay(__IO uint32_t nCount)
  171. {
  172.   while(nCount--);
  173. }
  174.  
  175.  
  176. void USART_init(uint32_t baudrate)
  177. {
  178.     /* Enable the UART1 Clock */
  179.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
  180.                             RCC_APB2Periph_GPIOA, ENABLE);
  181.     /*
  182.      * Initial UART1
  183.      * 1-bit stop and no parity bit
  184.      * used tx mode
  185.      */
  186.     USART_InitTypeDef USART_InitStructure;
  187.     USART_InitStructure.USART_BaudRate              = baudrate;
  188.     USART_InitStructure.USART_WordLength            = USART_WordLength_8b;
  189.     USART_InitStructure.USART_StopBits              = USART_StopBits_1;
  190.     USART_InitStructure.USART_Parity                = USART_Parity_No;
  191.     USART_InitStructure.USART_HardwareFlowControl   = USART_HardwareFlowControl_None;
  192.     USART_InitStructure.USART_Mode                  = USART_Mode_Tx;
  193.     USART_Init(USART1, &USART_InitStructure);
  194.  
  195.     /* Enable USART1 */
  196.     USART_Cmd(USART1, ENABLE);
  197. }
  198.  
  199. void USART_puts(USART_TypeDef* USARTx, volatile char *c)
  200. {
  201.     while(*c){
  202.         // wait until data register is empty
  203.         while( !(USARTx->SR & 0x00000040) );
  204.         USART_SendData(USARTx, *c);
  205.         *c++;
  206.     }
  207. }
  208.  
  209. void GPIO_init(void)
  210. {
  211.     /* Enable the GPIO Clock */
  212.     RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
  213.  
  214.     GPIO_InitTypeDef GPIO_InitStructure;
  215.     /* Configure USART1 Tx (PA.02) as alternate function push-pull */
  216.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
  217.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
  218.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  219.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  220.  
  221.     /* Initial Input GPIO A pin 4 */
  222.     GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
  223.     GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
  224.     GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
  225.     GPIO_Init(GPIOA, &GPIO_InitStructure);
  226.  
  227. }
  228.  
  229. uint32_t micro(void) {
  230.  
  231.     /* return counter value form timer 3 */
  232.     return (u32)TIM_GetCounter(TIM3);
  233. }
  234.  
  235. void Timer_init(void) {
  236.  
  237.     /* Enable the Timer3 Clock */
  238.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
  239.  
  240.     /* Initial Timer 3,
  241.      * frequency counter 72MHz / 720 =  100 KHz,
  242.      * set Autoreload value(ARR) = 0xFFFF(65535)
  243.      */
  244.     TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  245.     TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1 ;
  246.     TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
  247.     TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  248.     TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;
  249.     TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
  250.  
  251.     /* TIM3 enable counter */
  252.     TIM_Cmd(TIM3, ENABLE);
  253.  
  254. }
  255.  
  256. void sentButton( uint32_t data ){
  257.         uint32_t input = data;
  258.     switch( input ){
  259.         case 101100:
  260.             sprintf(buf, "Button: UP\r\n");
  261.             USART_puts(USART1, buf);
  262.           //  SerialTransmit(buf);
  263.             break;
  264.         case 110101:
  265.             sprintf(buf, "Button: DOWN\r\n");
  266.             USART_puts(USART1, buf);
  267.          //   SerialTransmit(buf);
  268.             break;
  269.         case 100100:
  270.             sprintf(buf, "Button: LEFT\r\n");
  271.             USART_puts(USART1, buf);
  272.          //   SerialTransmit(buf);
  273.             break;
  274.         case 111000:
  275.             sprintf(buf, "Button: RIGHT\r\n");
  276.             USART_puts(USART1, buf);
  277.          //   SerialTransmit(buf);
  278.             break;
  279.         case 100000:
  280.             sprintf(buf, "Button: OK\r\n");
  281.             USART_puts(USART1, buf);
  282.          //   SerialTransmit(buf);
  283.             break;
  284.         case 101101:
  285.             sprintf(buf, "Button: %d\r\n", 1);
  286.  
  287.             USART_puts(USART1, buf);
  288.         //    SerialTransmit(buf);
  289.             break;
  290.         case 110011:
  291.             sprintf(buf, "Button: %d\r\n", 2);
  292.             USART_puts(USART1, buf);
  293.         //    SerialTransmit(buf);
  294.             break;
  295.         case 110110:
  296.             sprintf(buf, "Button: %d\r\n", 3);
  297.             USART_puts(USART1, buf);
  298.         //    SerialTransmit(buf);
  299.             break;
  300.         case 100110:
  301.             sprintf(buf, "Button: %d\r\n", 4);
  302.             USART_puts(USART1, buf);
  303.         //    SerialTransmit(buf);
  304.             break;
  305.         case 100011:
  306.             sprintf(buf, "Button: %d\r\n", 5);
  307.             USART_puts(USART1, buf);
  308.         //    SerialTransmit(buf);
  309.             break;
  310.         case 101111:
  311.             sprintf(buf, "Button: %d\r\n", 6);
  312.             USART_puts(USART1, buf);
  313.         //    SerialTransmit(buf);
  314.             break;
  315.         case 100010:
  316.             sprintf(buf, "Button: %d\r\n", 7);
  317.             USART_puts(USART1, buf);
  318.         //    SerialTransmit(buf);
  319.             break;
  320.         case 100111:
  321.             sprintf(buf, "Button: %d\r\n", 8);
  322.             USART_puts(USART1, buf);
  323.        //     SerialTransmit(buf);
  324.             break;
  325.         case 101011:
  326.             sprintf(buf, "Button: %d\r\n", 9);
  327.             USART_puts(USART1, buf);
  328.        //     SerialTransmit(buf);
  329.             break;
  330.         case 101001:
  331.             sprintf(buf, "Button: %d\r\n", 0);
  332.             USART_puts(USART1, buf);
  333.         //    SerialTransmit(buf);
  334.             break;
  335.         case 101000:
  336.             sprintf(buf, "Button: *\r\n");
  337.             USART_puts(USART1, buf);
  338.         //    SerialTransmit(buf);
  339.             break;
  340.         case 101010:
  341.             sprintf(buf, "Button: #\r\n");
  342.             USART_puts(USART1, buf);
  343.         //    SerialTransmit(buf);
  344.             break;
  345.     }
  346. } // end-sentButton
Advertisement
Add Comment
Please, Sign In to add comment