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

📄 wave_pwm_ads.c

📁 ARM入门的好帮手.包含了从简单到相对较复杂的程序.
💻 C
字号:
//*-----------------------------------------------------------------------------
//*      ATMEL Microcontroller Software Support  -  ROUSSET  -
//*-----------------------------------------------------------------------------
//* The software is delivered "AS IS" without warranty or condition of any
//* kind, either express, implied or statutory. This includes without
//* limitation any warranty or condition with respect to merchantability or
//* fitness for any particular purpose, or against the infringements of
//* intellectual property rights of others.
//*-----------------------------------------------------------------------------
//* File Name           : wave_pwm_ads.c
//* Object              : AT91EB40A - Timer Counter - PWM generation
//*
//* Imported resources  : none
//* Exported resources  : main
//*
//* 1.0 10/01/02 PFi    : Creation
//*-----------------------------------------------------------------------------
/*
Configure the channel 0 of the Timer Counter 1 (TC1) of the AT91
to aim dual waveform generation :
- clock = MCKI / 2
- Register A toggle TIOA1 when reached
- Register B toggle TIOB1 when reached
- Register C = 0xF000 toggle TIOA1 and TIOB1 when reached
  generate trigger to restart the timer

When pushing on FIQ (P12) :
- red LED switches on
- green LED switches off
When pushing on IRQ0 (P9) :
- red LED switches off
- green LED switches on
--------------------------------------------------------------------------------
At start-up :
           ________          ________          ______
  TIOA1 __|        |________|        |________|             red LED / output
           ________          ________          ______
  TIOB1 __|        |________|        |________|           green LED / output

When pushing IRQ0 :
           ____ <<           ____              ______
  TIOA1 __|    |____________|    |____________|             red LED / output
           ____________      ____________      ______
  TIOB1 __|          >>|____|            |____|           green LED / output

When pushing FIQ :
           ____________      ____________      ______
  TIOA1 __|          >>|____|            |____|             red LED / output
           ____ <<           ____              ______
  TIOB1 __|    |____________|    |____________|           green LED / output
--------------------------------------------------------------------------------
*/

/*----- Called Macro instructions definition -----*/
/* None */


#include    "periph/stdc/std_c.h"
#include    "targets/eb40a/eb40a.h"
#include    "parts/r40008/reg_r40008.h"


/*---- Internal Resources Definition -----*/

volatile static unsigned long status;


//*-----------------------------------------------------------------------------
//* Function Name       : timer1_irq_handler
//* Object              : Timer 1 interrupt Handler
//*-----------------------------------------------------------------------------
__irq void timer1_irq_handler ( void )
//* Begin
{
    // Acknowledge at timer level
    status = AIC_EOICR = TC1_SR ;       //* Here we are reading the TC1 Status Register to clear it
                                        //* and we are writing to the End Of Interrupt Command Register
                                        //* to end the interrupt at AIC Level
}
//* End


//*-----------------------------------------------------------------------------
//* Function Name       : main
//* Object              : AT91 - Timer Counter- PWM generation
//* Input Parameters    : none
//* Output Parameters   : none
//* Functions called    : none
//*-----------------------------------------------------------------------------
int main ( void )
//* Begin
{
    unsigned long pwm;
    long value ;

    TC1_CCR = TC_CLKDIS ;       /* Disable the  Clock Counter */

    PIO_PER  = (1<<PIOTIOA0) | (1<<PIOTIOA1) | (1<<PIOTIOB1) ;      /* Enable the PIO/TIMER pin */
    PIO_OER  = (1<<PIOTIOA0) | (1<<PIOTIOA1) | (1<<PIOTIOB1) ;      /* Enable the PIO/TIMER pin as Output */
    PIO_CODR = (1<<PIOTIOA0) | (1<<PIOTIOA1) | (1<<PIOTIOB1) ;      /* Clear this outputs */

    PIO_PDR = (1<<PIOTIOA1) | (1<<PIOTIOB1) ;                       /* Define TIOA1 and TIOB1 as peripheral */
    PIO_PER = (1<<PIOIRQ0) | (1<<PIOFIQ) ;                          /* Define IRQ0 and FIQ as PIO (enable SW Button) */

    AIC_IDCR = (1<<TC1_ID) ;                                        /* Disabe timer 1 interrupt at AIC level */
    AIC_ICCR = (1<<TC1_ID) ;                                        /* Clear the TC1 interrupt */

    AIC_SMR4 = ( AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 0x1 ) ;          /* Set the trigg and priority for TC1 interrupt */
    AIC_SVR4 = ((u_int)timer1_irq_handler ) ;                       /* Set the TC1 IRQ handler address */
    AIC_IECR = (1<<TC1_ID) ;                                        /* Enable the TC1 interrupt */

    // Initialize the mode of the timer 0
    TC1_CMR =
              TC_BSWTRG_SET_OUTPUT      |                   /* BSWTRG  : software trigger set TIOB  */
              TC_BCPC_SET_OUTPUT        |                   /* BCPC    : Register C compare set TIOB  */
              TC_BCPB_CLEAR_OUTPUT      |                   /* BCPB    : Register B compare clear TIOB  */
              TC_ASWTRG_SET_OUTPUT      |                   /* ASWTRG  : software trigger set TIOA */
              TC_ACPC_SET_OUTPUT        |                   /* ACPC    : Register C compare set TIOA */
              TC_ACPA_CLEAR_OUTPUT      |                   /* ACPA    : Register A compare clear TIOA */
              TC_WAVE                   |                   /* WAVE    : Waveform mode */
              TC_CPCTRG                 |                   /* CPCTRG  : Register C compare trigger enable */
              TC_EEVT_XC0               |                   /* EEVT    : XC0 as external event (TIOB = output) */
              TC_CLKS_MCK2  ;                               /* TCCLKS  : MCKI / 2 */


    TC1_IER  = TC_CPCS ;                                    /* Validate the RC compare interrupt */
    TC1_RC = 0xF000 ;                                       /* Initialize the counter */

    TC1_CCR = TC_CLKEN ;                                    /* Enable the Clock counter */
    TC1_CCR = TC_SWTRG ;                                    /* Trigg the timer */

    pwm = 0x400 ;
    value = 0x4000 ;

    for (;;)
    {
        if (status != 0)                                    /* Check if interrupt received */
        {
            status = 0;

            if (((PIO_PDSR & (1<<PIOFIQ)) != 0) && (TC1_RA < (TC1_RC-1)))  /* Check if PIOFIQ pressed */
            {
                if ((value = TC1_RA + pwm ) >= TC1_RC )
                {
                    value = (long)(TC1_RC)-1 ;
                } //* End if

            }//* End if

            if (((PIO_PDSR & (1<<PIOIRQ0)) != 0) && (TC1_RA > 1))       /* Check if PIOIRQ0 pressed */
            {
                if (( value = TC1_RA - pwm ) <= 0 )
                {
                    value = 1 ;
                }//* End if

            }//* End if

        }//* Enf if

        if ( value != 0 )
        {
            TC1_RA = (u_int)value ;
            TC1_RB = (u_int)( TC1_RC - value ) ;
            value = 0 ;
        }//* End if

    }//* End for

    return(0) ;

}//*End

⌨️ 快捷键说明

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