#include "main.h"
#include "dma.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include <stdio.h>
#include <string.h>
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
extern DMA_HandleTypeDef hdma_usart1_tx;
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
uint8_t RxData;
uint8_t RxBuff[255];
static uint8_t RxCnt;
float dcbusval[2];/*存放母线电压*///8B+4B=2*4+4
uint8_t vofaData[8]={0,0,0,0,0,0,0x80,0x7f};
float AddStep[1];
/* USER CODE END 0 */
int main(void)
{
RxCnt=0;
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_DMA_Init();
MX_USART1_UART_Init();
/* USER CODE BEGIN 2 */
HAL_UART_Receive_IT(&huart1, (uint8_t*)&RxData, 1);
/* USER CODE BEGIN WHILE */
while (1)
{
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE1_BOOST);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = RCC_PLLM_DIV3;
RCC_OscInitStruct.PLL.PLLN = 40;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = RCC_PLLQ_DIV2;
RCC_OscInitStruct.PLL.PLLR = RCC_PLLR_DIV2;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_4) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
int fputc(int ch,FILE *f)
{
while((USART1->ISR&0X40)==0);//中断状态寄存器
USART1->TDR=(uint8_t)ch;//传输数据寄存器
return ch;
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
uint8_t DataB1[17]="key down dma\r\n";
/* Prevent unused argument(s) compilation warning */
UNUSED(GPIO_Pin);
if(B1_Pin==GPIO_Pin)
{
HAL_GPIO_TogglePin(LD2_GPIO_Port,LD2_Pin);
while(HAL_DMA_GetState(&hdma_usart1_tx)!=HAL_DMA_STATE_READY);
HAL_UART_Transmit_DMA(&huart1,DataB1,sizeof(DataB1));
//printf("key down\r\n");
}
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
/* Prevent unused argument(s) compilation warning */
UNUSED(huart);
if(RxCnt>=20)
{
RxCnt=0;
memset(RxBuff,0x00,sizeof(RxBuff));//函数的功能:将指针变量 s 所指向的前 n 字节的内存单元用一个“整数” c 替换,注意 c 是 int 型。s 是 void* 型的指针变量,所以它可以为任何类型的数据进行初始化
//将数组指向的地方的前sizeof内存用0X00替换,将数组初始化为0
HAL_UART_Transmit(&huart1,(uint8_t *)"???",20,0xffff);
}
else
{
RxBuff[RxCnt++]=RxData;//这个数传过来就要写入数组
//RxCnt++;
}
// HAL_UART_Transmit(&huart1,RxBuff,10,0xffff);
HAL_UART_Receive_IT(&huart1,(uint8_t*)&RxData,1);//在接受中断中接受数据
}
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
使用CUBEMX配置的外设,但是可以往外发,但是变量接受不到串口往下发的数据,仿真的时候,看到即使我没有发数据他越会自己跑到接受中断中, |
也可以在库中找个例程先跑一下,看看能不能收到数据。