⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timerbpluse.c

📁 利用430做的一个产生时钟脉冲的程序
💻 C
字号:
//方法:利用定时器产生方波脉冲;考虑使用中断法
#include<msp430x14x.h>

/*====================================================================
*                        数据类型定义部分
====================================================================*/
#define uint32 unsigned long
#define ulong  unsigned long
#define uint16 unsigned int
#define uint   unsigned int
#define uchar  unsigned char
#define uint8  unsigned char

/*====================================================================
*                        数据端口定义部分
====================================================================*/
#define   CLOCK_DIR   P4DIR
#define   CLOCK_SEL   P4SEL
#define   CLOCK_OUT   P4OUT
#define   CLOCK_IN    P4IN
//#define   CLOCL_IO  
//#define   LED1  3
//#define   LED2  2  

uchar g_channel = BIT2;                 //输出端口选择(默认输出通道为BIT2口)
uchar g_TimerB_cycle = 1000;            //设置时钟脉冲周期(默认周期为1MS)
/*====================================================================
*                         函数声明部分
====================================================================*/
void Init_TimerB( void );                       //定时器B初始化函数
void Set_TimerBcycle( uint cycle );
void Set_Clock_Channel( uint channel) ;        //端口输出设置函数
void InitSys( void );                          //系统初始化函数
void Delay_n_Us( uint us );
void Delay_n_Ms( uint ms );

void main(void)
{ 
  Set_TimerBcycle(100);
  InitSys( );
  Set_Clock_Channel( BIT1 );
  while(1);
}

/*====================================================================
*                       定时器B初始化函数
====================================================================*/
void Init_TimerB( void )
{
  TBCTL = TBSSEL1 + TBCLR + TBIE ;            //Select MCLK And Clear TAR( + TBIE)
  TBCCTL1 = CCIE;                            //Enable CRCO Interrupt
  TBCCR1 = g_TimerB_cycle;                  //1MS  初值
  TBCTL |= ID1 + MC1;                        //4分频 增模式 
       
}

/*====================================================================
*                         端口输出选择函数
====================================================================*/
void Set_Clock_Channel( uint channel)        //选择输出端口
{
  g_channel = channel;
  CLOCK_SEL |= 0x00;
  CLOCK_DIR |= g_channel;          //选择输出通道  
}
/*====================================================================
*                         定时器B周期设置函数
====================================================================*/
void Set_TimerBcycle( uint cycle )
{
  g_TimerB_cycle = cycle;
}
/*====================================================================
*                        定时器B中断输出脉冲函数
====================================================================*/
interrupt [ TIMERB1_VECTOR ] void TimerBCCR_ISR( void )
{
  static uchar outclockFg = 0;
  
  if( outclockFg ==0 )
  {
    outclockFg = 1;
    CLOCK_OUT &= ~g_channel;
  }
  else
  {
    outclockFg = 0;
    CLOCK_OUT |= g_channel;
  }
}
/*====================================================================
*                        系统初始化函数
====================================================================*/
void InitSys( void )
{
  uint iq0;
  
  WDTCTL = WDTPW + WDTHOLD;              //关闭看门狗 
  BCSCTL1 &= ~XT2OFF;                     //打开XT2振荡器
  do
  {
    IFG1 &= ~OFIFG;                       // 清除振荡器失效标志
    for( iq0 = 0xFF; iq0 > 0; iq0-- );
  }
  while((IFG1 & OFIFG) != 0 );
  
  BCSCTL2 =SELM_2+SELS;				//选择MCLK、SMCLK为XT2
  Init_TimerB( );
  _EINT();			                        //打开全局中断控制
}
/*====================================================================
*                         微秒延时函数
====================================================================*/
void Delay_n_Us( uint us )
{
  uchar j;
  while( us )
  {
      for( j==4; j>0; j-- ) ;
  }
}
/*====================================================================
*                         毫秒延时函数
====================================================================*/
void Delay_n_MS( uint ms )
{
  while( ms )
  {
    Delay_n_Us( 1000 );
  }
}

/*********************************************************************************************************
** 函数名称: void  PWM_module(uint16  cycle,uint8 channel)
** 功能描述: 启动PWM,设置周期cycle,初始PWM为0,选择通道channel,确定管脚;
** 晶  振:   4MHz
** 输 入:   cycle  需要设置的周期

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -