AT8236控制代码详解及示例
关于AT8236不得不看的一些配置说明。
motor.c配置
#include "motor.h"
#include "adc.h"
#include "stm32f0xx_tim.h"
#include "stm32f0xx_exti.h"
#include "stm32f0xx_syscfg.h"
//#include "stdlib.h"
u16 motor_curret;
//u8 couter1,couter2;
u8 flag;
//uint16_t TimerPeriod = 255;
//uint16_t Channel1Pulse = 0, Channel2Pulse = 0, Channel3Pulse = 0, Channel4Pulse = 0,Channel5Pulse = 0;
//motor的初始化
u32 couter1=0;
void motor_Inint_PWM1(uint16_t ChannelPulse)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin =PWM1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_2);
TIM_DeInit(TIM2);
/* TIM1 counter enable */
TIM_Cmd(TIM2, ENABLE);
//
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
/* Channel 1, 2, 3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = (uint16_t ) ChannelPulse;
TIM_OC3Init(TIM2, &TIM_OCInitStructure);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
void motor_Inint_PWM2(uint16_t ChannelPulse)
{
GPIO_InitTypeDef GPIO_InitStructure;
TIM_OCInitTypeDef TIM_OCInitStructure;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);
GPIO_InitStructure.GPIO_Pin =PWM2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource1,GPIO_AF_2);
TIM_DeInit(TIM2);
/* TIM1 counter enable */
TIM_Cmd(TIM2, ENABLE);
//
/* Time Base configuration */
TIM_TimeBaseStructure.TIM_Prescaler = 0;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStructure.TIM_Period = TimerPeriod;
TIM_TimeBaseStructure.TIM_ClockDivision = 0;
// TIM_TimeBaseStructure.TIM_RepetitionCounter = 0;
/* Channel 1, 2, 3 and 4 Configuration in PWM mode */
TIM_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
TIM_OCInitStructure.TIM_OutputNState = TIM_OutputState_Disable;
TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_Low;
// TIM_OCInitStructure.TIM_OCNPolarity = TIM_OCNPolarity_High;
TIM_OCInitStructure.TIM_OCIdleState = TIM_OCIdleState_Set;
// TIM_OCInitStructure.TIM_OCNIdleState = TIM_OCIdleState_Reset;
TIM_OCInitStructure.TIM_Pulse = (uint16_t ) ChannelPulse;
TIM_OC2Init(TIM2, &TIM_OCInitStructure);
/* TIM1 Main Output Enable */
TIM_CtrlPWMOutputs(TIM2, ENABLE);
}
void motor_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
///电机电源开关
GPIO_DeInit(GPIOA);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_7);电机电源开关
//故障检测脚,故障时为低电平欠压//欠压锁定 (UVLO)、 过流保护 (OCP) 和热关断 (TSD)
GPIO_InitStructure.GPIO_Pin = Motor_nFAULT_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//内部上拉
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_SetBits(GPIOA,GPIO_Pin_5);电机故障检测
///霍尔检测
// GPIO_InitStructure.GPIO_Pin = HALL_S_PIN;
// GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
// GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
// GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;//内部上拉
// GPIO_Init(GPIOA, &GPIO_InitStructure);
// GPIO_SetBits(GPIOA,HALL_S_PIN);hall检测
//
}
void EXTI4_Config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable GPIOA clock */
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
/* Enable SYSCFG clock */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_SYSCFG, ENABLE);
/* Configure PA0 pin as input floating */
GPIO_InitStructure.GPIO_Pin = HALL_S_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(EXTI_HALL, &GPIO_InitStructure);
// GPIO_PinAFConfig(GPIOA,GPIO_PinSource4,GPIO_AF_4);
//
/* Connect EXTI0 Line to PA0 pin */
SYSCFG_EXTILineConfig(EXTI_PortSourceGPIOA, EXTI_PinSource4);
/* Configure EXTI0 line */
EXTI_InitStructure.EXTI_Line = EXTI_Line4;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising_Falling;
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
/* Enable and set EXTI0 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel =EXTI4_15_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPriority = 0x02;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
//*******正转****/
void Forward_motor(uint16_t ChannelPulse)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin =PWM2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
PWM2_LOW;
motor_Inint_PWM1(ChannelPulse);
}
//*******反转****/
void Rev_motor(uint16_t ChannelPulse)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
GPIO_InitStructure.GPIO_Pin =PWM1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_OUT;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL;
GPIO_Init(GPIOA, &GPIO_InitStructure);
PWM1_LOW;
motor_Inint_PWM2(ChannelPulse);
}
//*******刹车****/
void Break_motor(void)
{
PWM2_HIGHT;
PWM1_HIGHT;
}
//故障检测
void Motor_nFAULT(void)
{
//if()
Motor_power_OFF;
}
void EXTI4_15_IRQHandler(void)
{
// flag=0;
if (EXTI_GetITStatus(EXTI_Line4) != RESET) {
// flag++;
EXTI_ClearITPendingBit(EXTI_Line4);// Clear the EXTI line pending bit
couter1++;
}
}
motor.h配置
#ifndef __motor_H
#define __motor_H
#include "stm32f0xx.h"
//-----------------电机端口定义----------------
#define PWM1_LOW GPIO_ResetBits(GPIOA,GPIO_Pin_2)
#define PWM1_HIGHT GPIO_SetBits(GPIOA,GPIO_Pin_2)
#define PWM2_LOW GPIO_ResetBits(GPIOA,GPIO_Pin_1)
#define PWM2_HIGHT GPIO_SetBits(GPIOA,GPIO_Pin_1)
#define Motor_power_ON GPIO_SetBits(GPIOA,GPIO_Pin_10)
#define Motor_power_OFF GPIO_ResetBits(GPIOA,GPIO_Pin_10)
#define Motor_nFAULT_PIN GPIO_Pin_5
#define PWM1 GPIO_Pin_2
#define PWM2 GPIO_Pin_1
#define HALL_S_PIN GPIO_Pin_4
#define EXTI_HALL GPIOA
#define Hall_Read GPIO_ReadInputDataBit(GPIOA,HALL_S_PIN)
//#define TimerPeriod 255;
#define TimerPeriod ((SystemCoreClock / 100 ) - 1)
#define ChannelONEPulse (uint32_t) 5 * (TimerPeriod - 1)
/* Compute CCR1 value to generate a duty cycle at 50% for channel 1 */
#define Channel1Pulse (uint16_t) (((uint32_t) 5 * (TimerPeriod - 1)) / 10)
/* Compute CCR2 value to generate a duty cycle at 37.5% for channel 2 */
#define Channel2Pulse (uint16_t) (((uint32_t) 375 * (TimerPeriod - 1)) / 1000)
/* Compute CCR3 value to generate a duty cycle at 25% for channel 3 */
#define Channel3Pulse (uint16_t) (((uint32_t) 25 * (TimerPeriod - 1)) / 100)
/* Compute CCR4 value to generate a duty cycle at 12.5% for channel 4 */
#define Channel4Pulse (uint16_t) (((uint32_t) 125 * (TimerPeriod- 1)) / 1000)
#define Channel5Pulse (uint16_t) (((uint32_t) 75 * (TimerPeriod- 1)) / 1000)
void motor_Inint_PWM1(uint16_t ChannelPulse);
void motor_Inint_PWM2(uint16_t ChannelPulse);
void Rev_motor(uint16_t ChannelPulse);
void Forward_motor(uint16_t ChannelPulse);
//void Rev_motor(void);
void Break_motor(void);
//void Forward_motor(void);
void motor_Init(void);
void MotoStop(void);
void EXTI4_Config(void);
void Hall_Couter(void);
//void motor_Inint_PWM1(void);
//void motor_Inint_PWM2(void);
#endif
main.c 主测试函数
//
//2020-10-10生成
//2020-10-20 CAN HALL CAN 波形有尖峰输出波形不美丽
/*驱动测试完毕 **/
//******************************************************************************/
#include "stm32f0xx.h"
#include "stm32f0xx_dma.h"
#include "stm32f0xx_exti.h"
#include "can.h"
#include "stm32f0xx_can.h"
#include "delay.h"
#include "motor.h"
#include "GPIO_con.h"
#include "adc.h"
#include "timer.h"
__IO uint32_t ADC_12V = 0, ADC_ACCV = 0;
__IO uint16_t ADC_12_Votg[4];
//#include "oled.h"
//#include "bmp.h"
u16 *ADC_12V_Votg;
u16 *ADC_Votg;
u16 ADCValue[] = {0};
extern u32 couter1;
u32 revcouter=0;
u32 Forwardcouter=0;
u8 number;
extern u8 BAT_Vlt_flag;
extern u16 ADCConvertedValue_AVG[4];
//extern u8 flag;
//uint32_t ADC_12_Votg;
//***开始标志***///
u8 begin_flag=0;
int main(void)
{
int i=0;
SystemInit();
gpio_con_Init();
delay_init();
motor_Init();
CAN_init();
EXTI4_Config();
EXTI_GenerateSWInterrupt(EXTI_Line4);
// Rev_motor(ChannelONEPulse);
ADC1_CH_DMA_Config();
ADC_DATA_AVG();
can_tx(0X55,0X77);
Break_motor();
// }
/**工作原理先自检,电压,电流,判断hall位置开始正转,直到转到底,卡住**测试:命令控制*/
//时间上需要460S
while (1)
{
// Rev_motor(ChannelONEPulse);
//test
Forward_motor(ChannelONEPulse);
// Forward_motor(ChannelONEPulse);
delay_ms(460);
Break_motor();
delay_ms(460);
Rev_motor(ChannelONEPulse);
delay_ms(460);
Break_motor();
delay_ms(460);
//test
}
}
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
说明:霍尔传感器部分的设置可忽略,这个是判断电机转动圈数的参数。均测试已通过。电机可以正常转动
电机转动