📄 adc_menu.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 : adc_menu.c
//* Object : manage ADC for the AT91EB55
//*
//* 1.0 25/08/00 JPP : Creation
//*----------------------------------------------------------------------------
#ifndef AT91_DEBUG_NONE
#include <stdio.h>
#endif
#include "parts/m55800/lib_m55800.h"
#include "targets/eb55/eb55.h"
#include "drivers/analog/analog.h"
#include "lib_tools.h"
extern TypeAICHandler at91_asm_irq_adc ;
extern u_int mcki;
extern u_short input_val[1024] ;
extern AnalogAdcIrqDesc it_adc;
extern AnalogDescAdc desc_analog_adc;
// * Local variable
static const char main_menu[]=
{
"\n\r COMMAND MENU:ADC IT \n\r"
" 0) Info \n\r"
" 1) Set mode & res \n\r"
" 3) Read \n\r"
" 4) Read Status \n\r"
" 6) Stop ADC 0\n\r"
" 7) Stop ADC 1\n\r"
" 8) Run and init ADC 0 \n\r"
" 9) Run and init ADC 1 \n\r"
" 99) Quit program\n\r"
"Enter command to execute: "
};
static const char main_adc[]=
{
"\n\r COMMAND MENU:ADC \n\r"
" 0) Info \n\r"
" 1) Set mode & res \n\r"
" 3) Loop Read \n\r"
" 4) continous read set ADC before (8) \n\r"
" 5) read ADC0 and ADC1 (10 before) \n\r"
" 6) Stop ADC 0\n\r"
" 7) Stop ADC 1\n\r"
" 8) Run and init ADC 0 \n\r"
" 9) Run and init ADC 1 \n\r"
" 10) Run and init ADC 0 and 1\n\r"
" 11) Use IT \n\r"
" 99) Quit program\n\r"
"Enter command to execute: "
};
//*----------------------------------------------------------------------------
//* Function Name : Get_Command_adc_it
//* Object : Get command val
//* Input Parameters : None
//* Output Parameters : int : Command num
//*----------------------------------------------------------------------------
int Get_Command_adc_it(void)
{
int command;
at91_print_frame(&COM,(char *)main_menu,sizeof(main_menu));
at91_scanf(&COM,"%d", &command);
at91_print_crlf(&COM);
return command;
}
//*----------------------------------------------------------------------------
//* Function Name : menu
//* Object : check general menu
//* Input Parameters : None
//* Output Parameters : None
//*----------------------------------------------------------------------------
void menu_adc_it(void)
{
int command = 0;
int tioc,tc_div;
int adc_mode=0;
int adc_channel=0;
int adc_drv_channel=0;
int status;
int div;
it_adc.AsmADCHandler=at91_asm_irq_adc;
tioc =100;
tc_div = TC_CLKS_MCK32;
adc_drv_channel=1;
adc_drv_channel = (adc_channel>>1);
it_adc.loop_mode =LOOP_DAC;
adc_mode &= ~ADC_8_BIT_RES;
adc_mode |= (24 << ADC_B_PRESCAL)& ADC_PRESCAL;
while(command!=99)
{
command = Get_Command_adc_it();
switch(command)
{
case 0: /* Info */
print_adc_mode(adc_mode,adc_channel);
adc_drv_channel = (adc_channel>>1);
sprintf(message,"channel num %d \n\r",adc_drv_channel);
at91_print(&COM,message);
print_timer_mode( tioc, tc_div);
if (it_adc.loop_mode == LOOP_DAC)
at91_print(&COM,"DAC 0 in loop on ADC\n\r");
else
at91_print(&COM,"No loop on ADC\n\r");
sprintf(message,"channel num %d \n\r",adc_drv_channel);
// * calculate the frequnecy
if (tc_div == TC_CLKS_MCK1024) div = 1024;
if (tc_div == TC_CLKS_MCK128) div = 128;
if (tc_div == TC_CLKS_MCK32) div = 32;
if (tc_div == TC_CLKS_MCK8) div = 8;
if (tc_div == TC_CLKS_MCK2) div = 2;
div = (tioc*div);
if (div > mcki)
sprintf(message," frequency generation = %d mhZ\n\r",(mcki*1000)/div);
else
sprintf(message," frequency generation = %d Hz\n\r", mcki/div );
at91_print(&COM,message);
break;
case 1: /* Get ADC mode */
get_adc_mode(&adc_mode,&adc_channel);
adc_drv_channel = (adc_channel>>1);
get_timer_mode( &tioc, &tc_div );
at91_print(&COM,"Loop DAC 0 and ADCx (0=>no loop 1=>Loop):");
it_adc.loop_mode = at91_get_val(&COM, it_adc.loop_mode);
at91_print(&COM,message);
break;
case 3: //* test
if (it_adc.adc == &ADC0_DESC)
at91_print(&COM,"ADC 0");
else
at91_print(&COM,"ADC 1");
if (timer_interrup.nb_read !=0)
{
sprintf(message,"Channel:%d Val:0x%04X Min:0x%04X Max:0x%04X Av:0x%04X nb:%d\n\r",
it_adc.adc_channel,timer_interrup.read,timer_interrup.min,timer_interrup.max,timer_interrup.moy /timer_interrup.nb_read,timer_interrup.nb_read);at91_print(&COM,message);
}
else
{
sprintf(message,"Channel:%d Val:0x%04X Min:0x%04X Max:0x%04X Av:0x%04X nb:%d \n\r",
it_adc.adc_channel,timer_interrup.read,timer_interrup.min,timer_interrup.max,timer_interrup.moy ,timer_interrup.nb_read);at91_print(&COM,message);
}
break;
case 4:
status = at91_adc_get_status(&ADC0_DESC);
sprintf(message,"Status:0x%04X \n\r",status);at91_print(&COM,message);
break;
case 6: /* Command Reset */
it_adc.adc = &ADC0_DESC;
it_adc.timer = timer_base.TC_adc0;
at91_analog_close_adc_irq(&it_adc);
break;
case 7: /* Command Reset */
it_adc.adc = &ADC1_DESC;
it_adc.timer = timer_base.TC_adc1;
at91_analog_close_adc_irq(&it_adc);
break;
case 8: /* */
it_adc.adc_channel = adc_drv_channel;
timer_interrup.read = 0;
timer_interrup.min = DAC_DATA_10BITS;
timer_interrup.max = 0;
timer_interrup.moy = 0;
timer_interrup.nb_read = 0;
it_adc.adc = &ADC0_DESC;
it_adc.timer = timer_base.TC_adc0;
it_adc.dac_loop = &DAC0_DESC;
at91_analog_open_adc_irq(&it_adc,(adc_mode|timer_base.trig_selection_adc0) ,tioc,tc_div,input_val,1000);
break;
case 9: /* */
it_adc.adc_channel = adc_drv_channel;
timer_interrup.read = 0;
timer_interrup.min = DAC_DATA_10BITS;
timer_interrup.max = 0;
timer_interrup.moy = 0;
timer_interrup.nb_read = 0;
it_adc.adc = &ADC1_DESC;
it_adc.timer =timer_base.TC_adc1;
it_adc.dac_loop = &DAC1_DESC;
at91_analog_open_adc_irq(&it_adc,(adc_mode|timer_base.trig_selection_adc1), tioc,tc_div,input_val,1000);
break;
case 99: /* quit */
break;
default: /* invalid command */
at91_print(&COM,"Invalid command entered. Please enter again!\n\r");
}
}
}
//*----------------------------------------------------------------------------
//* Function Name : Get_Command_adc
//* Object : Get command val
//* Input Parameters : None
//* Output Parameters : int : Command num
//*----------------------------------------------------------------------------
int Get_Command_adc(void)
{
int command;
at91_print_frame(&COM,(char *)main_adc,sizeof(main_adc));
at91_scanf(&COM,"%d", &command);
at91_print_crlf(&COM);
return command;
}
//*----------------------------------------------------------------------------
//* Function Name : menu
//* Object : check general menu
//* Input Parameters : None
//* Output Parameters : None
//*----------------------------------------------------------------------------
void menu_adc(void)
{
int command = 0;
int read =0;
int adc_mode=0;
int adc_channel=0;
u_int adc_val0[4];
u_int adc_val1[4];
u_int nb_read,min,max,moyenne;
int channel;
int cmpt;
u_short adc_val;
while(command!=99)
{
command = Get_Command_adc();
switch(command)
{
case 0: /* Info */
print_adc_mode(adc_mode,adc_channel);
break;
case 1: /* Get ADC mode */
get_adc_mode(&adc_mode,&adc_channel);
break;
case 3:
at91_print(&COM,"ADC 0 to 1:");
at91_scanf(&COM,"%d", &read);
at91_print_crlf(&COM);
if(read==0)
desc_analog_adc.adc_desc=&ADC0_DESC;
else
desc_analog_adc.adc_desc=&ADC1_DESC;
at91_print(&COM,"Channel 0 to 3:");
at91_scanf(&COM,"%d", &channel);
at91_print(&COM,"\n\r Nb read:");
at91_scanf(&COM,"%d", &cmpt);
at91_print_crlf(&COM);
//* init
for (moyenne = 0,max=0,min =-1,nb_read= 1 ; nb_read < cmpt; nb_read++ )
{
adc_val = at91_analog_read_adc(&desc_analog_adc,channel);
if (min > adc_val) min = adc_val;
if (max < adc_val) max = adc_val;
moyenne = moyenne + adc_val;
sprintf(message,"Channel:%d Val:0x%04X Min:0x%04X Max:0x%04X Av:0x%04X nb:%d \n\r",channel,adc_val,min,max,moyenne/nb_read,nb_read);
at91_print(&COM,message);
}
break;
case 4: /* continous Read */
at91_print(&COM,"ADC 0 to 1:");
at91_scanf(&COM,"%d", &read);
at91_print_crlf(&COM);
if(read==0)
desc_analog_adc.adc_desc=&ADC0_DESC;
else
desc_analog_adc.adc_desc=&ADC1_DESC;
at91_print(&COM,"Channel 0 to 3:");
at91_scanf(&COM,"%d", &channel);
at91_print_crlf(&COM);
//* init
moyenne = max =0;
min =-1 ;
while ( at91_getch(&COM,&read))
{
nb_read++;
adc_val = at91_analog_read_adc(&desc_analog_adc,channel);
if (min > adc_val) min = adc_val;
if (max < adc_val) max = adc_val;
moyenne = moyenne + adc_val;
sprintf(message,"Channel:%d Val:0x%04X Min:0x%04X Max:0x%04X Av:0x%04X nb:%d \n\r",channel,adc_val,min,max,moyenne/nb_read,nb_read);
at91_print(&COM,message);
}
break;
case 5: /* read */
at91_adc_trig_cmd ( &ADC0_DESC,ADC_START);
at91_adc_trig_cmd ( &ADC1_DESC,ADC_START);
while ( at91_adc_get_status(&ADC0_DESC) == 0) {}
while ( at91_adc_get_status(&ADC1_DESC) == 0) {}
at91_adc_read ( &ADC0_DESC, adc_val0 ) ;
at91_adc_read ( &ADC1_DESC, adc_val1 ) ;
sprintf(message,"ADC0 channel 0: %04X 1: %04X 2: %04X 3: %04X \n\r",adc_val0[0],adc_val0[1],adc_val0[2],adc_val0[3]);
at91_print(&COM,message);
sprintf(message,"ADC1 channel 0: %04X 1: %04X 2: %04X 3: %04X \n\r",adc_val1[0],adc_val1[1],adc_val1[2],adc_val1[3]);
at91_print(&COM,message);
break;
case 6: /* Command Reset */
desc_analog_adc.adc_desc=&ADC0_DESC;
at91_analog_close_adc(&desc_analog_adc);
break;
case 7: /* Command Reset */
desc_analog_adc.adc_desc=&ADC1_DESC;
at91_analog_close_adc(&desc_analog_adc);
break;
case 8: /* */
desc_analog_adc.adc_desc=&ADC0_DESC;
desc_analog_adc.channel= adc_channel;
at91_analog_open_adc(&desc_analog_adc,adc_mode);
break;
case 9: /* */
desc_analog_adc.adc_desc=&ADC1_DESC;
desc_analog_adc.channel= adc_channel;
at91_analog_open_adc(&desc_analog_adc,adc_mode);
break;
case 10: /* read */
at91_adc_open(&ADC0_DESC,adc_mode);
at91_adc_open(&ADC1_DESC,adc_mode);
at91_adc_channel_open(&ADC0_DESC,channel);
at91_adc_channel_open(&ADC1_DESC,channel);
break;
case 11: /* */
menu_adc_it();
break;
case 99: /* quit */
break;
default: /* invalid command */
at91_print(&COM,"Invalid command entered. Please enter again!\n\r");
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -