STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx

上传人:b****4 文档编号:4818461 上传时间:2023-05-07 格式:DOCX 页数:14 大小:17.26KB
下载 相关 举报
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第1页
第1页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第2页
第2页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第3页
第3页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第4页
第4页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第5页
第5页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第6页
第6页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第7页
第7页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第8页
第8页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第9页
第9页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第10页
第10页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第11页
第11页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第12页
第12页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第13页
第13页 / 共14页
STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx_第14页
第14页 / 共14页
亲,该文档总共14页,全部预览完了,如果喜欢就下载吧!
下载资源
资源描述

STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx

《STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx》由会员分享,可在线阅读,更多相关《STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx(14页珍藏版)》请在冰点文库上搜索。

STM32 步进电机换向程序中断优先级的嵌套和通用定时器的使用.docx

STM32步进电机换向程序中断优先级的嵌套和通用定时器的使用

/*******************************************************************************STM32步进电机换向程序按中断0,步进电机反向运转。

串口调试助手中出现“ItisEXIT0IRQHandlerenter.”采用三个定时器,TIM2,4输出脉冲。

TIM3控制相位差。

*******************************************************************************

#include"stm32f10x_lib.h"

#include"stdio.h"

#include"time.h"

#include"pwm.h"

voidRCC_Configuration(void);

voidGPIO_Configuration(void);

voidNVIC_Configuration(void);

voidEXIT_Configuration(void);

voidUSART_Configuration(void);

intmain(void)

{

RCC_Configuration();//函数略去

NVIC_Configuration();

GPIO_Configuration();

USART_Configuration();

EXIT_Configuration();

TIM3_NVIC_Configuration();

TIM3_Configuration();

TIM4_PWM_Init();

TIM2_PWM_Init();

TIM_Cmd(TIM4,ENABLE);

START_TIME;

while

(1)

{

}

}

/*******************************************************************************

*函数名:

GPIO_Configuration

*******************************************************************************/

voidGPIO_Configuration(void)

{

/*定义GPIO初始化结构体GPIO_InitStructure*/

GPIO_InitTypeDefGPIO_InitStructure;

/*设置PA.0,PA.1,PA.2为上拉输入(EXTILine0)*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0|GPIO_Pin_1;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

/*定义PA.0为外部中断0输入通道(EXIT0)*/

GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource0);

/*定义PA.1为外部中断0输入通道(EXIT1)*/

GPIO_EXTILineConfig(GPIO_PortSourceGPIOA,GPIO_PinSource1);

/*设置USART1的Tx脚(PA.9)为第二功能推挽输出功能*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_9;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure);

/*设置USART1的Rx脚(PA.10)为浮空输入脚*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IN_FLOATING;

GPIO_Init(GPIOA,&GPIO_InitStructure);

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_5;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;

GPIO_Init(GPIOA,&GPIO_InitStructure);

}

:

NVIC_Configuration

*****************************************************************/

voidNVIC_Configuration(void)

