赞
踩
芯片:stm32f103rct6
需要输出的信号有16个:
X(A~E):PB15,PC6,PA11,PB4,PD2
Y(A~E):PA12,PA15,PC10,PB9,PB7
U(A~C):PC11,PC12,PB3
V(A~C):PB5,PB6,PB8
需要注意PB3,PB4默认为非普通IO口,需要进行引脚重定义
需要一秒输出3000次信号,在配置定时器代码时需要设置ARR=100,PRC=240,见“Timer.c"
PB3、PB4,还有PA13-15这五个引脚不是普通的IO口,这些引脚只有重定义功能后才能作为普通的IO口使用,所以我们需要对这些引脚进行配置的时候开启AFIO复用时钟。下面以PA15为例:
//开时钟
RCC_APB2PeriphClockCmd( RCC_APB2Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//选择以下三种的一个进行引脚重映射
//只用PB4做普通IO口
GPIO_PinRemapConfig(GPIO_Remap_SWJ_NoJTRST, ENABLE);
//PB3,PB4,PA15做普通IO,PA13&14用于SWD调试
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
//五个引脚当做普通IO口
//!!!注意使用后就无法再用stlink进行调试,重启也无效,需要进入bootloard才能恢复,此处不做说明!!!
GPIO_PinRemapConfig(GPIO_Remap_SWJ_Disable, ENABLE);
#ifndef __IOTEST_H
#define __IOTEST_H
/*
DB25
PA 11 12 15
PB 3 4 5 6 7 8 9 15
PC 6 10 11 12
PD 2
*/
#define DB25_XA(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_15,BitVal)
#define DB25_XB(BitVal) GPIO_WriteBit(GPIOC,GPIO_Pin_6,BitVal) //TIM3_CH1
#define DB25_XC(BitVal) GPIO_WriteBit(GPIOA,GPIO_Pin_11,BitVal)
#define DB25_XD(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_4,BitVal)
#define DB25_XE(BitVal) GPIO_WriteBit(GPIOD,GPIO_Pin_2,BitVal)
#define DB25_YA(BitVal) GPIO_WriteBit(GPIOA,GPIO_Pin_12,BitVal)
#define DB25_YB(BitVal) GPIO_WriteBit(GPIOA,GPIO_Pin_15,BitVal)
#define DB25_YC(BitVal) GPIO_WriteBit(GPIOC,GPIO_Pin_10,BitVal)
#define DB25_YD(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_9,BitVal)
#define DB25_YE(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_7,BitVal)
#define DB25_UA(BitVal) GPIO_WriteBit(GPIOC,GPIO_Pin_11,BitVal)
#define DB25_UB(BitVal) GPIO_WriteBit(GPIOC,GPIO_Pin_12,BitVal)
#define DB25_UC(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_3,BitVal)
#define DB25_VA(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_5,BitVal)
#define DB25_VB(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_6,BitVal)
#define DB25_VC(BitVal) GPIO_WriteBit(GPIOB,GPIO_Pin_8,BitVal)
void IOtest_DB25init(void);
void X_set(u8 val);
void Y_set(u8 val);
void U_set(u8 val);
void V_set(u8 val);
#endif
#include "stm32f10x.h" // Device header
#include "IOtest.h"
void IOtest_DB25init(void)
{
/*
DB25
PA 11 12 15
PB 3 4 5 6 7 8 9 15
PC 6 10 11 12
PD 2
*/
GPIO_InitTypeDef GPIO_InitStructure;
//开时钟
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
//引脚重映射
//PB3,PB4,PA15做普通IO,PA13&14用于SWD调试
GPIO_PinRemapConfig(GPIO_Remap_SWJ_JTAGDisable, ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11|GPIO_Pin_12|GPIO_Pin_15;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3|GPIO_Pin_4|GPIO_Pin_5|GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_8|GPIO_Pin_9|GPIO_Pin_15;
GPIO_Init(GPIOB, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_10|GPIO_Pin_11|GPIO_Pin_12;
GPIO_Init(GPIOC, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
void X_set(u8 val)
{
DB25_XA((BitAction)(val%2));
val/=2;
DB25_XB((BitAction)(val%2));
val/=2;
DB25_XC((BitAction)(val%2));
val/=2;
DB25_XD((BitAction)(val%2));
val/=2;
DB25_XE((BitAction)(val%2));
}
void Y_set(u8 val)
{
DB25_YA((BitAction)(val%2));
val/=2;
DB25_YB((BitAction)(val%2));
val/=2;
DB25_YC((BitAction)(val%2));
val/=2;
DB25_YD((BitAction)(val%2));
val/=2;
DB25_YE((BitAction)(val%2));
}
void U_set(u8 val)
{
DB25_UA((BitAction)(val%2));
val/=2;
DB25_UB((BitAction)(val%2));
val/=2;
DB25_UC((BitAction)(val%2));
}
void V_set(u8 val)
{
DB25_VA((BitAction)(val%2));
val/=2;
DB25_VB((BitAction)(val%2));
val/=2;
DB25_VC((BitAction)(val%2));
}
/* Timer.h */
#ifndef __TIMER_H
#define __TIMER_H
void Timer_Init(void);
u8 Timer_getnum(void);
#endif
#include "stm32f10x.h" // Device header
u8 num=0;
extern u8 Motor_signalnum;
u8 Timer_getnum(void)
{
return num;
}
void Timer_Init(void)
{
RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);
TIM_InternalClockConfig(TIM2);
TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up;
TIM_TimeBaseInitStruct.TIM_Period=100-1; //10000-1
TIM_TimeBaseInitStruct.TIM_Prescaler=240-1; //7200-1
TIM_TimeBaseInitStruct.TIM_RepetitionCounter=0;
TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);
TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitTypeDef NVIC_InitStruct;
NVIC_InitStruct.NVIC_IRQChannel=TIM2_IRQn;
NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=1;
NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
NVIC_Init(&NVIC_InitStruct);
TIM_Cmd(TIM2, ENABLE);
}
/*
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update)==SET){
num++;
if(num>70)num=0;
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
*/
#ifndef __MOTOR_H
#define __MOTOR_H
void Five_phase_init(u8 motor_status);
void Five_phase_move();
#endif
#include "stm32f10x.h"// Device header
#include "Motor.h"
#include "Timer.h"
#include "IOtest.h"
static u8 Motor_signalnum=0;
static u8 Motor_Fivephase_signals[]={0x10,0x18,0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x11};
static u8 Motor_Threephase_signals[]={4,6,2,3,1,5};
static u8 Motor_status=0;
void Five_phase_init(u8 motor_status)
{
IOtest_DB25init();
Timer_Init();
Motor_signalnum=0;
Motor_status=motor_status;
}
void Five_phase_move()
{
switch(Motor_status){
case 0:
X_set(Motor_Fivephase_signals[Motor_signalnum]);
break;
case 1:
Y_set(Motor_Fivephase_signals[Motor_signalnum]);
break;
case 2:
U_set(Motor_Threephase_signals[Motor_signalnum]);
break;
case 3:
V_set(Motor_Threephase_signals[Motor_signalnum]);
break;
default:
break;
}
Motor_signalnum=(Motor_signalnum+1)%10;
}
void TIM2_IRQHandler(void)
{
if(TIM_GetITStatus(TIM2, TIM_IT_Update)==SET){
Five_phase_move();
TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
}
}
#include "stm32f10x.h" // Device header
#include "IOtest.h"
#include "Motor.h"
#include "Timer.h"
static u8 main_flag=1;
int main(void)
{
Five_phase_init(0);
while(1){
}
}
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。