• 大小: 34KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-25
  • 语言: C/C++
  • 标签: PWM  

资源简介

用C语言编写的定时器输出PWM程序,适和于MSP430单片机

资源截图

代码片段和文件信息

#include
#define uchar unsigned char
#define uint unsigned int

extern uint counter_1;
extern uchar Change;
extern uint TB_flag_1;
extern uint TB_flag_2;
extern uint stop;
extern uint run_add;
extern uint run_minus;

#include “KEY.C“

#define PWM_1 BIT1
#define PWM_2 BIT2
#define PWM_3 BIT3
#define PWM_4 BIT4
#define PWM_5 BIT5
#define PWM_6 BIT6



void int_clk()
{
    uchar i;
    BCSCTL1&=~XT2OFF;  //打开XT振荡器
    BCSCTL2|=SELM1+SELS;//MCLK 8M and SMCLK 1M  
    do
{
        IFG1 &= ~OFIFG;                 //清除振荡错误标志 
        for(i = 0; i < 100; i++)
  _NOP(); //延时等待
        }
    while ((IFG1 & OFIFG) != 0); //如果标志为1继续循环等待
    IFG1&=~OFIFG;
}

void delay_1ms(void)                 //1ms延时函数
  {
   uint i;
   for (i=0;i<1140;i++);
  }

void delay_nms(uint n)       //N ms延时函数
  {
   uint i=0;
   for (i=0;i   delay_1ms();
  }

void int_pwm()

  P4SEL|=PWM_1+PWM_2+PWM_3+PWM_4+PWM_5+PWM_6;
  P4DIR|=PWM_1+PWM_2+PWM_3+PWM_4+PWM_5+PWM_6;  
  TBCCR0 = 512-1;                       // PWM Period
  TBCCTL1 = OUTMOD_7;                   // CCR1 reset/set
  TBCCR1 = 384;                         // CCR1 PWM duty cycle
  TBCCTL2 = OUTMOD_7;                   
  TBCCR2 = 128;                         
  TBCCTL3 = OUTMOD_7;                   
  TBCCR3 = 64;                         
  TBCCTL4 = OUTMOD_7;                   
  TBCCR4 = 32;                         
  TBCCTL5 = OUTMOD_7;                   
  TBCCR5 = 16;                         
  TBCCTL6 = OUTMOD_7;                   
  TBCCR6 = 8;                         
  TBCTL = TBSSEL_2 + MC_1;              // SMCLK up mode 
}

void main()
  {
    uint counter=0;
    uint counter_stop=0;
    uint MAX=510;
   WDTCTL=WDTPW+WDTHOLD;//关看门狗 
   int_clk();
   int_pwm();
   InitKey44();
   _EINT();
   E_Key44();
   while(1)
     {
      delay_nms(10);
      counter=counter+1;
      if(counter==MAX)
        counter=0;
      else
        {TBCCR6=counter;
         TBCCR5=counter;
        }
      if(TB_flag_1)
        {TBCCR4=counter;counter_stop=counter;}
      if(TB_flag_2)
        {TBCCR4=MAX-counter;counter_stop=MAX-counter;}
      if(stop)
        {
          TBCCR4=counter_stop;
          stop=0;
        }
      if(run_add)
        {
          MAX=1000;
          run_add=0;
        }
      if(run_minus)
        {
          MAX=700;
          run_minus=0;
        }
      while(Change==1)
        {
          GetInput();
          if(!(TB_flag_1&TB_flag_2))
          TBCCR4=counter_1;
          Change=0;
        } 
     }
  }

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     目录           0  2012-07-14 18:45  PWM_4\
     文件       17879  2012-04-29 22:00  PWM_4\Backup of PWM_4.ewd
     文件       47232  2012-04-29 22:00  PWM_4\Backup of PWM_4.ewp
     目录           0  2012-04-12 19:07  PWM_4\Debug\
     目录           0  2012-04-29 22:00  PWM_4\Debug\Exe\
     文件       23228  2012-04-29 22:00  PWM_4\Debug\Exe\PWM_4.d43
     目录           0  2012-04-12 19:07  PWM_4\Debug\List\
     目录           0  2012-07-14 18:48  PWM_4\Debug\Obj\
     文件       32268  2012-04-29 22:00  PWM_4\Debug\Obj\main.r43
     文件         127  2012-07-14 18:45  PWM_4\Debug\Obj\PWM_4.pbd
     文件        4147  2012-04-12 17:37  PWM_4\KEY.C
     文件         227  2012-04-10 13:26  PWM_4\KEY.h
     文件        2683  2012-04-12 19:27  PWM_4\main.c
     文件           0  2012-07-14 18:45  PWM_4\path.txt
     文件        2510  2012-07-14 18:48  PWM_4\PWM_4.dep
     文件       20123  2012-07-12 14:11  PWM_4\PWM_4.ewd
     文件       49962  2012-07-12 14:11  PWM_4\PWM_4.ewp
     文件         159  2012-04-12 19:07  PWM_4\PWM_4.eww
     目录           0  2012-04-12 19:20  PWM_4\settings\
     文件        1299  2012-07-14 18:45  PWM_4\settings\PWM_4.cspy.bat
     文件        4576  2012-07-14 18:48  PWM_4\settings\PWM_4.dbgdt
     文件        1493  2012-07-14 18:48  PWM_4\settings\PWM_4.dni
     文件        3078  2012-07-14 18:48  PWM_4\settings\PWM_4.wsdt

评论

共有 条评论