вот код на STM
#include "stm32f4xx.h" #include <stdio.h>
void USART2_ini(void); GPIO_InitTypeDef GPIO_InitStructure; USART_InitTypeDef USART_Init_Usa; GPIO_InitTypeDef GPIO_Init_Button; void Button_ini(void);
uint16_t delya_count=0; void SysTick_Handler(void) { if (delya_count>0) { delya_count--; } }
void delay_ms(uint16_t delya_temp) { delya_count = delya_temp; while(delya_count){} }
void SendStrtoPC(char* str_p, uint16_t count) { uint16_t i=0; if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)==1) { while(i< count) { while(USART_GetFlagStatus(USART2, USART_FLAG_TXE)==RESET){} USART_SendData ( USART2, str_p[i]); i++; } } }
int main (void) { char str[30]; SysTick_Config(SystemCoreClock/1000); USART2_ini(); Button_ini(); sprintf(str, "123"); //sprintf(str, "0xF0, 0x7F|0xF0"); sprintf(str, "0xF0"); //SendStrtoPC(str, 30); //sprintf(str, "0xF0"); while(1) { delay_ms(500); SendStrtoPC(str, 30); } }
void USART2_ini(void) { { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF; GPIO_InitStructure.GPIO_OType = GPIO_OType_PP; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP; GPIO_Init(GPIOA, &GPIO_InitStructure); GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_USART2); GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_USART2); }
{ RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE); USART_Init_Usa.USART_BaudRate = 9600; USART_Init_Usa.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init_Usa.USART_Mode = USART_Mode_Tx|USART_Mode_Rx; USART_Init_Usa.USART_Parity = USART_Parity_No; USART_Init_Usa.USART_StopBits = USART_StopBits_1; USART_Init_Usa.USART_WordLength = USART_WordLength_8b; USART_Init(USART2, &USART_Init_Usa); USART_Cmd(USART2, ENABLE); } } void Button_ini(void) { RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE); GPIO_Init_Button.GPIO_Pin = GPIO_Pin_0; GPIO_Init_Button.GPIO_Mode = GPIO_Mode_IN; GPIO_Init_Button.GPIO_OType = GPIO_OType_PP; GPIO_Init_Button.GPIO_Speed = GPIO_Speed_2MHz; GPIO_Init_Button.GPIO_PuPd = GPIO_PuPd_NOPULL; GPIO_Init(GPIOA, &GPIO_Init_Button); GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0); }
а схема подключения, на STM PORTA2 и PORTA3 и земля, ну и на драйвере RX TX и GND
|