Advertisement
daskalot

Untitled

Dec 7th, 2018
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. /* Includes ------------------------------------------------------------------*/
  2. #include "stm32f4_discovery.h"
  3. #include "stm32f4xx_usart.h"
  4. volatile uint16_t calc = (uint16_t)1;
  5. void DELAY(){
  6. uint32_t d = 0x01000000;
  7. while(d--);
  8. }
  9. void EXTI0_IRQHandler(){
  10. if (EXTI_GetITStatus(EXTI_Line0)){
  11. while(!USART_GetFlagStatus(USART1, USART_FLAG_TXE));
  12. USART_SendData(USART1, calc);
  13. EXTI_ClearITPendingBit(EXTI_Line0);
  14. }
  15. calc++;
  16. if(calc>153) calc = 0;
  17. }
  18. volatile uint16_t data = (uint16_t)0x0;
  19. void USART1_IQRHandler(){
  20. if(USART_GetITStatus(USART1, USART_FLAG_RXNE)){
  21. data=USART_ReceiveData(USART1);
  22. if(data){
  23. GPIO_SetBits(GPIOD,0xF000);
  24. DELAY();
  25. GPIO_ResetBits(GPIOD, 0xF000);
  26. DELAY();
  27. }
  28. data = (uint16_t)0x0;
  29. }
  30. USART_ClearFlag(USART1, USART_FLAG_RXNE);
  31. }
  32. int main(void){
  33. //Start the device(= pins) clock
  34. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE);
  35. //Init the Device Structure
  36. GPIO_InitTypeDef u1;
  37. u1.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7;
  38. u1.GPIO_Mode = GPIO_Mode_AF;
  39. u1.GPIO_Speed = GPIO_Speed_2MHz;
  40. u1.GPIO_OType = GPIO_OType_PP;
  41. u1.GPIO_PuPd = GPIO_PuPd_UP;
  42. GPIO_Init(GPIOB, &u1);
  43. //Alternative function pins setup
  44. GPIO_PinAFConfig(GPIOB, GPIO_PinSource6,GPIO_AF_USART1);
  45. GPIO_PinAFConfig(GPIOB, GPIO_PinSource7,GPIO_AF_USART1);
  46. //Usart Setup
  47. //Start the device(=usart1)
  48. RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1, ENABLE);
  49. USART_InitTypeDef USART_InitStruct;
  50. USART_InitStruct.USART_BaudRate = 9600;
  51. USART_InitStruct.USART_WordLength = USART_WordLength_8b;
  52. USART_InitStruct.USART_StopBits = USART_StopBits_1;
  53. USART_InitStruct.USART_Parity = USART_Parity_No;
  54. USART_InitStruct.USART_HardwareFlowControl =
  55. USART_HardwareFlowControl_None;
  56. USART_InitStruct.USART_Mode = USART_Mode_Tx |
  57. USART_Mode_Rx;
  58. USART_Init(USART1, &USART_InitStruct);
  59. USART_Cmd(USART1, ENABLE);
  60.  
  61. //Start the device(=leds) clock
  62. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
  63. //Init the Device Structure
  64. GPIO_InitTypeDef leds;
  65. leds.GPIO_Pin = GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
  66. leds.GPIO_Mode = GPIO_Mode_OUT;
  67. leds.GPIO_Speed = GPIO_Speed_2MHz;
  68. leds.GPIO_OType = GPIO_OType_PP;
  69. leds.GPIO_PuPd = GPIO_PuPd_NOPULL;
  70. GPIO_Init(GPIOD, &leds);
  71. //Start the device(= button) clock
  72. RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);
  73. //Init the device(=button) structure
  74. GPIO_InitTypeDef button;
  75. button.GPIO_Pin = GPIO_Pin_0;
  76. button.GPIO_Mode = GPIO_Mode_IN;
  77. button.GPIO_Speed = GPIO_Speed_2MHz;
  78. button.GPIO_OType = GPIO_OType_PP;
  79. button.GPIO_PuPd = GPIO_PuPd_NOPULL;
  80. GPIO_Init(GPIOD, &button);
  81. //Interupt for button
  82. NVIC_InitTypeDef NVIC_InitStructure;
  83. NVIC_InitStructure.NVIC_IRQChannel = EXTI0_IRQn;
  84. NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
  85. NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1;
  86. NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
  87. NVIC_Init(&NVIC_InitStructure);
  88. //Exception handler for button
  89. EXTI_InitTypeDef EXTI_InitStructure;
  90. EXTI_InitStructure.EXTI_Line = EXTI_Line0;
  91. EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
  92. EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;
  93. EXTI_InitStructure.EXTI_LineCmd = ENABLE;
  94. EXTI_Init(&EXTI_InitStructure);
  95. //Interrupt for USART
  96. NVIC_InitTypeDef NVIC_InitStructure2;
  97. NVIC_InitStructure2.NVIC_IRQChannel = USART1_IRQn;
  98. NVIC_InitStructure2.NVIC_IRQChannelPreemptionPriority = 0;
  99. NVIC_InitStructure2.NVIC_IRQChannelSubPriority = 0;
  100. NVIC_InitStructure2.NVIC_IRQChannelCmd = ENABLE;
  101. NVIC_Init(&NVIC_InitStructure2);
  102. USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
  103. //Start
  104. RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
  105. SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource0);
  106. while (1){
  107.  
  108. }
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement