Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f4xx.h"
- #include "stm32f4xx_rcc.h"
- #include "stm32f4xx_gpio.h"
- #include "stm32f4xx_usart.h"
- #include "stm32f4xx_it.h"
- #include <stdio.h>
- #include "misc.h"
- //Funciones
- void configPER(void);
- void configGPIO(void);
- void USART_send(USART_TypeDef *USARTx, volatile char *t);
- int main(void) {
- configPER();
- configGPIO();
- while (1)
- {
- if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
- {
- GPIO_SetBits(GPIOG, GPIO_Pin_13);
- USART_send(USART2, "Hello world\n");
- }
- else
- {
- GPIO_ResetBits(GPIOG, GPIO_Pin_13);
- }
- }
- }
- uint32_t sEE_TIMEOUT_UserCallback(void)
- {
- /* TODO, implement your code here */
- while (1)
- {
- }
- }
- void configPER(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct;
- USART_InitTypeDef USART_InitStruct;
- NVIC_InitTypeDef NVIC_InitStruct;
- // Enable clock for USART2
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- // Enable clock for GPIOA
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- /**
- * Tell pins PA2 and PA3 which alternating function you will use
- * @important Make sure, these lines are before pins configuration!
- */
- // Initialize pins as alternating function
- GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
- GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
- GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
- GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
- GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_USART2);
- /**
- * Set Baudrate to value you pass to function
- * Disable Hardware Flow control
- * Set Mode To TX and RX, so USART will work in full-duplex mode
- * Disable parity bit
- * Set 1 stop bit
- * Set Data bits to 8
- *
- * Initialize USART2
- * Activate USART2
- */
- USART_InitStruct.USART_BaudRate = 9600;
- USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
- USART_InitStruct.USART_Parity = USART_Parity_No;
- USART_InitStruct.USART_StopBits = USART_StopBits_1;
- USART_InitStruct.USART_WordLength = USART_WordLength_8b;
- USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
- USART_Init(USART2, &USART_InitStruct);
- //Configuramos interrupción para al recibir (RX)
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- /**
- * Set Channel to USART2
- * Set Channel Cmd to enable. That will enable USART2 channel in NVIC
- * Set Both priorities to 0. This means high priority
- *
- * Initialize NVIC
- */
- NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
- NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
- NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStruct);
- USART_Cmd(USART2, ENABLE);
- #define MAX_WORD_LENGTH 10
- volatile char received_string[MAX_WORD_LENGTH+1];
- void USART2_IRQHandler(void)
- {
- if (USART_GetITStatus(USART2, USART_IT_RXNE))
- {
- GPIO_SetBits(GPIOG, GPIO_Pin_15);
- static int cnt=0;
- //flag es 1
- char c=USART2->DR;
- if((c!='\n') && (cnt<MAX_WORD_LENGTH))
- {
- received_string[cnt++]=c;
- }
- else
- {
- received_string[cnt]='\0';
- if(received_string[0]=='L')
- GPIO_SetBits(GPIOG, GPIO_Pin_13);
- cnt=0;
- }
- }
- }
- }
- void configGPIO(void)
- {
- //GPIOA
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
- GPIO_InitTypeDef GPIO_InitDef;
- GPIO_InitDef.GPIO_Pin = GPIO_Pin_0;
- GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
- GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &GPIO_InitDef);
- //GPIOG
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
- GPIO_InitDef.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15 ;
- GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
- GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
- GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
- GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOG, &GPIO_InitDef);
- RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
- GPIO_InitDef.GPIO_Pin = GPIO_Pin_11;
- GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
- GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;
- GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOG, &GPIO_InitDef);
- }
- void USART_send(USART_TypeDef *USARTx, volatile char *t)
- {
- while(*t)
- {
- while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET); //Asegurarse de que el bit se envía
- USART_SendData(USARTx, *t);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement