欢迎来到虫虫下载站 | 资源下载 资源专辑 关于我们
虫虫下载站

lcodu_init.c

卫星接受机控制卫星和LCD显示
C
字号:
#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    (0x0)    // 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
#define   CH       (0x00)                            

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

#define DB  1
//定义行
#define row_0  ((unsigned int) AT91C_PIO_PA28)
#define row_1  ((unsigned int) AT91C_PIO_PA29)
#define row_2  ((unsigned int) AT91C_PIO_PA30)
#define row_3  ((unsigned int) AT91C_PIO_PA31)
//定义列
#define cow_0  ((unsigned int) AT91C_PIO_PA24)
#define cow_1  ((unsigned int) AT91C_PIO_PA25)
#define cow_2  ((unsigned int) AT91C_PIO_PA26)
#define cow_3  ((unsigned int) AT91C_PIO_PA27)

#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;

  //Set up by using ADC Mode register
  AT91C_BASE_ADC->ADC_MR = ( (SHTIM << 24) | (STARTUP << 16) | (PRESCAL << 8) |
                             (SLEEP << 5) | (LOWRES <<4) | (TRGEN ) );
  //Select the active channel 0 4 5 6 7
//    AT91C_BASE_ADC->ADC_CHER = (1<<0);
  AT91C_BASE_ADC->ADC_CHER = (1<<CH);
  
   //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;
  //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
//*-----------------------------------------------------------------------------

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

    
  }

//*-----------------------------------------------------------------------------
//* Function Name       : US_int
//* Object              : initiate US0 and US1.
//* autor               : wang ping
//* Input Parameters    : none
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void US_int()
{
#if(ENABLE_TELEMETRY)  
  AT91PS_USART COM0 = AT91C_BASE_US0;

 	//* Configure PIO controllers to periph mode
 	AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA,
 		((unsigned int) AT91C_PA5_RXD0    ) |
 		((unsigned int) AT91C_PA6_TXD0    ) , // Peripheral A
 		0); // Peripheral B


   	// First, enable the clock of the PIOA
    	AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_US0 ) ;
	// Usart Configure
        AT91F_US_Configure (COM0, MCK, AT91C_US_ASYNC_MODE_WP, AT91_BAUD_RATE, 0);
        //initiation the adc pdc
        AT91F_PDC_Open (AT91C_BASE_PDC_US0);
       //Set the PDC buffer and counter ; only use the first buffer 
        AT91F_PDC_ReceiveFrame ( AT91C_BASE_PDC_US0 , (char *) &us0_recv_dma[0], MAX_RECV_BUF,0,0);
   
        //enbale pdc interrupt        
         COM0->US_IDR = 0xffffffff;
         COM0->US_IER = AT91C_US_RXBUFF;
        //* open Usart 1 interrupt
         AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_US0, US0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, US0_c_irq_handler);
	 AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US0);
   
         // Enable usart
	COM0->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;
#endif
//	AT91F_US_PutChar (COM0,'X');
        
        //usart1       
        AT91PS_USART COM1 = AT91C_BASE_US1;

 	//* Configure PIO controllers to periph mode
 	AT91F_PIO_CfgPeriph( AT91C_BASE_PIOA,
 		((unsigned int) AT91C_PA21_RXD1    ) |
 		((unsigned int) AT91C_PA22_TXD1    ) , // Peripheral A
 		0); // Peripheral B


   	// First, enable the clock of the PIOA
    	AT91F_PMC_EnablePeriphClock ( AT91C_BASE_PMC, 1 << AT91C_ID_US1 ) ;
	// Usart Configure
        AT91F_US_Configure (COM1, MCK, AT91C_US_ASYNC_MODE_WP, AT91_BAUD_RATE, 0);
        //initiation the adc pdc
        AT91F_PDC_Open (AT91C_BASE_PDC_US1);
       //Set the PDC buffer and counter ; only use the first buffer 
        //AT91F_PDC_ReceiveFrame ( AT91C_BASE_PDC_US1 , (char *) &us1_recv_dma[0], MAX_RECV_BUF,0,0);
   
        //enbale pdc interrupt        
         COM1->US_IDR = 0xffffffff;
         COM1->US_IER = AT91C_US_RXBUFF;
        //* open Usart 1 interrupt
         AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_US1, US1_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, US1_c_irq_handler);
	 AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_US1);
   
         // Enable usart
	COM1->US_CR = AT91C_US_RXEN | AT91C_US_TXEN;

    AT91F_US_PutChar (COM1,'X');
  
}

