生成之后,我用的事keil 编译的, 要去修改启动文件里面一个代码
打开工程的启动文件 startup_stm32f407xx.s
将里面的 Heap_Size EQU 0x00000200
改成 Heap_Size EQU 0x00001000
或者改成Heap_Size EQU 0x00001500
其中1500是官方那个演示程序的设置.
再次打开main.c文件
添加头文件
include "usbd_cdc.h"算了直接上main.c里面的全部代码,其他的文件都不用修改
- /* Includes ------------------------------------------------------------------*/
- include "stm32f4xx_hal.h"
- include "usb_device.h"
- include "gpio.h"
- /* USER CODE BEGIN Includes */
- include "usbd_cdc.h"
- /* USER CODE END Includes */
- /* Private variables ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* USER CODE BEGIN 0 */
- uint16_t mmss;
- /* USER CODE END 0 */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- uint8_t UserTxBuffer55 = 0x55;
- /* USER CODE END 1 */
- /* MCU Configuration----------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_USB_DEVICE_Init();
- /* USER CODE BEGIN 2 */
- /* USER CODE END 2 */
- /* USER CODE BEGIN 3 */
- /* Infinite loop */
- while (1)
- {
- if(mmss>=500)
- {
- HAL_GPIO_TogglePin (GPIOD,GPIO_PIN_12);
- USBD_CDC_SetTxBuffer(&hUsbDeviceFS, (uint8_t*)&UserTxBuffer55, 1);
- USBD_CDC_TransmitPacket(&hUsbDeviceFS);
- mmss = 0;
- }
- }
- /* USER CODE END 3 */
- }
- /** System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct;
- RCC_ClkInitTypeDef RCC_ClkInitStruct;
- __PWR_CLK_ENABLE();
- __HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
- 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 = 8;
- RCC_OscInitStruct.PLL.PLLN = 336;
- RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
- RCC_OscInitStruct.PLL.PLLQ = 7;
- HAL_RCC_OscConfig(&RCC_OscInitStruct);
- RCC_ClkInitStruct.ClockType = 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_DIV4;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV2;
- HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_5);
- }
- /* USER CODE BEGIN 4 */
- void HAL_SYSTICK_Callback(void)
- {
- /* NOTE : This function Should not be modified, when the callback is needed,
- the HAL_SYSTICK_Callback could be implemented in the user file
- */
- mmss++;
- }
- /* USER CODE END 4 */
- ifdef USE_FULL_ASSERT
- /**
- * [url=home.php?mod=space&uid=247401]@brief[/url] Reports the name of the source file and the source line number
- * where the assert_param error has occurred.
- * @param file: pointer to the source file name
- * @param line: assert_param error line source number
- * @retval None
- */
- void assert_failed(uint8_t* file, uint32_t line)
- {
- /* USER CODE BEGIN 6 */
- /* User can add his own implementation to report the file name and line number,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- endif
好了,编译烧录,打开串口调试助手
额,先上图


注意啊,串口调试助手里面,只需要选择相应的串口就可以了......其他的什么波特率啊,位数啊,都没用的.......... 咱是usb虚拟的,没定义这个....不信你可以自己试试
|