pongfactory

Lab 4-3 STM v.1

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