//*-----------------------------------------------------------------------------
//* Function Name       : init_pio
//* Object              : initiate PIO,enable PIO clock.
//* autor               : wang ping
//* Input Parameters    : none
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void init_pio()
{
   unsigned int tmp;
   AT91C_BASE_PMC->PMC_PCER = 0x1;
   AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA;

   AT91C_BASE_PIOA->PIO_PDR=0xFFFFFFFF;
   tmp=AT91C_BASE_PIOA->PIO_PSR;
   AT91C_BASE_PIOA->PIO_PER = row_3|row_2|row_1|row_0|PA8_LED;
   AT91C_BASE_PIOA->PIO_OER = row_3|row_2|row_1|row_0|PA8_LED;
   AT91C_BASE_PIOA->PIO_CODR= row_3|row_2|row_1|row_0|PA8_LED;
   
   AT91C_BASE_PIOA->PIO_PER = AT91C_PIO_PA27|cow_2|cow_1|cow_0;
   AT91C_BASE_PIOA->PIO_ODR = AT91C_PIO_PA27|cow_2|cow_1|cow_0;
   AT91C_BASE_PIOA->PIO_IDR=0xFFFFFFFF;
   AT91C_BASE_PIOA->PIO_IER=AT91C_PIO_PA27|cow_2|cow_1|cow_0;
   //AT91C_BASE_PIOA->PIO_IMR=AT91C_PIO_PA27|cow_2|cow_1|cow_0;
   
  
   tmp=AT91C_BASE_PIOA->PIO_ISR;
     // 允许PIO时钟
  
   //AT91C_BASE_PIOA->PIO_IFER=AT91C_PIO_PA27|cow_2|cow_1|cow_0;
   AT91F_AIC_ConfigureIt ( AT91C_BASE_AIC, AT91C_ID_PIOA, IRQ0_INTERRUPT_LEVEL,AT91C_AIC_SRCTYPE_INT_HIGH_LEVEL, keyboard_c_irq_handler);
   AT91F_AIC_EnableIt (AT91C_BASE_AIC, AT91C_ID_PIOA);
   
   
   //tmp=0;
   //tmp=0;
   //输出有AT0,AT1,LED,MUTE,LE1,LE2,两个未定义(PA18,PA28)
   
   //AT91C_BASE_PIOA->PIO_PER = tmp;  // 配置PA0为输出->att0 and att1
   //AT91C_BASE_PIOA->PIO_OER = tmp;  
   //AT91C_BASE_PIOA->PIO_SODR= tmp;
   
   //AT91F_PIO_CfgOutput( AT91C_BASE_PIOA, tmp ) ;
}


//*-----------------------------------------------------------------------------
//* Function Name       : initiation
//* Object              : initiate RTT.And it's the entrance of the initiation
//* autor               : wang ping
//* Input Parameters    : none
//* Output Parameters   : none
//*-----------------------------------------------------------------------------
void initiation(void)
{
     //4HZ,即每秒计4次
  AT91C_BASE_RTTC->RTTC_RTMR = 0X2000;
  AT91C_BASE_AIC->AIC_IDCR=0xFFFFFFFF;
  ADC_int();
  //IRQ_int();
  //TC_int();
  //US_int();
  init_pio();

}

⌨️ 快捷键说明

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