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

📄 lcodu_init.c

📁 是关于arm7的代码
💻 C
📖 第 1 页 / 共 2 页
字号:
#define __SYS__INIT__  1

//*----------------------------------------------------------------------------
//* File Name           : initiation.c
//* Object              : initiate the mcu
//* Creation            : wzm   11/10/2007
//* Modif               : wp    26/10/2007
//*----------------------------------------------------------------------------

// Include Standard files
#include "Board.h"
#include <stdio.h>    	
#include "vars.h"
#include "usart.h"
#include "hardctrl.h"

int test_val;


//7 HIGHEST ;0 LOWEST
//ATPC
#define TIMER0_INTERRUPT_LEVEL		2
//alarm
#define TIMER1_INTERRUPT_LEVEL		1
//TELEMETRY
#define US0_INTERRUPT_LEVEL		5
//MONITOR
#define US1_INTERRUPT_LEVEL		4
#define ADC_INTERRUPT_LEVEL		3
//TX_LO
#define IRQ0_INTERRUPT_LEVEL		7
//RX_LO
#define IRQ1_INTERRUPT_LEVEL		6


//#define   NSAMPLE    50   // 5*10 ; 5 adc input;every adc 10 value
//volatile unsigned short AdcBuffer[NSAMPLE];

#define   TRGEN    (0x1)    // Hardware triggering
#define   TRGSEL   (0x2)    // Use a Timer output signal (on rising edge) from TIOA0 
#define   LOWRES   (0x0)    // 10-bit result output
#define   SLEEP    (0x0)    // Normal Mode
#define   PRESCAL  (0x4)    // Max value
#define   STARTUP  (0xc)    // This time period must be higher than 20 and not 20 ms
#define   SHTIM    (0x2)    // Must be higher than 3 ADC clock cycles but depends on output
                            // impedance of the analog driver to the ADC input

/* Select the sampling frequency in hertz */
#define   SAMPLING_FREQUENCY    50  // In Hertz
/* You have to capture, in this formula, 2, 8, 32, 128 or 1024 according to TC_MCK_DIV */
#define   TIMER_VALUE            (MCK/(1024*SAMPLING_FREQUENCY))
/* Targeted Pulse Width */
#define   PULSEWIDTH  20             // Pulse duration in term of Timer Clock Cycle(s)


//* \fn    AT91F_US_Baudrate
//* \brief Calculate the baudrate
//* Standard Asynchronous Mode : 8 bits , 1 stop , even parity
#define AT91C_US_ASYNC_MODE_WP ( AT91C_US_USMODE_NORMAL + \
                        AT91C_US_NBSTOP_1_BIT + \
                        AT91C_US_PAR_EVEN + \
                        AT91C_US_CHRL_8_BITS + \
                        AT91C_US_CLKS_CLOCK )

#define AT91_BAUD_RATE				38400


#include "lcodu_interrupth.c"

//*-----------------------------------------------------------------------------
//* Function Name       : ADC_int
//* Object              : initiate the ADC device.Using PDC and interrupt.
//*                     : And initiate TC2.TC2 is the trigger,Auto mode.
//* autor               : wang ping
//* Input Parameters    : none
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void ADC_int()
{
       
        
    //////adc--------PRESCAL STARTUP SHTIM需要依据MCK重新计算
  //software reset adc
  AT91C_BASE_ADC->ADC_CR = AT91C_ADC_SWRST;//复位ADC

  //Set up by using ADC Mode register
  AT91C_BASE_ADC->ADC_MR = ( (SHTIM << 24) | (STARTUP << 16) | (PRESCAL << 8) |
                             (SLEEP << 5) | (LOWRES <<4) | (TRGSEL << 1) | (TRGEN ) ); //TC2触发
  //Select the active channel 0 4 5 6 7
//    AT91C_BASE_ADC->ADC_CHER = (1<<0);
  AT91C_BASE_ADC->ADC_CHER = (1<<0)|(1<<4)|(1<<5)|(1<<6)|(1<<7);
  
   //initiation the adc pdc
  AT91F_PDC_Open (AT91C_BASE_PDC_ADC);
  //Set the PDC buffer and counter ; only use the first buffer 
  AT91F_PDC_ReceiveFrame ( AT91C_BASE_PDC_ADC , (char *) &adc_dma[0], MAX_ADC_BUF>>1,0,0);
  
  //enbale pdc interrupt    
  AT91C_BASE_ADC->ADC_IDR = 0xffffffff;
  AT91C_BASE_ADC->ADC_IER = AT91C_ADC_RXBUFF;//使能PDC的RXBuff中断
  AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_ADC, ADC_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_POSITIVE_EDGE, ADC_c_irq_handler);
  AT91F_AIC_EnableIt(AT91C_BASE_AIC, AT91C_ID_ADC);
  
 
//// timer counter 2 for adc----MCK改变时,TIMER_VALUE ,PULSEWIDTH会自动计算,可以不改
        
// tc2 clock enbale
 AT91C_BASE_PMC->PMC_PCER = 1<<AT91C_ID_TC2;
//Disable the clock and the interrupts
  AT91C_BASE_TC2->TC_CCR = AT91C_TC_CLKDIS;
  AT91C_BASE_TC2->TC_IDR = 0xFFFFFFFF ;
