#include "stm32f0xx.h" #define Input1 GPIOB, GPIO_Pin_0 #define Input2 GPIOB, GPIO_Pin_1 #define Input3 GPIOB, GPIO_Pin_2 #define Input4 GPIOB, GPIO_Pin_6 #define Output1 GPIOA, GPIO_Pin_8 #define Output2 GPIOB, GPIO_Pin_3 #define Output3 GPIOB, GPIO_Pin_4 #define Output4 GPIOB, GPIO_Pin_5 #define RS485_DIR GPIOA, GPIO_Pin_1 char RecvData; /* Private variables ---------------------------------------------------------*/ GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_InitStructure; int main(void) { /* GPIOA, GPIOB Periph clock enable */ RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE); RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOB, ENABLE); /*GPIO Output Pin init*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_1; //PA8 & PA1 als Output GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_5; //PB3,4,5 als Output GPIO_Init(GPIOB, &GPIO_InitStructure); /*GPIO Input Pin init*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 | GPIO_Pin_2 | GPIO_Pin_6; //PB0,1,2,6 als Input GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_Init(GPIOB, &GPIO_InitStructure); //***********************************************************************\\ /* USART2 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); /* Connect PA2 to USART2_Tx */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_Mode_AF); /* Connect PA3 to USART2_Rx */ GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_Mode_AF); //Set USART2 Rx & Tx as AF GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2 | GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); USART_InitStructure.USART_BaudRate = 115200; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx; /* USART configuration */ USART_Init(USART2, &USART_InitStructure); //Gedankenstütze, vergleiche mit GPIO /* Enable USART */ USART_Cmd(USART2, ENABLE); while (1) { if (GPIO_ReadInputDataBit(Input1) == 1) { GPIO_WriteBit(RS485_DIR,Bit_SET); while (USART_GetFlagStatus(USART2, USART_FLAG_TXE) == RESET) {} USART_SendData(USART2, 'X'); GPIO_WriteBit(Output1,Bit_SET); //Lokal zur kontrolle } else { GPIO_WriteBit(Output1,Bit_RESET); GPIO_WriteBit(RS485_DIR,Bit_RESET); } } }