Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Includes ------------------------------------------------------------------*/
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "stm32f10x.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_tim.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_usart.h"
- /* Declare function prototype ------------------------------------------------*/
- void GPIO_init(void);
- void Delay(__IO uint32_t nCount);
- void USART_puts(USART_TypeDef* USARTx, volatile char *c);
- void USART_init(uint32_t baudrate);
- void Timer_init(void);
- uint32_t micro(void);
- int main(void)
- {
- /* variable for string buffer uart */
- char str_value[15];
- /* variable for store signal sequence */
- uint32_t data[100];
- uint8_t index = 0;
- /* variable for calculate time */
- uint32_t lastTime;
- uint32_t temp;
- /* variable for signal state */
- uint8_t state = 0;
- /* Configure General-Purpose Input/Output */
- GPIO_init();
- /* Configure Timer 100Hz 1-tick is 10ms*/
- Timer_init();
- /* Configure UART2 Baudrate 115200, */
- USART_init(115200);
- USART_puts(USART1,"Program started\r\n");
- while(1)
- {
- if( state == 0 )
- {
- /* wait until signal reset */
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0 )
- {
- /* store current time when reset signal */
- lastTime = micro();
- /* next state */
- state = 1;
- }
- }
- else if( state == 1)
- {
- /* wait until signal set */
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0 )
- {
- /* store length of time into temp and store curent time */
- temp = micro() - lastTime;
- lastTime = micro();
- /*
- * if signal reset = 9ms is first start bit and next state
- * else back first state
- */
- if( temp > 800)
- state = 2;
- else
- state = 0;
- }
- }
- else if( state == 2 )
- {
- /* wait until signal reset */
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0 )
- {
- /* store length of time into temp and store curent time */
- temp = micro() - lastTime;
- lastTime = micro();
- /*
- * if signal set = 4.4ms is second start bit and next state
- * else back first state
- */
- if( temp > 400)
- {
- state = 3;
- }
- else {
- state = 0;
- /* some delay */
- Delay(0xFFF);
- }
- }
- }
- else if( state == 3 )
- {
- /* wait until signal set */
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) != 0 )
- {
- /* store curent time and next state */
- lastTime = micro();
- state = 4;
- }
- }
- else if( state == 4 )
- {
- /* wait until signal reset */
- if( GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_4) == 0 )
- {
- /* store length of time into temp */
- temp = micro() - lastTime;
- /*
- * consider time, if signal set < 35ms is not end
- * let store length of time and back state 3
- * and else not store length of time and next state 5
- */
- if( temp < 3500 ) // 35ms
- {
- data[index++] = temp;
- state = 3;
- }
- else
- {
- state = 5;
- }
- /* store curent time */
- lastTime = micro();
- }
- }
- else if( state == 5 )
- {
- /*
- * consider length of time of signal set,
- * condition ( time < 1ms ).
- * if length of time = 1.6ms set = '1'
- * else if length of time = 0.4ms set = '0'
- */
- int i;
- for( i = 15; i < 21; i++ ){
- if(data[i] < 100){
- data[i] = 0;
- }
- else{
- data[i] = 1;
- }
- }
- /* clear variable str_value */
- strcpy(str_value, "");
- /* consider only bit 15-20 is specifically bit */
- for( i = 15; i < 21; i++ ){
- sprintf(str_value,"%s%d",str_value,(int)data[i] );
- data[i] = 0;
- }
- /* convert string to int */
- temp = (u32)atoi(str_value);
- /*
- * consider sequence data bit 15-20
- * 101101 is button 1
- * 110011 is button 2
- * 110110 is button 3
- * 100110 is button 4
- * 100011 is button 5
- * 101111 is button 6
- * 100010 is button 7
- * 100111 is button 8
- * 101011 is button 9
- * 101001 is button 0
- * 101000 is button *
- * 101010 is button #
- * 100100 is button 'left'
- * 101100 is button 'up'
- * 111000 is button 'right'
- * 110101 is button 'down'
- * 100000 is button 'ok'
- */
- switch(temp)
- {
- case 101101 :
- strcpy(str_value,"1" );
- break;
- case 110011 :
- strcpy(str_value,"2" );
- break;
- case 110110 :
- strcpy(str_value,"3" );
- break;
- case 100110 :
- strcpy(str_value,"4" );
- break;
- case 100011 :
- strcpy(str_value,"5" );
- break;
- case 101111 :
- strcpy(str_value,"6" );
- break;
- case 100010 :
- strcpy(str_value,"7" );
- break;
- case 100111 :
- strcpy(str_value,"8" );
- break;
- case 101011 :
- strcpy(str_value,"9" );
- break;
- case 101001 :
- strcpy(str_value,"0" );
- break;
- case 101000 :
- strcpy(str_value,"*" );
- break;
- case 101010 :
- strcpy(str_value,"#" );
- break;
- case 100100 :
- strcpy(str_value,"Left" );
- break;
- case 101100 :
- strcpy(str_value,"Up" );
- break;
- case 111000 :
- strcpy(str_value,"Right" );
- break;
- case 110101 :
- strcpy(str_value,"Down" );
- break;
- case 100000 :
- strcpy(str_value,"Ok" );
- break;
- default :
- strcpy(str_value,"error" );
- break;
- }
- if( strcmp(str_value,"error") )
- {
- /* show button pressed by uart */
- sprintf(str_value,"%s\r\n",str_value );
- USART_puts(USART1,str_value);
- }
- index = 0;
- state = 0;
- }
- }
- }
- void Delay(__IO uint32_t nCount)
- {
- while(nCount--);
- }
- void USART_init(uint32_t baudrate)
- {
- /* Enable the UART1 Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
- RCC_APB2Periph_GPIOA, ENABLE);
- /*
- * Initial UART1
- * 1-bit stop and no parity bit
- * used tx mode
- */
- USART_InitTypeDef USART_InitStructure;
- USART_InitStructure.USART_BaudRate = baudrate;
- USART_InitStructure.USART_WordLength = USART_WordLength_8b;
- USART_InitStructure.USART_StopBits = USART_StopBits_1;
- USART_InitStructure.USART_Parity = USART_Parity_No;
- USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStructure.USART_Mode = USART_Mode_Tx;
- USART_Init(USART1, &USART_InitStructure);
- /* Enable USART1 */
- USART_Cmd(USART1, ENABLE);
- }
- void USART_puts(USART_TypeDef* USARTx, volatile char *c)
- {
- while(*c){
- // wait until data register is empty
- while( !(USARTx->SR & 0x00000040) );
- USART_SendData(USARTx, *c);
- *c++;
- }
- }
- void GPIO_init(void)
- {
- /* Enable the GPIO Clock */
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO , ENABLE);
- GPIO_InitTypeDef GPIO_InitStructure;
- /* Configure USART1 Tx (PA.02) as alternate function push-pull */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- /* Initial Input GPIO A pin 4 */
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- uint32_t micro(void) {
- /* return counter value form timer 3 */
- return (u32)TIM_GetCounter(TIM3);
- }
- void Timer_init(void) {
- /* Enable the Timer3 Clock */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* Initial Timer 3,
- * frequency counter 72MHz / 720 = 100 KHz,
- * set Autoreload value(ARR) = 0xFFFF(65535)
- */
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- TIM_TimeBaseStructure.TIM_Prescaler = 720 - 1 ;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseStructure.TIM_ClockDivision = 0;
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE);
- }
Advertisement
Add Comment
Please, Sign In to add comment