{

NVIC_InitTypeDefNVIC_InitStructure;

/*#ifdef...#else...#endif

#ifdefVECT_TAB_RAM

NVIC_SetVectorTable(NVIC_VectTab_RAM,0x0);

#else/*VECT_TAB_FLASH*/

NVIC_SetVectorTable(NVIC_VectTab_FLASH,0x0);

#endif

/*选择NVIC优先级分组2*/

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

/*使能EXIT0通道,0级先占优先级,0级次占优先级*/

NVIC_InitStructure.NVIC_IRQChannel=EXTI0_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=0;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=0;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

/*******************************************************************************

voidEXIT_Configuration(void)

{

EXTI_InitTypeDefEXTI_InitStructure;

外部中断0通道、1通道(EXITLine0)在下降沿时触发中断

EXTI_InitStructure.EXTI_Line=EXTI_Line0|EXTI_Line1;

EXTI_InitStructure.EXTI_Mode=EXTI_Mode_Interrupt;

EXTI_InitStructure.EXTI_Trigger=EXTI_Trigger_Falling;

EXTI_InitStructure.EXTI_LineCmd=ENABLE;

EXTI_Init(&EXTI_InitStructure);

}

*******************************************************************************/

voidUSART_Configuration(void)

{

USART_InitTypeDefUSART_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_HardwareFlowControl=USART_HardwareFlowControl_None;

USART_InitStructure.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;

USART_Init(USART1,&USART_InitStructure);

USART_Cmd(USART1,ENABLE);

}

intfputc(intch,FILE*f)

{

USART_SendData(USART1,(u8)ch);

while(USART_GetFlagStatus(USART1,USART_FLAG_TC)==RESET);

returnch;

}

#include"pwm.h"

staticvoidTIM4_GPIO_Config(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

/*TIM4clockenable*/

//PCLK1经过2倍频后作为TIM3的时钟源等于36MHz

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);

/*GPIOAandGPIOBclockenable*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);

/*GPIOAConfiguration:

TIM4channel1and2asalternatefunctionpush-pull*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6|GPIO_Pin_7;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOB,&GPIO_InitStructure);

}

/*

*描述:

配置TIM4输出的PWM信号的模式,如周期、极性、占空比

staticvoidTIM4_Mode_Config(void)

{

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

TIM_OCInitTypeDefTIM_OCInitStructure;

/*PWM信号50%占空比

u16CCR1_Val=4000;

u16CCR2_Val=4000;

/*Timebaseconfiguration*/

TIM_TimeBaseStructure.TIM_Period=8000;//当定时器从0计数到7999,即为8000次,为一个定时周期

TIM_TimeBaseStructure.TIM_Prescaler=72-1;//1us

TIM_TimeBaseStructure.TIM_ClockDivision=0;//设置时钟分频系数:

不分频

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;//向上计数模式

TIM_TimeBaseInit(TIM4,&TIM_TimeBaseStructure);

/*PWM1Modeconfiguration:

Channel1*/

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//配置为PWM模式1

TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse=CCR1_Val;//设置跳变值,当计数器计数到这个值时,电平发生跳变

TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//当定时器计数值小于CCR1_Val时为高电平

TIM_OC1Init(TIM4,&TIM_OCInitStructure);//使能通道1

TIM_OC1PreloadConfig(TIM4,TIM_OCPreload_Enable);

/*PWM1Modeconfiguration:

Channel2*/

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;

TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse=CCR2_Val;//设置通道2的电平跳变值,输出另外一个占空比的PWM

TIM_OC2Init(TIM4,&TIM_OCInitStructure);//使能通道2

TIM_OC2PreloadConfig(TIM4,TIM_OCPreload_Enable);

/*TIM4enablecounter*/

//TIM_Cmd(TIM4,ENABLE);//使能定时器4

}

staticvoidTIM2_GPIO_Config(void)

{

GPIO_InitTypeDefGPIO_InitStructure;

/*TIM3clockenable*/

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2,ENABLE);

/*GPIOAandGPIOBclockenable*/

RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA|RCC_APB2Periph_GPIOB,ENABLE);

/*GPIOAConfiguration:

TIM2channel1and2asalternatefunctionpush-pull*/

GPIO_InitStructure.GPIO_Pin=GPIO_Pin_2|GPIO_Pin_3;

GPIO_InitStructure.GPIO_Mode=GPIO_Mode_AF_PP;

GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;

GPIO_Init(GPIOA,&GPIO_InitStructure);

}

/*

*描述:

配置TIM3输出的PWM信号的模式,如周期、极性、占空比

staticvoidTIM2_Mode_Config(void)

{

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

TIM_OCInitTypeDefTIM_OCInitStructure;

/*PWM信号电平跳变值*/

u16CCR3_Val=4000;

u16CCR4_Val=4000;

/*Timebaseconfiguration*/

TIM_TimeBaseStructure.TIM_Period=8000;

TIM_TimeBaseStructure.TIM_Prescaler=72-1;

