首 页文档资料下载资料维修视频包年699元
请登录  |  免费注册
当前位置:精通维修下载 > 文档资料 > 家电技术 > 单元电路介绍 > 其它电路
c8051f005单片机PWM测试程序
来源:本站整理  作者:佚名  2011-08-04 10:13:59



c8051f005单片机PWM测试程序

//PWM输出脚P0.3
//-----------------------------------------------------------------------------
// Includes
//-----------------------------------------------------------------------------
#include <c8051f000.h>                 // SFR declarations
#include <INTRINS.H>
#include <stdio.h>
//-----------------------------------------------------------------------------
// Function PROTOTYPES
//-----------------------------------------------------------------------------
void PORT_Init (void);
void PCA_Init (void);
void PCA_ISR (void);
void SYSCLK_Init (void);
//-----------------------------------------------------------------------------
// MAIN Routine
//-----------------------------------------------------------------------------
void main (void)
{    
    unsigned long b;
    WDTCN = 0xde;                       // dISAble watchdog timer
    WDTCN = 0xad;
    SYSCLK_Init ();
    PORT_Init ();            /*初始化I/O口*/
       PCA_Init ();                /*PCA初始化成8位脉宽调置方式*/
    EIE1= 0x08;
    EA=1;
    PCA0CPH0=0x20;
       while (1)      
    {    }
}
//-----------------------------------------------------------------------------
// Initialization Subroutines
//-----------------------------------------------------------------------------


//-----------------------------------------------------------------------------
// PORT_Init
//-----------------------------------------------------------------------------
void PORT_Init (void)
{     
    XBR0 = 0x11;    // XBAR0: Initial Reset Value
    XBR1 = 0x00;    // XBAR1: Initial Reset Value
    XBR2 = 0x40;    // XBAR2: Initial Reset Value

                      // Port configuration (1 = Push Pull Output)
    PRT0CF = 0x08; // Output configuration for P0 
    PRT1CF = 0x00; // Output configuration for P1 
    PRT2CF = 0x00; // Output configuration for P2 
    PRT3CF = 0x00; // Output configuration for P3 
}
//-----------------------------------------------------------------------------
// SYSCLK_Init
//-----------------------------------------------------------------------------
void SYSCLK_Init (void)
{
      unsigned int i;                            // delaynms counter
      OSCXCN = 0x67;                      // start external oscillator with
      for (i=0; i < 256; i++) ;           // XTLVLD blanking interval (>1ms)
      while (!(OSCXCN & 0x80)) ;          // Wait for Crystal osc. to settle
      OSCICN = 0x88;                      // select external oscillator as SYSCLK                              
}
//-----------------------------------------------------------------------------
// PCA_Init
//-----------------------------------------------------------------------------
void PCA_Init (void)
{  
//  PCA0CPM0 = 0x42;    // PCA Capture/Compare Register 0
    PCA0CN    = 0x40;
    PCA0CPM1  = 0x42;
    PCA0CPL0  = 0x00;
    PCA0CPH0  = 0x00;
    
//    PCA0MD   &= 0x40;
    PCA0MD    = 0x01;
}
//-----------------------------------------------------------------------------
// PCA_ISR
//-----------------------------------------------------------------------------
void PCA_ISR (void) interrupt 9
{     unsigned char a ;
      bit    PWM_PAC;
      CF = 0;
      if(PWM_PAC==0)            //占空比由0    >>0xfe
        {
            PCA0CPH1=a;
             a++;
             if(a==0xfe){PWM_PAC=1;}
        }
        else                    //占空比由0xfe    >>0
        {
            PCA0CPH1=a;
             a--;
             if(a==0){PWM_PAC=0;}
        }
}

关键词:

文章评论评论内容只代表网友观点,与本站立场无关!

   评论摘要(共 0 条,得分 0 分,平均 0 分)
Copyright © 2007-2017 down.gzweix.Com. All Rights Reserved .
页面执行时间:14,757.81000 毫秒