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

📄 wave_pwm.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
//*
//* Imported resources  : none
//* Exported resources  : main
//*
//* 1.0 30/09/98 JCZ    : Creation
//* 2.0 01/11/00 JPP    : Clean up
//*-----------------------------------------------------------------------------
/*
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 */


#include    "periph/stdc/std_c.h"
#include    "targets/eb40/eb40.h"
#include    "parts/r40807/reg_r40807.h"


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

volatile static unsigned long status;


//*-----------------------------------------------------------------------------
//* Function Name       : timer0_irq_handler
//* Object              : Timer 0 interrupt Handler
//*-----------------------------------------------------------------------------
__irq void timer0_irq_handler ( void )
//* Begin
{
    // Acknowledge at timer level
    status = AIC_EOICR = TC0_SR ;   	//* Here we are reading the TC0 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 ;

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

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

	PIO_PDR = (1<<PIOTIOA0) | (1<<PIOTIOB0) ;						/* Define TIOA0 and TIOB0 as peripheral */
	PIO_PER = (1<<PIOIRQ0) | (1<<PIOFIQ) ;							/* Define IRQ0 and FIQ as PIO (enable SW Button) */

	AIC_IDCR = (1<<TC0_ID) ;										/* Disabe timer 0 interrupt at AIC level */
	AIC_ICCR = (1<<TC0_ID) ;										/* Clear the TC0 interrupt */

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

    // 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 */


	TC0_IER  = TC_CPCS ;	 								/* Validate the RC compare interrupt */
    TC0_RC = 0xF000 ;										/* Initialize the counter */

    TC0_CCR = TC_CLKEN ;									/* Enable the Clock counter */
    TC0_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) && (TC0_RA < (TC0_RC-1)))  /* Check if PIOFIQ pressed */
            {
                if ((value = TC0_RA + pwm ) >= TC0_RC )
                {
                	value = (long)(TC0_RC)-1 ;
              	} //* End if

			}//* End if

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

			}//* End if

     	}//* Enf if

		if ( value != 0 )
		{
	     	TC0_RA = (u_int)value ;
	        TC0_RB = (u_int)( TC0_RC - value ) ;
	        value = 0 ;
		}//* End if

	}//* End for

    return(0) ;

}//*End

⌨️ 快捷键说明

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