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

📄 pwm_sample.c

📁 oki67500系列arm工程例程源代码
💻 C
字号:
/**********************************************************************************/
/*                                                                                */
/*    Copyright (C) 2003 Oki Electric Industry Co., LTD.                          */
/*                                                                                */
/*    System Name    :  ML675001 series                                           */
/*    Module Name    :  PWM sample program                                        */
/*    File   Name    :  pwm_sample.c                                              */
/*    Revision       :  01.00                                                     */
/*    Date           :  2003/03/09 initial version                                */
/*                                                                                */
/**********************************************************************************/
#include "ML675001.h"
#include "common.h"
#include "irq.h"
#include "cache.h"

/*

     PWM0 (Ds=10, De=100)
  
 duty(%)
   |       250      500
   +--------+--------+------------>n(cycle)
   |
   |   1cycle=10ms
   |     ->||<-
 De+ - - -_--_
   |    _-    -_          _-
   |  _-        -_      _-
   |_-            -_  _-
 Ds+                --
   |
  0+--------+--------+------------>time(ms)
   0       2500     5000


     PWM1 (Ds=100, De=10)
  
 duty(%)            
   |       250      500
   +--------+--------+------------>n(cycle)
   |
   |           1cycle=10ms 
   |              ->||<-
 Ds+_              _--_       
   | -_          _-    -_     
   |   -_      _-        -_   
   |     -_  _-            -_ 
 De+ - - - --                
   |                
  0+--------+--------+------------>time(ms)
   0       2500     5000

*/

/* constants */
#define MHz     (1000000L)
#define CCLK    (60*MHz)    /* frequency of CCLK (Hz) */
#define PWM_CYC (100)    /* frequency of PWM (Hz) */
#define PWCK    (32)    /*  */
#define VALUE_OF_PWCY   /* reload value of PWM */\
    ((65536*PWCK*PWM_CYC-CCLK)/(PWCK*PWM_CYC))
#define DUTY(n, Ds, De) /* duty(%) n,Ds,De:show above figure */ \
    (((((long)(De))-(Ds))*(n)+(Ds)*((CYCLE)/2-1))/((CYCLE)/2-1))
#define VALUE_OF_PWR(duty)  /* value of PWR duty:duty(%) */ \
    (((65536-(VALUE_OF_PWCY))*(duty)+(VALUE_OF_PWCY)*100)/100)
#define CYCLE   500

/* check VALUE_OF_PWCY */
#if (VALUE_OF_PWCY < 0 || 0x10000 <= VALUE_OF_PWCY)
#error Invalid value : VALUE_OF_PWCY
#endif

/* functions */
int main(void);                     /* main routine */
static void reg_irq_handler(void);  /* registration of IRQ handler */
static void set_pwm(void);          /* setup PWM */
static void pwm0_handler(void);     /* PWM0 handler */
static void pwm1_handler(void);     /* PWM1 handler */

/* global variables */
UHWORD value_of_pwr0[CYCLE];
UHWORD value_of_pwr1[CYCLE];

/****************************************************************************/
/*  Entry point                                                             */
/*  Function : main                                                         */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   0                                                   */
/****************************************************************************/
int main(void)
{

    init_cache();  /* Initialize CACHE memory */
    cache_on(CACHE_BANK0);  /* Bank0 : Cache enable */

    /* initialize IRQ */
    init_irq();

    /* registration of IRQ handler */
    reg_irq_handler();

    /* setup TIMER */
    set_pwm();
    
    /* enable IRQ */
    irq_en();

    /* light LED */
    put_hvalue(GPPMB, 0x00ff);    /* output mode */
    led_on(LED_START_PATTERN);

    /* setup port */
    set_hbit(GPCTL, 0x0020);
    
    /* start pwm */
    set_hbit(PWCON0, PWCON_PWR);
    set_hbit(PWCON1, PWCON_PWR);

    /* infinite loop */
    for(;;)
        ;

    return 0;
}

/****************************************************************************/
/*  Registration of IRQ Handler                                             */
/*  Function : reg_irq_handler                                              */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void reg_irq_handler(void)
{
    /* register IRQ handlers into IRQ handler table */
    IRQ_HANDLER_TABLE[INT_PWM0] = pwm0_handler;
    IRQ_HANDLER_TABLE[INT_PWM1] = pwm1_handler;

    /* setup interrupt level */
    set_wbit(ILC1, ILC1_ILR12 & ILC1_INT_LV1); /* PWM0(nIR[12]) -> level1 */
    set_wbit(ILC1, ILC1_ILR13 & ILC1_INT_LV2); /* PWM1(nIR[13]) -> level2 */

    return;
}

/****************************************************************************/
/*  setup of PWM                                                            */
/*  Function : set_pwm                                                      */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void set_pwm(void)
{
    int i;
    
    /* stop PWM */
    put_hvalue(PWCON0, 0);  /* PWM0 */
    put_hvalue(PWCON1, 0);  /* PWM1 */

    /* clear PWM interrupt status register */
    put_hvalue(PWINTSTS, PWINTSTS_INT0CLR|PWINTSTS_INT1CLR);

    /* calculate duty value */
    for(i=0; i<(CYCLE/2); i++){
        value_of_pwr0[i] = value_of_pwr0[(CYCLE)-1-i]
            = (UHWORD)VALUE_OF_PWR(DUTY(i, 10, 100));
        value_of_pwr1[i] = value_of_pwr1[(CYCLE)-1-i]
            = (UHWORD)VALUE_OF_PWR(DUTY(i, 100, 10));
    }

    /* set duty value */
    put_hvalue(PWR0, value_of_pwr0[0]);
    put_hvalue(PWR1, value_of_pwr1[0]);

    /* setup cycle */
    put_hvalue(PWC0, (UHWORD)VALUE_OF_PWCY);
    put_hvalue(PWC1, (UHWORD)VALUE_OF_PWCY);

    /* setup interrupt couse and clock */
    put_hvalue(PWCON0, PWCON_CLK32|PWCON_INTIE|PWCON_PWCOV);
    put_hvalue(PWCON1, PWCON_CLK32|PWCON_INTIE|PWCON_PWCOV);

    return;
}

/****************************************************************************/
/*  PWM0 handler                                                            */
/*  Function : pwm0_handler                                                 */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void pwm0_handler(void)
{
    static int index = 0;   /* index of value_of_pwr0[] */

    put_hvalue(PWINTSTS, PWINTSTS_INT0CLR); /* clear PWD0 interrupt */

    /* re-set duty value */
    put_hvalue(PWR0, value_of_pwr0[index]);
    index++;
    if(index >= CYCLE)
        index = 0;

    return;
}

/****************************************************************************/
/*  PWM1 handler                                                            */
/*  Function : pwm1_handler                                                 */
/*      Parameters                                                          */
/*          Input   :   Nothing                                             */
/*          Output  :   Nothing                                             */
/****************************************************************************/
void pwm1_handler(void)
{
    static int index = 0;   /* index of value_of_pwr1[] */

    put_hvalue(PWINTSTS, PWINTSTS_INT1CLR); /* clear PWD1 interrupt */

    /* re-set duty value */
    put_hvalue(PWR1, value_of_pwr1[index]);
    index++;
    if(index >= CYCLE)
        index = 0;

    return;
}

⌨️ 快捷键说明

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