/*********************************
** File:adc.c
** MCF52211 ADC module driver
********************************/
#include "support_common.h" /* include peripheral declarations and more */
#include "adc.h"
#include "delay.h"
#define ALL_ADC 0xFF
/* Samples Enable SDIS Register */
#define ALL_ENABLED 0x00
/* Delay in the POWER Reg by reset */
#define DEFAULT_DELAY 0x00D0
volatile uint16 g_adcStatus;
/*******************
* set mcf52211's ADC module to loop sequential mode from SAMPLE0 to SAMPLE7
* set ADC interrupt enable
* set ADC Low Limit compare mode
********************/
void init_adc(uint16 llimit0,uint16 llimit1)
{
uint8 i;
g_adcStatus=0;
MCF_GPIO_PANPAR =ALL_ADC;
//set LOW limit interrupt enable
MCF_ADC_CTRL1 = MCF_ADC_CTRL1_LLMTIE |MCF_ADC_CTRL1_SMODE(2); /* SMODE: This field controls the scan mode of the ADC module.
For Loop sequential mode it has to be set 010 bin (2). */
MCF_ADC_CTRL2 = MCF_ADC_CTRL2_DIV(9);//set adc clock rate to 3MHZ
/* The sampling order for channels */ /* ADLST1: contains an ordered list of the analog input channels to be
converted when the next scan is initialized*/
MCF_ADC_ADLST1 = MCF_ADC_ADLST1_SAMPLE0(0) |
MCF_ADC_ADLST1_SAMPLE1(1) |
MCF_ADC_ADLST1_SAMPLE2(2) |
MCF_ADC_ADLST1_SAMPLE3(3);
/* The sampling order for channels */
MCF_ADC_ADLST2 = MCF_ADC_ADLST2_SAMPLE4(4) |
MCF_ADC_ADLST2_SAMPLE5(5) |
MCF_ADC_ADLST2_SAMPLE6(6) |
MCF_ADC_ADLST2_SAMPLE7(7);
/* set ADC interrupt*/
MCF_INTC0_IMRH &= ~(MCF_INTC_IMRH_INT_MASK51 );
MCF_INTC0_IMRL &=~( MCF_INTC_IMRL_MASKALL);
MCF_INTC0_ICR51 =MCF_INTC_ICR_IP(7)+MCF_INTC_ICR_IL(2);
/*set channel's low limit*/
MCF_ADC_ADLLMT(0)=MCF_ADC_ADLLMT_LLMT(llimit0);
MCF_ADC_ADLLMT(1)=MCF_ADC_ADLLMT_LLMT(llimit1);
for(i=0;i<6;i++)
{
MCF_ADC_ADLLMT(i+2)=MCF_ADC_ADLLMT_LLMT(50);
}
/* All channels enabled */
MCF_ADC_ADSDIS = ALL_ENABLED; /* ADSDIS: enables only the desired analog channels*/
/* set the power-up delay in ADC */
MCF_ADC_POWER = DEFAULT_DELAY; /* POWER: controls the power management of the ADC module*/
/* wait until module is powered-up */
while(MCF_ADC_POWER & MCF_ADC_POWER_PSTS0) /* PSTS0 register: 0: ADC converter A is currently powered up,
1: ADC converter A is currently powered down */
;
return;
}
/*
* Start the ADC module
*
* Parameters: none
*
* Return : None.
*/
void start_adc()
{
MCF_ADC_CTRL1 &= ~MCF_ADC_CTRL1_STOP0 ;
MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_START0; /* CTRL1: is used to configure and control the ADC module.
START0: A scan is started by writing a 1 to this bit.*/
return;
}
//stop adc module
void stop_adc()
{
MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_STOP0 ; /* CTRL1: is used to configure and control the ADC module.
STOP0: A scan is stoped by writing a 1 to this bit.*/
return;
}
//the adc isr
__declspec(interrupt:0) void adc_isr(void)
{
uint16 prev_status,cur_status;
if (MCF_ADC_ADSTAT & MCF_ADC_ADSTAT_LLMTI)
{
prev_status=MCF_ADC_ADLSTAT;
MCF_ADC_ADLSTAT =0xffff; //clear interrupt
MCF_ADC_ADSTAT |= MCF_ADC_ADSTAT_EOSI0;
MCF_ADC_CTRL1&=~MCF_ADC_CTRL1_LLMTIE;
stop_adc();
delay_ms(50);
start_adc();
//wait for adc conversion finish
while((MCF_ADC_ADSTAT&MCF_ADC_ADSTAT_EOSI0)==0);
MCF_ADC_ADSTAT |= MCF_ADC_ADSTAT_EOSI0;
cur_status=MCF_ADC_ADLSTAT;
if(cur_status==prev_status)
{
g_adcStatus=cur_status;
}
else
{
g_adcStatus=0;
}
MCF_ADC_ADLSTAT =0xffff; //clear interrupt
MCF_ADC_CTRL1|=MCF_ADC_CTRL1_LLMTIE;
}
}
/***********************************
** File :test.c
** test code for adc driver
** demo the keyboard break detect
** the keyboard is connect with mcf52211's AN2~AN7
************************************/
#include "support_common.h" /* include peripheral declarations and more */
//#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
/* Standard IO is only possible if Console or UART support is enabled. */
#include
//#endif
#include "adc.h"
#include "uart0.h"
#define EnableInterrupts asm { move.w SR,D0; andi.l #0xF8FF,D0; move.w D0,SR; }
int main(void)
{
int counter = 0;
unsigned char buf[30];
#if (CONSOLE_IO_SUPPORT || ENABLE_UART_SUPPORT)
printf("Hello World in C from MCF52211 derivative on MCF52211 board\n\r");
fflush(stdout);
#endif
uart0_init(9600);
init_adc(100,100);
uart0_putstr((unsigned char*)("MCF52211 ADC test\n"));
EnableInterrupts;
start_adc();
for(;;) {
counter++;
if(g_adcStatus>0)
{
sprintf((char*)buf,"adc result: %d\n",g_adcStatus);
uart0_putstr(buf);
g_adcStatus=0;
}
}
}