完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
Main ()
{ #include “stm32f10x.h” #include “stdio.h” float Frequency_value; #define LED_ON GPIO_ResetBits(GPIOA,GPIO_Pin_12) #define LED_OFF GPIO_SetBits(GPIOA,GPIO_Pin_12) Void TIM2_Configuart(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);//使能其所在时钟 TIM_TimeBaseStructure.TIM_Period = 400-1; //设置重装载寄存器的周期 TIM_TimeBaseStructure.TIM_Prescaler = 36000-1; //设置时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上的计数模式 TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); //TIM3的初始化 TIM_ETRClockMode2Config(TIM3,TIM_ExtTRGPSC_OFF,TIM_ExtTRGPolarity_Inverted,0); // TIM_ExtTRGPSC_OFF :ETRP 预分频 OFF // TIM_ExtTRGPolarity_NonInverted : TIM外部触发极性非翻转:高电平或者上升沿有效 TIM_SetCounter(TIM3,0); TIM_Cmd(TIM2, ENABLE); //使能TIM3 } Void TIM3_Configuart(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);//使能其所在时钟 // TIM_Period | TIM_Prescaler 16位 TIM_TimeBaseStructure.TIM_Period = 0XFFFF - 1; //设置重装载寄存器的周期 TIM_TimeBaseStructure.TIM_Prescaler = 0X00; //设置时钟频率除数的预分频值 TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割 TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; //向上的计数模式 TIM_TimeBaseInit(TIM3, &TIM_TimeBaseStructure); //TIM3的初始化 TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE ); //TIM3更新中断使能 TIM_Cmd(TIM2, ENABLE); //使能TIM2 } Void IO_Configuart(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);//使能其所在时钟 GPIO_InitStructure.GPIO_Pin= GPIO_Pin_12 //设置引脚 GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP; //设置模式 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置速度 GPIO_Init(GPIOA,&GPIO_InitStructure); } Void NVIC_ Configuart(void) { NVIC_InitTypeDef NVIC_InitStructure; NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; //TIM2中断 NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; //先占优先0级 NVIC_InitStructure.NVIC_IRQChannelSubPriority = 1; //从占优先3级 NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道使能 NVIC_Init(&NVIC_InitStructure); //初始化NVIC寄存器 } void USART1_Configuart(void) { GPIO_InitTypeDef GPIO_InitStructure; RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE); GPIO_InitStructure.GPIO_Pin= GPIO_Pin_9 | GPIO_Pin_10; //设置引脚TX | RX GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP; //设置串行通信模式 GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;//设置速度 GPIO_Init(GPIOA,&GPIO_InitStructure); 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_HardWareFlowControl = USART_HardWareFlowControl_None;//无流控 USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;//使能发送和接收 USART_Init(USART1,&USART_InitStructure); USART_Cmd(USART1,ENABLE); } int fputc(int ch, FILE *f) { USART_SendData(USART1, (unsigned char) ch); while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET) {} return ch; } void Delay_ms(void) { long int i,j; for (i = 0; i 《 10000; i++) for (j = 0; j 《 1000; j++); } int main(void) { TIM3_Configuart(); TIM2_Configuart(); IO_Configuart(); NVIC_ Configuart(); USART1_Configuart(); While(1) { LED_ON; printf(“nHello!n”); printf(“n%f”,Frequency_value); Delay_ms(); LED_OFF; Delay_ms(); } Return 0; } /stm32f10x_it.c********/ //定时器3中断服务程序 void TIM3_IRQHandler(void) //TIM3中断 { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) //检查TIM3更新中断发生与否 { TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); //清除TIMx更新中断标志 Frequency_value = TIM_GetCounter(TIM3)/0.2; //周期200ms TIM_SetCounter(TIM3,0);//从0开始计数字 } /stm32f10x_it.c********/ /stm32f10x_it.h********/ Extern float Frequency_value; void TIM2_IRQHandler(void); /stm32f10x_it.h********/ |
|
|
|
只有小组成员才能发言,加入小组>>
调试STM32H750的FMC总线读写PSRAM遇到的问题求解?
1885 浏览 1 评论
X-NUCLEO-IHM08M1板文档中输出电流为15Arms,15Arms是怎么得出来的呢?
1663 浏览 1 评论
1149 浏览 2 评论
STM32F030F4 HSI时钟温度测试过不去是怎么回事?
763 浏览 2 评论
ST25R3916能否对ISO15693的标签芯片进行分区域写密码?
1720 浏览 2 评论
1965浏览 9评论
STM32仿真器是选择ST-LINK还是选择J-LINK?各有什么优势啊?
790浏览 4评论
STM32F0_TIM2输出pwm2后OLED变暗或者系统重启是怎么回事?
616浏览 3评论
631浏览 3评论
stm32cubemx生成mdk-arm v4项目文件无法打开是什么原因导致的?
593浏览 3评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2025-1-14 12:10 , Processed in 0.890690 second(s), Total 77, Slave 61 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号