Advertisement
Guest User

Untitled

a guest
Feb 8th, 2017
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.68 KB | None | 0 0
  1. #include "stm32f4xx.h"
  2. #include "stm32f4xx_rcc.h"
  3. #include "stm32f4xx_gpio.h"
  4. #include "stm32f4xx_usart.h"
  5. #include "stm32f4xx_it.h"
  6. #include <stdio.h>
  7. #include "misc.h"
  8.  
  9.  
  10. //Funciones
  11. void configPER(void);
  12. void configGPIO(void);
  13. void USART_send(USART_TypeDef *USARTx, volatile char *t);
  14.  
  15. int main(void) {
  16.  
  17.     configPER();
  18.     configGPIO();
  19.  
  20.     while (1)
  21.     {
  22.  
  23.    
  24.  
  25.          if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0))
  26.          {
  27.                    GPIO_SetBits(GPIOG, GPIO_Pin_13);
  28.  
  29.                    USART_send(USART2, "Hello world\n");
  30.          }
  31.          else
  32.          {
  33.                     GPIO_ResetBits(GPIOG, GPIO_Pin_13);
  34.          }
  35.     }
  36. }
  37.  
  38.  
  39. uint32_t sEE_TIMEOUT_UserCallback(void)
  40. {
  41.   /* TODO, implement your code here */
  42.   while (1)
  43.   {
  44.   }
  45. }
  46.  
  47. void configPER(void)
  48. {
  49.     GPIO_InitTypeDef GPIO_InitStruct;
  50.     USART_InitTypeDef USART_InitStruct;
  51.     NVIC_InitTypeDef NVIC_InitStruct;
  52.  
  53.     // Enable clock for USART2
  54.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  55.     // Enable clock for GPIOA
  56.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  57.  
  58.     /**
  59.     * Tell pins PA2 and PA3 which alternating function you will use
  60.     * @important Make sure, these lines are before pins configuration!
  61.     */
  62.  
  63.     // Initialize pins as alternating function
  64.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  65.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  66.     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  67.     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  68.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  69.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  70.  
  71.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource2,GPIO_AF_USART2);
  72.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource3,GPIO_AF_USART2);
  73.  
  74.     /**
  75.      * Set Baudrate to value you pass to function
  76.      * Disable Hardware Flow control
  77.      * Set Mode To TX and RX, so USART will work in full-duplex mode
  78.      * Disable parity bit
  79.      * Set 1 stop bit
  80.      * Set Data bits to 8
  81.      *
  82.      * Initialize USART2
  83.      * Activate USART2
  84.      */
  85.     USART_InitStruct.USART_BaudRate = 9600;
  86.     USART_InitStruct.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  87.     USART_InitStruct.USART_Parity = USART_Parity_No;
  88.     USART_InitStruct.USART_StopBits = USART_StopBits_1;
  89.     USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  90.     USART_InitStruct.USART_Mode = USART_Mode_Tx | USART_Mode_Rx;
  91.     USART_Init(USART2, &USART_InitStruct);
  92.  
  93.  
  94.     //Configuramos interrupción para al recibir (RX)
  95.     USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  96.  
  97.     /**
  98.      * Set Channel to USART2
  99.      * Set Channel Cmd to enable. That will enable USART2 channel in NVIC
  100.      * Set Both priorities to 0. This means high priority
  101.      *
  102.      * Initialize NVIC
  103.      */
  104.     NVIC_InitStruct.NVIC_IRQChannel = USART2_IRQn;
  105.     NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 0;
  106.     NVIC_InitStruct.NVIC_IRQChannelSubPriority = 0;
  107.     NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  108.     NVIC_Init(&NVIC_InitStruct);
  109.  
  110.     USART_Cmd(USART2, ENABLE);
  111.  
  112.  
  113. #define MAX_WORD_LENGTH 10
  114. volatile char received_string[MAX_WORD_LENGTH+1];
  115.  
  116. void USART2_IRQHandler(void)
  117. {
  118.     if (USART_GetITStatus(USART2, USART_IT_RXNE))
  119.     {
  120.         GPIO_SetBits(GPIOG, GPIO_Pin_15);
  121.         static int cnt=0;
  122.         //flag es 1
  123.         char c=USART2->DR;
  124.         if((c!='\n') && (cnt<MAX_WORD_LENGTH))
  125.         {
  126.             received_string[cnt++]=c;
  127.         }
  128.         else
  129.         {
  130.  
  131.             received_string[cnt]='\0';
  132.  
  133.             if(received_string[0]=='L')
  134.             GPIO_SetBits(GPIOG, GPIO_Pin_13);
  135.  
  136.             cnt=0;
  137.         }
  138.     }
  139. }
  140.  
  141. }
  142.  
  143.  
  144. void configGPIO(void)
  145. {
  146.  
  147.     //GPIOA
  148.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  149.         GPIO_InitTypeDef GPIO_InitDef;
  150.  
  151.         GPIO_InitDef.GPIO_Pin = GPIO_Pin_0;
  152.         GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  153.         GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;
  154.         GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_DOWN;
  155.         GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
  156.  
  157.         GPIO_Init(GPIOA, &GPIO_InitDef);
  158.        
  159.  //GPIOG
  160.  
  161.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  162.  
  163.         GPIO_InitDef.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_15 ;
  164.         GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  165.         GPIO_InitDef.GPIO_Mode = GPIO_Mode_OUT;
  166.         GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_NOPULL;
  167.         GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
  168.         GPIO_Init(GPIOG, &GPIO_InitDef);
  169.         RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOG, ENABLE);
  170.         GPIO_InitDef.GPIO_Pin = GPIO_Pin_11;
  171.         GPIO_InitDef.GPIO_OType = GPIO_OType_PP;
  172.         GPIO_InitDef.GPIO_Mode = GPIO_Mode_IN;
  173.         GPIO_InitDef.GPIO_PuPd = GPIO_PuPd_DOWN;
  174.         GPIO_InitDef.GPIO_Speed = GPIO_Speed_50MHz;
  175.         GPIO_Init(GPIOG, &GPIO_InitDef);
  176.  
  177.  
  178.  
  179. }
  180.  
  181.  
  182. void USART_send(USART_TypeDef *USARTx, volatile char *t)
  183. {
  184.     while(*t)
  185.     {
  186.         while(USART_GetFlagStatus(USARTx, USART_FLAG_TC) == RESET);  //Asegurarse de que el bit se envía
  187.  
  188.         USART_SendData(USARTx, *t);
  189.  
  190.     }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement