- #include "stm32f10x.h"
- #include "stdio.h"
- uint8_t flag = 0;
- void STM32_LED_Configuration(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
-
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
-
- GPIOD->BSRR = GPIO_Pin_2|GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_7;
- }
- void NVIC_TIM5Configuration(void)
- {
- NVIC_InitTypeDef NVIC_InitStructure;
-
- NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
- NVIC_InitStructure.NVIC_IRQChannel = TIM5_IRQn;
- NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
- NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;
- NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
- NVIC_Init(&NVIC_InitStructure);
- }
- void TIM5_Init(void)
- {
- TIM_TimeBaseInitTypeDef TIM_Time_BaseInitStructure;
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM5,ENABLE);//打开TIM5的时钟;
-
- TIM_Time_BaseInitStructure.TIM_Prescaler = (100 - 1);//预分频器寄存器1ms
- //这个是你要设置的预分频器的数值;
- //就是将你的频率分成多少比如说你要将其分频成36000HZ的话就是预分频的数值设置成72000000/36000;
- //这个值就是用来和计数值相互合作的,可以减少一个数值的使用大小的范围;
- //16位可编程(可以实时修改)预分频器,计数器时钟频率的分频系数为1~65536之间的任意
-
- TIM_Time_BaseInitStructure.TIM_Period = (720 - 1);//自动装载寄存器
- //这个是你要自动重装载的值;
- //只要这两个数是72M的整数倍即可(72M是APB1没有进行分频的情况下的数值,如果分频 的话
- //需要的就是*2,
- TIM_Time_BaseInitStructure.TIM_ClockDivision = 0;//这个就是一个时钟分割;现阶段没怎么用的到
-
- TIM_Time_BaseInitStructure.TIM_CounterMode = TIM_CounterMode_Down;
- //计数的方式;
- //向下计数;
-
- TIM_TimeBaseInit(TIM5,&TIM_Time_BaseInitStructure);
- //初始化TIM5
-
- TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
-
-
- TIM_ITConfig(TIM5,TIM_IT_Update,ENABLE);
- //这个是使能中断;
- TIM_Cmd(TIM5,ENABLE);
- //使能TIM5
- }
- void TIM5_IRQHandler(void)
- {
- if(TIM_GetITStatus(TIM5,TIM_IT_Update) != RESET)
- {
- TIM_ClearITPendingBit(TIM5,TIM_IT_Update);
- flag = 1;
- GPIOD->ODR ^= GPIO_Pin_7;
- }
- }
- void STM32_USART_Config()
- {
- USART_InitTypeDef USART_InitStructure;
- GPIO_InitTypeDef GPIO_InitStructure;
-
- RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD|RCC_APB2Periph_AFIO,ENABLE);
- GPIO_PinRemapConfig(GPIO_Remap_USART2,ENABLE);
- RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2,ENABLE);
-
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
-
- GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
- GPIO_Init(GPIOD,&GPIO_InitStructure);
-
- 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(USART2,&USART_InitStructure);
-
- USART_Cmd(USART2,ENABLE);
- }
- #ifndef MicroLIB
- //#pragma import(__use_no_semihosting) //没有实现fgetc时需要声明该参数
- /* 标准库需要的支持函数 使用printf()调试打印不需要实现该函数 */
- struct __FILE
- {
- int handle;
- /* Whatever you require here. If the only file you are using is */
- /* standard output using printf() for debugging, no file handling */
- /* is required. */
- };
- FILE __stdout;
- //定义_sys_exit()以避免使用半主机模式
- _sys_exit(int x)
- {
- x = x;
- }
- /* 重定义fputc函数 如果使用MicroLIB只需要重定义fputc函数即可 */
- int fputc(int ch, FILE *f)
- {
- /* Place your implementation of fputc here */
- /* e.g. write a character to the USART */
- USART_SendData(USART2, (uint8_t) ch);
- /* Loop until the end of transmission */
- while (USART_GetFlagStatus(USART2, USART_FLAG_TC) == RESET)
- {}
- return ch;
- }
- /*
- 可以直接使用putchar
- 不需要再定义 int putchar(int ch),因为stdio.h中有如下定义
- #define putchar(c) putc(c, stdout)
- */
- int ferror(FILE *f) {
- /* Your implementation of ferror */
- return EOF;
- }
- #endif
- FILE __stdin;
- int fgetc(FILE *fp)
- {
- int ch = 0;
-
- while(USART_GetFlagStatus(USART2, USART_FLAG_RXNE) == RESET)
- {
- }
- ch = (int)USART2->DR & 0xFF;
-
- putchar(ch); //回显
-
- return ch;
- }
- int main()
- {
- uint8_t Count =0;
- uint32_t PwmValue = 0;
- uint32_t DirValue = 0;
- STM32_LED_Configuration();
- NVIC_TIM5Configuration();
- TIM5_Init();
- STM32_USART_Config();
-
- while(1)
- {
- if(flag == 1)
- {
- flag = 0;
- printf("\r\n TIMI的实验,第%d个周期发送数据 \r\n",Count++);
- }
-
- }
- }
- <img src="https://www.stmcu.org.cn/module/forum/forum.php?mod=image&aid=390661&size=300x300&key=254b7fc2b1c5f5b3&nocache=yes&type=fixnone" aid="attachimg_390661" alt="" border="0">
- 在下有个两个问题想问一下:
- 1:为什么我设置的是1ms去点亮一下,为什么测试的是500HZ;
- 2:我在改定时器的数值大小的时候,为什么我改到频率的数值好几M以后,还是显示300KHz;
- 希望大家帮我解决一下
复制代码
|
2、第二个我是猜想的,频率高了,系统频繁中断,实际上这些都是需要频繁执行执行压栈出栈指令的,因此时间都耗在这里了,所以频率高不了;解决的办法可以用定时器做50%输出的PWM波,频率肯定可以提高,但最高到多少我也没试过。
懂了,谢谢,嘿嘿