Advertisement
Guest User

New main.c for phivu

a guest
Oct 27th, 2014
280
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 7.39 KB | None | 0 0
  1. /**
  2.   ******************************************************************************
  3.   * @file    GPIO/GPIO_IOToggle/main.c
  4.   * @author  MCD Application Team
  5.   * @version V1.4.0
  6.   * @date    04-August-2014
  7.   * @brief   Main program body
  8.   ******************************************************************************
  9.   * @attention
  10.   *
  11.   * <h2><center>&copy; COPYRIGHT 2013 STMicroelectronics</center></h2>
  12.   *
  13.   * Licensed under MCD-ST Liberty SW License Agreement V2, (the "License");
  14.   * You may not use this file except in compliance with the License.
  15.   * You may obtain a copy of the License at:
  16.   *
  17.   *        http://www.st.com/software_license_agreement_liberty_v2
  18.   *
  19.   * Unless required by applicable law or agreed to in writing, software
  20.   * distributed under the License is distributed on an "AS IS" BASIS,
  21.   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  22.   * See the License for the specific language governing permissions and
  23.   * limitations under the License.
  24.   *
  25.   ******************************************************************************
  26.   */
  27.  
  28. /* Includes ------------------------------------------------------------------*/
  29. #include "main.h"
  30. #include "stm32f4xx.h"
  31. #include "string.h"
  32. #include "stdio.h"
  33.  
  34. /* Private typedef -----------------------------------------------------------*/
  35. GPIO_InitTypeDef  GPIOHandle;
  36. USART_InitTypeDef USARTHandle;
  37. NVIC_InitTypeDef    NVICHandle;
  38. EXTI_InitTypeDef EXTIHandle;
  39.  
  40. /* Private define ------------------------------------------------------------*/
  41. //static void GPIO_Config(void);
  42. static void USART2_Config(void);
  43. static void EXTI9_5_Config(void);
  44. static void TM_Delay_Init(void);
  45. static void TM_DelayMicros(uint32_t micros);
  46. static void TM_DelayMillis(uint32_t millis);
  47.  
  48. /* Private macro -------------------------------------------------------------*/
  49. /* Private variables ---------------------------------------------------------*/
  50. uint32_t multiplier;
  51. int pulsesv;
  52.  
  53.  
  54. /* We need to implement own __FILE struct */
  55. /* FILE struct is used from __FILE */
  56. struct __FILE {
  57.     int dummy;
  58. };
  59. /* You need this if you want use printf */
  60. /* Struct FILE is implemented in stdio.h */
  61. FILE __stdout;
  62.  
  63. int fputc(int ch, FILE *f) {
  64.     /* Do your stuff here */
  65.     /* Send your custom byte */
  66.     /* Send byte to USART */
  67.     USART_SendData(USART2, (uint8_t) ch);
  68.  
  69.    /* Loop until the end of transmission */
  70.    while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
  71.    {}
  72.              
  73.     /* If everything is OK, you have to return character written */
  74.     return ch;
  75.     /* If character is not correct, you can return EOF (-1) to stop writing */
  76.     //return -1;
  77. }
  78.  
  79. /* Private function prototypes -----------------------------------------------*/
  80. /* Private functions ---------------------------------------------------------*/
  81.  
  82. /**
  83.   * @brief  Main program
  84.   * @param  None
  85.   * @retval None
  86.   */
  87. int main(void)
  88. {
  89.   USART2_Config();
  90.     EXTI9_5_Config();
  91.     TM_Delay_Init();
  92.     printf("Value pulse:");
  93.     printf("%d\n",pulsesv);
  94.     TM_DelayMicros(100);
  95.     while (1)
  96.   {
  97.    
  98.      }
  99. }
  100.  
  101. /**
  102.   * @brief  Congig USART2
  103.   * @param  None
  104.   * @retval None
  105.   */
  106.  
  107.  
  108. void USART2_Config()
  109. {
  110.     /* ADDED BY Tilen start */
  111.     GPIO_InitTypeDef GPIO_InitStruct;
  112.    
  113.     /* Enable clock for GPIOA */
  114.     RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  115.    
  116.     /* Set alternating function to USART 2, PA2: TX; PA3: RX */
  117.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2);
  118.     GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2);
  119.    
  120.     /* Set pin settings, alternate function */
  121.     GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  122.     GPIO_InitStruct.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  123.     GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
  124.     GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
  125.     GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
  126.     /* Initialize pins for USART2 */
  127.     GPIO_Init(GPIOA, &GPIO_InitStruct);
  128.    
  129.     /* Enable clock for USART2 */
  130.     RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  131.    
  132.     /* ADDED BY Tilen end */
  133.    
  134.     /* USARTx configuration ------------------------------------------------------*/
  135.   /* USARTx configured as follows:
  136.         - BaudRate = 9600 baud  
  137.         - Word Length = 8 Bits
  138.         - Two Stop Bit
  139.         - Odd parity
  140.         - Hardware flow control disabled (RTS and CTS signals)
  141.         - Receive and transmit enabled
  142.   */
  143.     USARTHandle.USART_BaudRate = 115200;
  144.     USARTHandle.USART_WordLength = USART_WordLength_8b;
  145.     USARTHandle.USART_StopBits = USART_StopBits_2;
  146.     USARTHandle.USART_Parity = USART_Parity_Odd;
  147.     USARTHandle.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  148.     USARTHandle.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  149.  
  150.     USART_Init(USART2, &USARTHandle);
  151.    
  152.     USART_Cmd(USART2, ENABLE);
  153. }
  154.  
  155.  
  156. /**
  157.   * @brief  Congig EXTI9-15 PA8 and PA9
  158.   * @param  None
  159.   * @retval None
  160.   */
  161. void EXTI9_5_Config()
  162. {
  163.     /* Enable GPIOA clock */
  164.   RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  165.   /* Enable SYSCFG clock */
  166.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  167.    
  168.     /* Configure PA8 and PA9 pin as input floating */
  169.   GPIOHandle.GPIO_Mode = GPIO_Mode_IN;
  170.   GPIOHandle.GPIO_PuPd = GPIO_PuPd_NOPULL;
  171.   GPIOHandle.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9;
  172.   GPIO_Init(GPIOA, &GPIOHandle);
  173.  
  174.   /* Connect EXTI Line0 to PA8 and PA9 pin */
  175.   SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource8);
  176.     SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource9);
  177.    
  178.      /* Configure EXTI Line 8 and Line 9*/
  179.   EXTIHandle.EXTI_Line = EXTI_Line8 | EXTI_Line9;
  180.   EXTIHandle.EXTI_Mode = EXTI_Mode_Interrupt;
  181.   EXTIHandle.EXTI_Trigger = EXTI_Trigger_Rising;  
  182.   EXTIHandle.EXTI_LineCmd = ENABLE;
  183.   EXTI_Init(&EXTIHandle);
  184.  
  185.     /* Enable and set EXTI Line 8 & 9 Interrupt to the lowest priority */
  186.   NVICHandle.NVIC_IRQChannel = EXTI9_5_IRQn;
  187.   NVICHandle.NVIC_IRQChannelPreemptionPriority = 0;
  188.   NVICHandle.NVIC_IRQChannelSubPriority = 0;
  189.   NVICHandle.NVIC_IRQChannelCmd = ENABLE;
  190.   NVIC_Init(&NVICHandle);
  191. }
  192.  
  193.  
  194.  
  195.  
  196. /**
  197.   * @brief  Congig delay ms and us
  198.   * @param  None
  199.   * @retval None
  200.   */
  201.  
  202. void TM_Delay_Init(void) {
  203.     RCC_ClocksTypeDef RCC_Clocks;
  204.    
  205.     /* Get system clocks */
  206.     RCC_GetClocksFreq(&RCC_Clocks);
  207.    
  208.     /* While loop takes 4 cycles */
  209.     /* For 1 us delay, we need to divide with 4M */
  210.     multiplier = RCC_Clocks.HCLK_Frequency / 4000000;
  211. }
  212.  
  213. void TM_DelayMicros(uint32_t micros) {
  214.     /* Multiply micros with multipler */
  215.     /* Substract 10 */
  216.     micros = micros * multiplier - 10;
  217.     /* 4 cycles for one loop */
  218.     while (micros--);
  219. }
  220.  
  221. void TM_DelayMillis(uint32_t millis) {
  222.     /* Multiply millis with multipler */
  223.     /* Substract 10 */
  224.     millis = 1000 * millis * multiplier - 10;
  225.     /* 4 cycles for one loop */
  226.     while (millis--);
  227. }
  228.  
  229.  
  230.  
  231. #ifdef  USE_FULL_ASSERT
  232.  
  233. /**
  234.   * @brief  Reports the name of the source file and the source line number
  235.   *         where the assert_param error has occurred.
  236.   * @param  file: pointer to the source file name
  237.   * @param  line: assert_param error line source number
  238.   * @retval None
  239.   */
  240. void assert_failed(uint8_t* file, uint32_t line)
  241. {
  242.   /* User can add his own implementation to report the file name and line number,
  243.      ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
  244.  
  245.   /* Infinite loop */
  246.   while (1)
  247.   {
  248.   }
  249. }
  250. #endif
  251.  
  252. /**
  253.   * @}
  254.   */
  255.  
  256. /**
  257.   * @}
  258.   */
  259.  
  260. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement