Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stm32f0xx.h"
- #include "stm32f0xx_gpio.h"
- #include "stm32f0xx_rcc.h"
- #include "stm32f0xx_usart.h"
- #include "stm32f0xx_exti.h"
- volatile uint32_t timer_ms = 0x0;
- double napiecie = 0;
- volatile uint16_t received[128];
- volatile uint16_t free = 0x0;
- void delay(int time)
- {
- timer_ms = time;
- while(timer_ms)
- {
- }
- }
- void SysTick_Handler()
- {
- if(timer_ms){
- timer_ms--;
- }
- }
- void USART2_IRQHandler(void)
- {
- if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
- {
- USART_SendData(USART2,'c');
- received[free] = USART_ReceiveData(USART2);
- free++;
- if (free == 128)
- {
- free = 0;
- }
- }
- }
- void send_char(char c)
- {
- while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET);
- USART_SendData(USART2, c);
- }
- void send_string(const char* s)
- {
- while (*s)
- send_char(*s++);
- }
- int main(void)
- {
- GPIO_InitTypeDef gpio;
- USART_InitTypeDef uart;
- RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA | RCC_AHBPeriph_GPIOB | RCC_AHBPeriph_GPIOC | RCC_AHBPeriph_GPIOD, ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
- GPIO_StructInit(&gpio);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
- GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
- gpio.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3;
- gpio.GPIO_OType = GPIO_OType_PP;
- gpio.GPIO_Mode = GPIO_Mode_AF;
- gpio.GPIO_PuPd = GPIO_PuPd_UP;
- gpio.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOA, &gpio);
- gpio.GPIO_Pin = GPIO_Pin_5;
- gpio.GPIO_Mode = GPIO_Mode_OUT;
- gpio.GPIO_PuPd = GPIO_PuPd_DOWN;
- GPIO_Init(GPIOA, &gpio);
- gpio.GPIO_Pin = GPIO_Pin_13;
- gpio.GPIO_Mode = GPIO_Mode_IN;
- GPIO_Init(GPIOC, &gpio);
- SysTick_Config(SystemCoreClock / 0x3E8);
- USART_StructInit(&uart);
- uart.USART_BaudRate = 9600;
- USART_Init(USART2, &uart);
- USART_Cmd(USART2, ENABLE);
- USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
- NVIC_EnableIRQ(USART2_IRQn);
- while (1) {
- if (GPIO_ReadInputDataBit(GPIOC, GPIO_Pin_13) == 0) {
- GPIO_SetBits(GPIOA, GPIO_Pin_5);
- send_string("z");
- } else {
- GPIO_ResetBits(GPIOA, GPIO_Pin_5);
- send_string("y");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement