找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 2398|回复: 0
打印 上一主题 下一主题
收起左侧

STM32F103产生PWM波并计算发送个数源码

[复制链接]
跳转到指定楼层
楼主
ID:379721 发表于 2018-8-21 13:37 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
#include "stm32f10x.h"               
#include "user_Config.h"              
#include "stm32_eval.h"
#include <stdio.h>


/********************************************************************
********************************************************************/
void start_run(void);
void stop_run(void);




uint8_t  flag =0;
uint8_t  stop_flag =0;    //运转标志
uint8_t  auto_flag =0;
uint8_t  auto_flag1 =0;
uint32_t count =0;
uint32_t round =0;
uint32_t speed_cal = 750;




延时函数




void Delay(vu32 nCount)
{
        uint32_t a;
        uint32_t b;
  for(a=nCount;a>0;a--)
                for(b=8000;b>0;b--);
}







配置时钟函数

void RCC_Configuration(void)
{
  /* TIM3 clock enable */
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);

  /* GPIOA and GPIOB clock enable */
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC | RCC_APB2Periph_AFIO, ENABLE);
}


pwm波产生管脚设置

void GPIO_pwm_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;   //复用推挽输出
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOA, &GPIO_InitStructure);
}

正反转及调速

void GPIO_key_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOB, &GPIO_InitStructure);
}

步进电机驱动器使能及方向控制

void GPIO_type_Configuration(void)
{
        GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0 | GPIO_Pin_1 ;
  GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;

  GPIO_Init(GPIOC, &GPIO_InitStructure);
        GPIO_ResetBits(GPIOC,GPIO_Pin_0);
        GPIO_SetBits(GPIOC,GPIO_Pin_1);
}

定时器设置

void pwm(void)
{
  TIM_TimeBaseStructure.TIM_Period = speed_cal;
  TIM_TimeBaseStructure.TIM_Prescaler = 3-1;
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;
  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_OCInitStructure.TIM_OCMode = TIM_OCMode_PWM1;
  TIM_OCInitStructure.TIM_OutputState = TIM_OutputState_Enable;
  TIM_OCInitStructure.TIM_Pulse = speed_cal/2;
  TIM_OCInitStructure.TIM_OCPolarity = TIM_OCPolarity_High;
  TIM_OC1Init(TIM3, &TIM_OCInitStructure);
  TIM_OC1PreloadConfig(TIM3, TIM_OCPreload_Enable);
  TIM_ARRPreloadConfig(TIM3, ENABLE);

  //TIM_Cmd(TIM3, DISABLE);
}

中断优先级设置

void TIM3_NVIC_Configuration(void)   
{
        NVIC_InitTypeDef NVIC_InitStructure;                        
  NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);                                                                                                                                   
  NVIC_InitStructure.NVIC_IRQChannel = TIM3_IRQn;                     
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;        
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 3;                    
                  
  NVIC_Init(&NVIC_InitStructure);
}

中断函数(统计脉冲个数)

void TIM3_IRQHandler(void)  
{
        if(TIM_GetITStatus(TIM3,TIM_IT_Update!=RESET))
        {
                TIM_ClearITPendingBit(TIM3,TIM_FLAG_Update);
                count++;
                if(count == 768750)
                {
                        count = 0;
                        round++;
                }
                if(round == 5)
                {
                        round = 0;
                        if(flag == 1)
                        {
                                if(auto_flag == 0)
                                {
                                        auto_flag = 1;
                                        stop_run();
                                        Delay(2000);
                                        GPIO_SetBits(GPIOC,GPIO_Pin_0);
                                        start_run();
                                }
                                else
                                {
                                        auto_flag = 0;
                                        stop_run();
                                        Delay(2000);
                                        GPIO_ResetBits(GPIOC,GPIO_Pin_0);
                                        start_run();
                                }
                        }
                }
        }
}

开始发送pwm

void start_run(void)
{
        TIM_Cmd(TIM3, ENABLE);
}

关闭发送pwm

void stop_run(void)
{
        TIM_Cmd(TIM3, DISABLE);
}

正传

void start_scan1(void)
{
        if(stop_flag == 0)
        {
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12)==0)
                {
                        Delay(200);
                        if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12)==0)
                        {        
                                GPIO_ResetBits(GPIOC,GPIO_Pin_0);
                                start_run();
                                stop_flag = 1;
                                while(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_12));
                        }
                }
  }
}

反转

void start_scan2(void)
{
        if(stop_flag == 0)
        {
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==0)
                {
                        Delay(200);
                        if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13)==0)
                        {        
                                GPIO_SetBits(GPIOC,GPIO_Pin_0);
                                start_run();
                                stop_flag =1;
                                while(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_13));
                        }
                }
  }
}

停止

void stop_scan(void)
{
        if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
        {
                Delay(200);
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
                {                                                
                        stop_run();
                        GPIO_ResetBits(GPIOC,GPIO_Pin_0);
                        Delay(1500);
                        stop_flag = 0;
                        while(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15));
                }
        }
}


调速


void change_speed(void)
{
        if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==0)
        {
                Delay(200);
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14)==0)
                {
                        if(speed_cal == 750)
                        {
                                speed_cal = 375;
                        }
                        else
                        {
                                speed_cal = 750;
                        }
                        TIM3->ARR = speed_cal;
                        pwm();
                        //TIM_Cmd(TIM3, ENABLE);
                        while(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_14));
                }
        }
}

自动旋转


void auto_run(void)
{
        if(stop_flag == 0)
        {
                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
                {
                        Delay(200);
                        if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_11)==0)
                        {
                                if(auto_flag1 == 0)
                                        auto_flag1 =1;
                        }
                }
  }
}
/****************************************************************************************************************************************/
int main(void)
{
  RCC_Configuration();
  GPIO_pwm_Configuration();                                                                                                        
  GPIO_key_Configuration();
  GPIO_type_Configuration();
  TIM3_NVIC_Configuration();
  pwm();
  while (1)
  {
                auto_run();
                if(auto_flag1 ==0)
                {
                        start_scan1();
                        start_scan2();
                        stop_scan();
                        change_speed();
                }
                else
                {        
                        if(flag == 0)
                        {
                                flag = 1;
                                GPIO_ResetBits(GPIOC,GPIO_Pin_0);
                                start_run();
                        }
                        if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
                        {
                                Delay(200);
                                if(GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15)==0)
                                {                                                
                                        stop_run();
                                        GPIO_ResetBits(GPIOC,GPIO_Pin_0);
                                        auto_flag1 =0;
                                        while(!GPIO_ReadInputDataBit(GPIOB,GPIO_Pin_15));
                                }
                        }
                }
        }
}



评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

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