#include "stm32f10x.h" #include "delay.h" #include "usart.h" #include "rs485.h" #include <stdio.h> #include <string.h> #include "OLED.h" #include "key.h" #include "motor.h" int value =0; int EncoderA ; int space = 90; int EncoderA_S=0; u8 rs485bufsend[5]={0xAA, 0x00, 0x00, 0x00, 0x55}; void Encoder_Init_TIM4(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 10; TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //Reset counter TIM_SetCounter(TIM4,0); TIM_Cmd(TIM4, ENABLE); } void TIM4_IRQHandler(void) { if(TIM4->SR&0X0001) { } TIM4->SR&=~(1<<0); } int Read_Encoder() { int Encoder_TIM; Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=0; return Encoder_TIM; } void motor_sen(){ TIM_SetCompare1(TIM3,0);TIM_SetCompare2(TIM3,space); LEDR_ON;LEDL_OFF; } void motor_shou(){ TIM_SetCompare1(TIM3,space);TIM_SetCompare2(TIM3,0); LEDL_ON;LEDR_OFF; } void motor_stop(){ TIM_SetCompare1(TIM3,0);TIM_SetCompare2(TIM3,0); LEDR_OFF;LEDL_OFF; } void TIM2_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Period = arr; TIM_TimeBaseStructure.TIM_Prescaler =psc; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM2, ENABLE); } void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); rs485bufsend[1]=space; rs485bufsend[2]=EncoderA/256; rs485bufsend[3]=EncoderA%256; RS485_RX_CNT=1; RS485_Send_Data(rs485bufsend,5); RS485_RX_CNT=0; } } void reset_motor(){ motor_shou(); EncoderA=1; while(EncoderA){ delay_ms(100); EncoderA = Read_Encoder(); } motor_stop(); } int main(void) { SystemInit(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); delay_init(); RS485_Init(115200); OLED_Init(); OLED_Clear(); LED_Init(); TIM2_Int_Init(9999,7199); Motor_Init(99,71); Encoder_Init_TIM4(); LEDL_OFF; LEDR_OFF; OLED_ShowStr(0,0,"INIT:",16); reset_motor(); OLED_Clear(); OLED_ShowStr(0,0,"status:",16); OLED_ShowStr(0,2,"dev:",16); OLED_ShowStr(0,4,"SET:",16); OLED_ShowStr(0,6,"space:",16); while(1){ EncoderA = Read_Encoder()+EncoderA; OLED_ShowNum(50,0,value,2,16); OLED_ShowNum(50,2,EncoderA/10,4,16); OLED_ShowNum(50,6,space,4,16); OLED_ShowNum(50,4,EncoderA_S,4,16); RS485_TX_EN=0; if(RS485_RX_CNT !=0){ if(RS485_RX_BUF[0] == 0xAA){ switch(RS485_RX_BUF[1]){ case 0x01:motor_sen();value=0; break; case 0x02:motor_shou();value=0; break; case 0x03:motor_stop();value =0;break; case 0x04:space =RS485_RX_BUF[2]; break; case 0x05:EncoderA_S=RS485_RX_BUF[2]*100; EncoderA_S=EncoderA_S+RS485_RX_BUF[3];value =1;break; } } RS485_RX_CNT=0; } if(value==1){ if(EncoderA/10>EncoderA_S){ motor_shou(); }else if(EncoderA/10<EncoderA_S){ motor_sen(); }else{ motor_stop(); } delay_ms(1); } } }
理解问题#include "stm32f10x.h" #include "delay.h" #include "usart.h" #include "rs485.h" #include <stdio.h> #include <string.h> #include "OLED.h" #include "key.h" #include "motor.h" int value =0; int EncoderA ; int space = 90; int EncoderA_S=0; u8 rs485bufsend[5]={0xAA, 0x00, 0x00, 0x00, 0x55}; void Encoder_Init_TIM4(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 10; TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //Reset counter TIM_SetCounter(TIM4,0); TIM_Cmd(TIM4, ENABLE); } void TIM4_IRQHandler(void) { if(TIM4->SR&0X0001) { } TIM4->SR&=~(1<<0); } int Read_Encoder() { int Encoder_TIM; Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=0; return Encoder_TIM; } void motor_sen(){ TIM_SetCompare1(TIM3,0);TIM_SetCompare2(TIM3,space); LEDR_ON;LEDL_OFF; } void motor_shou(){ TIM_SetCompare1(TIM3,space);TIM_SetCompare2(TIM3,0); LEDL_ON;LEDR_OFF; } void motor_stop(){ TIM_SetCompare1(TIM3,0);TIM_SetCompare2(TIM3,0); LEDR_OFF;LEDL_OFF; } void TIM2_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Period = arr; TIM_TimeBaseStructure.TIM_Prescaler =psc; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM2, ENABLE); } void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); rs485bufsend[1]=space; rs485bufsend[2]=EncoderA/256; rs485bufsend[3]=EncoderA%256; RS485_RX_CNT=1; RS485_Send_Data(rs485bufsend,5); RS485_RX_CNT=0; } } void reset_motor(){ motor_shou(); EncoderA=1; while(EncoderA){ delay_ms(100); EncoderA = Read_Encoder(); } motor_stop(); } int main(void) { SystemInit(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); delay_init(); RS485_Init(115200); OLED_Init(); OLED_Clear(); LED_Init(); TIM2_Int_Init(9999,7199); Motor_Init(99,71); Encoder_Init_TIM4(); LEDL_OFF; LEDR_OFF; OLED_ShowStr(0,0,"INIT:",16); reset_motor(); OLED_Clear(); OLED_ShowStr(0,0,"status:",16); OLED_ShowStr(0,2,"dev:",16); OLED_ShowStr(0,4,"SET:",16); OLED_ShowStr(0,6,"space:",16); while(1){ EncoderA = Read_Encoder()+EncoderA; OLED_ShowNum(50,0,value,2,16); OLED_ShowNum(50,2,EncoderA/10,4,16); OLED_ShowNum(50,6,space,4,16); OLED_ShowNum(50,4,EncoderA_S,4,16); RS485_TX_EN=0; if(RS485_RX_CNT !=0){ if(RS485_RX_BUF[0] == 0xAA){ switch(RS485_RX_BUF[1]){ case 0x01:motor_sen();value=0; break; case 0x02:motor_shou();value=0; break; case 0x03:motor_stop();value =0;break; case 0x04:space =RS485_RX_BUF[2]; break; case 0x05:EncoderA_S=RS485_RX_BUF[2]*100; EncoderA_S=EncoderA_S+RS485_RX_BUF[3];value =1;break; } } RS485_RX_CNT=0; } if(value==1){ if(EncoderA/10>EncoderA_S){ motor_shou(); }else if(EncoderA/10<EncoderA_S){ motor_sen(); }else{ motor_stop(); } delay_ms(1); } } }
已完成理解「#include "stm32f10x.h" #include "delay.h" #include "usart.h" #include "rs485.h" #include <stdio.h> #include <string.h> #include "OLED.h" #include "key.h" #include "motor.h" int value =0; int EncoderA ; int space = 90; int EncoderA_S=0; u8 rs485bufsend[5]={0xAA, 0x00, 0x00, 0x00, 0x55}; void Encoder_Init_TIM4(void) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; TIM_ICInitTypeDef TIM_ICInitStructure; GPIO_InitTypeDef GPIO_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOB, &GPIO_InitStructure); TIM_TimeBaseStructInit(&TIM_TimeBaseStructure); TIM_TimeBaseStructure.TIM_Prescaler = 0x0; TIM_TimeBaseStructure.TIM_Period = 65535; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); TIM_EncoderInterfaceConfig(TIM4, TIM_EncoderMode_TI12, TIM_ICPolarity_Rising, TIM_ICPolarity_Rising); TIM_ICStructInit(&TIM_ICInitStructure); TIM_ICInitStructure.TIM_ICFilter = 10; TIM_ICInit(TIM4, &TIM_ICInitStructure); TIM_ClearFlag(TIM4, TIM_FLAG_Update); TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //Reset counter TIM_SetCounter(TIM4,0); TIM_Cmd(TIM4, ENABLE); } void TIM4_IRQHandler(void) { if(TIM4->SR&0X0001) { } TIM4->SR&=~(1<<0); } int Read_Encoder() { int Encoder_TIM; Encoder_TIM= (short)TIM4 -> CNT; TIM4 -> CNT=0; return Encoder_TIM; } void motor_sen(){ TIM_SetCompare1(TIM3,0);TIM_SetCompare2(TIM3,space); LEDR_ON;LEDL_OFF; } void motor_shou(){ TIM_SetCompare1(TIM3,space);TIM_SetCompare2(TIM3,0); LEDL_ON;LEDR_OFF; } void motor_stop(){ TIM_SetCompare1(TIM3,0);TIM_SetCompare2(TIM3,0); LEDR_OFF;LEDL_OFF; } void TIM2_Int_Init(u16 arr,u16 psc) { TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure; NVIC_InitTypeDef NVIC_InitStructure; RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); TIM_TimeBaseStructure.TIM_Period = arr; TIM_TimeBaseStructure.TIM_Prescaler =psc; TIM_TimeBaseStructure.TIM_ClockDivision = TIM_CKD_DIV1; TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up; TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure); TIM_ITConfig(TIM2,TIM_IT_Update,ENABLE ); NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQn; NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1; NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3; NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; NVIC_Init(&NVIC_InitStructure); TIM_Cmd(TIM2, ENABLE); } void TIM2_IRQHandler(void) { if (TIM_GetITStatus(TIM2, TIM_IT_Update) != RESET) { TIM_ClearITPendingBit(TIM2, TIM_IT_Update ); rs485bufsend[1]=space; rs485bufsend[2]=EncoderA/256; rs485bufsend[3]=EncoderA%256; RS485_RX_CNT=1; RS485_Send_Data(rs485bufsend,5); RS485_RX_CNT=0; } } void reset_motor(){ motor_shou(); EncoderA=1; while(EncoderA){ delay_ms(100); EncoderA = Read_Encoder(); } motor_stop(); } int main(void) { SystemInit(); NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); delay_init(); RS485_Init(115200); OLED_Init(); OLED_Clear(); LED_Init(); TIM2_Int_Init(9999,7199); Motor_Init(99,71); Encoder_Init_TIM4(); LEDL_OFF; LEDR_OFF; OLED_ShowStr(0,0,"INIT:",16); reset_motor(); OLED_Clear(); OLED_ShowStr(0,0,"status:",16); OLED_ShowStr(0,2,"dev:",16); OLED_ShowStr(0,4,"SET:",16); OLED_ShowStr(0,6,"space:",16); while(1){ EncoderA = Read_Encoder()+EncoderA; OLED_ShowNum(50,0,value,2,16); OLED_ShowNum(50,2,EncoderA/10,4,16); OLED_ShowNum(50,6,space,4,16); OLED_ShowNum(50,4,EncoderA_S,4,16); RS485_TX_EN=0; if(RS485_RX_CNT !=0){ if(RS485_RX_BUF[0] == 0xAA){ switch(RS485_RX_BUF[1]){ case 0x01:motor_sen();value=0; break; case 0x02:motor_shou();value=0; break; case 0x03:motor_stop();value =0;break; case 0x04:space =RS485_RX_BUF[2]; break; case 0x05:EncoderA_S=RS485_RX_BUF[2]*100; EncoderA_S=EncoderA_S+RS485_RX_BUF[3];value =1;break; } } RS485_RX_CNT=0; } if(value==1){ if(EncoderA/10>EncoderA_S){ motor_shou(); }else if(EncoderA/10<EncoderA_S){ motor_sen(); }else{ motor_stop(); } delay_ms(1); } } }」
展开阅读网页
根据提供的代码和参考资料,这段代码是用于STM32F10x系列微控制器的一个示例程序,主要功能是初始化并使用定时器TIM4读取编码器的值,并通过串口发送编码器数据。以下是对代码的详细解释:
-
头文件包含:程序开始处包含了STM32F10x系列微控制器的标准库头文件,以及一些自定义的函数和配置文件,如延时函数
delay.h
、串口通信函数usart.h
、RS485通信协议rs485.h
、OLED显示OLED.h
、按键处理key.h
和电机控制motor.h
。 -
全局变量定义:定义了一些全局变量,如
value
用于存储一些状态值,EncoderA
用于存储编码器的当前值,space
可能用于存储某种距离或位置的参数,EncoderA_S
可能用于存储编码器的目标位置或设定值,rs485bufsend
用于存储要通过RS485发送的数据。 -
编码器初始化函数
Encoder_Init_TIM4
:此函数用于初始化TIM4定时器,配置为编码器模式,并使能中断。它配置了GPIOB的6和7引脚作为编码器输入,并设置了定时器的预分频器、周期、时钟分频和计数模式。此外,还配置了编码器接口和滤波器,并清除了更新标志,使能了更新中断,并启动了定时器。 -
定时器中断处理函数
TIM4_IRQHandler
:当TIM4的更新中断触发时,此函数被调用。当前代码中,此函数尚未实现具体的功能。 -
读取编码器值函数
Read_Encoder
:此函数读取TIM4定时器的当前计数值,并将其重置为0,然后返回读取到的编码器值。 -
电机控制函数
motor_sen
、motor_shou
和motor_stop
:这些函数用于控制电机的正转、反转和停止,通过设置TIM3定时器的比较值来实现。 -
串口中断初始化函数
TIM2_Int_Init
:此函数用于初始化TIM2定时器,并配置中断,用于周期性地发送数据。 -
串口中断处理函数
TIM2_IRQHandler
:当TIM2的更新中断触发时,此函数被调用。它将编码器的当前值和space
变量的值通过RS485发送出去。 -
电机复位函数
reset_motor
:此函数用于将电机复位到初始状态,通过反转电机并等待编码器值归零来实现。 -
主函数
main
:在主函数中,首先进行了系统初始化,配置了中断优先级,初始化了延时、RS485通信、OLED显示、LED灯和电机控制。然后调用reset_motor
函数复位电机,并通过无限循环来持续读取编码器值,并在OLED上显示。同时,监听RS485接收到的指令,并根据指令控制电机的动作。
整体而言,这段代码是一个STM32F10x系列微控制器用于读取编码器值并控制电机的示例程序。通过编码器读取电机的位置或旋转信息,并通过RS485通信发送这些信息,同时接收外部指令来控制电机的动作。12345678910