Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f10x.h"
- #include "stm32f10x_tim.h"
- #include "stm32f10x_rcc.h"
- #include "stm32f10x_gpio.h"
- #include "stm32f10x_usart.h"
- #include "misc.h"
- #include "stm32f10x_exti.h"
- #include <stdio.h>
- USART_InitTypeDef USART_InitStruct; // this is for the USART1 initilization
- NVIC_InitTypeDef NVIC_InitStructure; // this is used to configure the NVIC (nested vector interrupt controller)
- GPIO_InitTypeDef GPIO_InitStructure;
- TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
- EXTI_InitTypeDef EXTI_InitStructure; //EXTI structure to init EXT
- NVIC_InitTypeDef NVIC_InitStructure; //NVIC structure to set up NVIC controller
- void INTTIM_Config(void);
- void GPIO_Config(void);
- uint32_t micro(void);
- void init_USART1(uint32_t baudrate);
- void USART_puts(USART_TypeDef* USARTx, volatile char *s);
- void init_EXTI(void);
- void init_NVIC(void);
- void EXTI0_IRQHandler(void);
- uint32_t data;
- int time = 0;
- int last_time = 0;
- char text[10] = "";
- uint8_t buf[1024];
- uint32_t micro(void){
- //return (u32)time;
- return (u32)TIM_GetCounter(TIM3);
- }
- int main(void)
- {
- INTTIM_Config();
- GPIO_Config();
- init_USART1(9600); // initialize USART1 @ 9600 baud
- USART_puts(USART1, "Start!\r\n"); // just send a message to indicate that it works
- sprintf (text, "%d s\r\n",TIM_GetCounter(TIM3));
- USART_puts(USART1, text);
- while (1)
- {
- //USART_puts(USART1, "Test!\r\n");
- // data = 0xFF6896; // 1
- data = 0xFF30CE; // 4
- sentButton(data);
- if ((u32)TIM_GetCounter(TIM3)-last_time > 500){
- GPIOB->ODR ^= GPIO_Pin_14;
- last_time = (u32)TIM_GetCounter(TIM3);
- }
- }
- }
- void INTTIM_Config(void)
- {
- /* TIM3 clock enable */
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
- /* Time base configuration */
- TIM_TimeBaseStructure.TIM_Period = 0xFFFF - 1; // 1 MHz down to 1 KHz (1 ms)
- TIM_TimeBaseStructure.TIM_Prescaler = 72 - 1; // 24 MHz Clock down to 1 MHz (adjust per your clock)
- TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1;
- TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
- TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure);
- /* TIM IT enable */
- TIM_ITConfig(TIM3, TIM_IT_Update, ENABLE);
- /* TIM3 enable counter */
- TIM_Cmd(TIM3, ENABLE);
- }
- void GPIO_Config(void)
- {
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB | RCC_APB2Periph_USART1 |
- RCC_APB2Periph_GPIOA, ENABLE);
- //Output B
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOB, &GPIO_InitStructure);
- //USART1 (PA9)
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; //USART1_TX
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- //EXTI0 (PA0)
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_Init(GPIOA, &GPIO_InitStructure);
- }
- void init_USART1(uint32_t baudrate){
- USART_InitStruct.USART_BaudRate = baudrate; // the baudrate is set to the value we passed into this init function
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;// we want the data frame size to be 8 bits (standard)
- USART_InitStruct.USART_StopBits = USART_StopBits_1; // we want 1 stop bit (standard)
- USART_InitStruct.USART_Parity = USART_Parity_No; // we don't want a parity bit (standard)
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None; // we don't want flow control (standard)
- USART_InitStruct.USART_Mode = USART_Mode_Tx; // we want to enable the transmitter and the receiver
- USART_Init(USART1, &USART_InitStruct); // again all the properties are passed to the USART_Init function which takes care of all the bit setting
- USART_ITConfig(USART1, USART_IT_RXNE, DISABLE); // enable the USART1 receive interrupt
- USART_Cmd(USART1, ENABLE);
- }
- void USART_puts(USART_TypeDef* USARTx, volatile char *s){
- while(*s){
- // wait until data register is empty
- while( !(USARTx->SR & 0x00000040) );
- USART_SendData(USARTx, *s);
- *s++;
- }
- }
- ////////////////////////////////////
- void init_EXTI(void){
- GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0);
- EXTI_InitStructure.EXTI_Line = EXTI_Line0;
- EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt; //select interrupt mode
- EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising; //generate interrupt on rising edge
- EXTI_InitStructure.EXTI_LineCmd = ENABLE; //enable EXTI line
- EXTI_Init(&EXTI_InitStructure); //send values to registers
- }
- void init_NVIC(void){
- NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn; //select NVIC channel to configure
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //enable IRQ channel
- NVIC_Init(&NVIC_InitStructure); //update NVIC registers
- }
- void EXTI0_IRQHandler(void){
- int i;
- //Check if EXTI_Line0 is asserted
- if(EXTI_GetITStatus(EXTI_Line0) != RESET)
- {
- GPIO_WriteBit(GPIOB,GPIO_Pin_9,1);
- for (i=0; i<100000; i++);
- GPIO_WriteBit(GPIOB,GPIO_Pin_9,0);
- for (i=0; i<100000; i++);
- }
- //we need to clear line pending bit manually
- EXTI_ClearITPendingBit(EXTI_Line0);
- }
- uint32_t data = 0xFF6896;
- void sentButton( uint32_t data ){
- uint32_t input = data;
- switch( input ){
- case 0xFF629C:
- sprintf(buf, "Button: UP\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFFA856:
- sprintf(buf, "Button: DOWN\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF22DC:
- sprintf(buf, "Button: LEFT\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFFC23C:
- sprintf(buf, "Button: RIGHT\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF02FC:
- sprintf(buf, "Button: OK\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF6896:
- sprintf(buf, "Button: %d\r\n", 1);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF9866:
- sprintf(buf, "Button: %d\r\n", 2);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFFB04E:
- sprintf(buf, "Button: %d\r\n", 3);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF30CE:
- sprintf(buf, "Button: %d\r\n", 4);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF18E6:
- sprintf(buf, "Button: %d\r\n", 5);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF7A84:
- sprintf(buf, "Button: %d\r\n", 6);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF10EE:
- sprintf(buf, "Button: %d\r\n", 7);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF38C6:
- sprintf(buf, "Button: %d\r\n", 8);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF5AA4:
- sprintf(buf, "Button: %d\r\n", 9);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF4AB4:
- sprintf(buf, "Button: %d\r\n", 0);
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF42BC:
- sprintf(buf, "Button: *\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- case 0xFF52AC:
- sprintf(buf, "Button: #\r\n");
- USART_puts(USART1, buf);
- // SerialTransmit(buf);
- break;
- }
- } // end-sentButton
Advertisement
Add Comment
Please, Sign In to add comment