OpenEdv-开源电子网

 找回密码
 立即注册
正点原子全套STM32/Linux/FPGA开发资料,上千讲STM32视频教程免费下载...
查看: 3269|回复: 3

本应产生四路四组相同的动作组的,然而。。。用四个定时器的程序波形正常,但用一个定时器的算法却不行。。。

[复制链接]

4

主题

8

帖子

0

精华

初级会员

Rank: 2

积分
72
金钱
72
注册时间
2015-8-4
在线时间
10 小时
发表于 2015-9-28 15:56:44 | 显示全部楼层 |阅读模式
5金钱
timer.c

#include "timer.h"
#include "delay.h"
//#include "led.h"
//////////////////////////////////////////////////////////////////////////////////  
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//Mini STM32开发板
//通用定时器 驱动代码   
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//修改日期:2010/12/03
//版本:V1.0
//版权所有,盗版必究。
//Copyright(C) 正点原子 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////  
unsigned char order=0;        //中断步长 计数 
#define PWM_OUR_TIME 20000/4      //周期  20000US=20MS 几路PWM :4
TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;   
unsigned int PWM_value1[4]={1500,1500,1500,1500};  //舵机PWM 输出值 改变它就改变输出宽度 (动作组1)
unsigned int PWM_value2[4]={1500,1500,1500,1500};  //舵机PWM 输出值 改变它就改变输出宽度 (动作组2)
unsigned int PWM_value3[4]={1500,1500,1500,1500};  //舵机PWM 输出值 改变它就改变输出宽度  (动作组3)
unsigned int PWM_value4[4]={1500,1500,1500,1500};  //舵机PWM 输出值 改变它就改变输出宽度 (动作组4)


//通用定时器中断初始化
//这里时钟选择为APB1的2倍,而APB1为36M


//arr:自动重装值。
//psc:时钟预分频数
//这里使用的是定时器4!
void TIM4_Int_Init(u16 arr,u16 psc)
{

NVIC_InitTypeDef NVIC_InitStructure;

RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4, ENABLE); //时钟使能

TIM_TimeBaseStructure.TIM_Period = arr; //设置在下一个更新事件装入活动的自动重装载寄存器周期的值 计数到5000为500ms
TIM_TimeBaseStructure.TIM_Prescaler =psc; //设置用来作为TIMx时钟频率除数的预分频值  10Khz的计数频率  
//TIM_TimeBaseStructure.TIM_ClockDivision = 0; //设置时钟分割:TDTS = Tck_tim
TIM_TimeBaseStructure.TIM_ClockDivision=TIM_CKD_DIV1;
TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //TIM向上计数模式
TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure); //根据TIM_TimeBaseInitStruct中指定的参数初始化TIMx的时间基数单位
 
TIM_ITConfig( TIM4,TIM_IT_Update , ENABLE );//使能或者失能指定的TIM中断//TIM2//使能
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
NVIC_InitStructure.NVIC_IRQChannel = TIM4_IRQn;  //TIM3中断
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;  //先占优先级0级
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;  //从优先级3级
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE; //IRQ通道被使能
NVIC_Init(&NVIC_InitStructure);  //根据NVIC_InitStruct中指定的参数初始化外设NVIC寄存器

TIM_Cmd(TIM4, ENABLE);  //使能TIMx外设
 
}
//舵机IO配置
void SERVO_GPIO_Config(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;  
  

        RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE);       

          GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9;        
          GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;    
          GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;  
          GPIO_Init(GPIOB, &GPIO_InitStructure);                 

        GPIO_ResetBits(GPIOB, GPIO_Pin_6 | GPIO_Pin_7 | GPIO_Pin_8| GPIO_Pin_9);   
}

