Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.22 KB | None | 0 0
  1. #include "stm32f0xx.h"
  2. #include "stm32f0xx_gpio.h"
  3. #include "stm32f0xx_rcc.h"
  4. #include "stm32f0xx_usart.h"
  5. #include "stm32f0xx_exti.h"
  6.  
  7. volatile uint32_t timer_ms = 0x0;
  8. double napiecie = 0;
  9. volatile uint16_t received[128];
  10. volatile uint16_t free = 0x0;
  11.  
  12. void delay(int time)
  13. {
  14. timer_ms = time;
  15. while(timer_ms)
  16. {
  17.  
  18. }
  19. }
  20.  
  21. void SysTick_Handler()
  22. {
  23. if(timer_ms){
  24. timer_ms--;
  25. }
  26. }
  27.  
  28. void USART2_IRQHandler(void)
  29. {
  30.  
  31. if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
  32. {
  33. USART_SendData(USART2,'c');
  34. received[free] = USART_ReceiveData(USART2);
  35. free++;
  36. if (free == 128)
  37. {
  38. free = 0;
  39. }
  40. }
  41. }
  42.  
  43. void send_char(char c)
  44. {
  45. while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
  46. USART_SendData(USART2, c);
  47. }
  48.  
  49. void send_string(const char* s)
  50. {
  51. while (*s)
  52. send_char(*s++);
  53. }
  54.  
  55. int main(void)
  56. {
  57. GPIO_InitTypeDef gpio;
  58. USART_InitTypeDef uart;
  59.  
  60. RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD, ENABLE);
  61. RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
  62.  
  63. GPIO_StructInit(&gpio);
  64. GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
  65. GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
  66. gpio.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
  67. gpio.GPIO_OType = GPIO_OType_PP;
  68. gpio.GPIO_Mode = GPIO_Mode_AF;
  69. gpio.GPIO_PuPd = GPIO_PuPd_UP;
  70. gpio.GPIO_Speed = GPIO_Speed_50MHz;
  71. GPIO_Init(GPIOA, &gpio);
  72.  
  73. gpio.GPIO_Pin = GPIO_Pin_5;
  74. gpio.GPIO_Mode = GPIO_Mode_OUT;
  75. gpio.GPIO_PuPd = GPIO_PuPd_DOWN;
  76. GPIO_Init(GPIOA, &gpio);
  77.  
  78. gpio.GPIO_Pin = GPIO_Pin_13;
  79. gpio.GPIO_Mode = GPIO_Mode_IN;
  80. GPIO_Init(GPIOC, &gpio);
  81.  
  82. SysTick_Config(SystemCoreClock / 0x3E8);
  83.  
  84. USART_StructInit(&uart);
  85. uart.USART_BaudRate = 9600;
  86. USART_Init(USART2, &uart);
  87.  
  88. USART_Cmd(USART2, ENABLE);
  89. USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
  90. NVIC_EnableIRQ(USART2_IRQn);
  91.  
  92. while (1) {
  93. if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == 0) {
  94. GPIO_SetBits(GPIOA, GPIO_Pin_5);
  95. send_string("z");
  96. } else {
  97. GPIO_ResetBits(GPIOA, GPIO_Pin_5);
  98. send_string("y");
  99. }
  100. }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement