完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
总共有4个文件,其中led.c和debug.c可以不需要
- main.c - system.c - debug.c - led.c 1. main.c 主要实现系统的初始化,led端口,串口的初始化,此处如果发现1S灯闪烁,打印出"hello world",就说明初步环境好了,可以在这基础上添加自己的代码了。 #include "stm32f10x.h" #include "includes.h" int main(void) { system_init(); led_init(); uart1_init(); led_on(0); sys_delay_ms(1000); led_off(0); sys_delay_ms(1000); while(1) { sys_delay_ms(2000); led_toggle(0); printf("hello worldrn"); } } #ifdef USE_FULL_ASSERT /** * @brief 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) { while (1) { } } #endif /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ 2. system.c 此文件实现系统时钟配置,中断向量表配置和systick配置。 #include "system.h" #include "stm32f10x_it.h" ErrorStatus HSEStartUpStatus; //1. clock config void rcc_configuration(void) { RCC_DeInit(); RCC_HSEConfig(RCC_HSE_ON); HSEStartUpStatus = RCC_WaitForHSEStartUp(); if(SUCCESS == HSEStartUpStatus) { FLASH_SetLatency(FLASH_Latency_2); FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable); RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9); RCC_PLLCmd(ENABLE); while(RESET == RCC_GetFlagStatus(RCC_FLAG_PLLRDY)) { } // HCLK up to 72MHz RCC_HCLKConfig(RCC_SYSCLK_Div1); //AHBÍâÉè RCC_PCLK1Config(RCC_HCLK_Div2); //APB1ÍâÉè RCC_PCLK2Config(RCC_HCLK_Div1); //APB2ÍâÉè RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK); while(0x08 != RCC_GetSYSCLKSource()) { } } } //2.nvic config void nvic_configuration(void) { #ifdef VECT_TAB_RAM NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif } //3. systick config/ 1ms void systick_configuration(void) { //clk configuration SysTick_CLKSourceConfig(SysTick_CLKSource_HCLK); SysTick_Config(72000); //1ms arise interrupt } //4. systick ms_delay void sys_delay_ms(uint16_t ms) { uint32_t _t = systick_ms_counter+ms; while(systick_ms_counter < _t); } //5. systick s_delay void sys_delay_1s(uint16_t s) { uint32_t _t = systick_s_counter + s; while(systick_s_counter < _t); } //6. sys_init void system_init(void) { rcc_configuration(); nvic_configuration(); systick_configuration(); } //7. uncertain_delay void delay(uint16_t dly) { int i,j; for(i = 0;i } 3. debug.c 该文件用于实现打印串口驱动,只提供打印输出,利用printf函数的重映射,还需要在Keil软件中进行设置,否则打印不出来滴 #include "includes.h" void usart1_gpio_config(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); //DBTX PA9 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA,&GPIO_InitStructure); //DBRX PA10 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA,&GPIO_InitStructure); } //2. void usart1_func_config(void) { USART_InitTypeDef USART_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1,ENABLE); 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_Mode = USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); // USART_ITConfig(USART1,USART_IT_RXNE,ENABLE); } //3. void uart1_nvic_config(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_PriorityGroupConfig(NVIC_PriorityGroup_1); NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); } //4.串口初始化,对外的接口. void uart1_init(void) { usart1_gpio_config(); usart1_func_config(); // uart1_nvic_config(); } //5.printf函数的重映射实现 int fputc(int ch, FILE * f) { //rs232 Debug USART_SendData(USART1, (uint8_t)ch); while(USART_GetFlagStatus(USART1, USART_FLAG_TXE) == RESET); return ch; } 4. led.c 该文件实现板子上led GPIO口的配置初始化,灯的开,关,闪烁功能。 #include "includes.h" //led config //pe8 pe9 void led_init(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOE, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; GPIO_Init(GPIOE,&GPIO_InitStructure); GPIO_SetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_9); } // void led_on(uint8_t ledid) { switch(ledid) { case 0:GPIO_ResetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_9);break; case 1:GPIO_ResetBits(GPIOE,GPIO_Pin_8);break; case 2:GPIO_ResetBits(GPIOE,GPIO_Pin_9);break; default:break; } } // void led_off(uint8_t ledid) { switch(ledid) { case 0:GPIO_SetBits(GPIOE,GPIO_Pin_8|GPIO_Pin_9);break; case 1:GPIO_SetBits(GPIOE,GPIO_Pin_8);break; case 2:GPIO_SetBits(GPIOE,GPIO_Pin_9);break; default:break; } } void led_toggle(uint8_t led_id){ switch(led_id) { case 0:GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8))); GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8))); break; case 1:GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8)));break; case 2:GPIO_WriteBit(GPIOE,GPIO_Pin_8,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOC,GPIO_Pin_8)));break; default:break; } } 5. stm32f10x_it.c 感觉也有必要贴出中断服务函数文件,这里面最开始配置了systick的中断服务函数 #include "stm32f10x_it.h" uint32_t systick_ms_counter = 0; uint32_t systick_s_counter = 0; void NMI_Handler(void) { } /** * @brief This function handles Hard Fault exception. * @param None * @retval : None */ void HardFault_Handler(void) { /* Go to infinite loop when Hard Fault exception occurs */ while (1) { } } /** * @brief This function handles Memory Manage exception. * @param None * @retval : None */ void MemManage_Handler(void) { /* Go to infinite loop when Memory Manage exception occurs */ while (1) { } } /** * @brief This function handles Bus Fault exception. * @param None * @retval : None */ void BusFault_Handler(void) { /* Go to infinite loop when Bus Fault exception occurs */ while (1) { } } /** * @brief This function handles Usage Fault exception. * @param None * @retval : None */ void UsageFault_Handler(void) { /* Go to infinite loop when Usage Fault exception occurs */ while (1) { } } /** * @brief This function handles SVCall exception. * @param None * @retval : None */ void SVC_Handler(void) { } /** * @brief This function handles Debug Monitor exception. * @param None * @retval : None */ void DebugMon_Handler(void) { } /** * @brief This function handles PendSVC exception. * @param None * @retval : None */ void PendSV_Handler(void) { } /** * @brief This function handles SysTick Handler. * @param None * @retval : None */ void EXTI1_IRQHandler() { } /* ¹¦ÄÜ£º µ±ÓнÓÊÕÖжÏʱ£¬½«½ÓÊÕµÄ×Ö·û´®´æ·ÅÔÚRxBuff[]ÖÐ µ±Óз¢ËÍÖжÏʱ£¬½«TxBuff[]ÖеÄÊý¾Ý·¢Ë͵½PC¶Ë */ void UART4_IRQHandler() { } void SysTick_Handler() { systick_ms_counter++; //ms if(systick_ms_counter%1000==0){ systick_s_counter++; //s } } void USART1_IRQHandler() { } /******************************************************************************/ /* STM32F10x Peripherals Interrupt Handlers */ /* Add here the Interrupt Handler for the used peripheral(s) (PPP), for the */ /* available peripheral interrupt handler's name please refer to the startup */ /* file (startup_stm32f10x_xx.s). */ /******************************************************************************/ /** * @brief This function handles PPP interrupt request. * @param None * @retval : None */ /*void PPP_IRQHandler(void) { }*/ /** * @} */ /******************* (C) COPYRIGHT 2009 STMicroelectronics *****END OF FILE****/ |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1780 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1621 浏览 1 评论
1081 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
728 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1679 浏览 2 评论
1938浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
731浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
570浏览 3评论
596浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
556浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-23 15:30 , Processed in 0.784628 second(s), Total 79, Slave 62 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号