//  tcclks=4 select timer_clock5; wavsel=2 up rc compare;wave=1;acpa=1 set;
//  acpc=2 clear;aswtrg=2 clear
  AT91C_BASE_TC2->TC_CMR = (4<<0) | (2<<13) | (1<<15) | (1<<16)|(2<<18)|(2<<22) ;
// The RC will give you the signal period and (RC-RA) the slot width 
	AT91C_BASE_TC2->TC_RC = TIMER_VALUE ;
        AT91C_BASE_TC2->TC_RA = (TIMER_VALUE - PULSEWIDTH) ;
// Start the timer counter 
	AT91C_BASE_TC2->TC_CCR = (AT91C_TC_CLKEN |AT91C_TC_SWTRG ) ; 
 
}

//*-----------------------------------------------------------------------------
//* Function Name       : IRQ_int
//* Object              : initiate IRQ interrupts,IRQ0 is TX RF PLL alarm interrupt 
//*                     : and IRQ1 is RX RF PLL alarm interrupt.Negative edge trige.
//* autor               : wang ping
//* Input Parameters    : none
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void IRQ_int()
{
      //* open external IRQ interrupt
   	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA,AT91C_PA20_IRQ0,0);
      //* open external IRQ0 interrupt
       AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_IRQ0, IRQ0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_EXT_NEGATIVE_EDGE, IRQ0_c_irq_handler);//SYNTX告警
       AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_IRQ0);  //tx 
       
       //* open external IR1 interrupt
   	AT91F_PIO_CfgPeriph(AT91C_BASE_PIOA,AT91C_PA30_IRQ1,0);  //rx
   	//* open external IRQ1 interrupt
       AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_IRQ1, IRQ1_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_EXT_NEGATIVE_EDGE, IRQ1_c_irq_handler);//SYNRX告警
       AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_IRQ1);

}


/*-----------------*/
/* Clock Selection */
/*-----------------*/
#define TC_CLKS                  0x7
#define TC_CLKS_MCK2             0x0
#define TC_CLKS_MCK8             0x1
#define TC_CLKS_MCK32            0x2
#define TC_CLKS_MCK128           0x3
#define TC_CLKS_MCK1024          0x4
//5ms RC FOR TC_CLKS_MCK1024
#define   RC_5MS            (MCK/(1024*200))
#define   RC_8MS            (MCK/(1024*125))


//*-----------------------------------------------------------------------------
//* Function Name       : TC_int
//* Object              : initiate TC0 and TC1.
//* autor               : wang ping
//* Input Parameters    : none
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void TC_int()
  {
    unsigned int dummy;
    
    //tc1 
    //tc1 clock enable
    AT91C_BASE_PMC->PMC_PCER = 1<<AT91C_ID_TC1;
    //* Disable the clock and the interrupts
    AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKDIS ;
    AT91C_BASE_TC1->TC_IDR = 0xFFFFFFFF ;
    //* Clear status bit
    dummy = AT91C_BASE_TC1->TC_SR;
    //* Suppress warning variable "dummy" was set but never used
    dummy = dummy;
    //* Set the Mode of the Timer Counter;SELECT THE TCCLKS AND ENBALBE CPCTRG
    AT91C_BASE_TC1->TC_CMR = TC_CLKS_MCK1024 |AT91C_TC_CPCTRG; //0x04|0x1 << 14
    //AT91C_BASE_TC1->TC_CMR = TC_CLKS_MCK1024 ; 
    //* Enable the clock
    AT91C_BASE_TC1->TC_CCR = AT91C_TC_CLKEN ;    
    //RC 
    AT91C_BASE_TC1->TC_RC = RC_5MS*100;
    //* Open tc1 interrupt
    AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC1, TIMER1_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, timer1_c_irq_handler);
    //  IRQ enable CPC
    AT91C_BASE_TC1->TC_IER = AT91C_TC_CPCS;  
    AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC1);
    
    //tc0
     //v_tmatpc=0x14;
      AT91C_BASE_PMC->PMC_PCER = 1<<AT91C_ID_TC0;
    //* Disable the clock and the interrupts
    AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKDIS ;
    AT91C_BASE_TC0->TC_IDR = 0xFFFFFFFF ;
    //* Clear status bit
    dummy = AT91C_BASE_TC0->TC_SR;
    //* Suppress warning variable "dummy" was set but never used
    dummy = dummy;
    //* Set the Mode of the Timer Counter;SELECT THE TCCLKS AND ENBALBE CPCTRG
    AT91C_BASE_TC0->TC_CMR = TC_CLKS_MCK1024 |AT91C_TC_CPCTRG; 
    //AT91C_BASE_TC1->TC_CMR = TC_CLKS_MCK1024 ; 
    //* Enable the clock
    AT91C_BASE_TC0->TC_CCR = AT91C_TC_CLKEN ;    
    //RC 
    AT91C_BASE_TC0->TC_RC = RC_8MS*100;
    //* Open tc1 interrupt
    AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_TC0, TIMER0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, timer0_c_irq_handler);
    //  IRQ enable CPC
    AT91C_BASE_TC0->TC_IER = AT91C_TC_CPCS;  
    AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_TC0);
    
   
    //* Start timer0
        AT91C_BASE_TC0->TC_CCR = AT91C_TC_SWTRG ;

    //* Start timer1
        AT91C_BASE_TC1->TC_CCR = AT91C_TC_SWTRG ;

    
  }

#if(0)

⌨️ 快捷键说明

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