TIM_TimeBaseStructure.TIM_ClockDivision=0;//设置时钟分频系数:

不分频

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM2,&TIM_TimeBaseStructure);

/*PWM1Modeconfiguration:

Channel3*/

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM1;//配置为PWM模式1

TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse=CCR3_Val;//,当计数器计数到这个值时,电平发生跳变

TIM_OCInitStructure.TIM_OCPolarity=TIM_OCPolarity_High;//当定时器计数值小于CCR1_Val时为高电平

TIM_OC3Init(TIM2,&TIM_OCInitStructure);

TIM_OC3PreloadConfig(TIM2,TIM_OCPreload_Enable);

/*PWM1Modeconfiguration:

Channe4*/

TIM_OCInitStructure.TIM_OCMode=TIM_OCMode_PWM2;

TIM_OCInitStructure.TIM_OutputState=TIM_OutputState_Enable;

TIM_OCInitStructure.TIM_Pulse=CCR4_Val;一个占空比的PWM

TIM_OC4Init(TIM2,&TIM_OCInitStructure);/

TIM_OC4PreloadConfig(TIM2,TIM_OCPreload_Enable);

/*TIM2enablecounter*/

//TIM_Cmd(TIM2,ENABLE);

}

voidTIM2_PWM_Init(void)

{

TIM2_GPIO_Config();

TIM2_Mode_Config();

}

voidTIM4_PWM_Init(void)

{

TIM4_GPIO_Config();

TIM4_Mode_Config();

}

#include"time.h"

externu32time;

*描述:

TIM3中断配置

voidTIM3_NVIC_Configuration(void)

{

NVIC_InitTypeDefNVIC_InitStructure;

NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);

NVIC_InitStructure.NVIC_IRQChannel=TIM3_IRQChannel;

NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority=2;

NVIC_InitStructure.NVIC_IRQChannelSubPriority=1;

NVIC_InitStructure.NVIC_IRQChannelCmd=ENABLE;

NVIC_Init(&NVIC_InitStructure);

}

voidTIM3_Configuration(void)

{

TIM_TimeBaseInitTypeDefTIM_TimeBaseStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);

TIM_DeInit(TIM3);

TIM_TimeBaseStructure.TIM_Period=2000;

TIM_TimeBaseStructure.TIM_Prescaler=(72-1);72M/72*/

TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;

TIM_TimeBaseStructure.TIM_CounterMode=TIM_CounterMode_Up;

TIM_TimeBaseInit(TIM3,&TIM_TimeBaseStructure);

TIM_ClearFlag(TIM3,TIM_FLAG_Update);

TIM_ITConfig(TIM3,TIM_IT_Update,ENABLE);

TIM_Cmd(TIM3,ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,DISABLE);}

voiddelay_nms(u16time)

{

u16i=0;

while(time--)

{

i=12000;//自己定义

while(i--);

}

}

#ifndef__PWM_H

#define__PWM_H

#include"stm32f10x_lib.h"

voidTIM4_PWM_Init(void);

voidTIM2_PWM_Init(void);

#endif

TIM3定时函数

#ifndefTIME_H

#defineTIME_H

#include"stm32f10x_lib.h"

voiddelay_nms(u16time);

#defineSTART_TIMERCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,ENABLE);TIM_Cmd(TIM3,ENABLE)

#defineSTOP_TIMETIM_Cmd(TIM3,DISABLE);RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3,DISABLE)

voidTIM3_NVIC_Configuration(void);

voidTIM3_Configuration(void);

#endif

以下是中断函数

voidEXTI0_IRQHandler(void)

{

printf("\r\nItisEXIT0IRQHandlerenter.\r\n");

//GPIO_WriteBit

展开阅读全文
相关资源
猜你喜欢
相关搜索
资源标签

当前位置:首页 > PPT模板 > 商务科技

copyright@ 2008-2023 冰点文库 网站版权所有

经营许可证编号:鄂ICP备19020893号-2