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

📄 wave_pwm_ghs.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.c
//* Object              : AT91 - Timer Counter - PWM generation
//* Translator          : ARM Software Development Toolkit V2.5
//*
//* Imported resources  : none
//* Exported resources  : main
//*
//* 1.0 30/09/98 JCZ    : Creation
//* 2.0 00/00/00 JCZ    : Clean up
//* 3.0 31/10/00 EL     : Green Hills & EB40 
//*-----------------------------------------------------------------------------
/*
Configure the channel 0 of the Timer Counter(TC) of the AT91
to aim dual waveform generation :
- clock = MCKI / 2
- Register A toggle TIOA0 when reached
- Register B toggle TIOB0 when reached
- Register C = 0xF000
  toggle TIOA0 and TIOB0 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 :
           ________          ________          ______
  TIOA0 __|        |________|        |________|             red LED / output
           ________          ________          ______
  TIOB0 __|        |________|        |________|           green LED / output

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

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

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

#ifndef AT91_DEBUG_NONE
#include <stdio.h>
#endif

#include    "periph/stdc/std_c.h"
#include    "targets/eb40/eb40.h"
#include    "parts/m40800/reg_m40800.h"


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

volatile static unsigned long status;


//*-----------------------------------------------------------------------------
//* Function Name       : timer0_irq_handler
//* Object              : Timer 0 interrupt Handler
//*-----------------------------------------------------------------------------


#pragma ghs interrupt
#pragma intvect timer0_irq_handler 0x1C
void timer0_irq_handler ( void )
//* Begin
{
    // Acknowledge at timer level
    status = 1;
    AIC_EOICR = TC0_SR ;   
}
//* 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 ;

      
    TC0_CCR = TC_CLKDIS ;

    // Enable and clear PIO pin corresponding to TIOA0, TIOB0 and TIOA1
        PIO_PER  = (1<<PIOTIOA0) | (1<<PIOTIOA1) | (1<<PIOTIOB0) ;
        PIO_OER  = (1<<PIOTIOA0) | (1<<PIOTIOA1) | (1<<PIOTIOB0) ;
        PIO_CODR = (1<<PIOTIOA0) | (1<<PIOTIOA1) | (1<<PIOTIOB0) ;

    // Define TIOA0 and TIOB0 as peripheral
        PIO_PDR = (1<<PIOTIOA0) | (1<<PIOTIOB0) ;

    // Define IRQ0 and FIQ as PIO
        PIO_PER = (1<<PIOIRQ0) | (1<<PIOFIQ) ;

    // Initialize timer 0 interrupt
        AIC_IDCR = (1<<TC0_ID) ;    
        AIC_ICCR = (1<<TC0_ID) ;

        AIC_SMR4 = ( AIC_SRCTYPE_INT_LEVEL_SENSITIVE | 0x1 ) ;
        AIC_SVR4 = ((u_int)timer0_irq_handler ) ;
        AIC_IECR = (1<<TC0_ID) ;

    // Initialize the mode of the timer 0
    TC0_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

    //* Validate interrupts
    status = TC0_SR ;
    TC0_IER  = TC_CPCS ; // RC compare

    // Initialize the counters :
    TC0_RC = 0xF000 ;

    // Start the timer :
    TC0_CCR = TC_CLKEN ;
    TC0_CCR = TC_SWTRG ;

   // Invert TIOA1 every time TIOB1 is pressed
    pwm = 0x0400;
    value = 0x00004000;
    for (;;)
    {
        // Check if interrupt received
        if (status != 0)
        {
            status = 0;
            // Check if PIOFIQ pressed
            if (((PIO_PDSR & (1<<PIOFIQ)) != 0) && (TC0_RA < (TC0_RC-1)))
            {
                if ((value = TC0_RA + pwm ) >= TC0_RC )
                {
                    value = (long)(TC0_RC)-1 ;
                }
            }

            // Check if PIOIRQ0 pressed
            if (((PIO_PDSR & (1<<PIOIRQ0)) != 0) && (TC0_RA > 1))
            {
                if (( value = TC0_RA - pwm ) <= 0 )
                {
                    value = 1 ;
                }
            }
        }
        if ( value != 0 )
        {
            TC0_RA = (u_int)value ;
            TC0_RB = (u_int)( TC0_RC - value ) ;
            value = 0 ;
        }
    }

//* End
    return(0) ;
}

⌨️ 快捷键说明

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