void TIM4_IRQHandler(void)   //TIM3中断
{
if (TIM_GetITStatus(TIM4, TIM_IT_Update) != RESET) //检查指定的TIM中断发生与否:TIM 中断源 
{
               switch(order)
                {
                        case 1:
                                PWM0_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value1[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 2:
                                PWM0_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value1[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 3:
                                PWM1_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value1[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 4:
                                PWM1_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value1[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 5:
                                PWM2_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value1[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 6:
                                PWM2_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value1[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 7:
                                PWM3_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value1[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
case 8:
                                PWM3_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value1[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
delay_ms(20);
case 9:
                                PWM0_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value2[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 10:
                                PWM0_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value2[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 11:
                                PWM1_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value2[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 12:
                                PWM1_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value2[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);       
                        break;
                        case 13:
                                PWM2_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value2[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 14:
                                PWM2_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value2[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 15:
                                PWM3_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value2[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
case 16:
                                PWM3_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value2[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
delay_ms(20);
case 17:
                                PWM0_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value3[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 18:
                                PWM0_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value3[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 19:
                                PWM1_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value3[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 20:
                                PWM1_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value3[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 21:
                                PWM2_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value3[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 22:
                                PWM2_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value3[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 23:
                                PWM3_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value3[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
case 24:
                                PWM3_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value3[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
delay_ms(20);
case 25:
                                PWM0_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value4[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 26:
                                PWM0_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value4[0];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 27:
                                PWM1_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value4[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 28:
                                PWM1_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value4[1];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);       
                        break;
                        case 29:
                                PWM2_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value4[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
                        break;
                        case 30:
                                PWM2_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value4[2];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);        
                        break;
                        case 31:
                                PWM3_output_high;
                          TIM_TimeBaseStructure.TIM_Period = PWM_value4[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
case 32:
                                PWM3_output_low;
                          TIM_TimeBaseStructure.TIM_Period = PWM_OUR_TIME-PWM_value4[3];//重新中断时间赋值
                          TIM_TimeBaseInit(TIM4, & TIM_TimeBaseStructure);
delay_ms(20);
                        order=0;  //最后一路输出   清除步长
                        break;

                        default: order=0;               
                }

                order++;
TIM_ClearFlag(TIM2, TIM_FLAG_Update);
TIM_ClearITPendingBit(TIM4, TIM_IT_Update  );  //清除TIMx的中断待处理位:TIM 中断源 

}
}
timer.h
//////////////////////////////////////////////////////////////////////////////////  
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK Mini STM32开发板
//通用定时器 驱动代码   
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//修改日期:2010/12/03
//版本:V1.0
//版权所有,盗版必究。
//Copyright(C) 正点原子 2009-2019
//All rights reserved
//////////////////////////////////////////////////////////////////////////////////  
#ifndef __TIMER_H
#define __TIMER_H
#include "sys.h"
#include"stm32f10x.h"

    
//除以几路 得到每通道分的值 在用这个值减去 已知的高电平时间(PWM_value) 得到的就是 每路PWM 输出的低电平
//这个是关键思路。
#define PWM0_output_high  GPIO_SetBits(GPIOB,GPIO_Pin_6)         //??? ????
#define PWM0_output_low          GPIO_ResetBits(GPIOB,GPIO_Pin_6)

#define PWM1_output_high  GPIO_SetBits(GPIOB,GPIO_Pin_7)
#define PWM1_output_low          GPIO_ResetBits(GPIOB,GPIO_Pin_7)

#define PWM2_output_high  GPIO_SetBits(GPIOB,GPIO_Pin_8)
#define PWM2_output_low          GPIO_ResetBits(GPIOB,GPIO_Pin_8)

#define PWM3_output_high  GPIO_SetBits(GPIOB,GPIO_Pin_9)
#define PWM3_output_low          GPIO_ResetBits(GPIOB,GPIO_Pin_9)

//#define PWM4_output_high  GPIO_SetBits(GPIOB,GPIO_Pin_4)
//#define PWM4_output_low          GPIO_ResetBits(GPIOC,GPIO_Pin_4)

//#define PWM5_output_high  GPIO_SetBits(GPIOB,GPIO_Pin_5)
//#define PWM5_output_low          GPIO_ResetBits(GPIOC,GPIO_Pin_5)



void SERVO_GPIO_Config(void);
void TIM2_NVIC_Configuration(void);
void TIM2_Configuration(void);
void TIM4_Int_Init(u16 arr,u16 psc); 
#endif
#include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
#include"stm32f10x.h"
//ALIENTEK Mini STM32开发板范例代码7
//定时器中断实验   
//技术支持:www.openedv.com
//广州市星翼电子科技有限公司

main.c
 int main(void)
 {
delay_init();     //延时函数初始化
NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
//LED_Init();   //初始化与LED连接的硬件接口
  SERVO_GPIO_Config();
TIM4_Int_Init(1000,71);//计数到1000为1ms 
  while(1);
 }

 #include "led.h"
#include "delay.h"
#include "sys.h"
#include "timer.h"
#include"stm32f10x.h"
//ALIENTEK Mini STM32开发板范例代码7
//定时器中断实验   
//技术支持:www.openedv.com
//广州市星翼电子科技有限公司


 int main(void)
 {
delay_init();     //延时函数初始化
NVIC_Configuration();//设置NVIC中断分组2:2位抢占优先级,2位响应优先级
//LED_Init();   //初始化与LED连接的硬件接口
  SERVO_GPIO_Config();
TIM4_Int_Init(1000,71);//计数到1000为1ms 
  while(1);
 }















正点原子逻辑分析仪DL16劲爆上市
回复

使用道具 举报

4

主题

8

帖子

0

精华

初级会员

Rank: 2

积分
72
金钱
72
注册时间
2015-8-4
在线时间
10 小时
 楼主| 发表于 2015-9-28 15:58:19 | 显示全部楼层
发的有点乱,各位大神见谅
回复

使用道具 举报

530

主题

11万

帖子

34

精华

管理员

Rank: 12Rank: 12Rank: 12

积分
165540
金钱
165540
注册时间
2010-12-1
在线时间
2117 小时
发表于 2015-9-28 21:07:49 | 显示全部楼层
帮顶.....
回复

使用道具 举报

4

主题

8

帖子

0

精华

初级会员

Rank: 2

积分
72
金钱
72
注册时间
2015-8-4
在线时间
10 小时
 楼主| 发表于 2015-9-29 20:45:53 | 显示全部楼层
初学STM32,求助
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则



关闭

原子哥极力推荐上一条 /2 下一条

正点原子公众号

QQ|手机版|OpenEdv-开源电子网 ( 粤ICP备12000418号-1 )

GMT+8, 2025-7-19 06:34

Powered by OpenEdv-开源电子网

© 2001-2030 OpenEdv-开源电子网

快速回复 返回顶部 返回列表