~CODE
//-----------------------------------------------------------------------------
// ADC0_Init
//-----------------------------------------------------------------------------
void ADC0_Init (void)
{
char old_SFRPAGE = SFRPAGE;
int i;
SFRPAGE = ADC0_PAGE; // Switch to ADC0 Page
ADC0CN = 0x44; // ADC Disabled, Timer3 start-of-conversion
// track 16 SAR clocks before data conversion
// upon Timer3 OV. DMA will enable ADC as needed
//
REF0CN = 0x03; // turn on bias generator and internal reference.
for(i=0;i<10000;i++); // Wait for Vref to settle (large cap used on target board)
AMX0SL = 0x00; // Single-ended mode
ADC0CF = (SYSCLK/25000000) << 4; // Select SAR clock frequency =~ 25MHz
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
//-----------------------------------------------------------------------------
// ADC1_Init
//-----------------------------------------------------------------------------
void ADC1_Init (void)
{
char old_SFRPAGE = SFRPAGE;
int i;
SFRPAGE = ADC1_PAGE; // Switch to ADC0 Page
ADC1CN = 0x44; // ADC Disabled, Timer3 start-of-conversion
// track 16 SAR clocks before data conversion
// upon Timer3 OV. DMA will enable ADC as needed
//
REF1CN = 0x03; // turn on bias generator and internal reference.
for(i=0;i<10000;i++); // Wait for Vref to settle (large cap used on target board)
ADC1CF = (SYSCLK/25000000) << 4; // Select SAR clock frequency =~ 25MHz
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
//-----------------------------------------------------------------------------
// DMA0_Init
//-----------------------------------------------------------------------------
void DMA0_Init (void)
{
char old_SFRPAGE = SFRPAGE;
SFRPAGE = DMA0_PAGE; // Switch to DMA0 Page
DMA0CN = 0x00; // Disable DMA interface
DMA0DA = XRAM_START_ADD; // Starting Point for XRAM addressing
DMA0CT = NUM_SAMPLES; // Get NUM_SAMPLES samples
DMA0IPT = 0x00; // Start writing at location 0
// Push instructions onto stack in order they will be executed
DMA0IDT = DMA0_GET_ADC01; // DMA to move ADC0 data.
DMA0IDT = DMA0_END_OF_OP;
DMA0BND = 0x00; // Begin instruction executions at address 0
DMA0CN = 0xA0; // Mode 1 Operations, Begin Executing DMA Ops
// (which will start ADC0)
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
//------------------------------------------------------------------------------------
// Timer3_Init
//------------------------------------------------------------------------------------
//
// Configure Timer3 to auto-reload and generate ADC sample rate
// specified by ?counts? using SYSCLK as its time base.
//
void Timer3_Init (int counts)
{
char old_SFRPAGE = SFRPAGE;
SFRPAGE = TMR3_PAGE; // Switch to Timer 3 page
TMR3CN = 0x00; // Stop Timer3; Clear TF3;
TMR3CF = 0x08; // use SYSCLK as timebase
RCAP3 = -counts; // Init reload values
TMR3 = 0xffff; // set to reload immediately
TR3 = 1; // start Timer3
SFRPAGE = old_SFRPAGE; // restore SFRPAGE
}
//-----------------------------------------------------------------------------
// EMIF_Init
//-----------------------------------------------------------------------------
//
// Configure the external memory interface to use upper port pins in
// non-multiplexed mode to a mixed on-chip/off-chip configuration without
// Bank Select.
//
void EMIF_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = EMI0_PAGE; // Save SFR_PAGE
EMI0CF = 0x3C; // upper ports; non-muxed mode;
// split mode w/o bank select
EMI0TC = 0x45; // timing (7-cycle MOVX)
SFRPAGE = CONFIG_PAGE;
P4MDOUT |= 0xFF; // all EMIF pins configured as
P5MDOUT |= 0xFF; // push-pull
P6MDOUT |= 0xFF;
P7MDOUT |= 0xFF;
SFRPAGE = SFRPAGE_SAVE; // restore SFR_PAGE
}
//-----------------------------------------------------------------------------
// SendData
//-----------------------------------------------------------------------------
//
//Send data out UART0
//
void SendData(void)
{
unsigned int i;
char old_SFRPAGE = SFRPAGE;
SFRPAGE = UART0_PAGE; // Switch to UART0 page
read_ptr = XRAM_START_ADD; // Set pointer to beginning of data
for(i=0;i<65000;i++);
for(i=0;i<60000;i++);
for (i=0;i<NUM_SAMPLES;i++)
{
printf (" %u, %u\n",*(read_ptr),*(read_ptr+1)); // Send data as unsigned integers
read_ptr+=2;
}
SFRPAGE = old_SFRPAGE;
}
//-----------------------------------------------------------------------------
// Support Subroutines
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// Wait_MS
//-----------------------------------------------------------------------------
//
// Return Value : None
// Parameters:
// 1) unsigned int ms - number of milliseconds of delay
// range is full range of integer: 0 to 65335
//
// This routine inserts a delay of ?ms? milliseconds.
//
//-----------------------------------------------------------------------------
void Wait_MS(unsigned int ms)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = TMR2_PAGE;
TMR2CN = 0x00; // Stop Timer3; Clear TF3;
TMR2CF = 0x00; // use SYSCLK/12 as timebase
RCAP2 = -(SYSCLK/1000/12); // Timer 2 overflows at 1 kHz
TMR2 = RCAP2;
ET2 = 0; // Disable Timer 2 interrupts
TR2 = 1; // Start Timer 2
while(ms)
{
TF2 = 0; // Clear flag to initialize
while(!TF2); // Wait until timer overflows
ms--; // Decrement ms
}
TR2 = 0; // Stop Timer 2
SFRPAGE = SFRPAGE_SAVE; // Restore SFRPAGE
}