📄 adc_main.c
字号:
/*
* This command displays the current contents of all of the ADC
* registers
*/
void
cmd_dump (int argc, char **argv)
{
/* Diplay the ADC control registers */
printf("\tADSTAT = %04X\n", (MCF_ADC_ADSTAT));
printf("\tADLSTAT = %04X\n", (MCF_ADC_ADLSTAT));
printf("\tADZCSTAT = %04X\n", (MCF_ADC_ADZCSTAT));
printf("\tADCR1 = %04X\n", (MCF_ADC_CTRL1));
printf("\tADZCC = %04X\n", (MCF_ADC_ADZCC));
printf("\tADLST1 = %04X\n", (MCF_ADC_ADLST1));
printf("\tADLST2 = %04X\n", (MCF_ADC_ADLST2));
printf("\tADSDIS = %04X\n", (MCF_ADC_ADSDIS));
/* Diplay the ADC result registers */
printf("\n");
printf("\tADRSLT0 = %04X\n", (MCF_ADC_ADRSLT0));
printf("\tADRSLT1 = %04X\n", (MCF_ADC_ADRSLT1));
printf("\tADRSLT2 = %04X\n", (MCF_ADC_ADRSLT2));
printf("\tADRSLT3 = %04X\n", (MCF_ADC_ADRSLT3));
printf("\tADRSLT4 = %04X\n", (MCF_ADC_ADRSLT4));
printf("\tADRSLT5 = %04X\n", (MCF_ADC_ADRSLT5));
printf("\tADRSLT6 = %04X\n", (MCF_ADC_ADRSLT6));
printf("\tADRSLT7 = %04X\n", (MCF_ADC_ADRSLT7));
/* Diplay the ADC low limit registers */
printf("\n");
printf("\tADLLMT0 = %04X\n", (MCF_ADC_ADLLMT0));
printf("\tADLLMT1 = %04X\n", (MCF_ADC_ADLLMT1));
printf("\tADLLMT2 = %04X\n", (MCF_ADC_ADLLMT2));
printf("\tADLLMT3 = %04X\n", (MCF_ADC_ADLLMT3));
printf("\tADLLMT4 = %04X\n", (MCF_ADC_ADLLMT4));
printf("\tADLLMT5 = %04X\n", (MCF_ADC_ADLLMT5));
printf("\tADLLMT6 = %04X\n", (MCF_ADC_ADLLMT6));
printf("\tADLLMT7 = %04X\n", (MCF_ADC_ADLLMT7));
/* Diplay the ADC high limit registers */
printf("\n");
printf("\tADHLMT0 = %04X\n", (MCF_ADC_ADHLMT0));
printf("\tADHLMT1 = %04X\n", (MCF_ADC_ADHLMT1));
printf("\tADHLMT2 = %04X\n", (MCF_ADC_ADHLMT2));
printf("\tADHLMT3 = %04X\n", (MCF_ADC_ADHLMT3));
printf("\tADHLMT4 = %04X\n", (MCF_ADC_ADHLMT4));
printf("\tADHLMT5 = %04X\n", (MCF_ADC_ADHLMT5));
printf("\tADHLMT6 = %04X\n", (MCF_ADC_ADHLMT6));
printf("\tADHLMT7 = %04X\n", (MCF_ADC_ADHLMT7));
/* Diplay the ADC offset registers */
printf("\n");
printf("\tADOFS0 = %04X\n", (MCF_ADC_ADOFS0));
printf("\tADOFS1 = %04X\n", (MCF_ADC_ADOFS1));
printf("\tADOFS2 = %04X\n", (MCF_ADC_ADOFS2));
printf("\tADOFS3 = %04X\n", (MCF_ADC_ADOFS3));
printf("\tADOFS4 = %04X\n", (MCF_ADC_ADOFS4));
printf("\tADOFS5 = %04X\n", (MCF_ADC_ADOFS5));
printf("\tADOFS6 = %04X\n", (MCF_ADC_ADOFS6));
printf("\tADOFS7 = %04X\n", (MCF_ADC_ADOFS7));
}
/********************************************************************/
/* This command issues a simple start of ADC conversions. */
void
cmd_start (int argc, char **argv)
{
/* Clear stop bit */
MCF_ADC_CTRL1 &= ~MCF_ADC_CTRL1_STOP0;
/* Set start bit */
MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_START0;
}
/********************************************************************/
/* This command stops all ADC conversions. */
void
cmd_stop (int argc, char **argv)
{
/* Set stop bit */
MCF_ADC_CTRL1 |= MCF_ADC_CTRL1_STOP0;
printf("\nAll Conversions stopped\n");
}
/********************************************************************/
/* This command disables ADC channels from conversions. */
void
cmd_disable (int argc, char **argv)
{
int i, success, channel;
/* If incorrect number of arguments */
if (argc == 0)
{
{
/* Display Disable register */
printf("ADSDIS = %#02X \n", MCF_ADC_ADSDIS);
}
}
else
{
/* Get user input for the desired channel to disable*/
channel = get_value(argv[1],&success,10);
if (success == 0)
{
printf(INVALUE,argv[1]);
return;
}
/* Set disable bit */
MCF_ADC_ADSDIS |= (0x1 << channel);
}
}
/**********************************************************************/
/*
* The zero command allows you to set the zero crossing condition
* for conversions crossing the 0V boundary. When setting an
* offset, it should be entered in decimal form.
*/
void
cmd_zero (int argc, char **argv)
{
int channel, success, mode;
/* If not the right number of arguments for the zero command,
display the current value in the ADZCC register */
if (argc == 0 || argc == 1)
{
printf("ADZCC = %#04X\n", MCF_ADC_ADZCC);
}
/* If there are the correct number of arguments for the command: */
else
{
/* Get user input of A/D channel */
channel = get_value(argv[1],&success,10);
if (success == 0 || channel > 7 || channel < 0)
{
printf(INVALUE,argv[1]);
printf(" Value must be between 0 and 7\n");
return;
}
/* Get user input of zero crossing configuration */
mode = get_value(argv[2],&success,10);
if (success == 0 || mode > 3 || mode < 0)
{
printf(INVALUE,argv[2]);
printf(" Value must be between 0 and 3\n");
return;
}
/* Set the Zero Crossing bits in the ADZCC to the desired configuration */
MCF_ADC_ADZCC = MCF_ADC_ADZCC & ~ (0x3 << (channel * 2))
| ((mode & 0x3) << (channel * 2));
}
}
/**********************************************************************/
/*
* The adlst command allows you to order the list of the analog
* input channels to be converted when the next scan is
* initiated. When setting the ADLST it should be entered in
* decimal form.
*/
void
cmd_adlst1 (int argc, char **argv)
{
int i, sample, success, mode;
/* If not the right number of arguments for the adlst1 command,
display the current value for the ADLST1 register */
if (argc == 0 || argc == 1)
{
/* Display ADLST1 register */
printf("ADLST1 = %#04X\n", MCF_ADC_ADLST1);
}
/* If there are the correct number of arguments for the command: */
else
{
/* Get user input of list configuration */
sample = get_value(argv[1],&success,10);
if (success == 0 || sample > 3 || sample < 0)
{
printf(INVALUE,argv[1]);
printf(" Value must be between 0 and 3\n");
return;
}
/* Get user input of A/D channel */
mode = get_value(argv[2],&success,10);
if (success == 0 || mode > 7 || mode < 0)
{
printf(INVALUE,argv[2]);
printf(" Value must be between 0 and 7\n");
return;
}
/* Set the Channel List 1 bits in the ADLST1 to the desired configuration */
MCF_ADC_ADLST1 = MCF_ADC_ADLST1 & ~ (0x7 << (sample * 4))
| ((mode & 0x7) << (sample * 4));
}
}
/********************************************************************/
/*
* The adlst command allows you to order the list of the analog
* input channels to be converted when the next scan is
* initiated. When setting the ADLST it should be entered in
* decimal form.
*/
void
cmd_adlst2 (int argc, char **argv)
{
int i, sample, success, mode;
/* If not the right number of arguments for the adlst2 command,
display the current value for the ADLST2 register */
if (argc == 0 || argc == 1)
{
/* Display ADLST2 register */
printf("ADLST2 = %#04X\n", MCF_ADC_ADLST2);
}
/* If there are the correct number of arguments for the command: */
else
{
/* Get user input of list configuration */
sample = get_value(argv[1],&success,10);
if (success == 0 || sample > 7 || sample < 4)
{
printf(INVALUE,argv[1]);
printf(" Value must be between 4 and 7\n");
return;
}
/* Get user input of A/D channel */
mode = get_value(argv[2],&success,10);
if (success == 0 || mode > 7 || mode < 0)
{
printf(INVALUE,argv[2]);
printf(" Value must be between 0 and 7\n");
return;
}
/* Set the Channel List 2 bits in the ADLST2 to the desired configuration */
MCF_ADC_ADLST2 = MCF_ADC_ADLST2 & ~ (0x7 << ((sample - 4) * 4))
| ((mode & 0x7) << ((sample - 4) * 4));
}
}
/********************************************************************/
/*
* This command allows you to set the low limit comparison for
* conversions. When declaring an offset, it should be entered
* in decimal form, with a value of between 0 and 4095.
*/
void
cmd_adllmt (int argc, char **argv)
{
int i, success, llmt;
/*
* If not the right number of arguments for the adllmt command,
* display the current adllmt value for all 8 A/D channels
*/
if (argc == 0 || argc == 1)
{
for (i = 0; i < 8; i++ )
{
printf("ADLLMT%d = %#04X (%d)\n", i, MCF_ADC_ADLLMT(i), MCF_ADC_ADLLMT(i) >> 3);
}
}
else
{
/* If correct number of arguments, get the value of low limit */
llmt = get_value(argv[2],&success,10);
if (success == 0 || llmt > 4095 || llmt < 0)
{
/* If incorrect range for offset, display error message */
printf(INVALUE,argv[2]);
printf(" Value must be between 0 and 4095\n");
return;
}
/* Get channel for the low limit */
i = get_value(argv[1],&success,10);
if (success == 0 || i > 7 || i < 0)
{
/* If incorrect range for channel, display error message */
printf(INVALUE,argv[1]);
printf(" Value must be between 0 and 7\n");
return;
}
/* Set the low limit to the appropriate channel */
MCF_ADC_ADLLMT(i) = MCF_ADC_ADLLMT_LLMT(llmt);
}
}
/********************************************************************/
/*
* This command allows you to set the high limit comparison for
* conversions. When declaring an offset, it should be entered
* in decimal form, with a value of between 0 and 4095.
*/
void
cmd_adhlmt (int argc, char **argv)
{
int i, success, hlmt;
/*
* If not the right number of arguments for the adhlmt command,
* display the current adhlmt value for all 8 A/D channels
*/
if (argc == 0 || argc == 1)
{
for (i = 0; i < 8; i++ )
{
printf("ADHLMT%d = %#04X (%d)\n", i, MCF_ADC_ADHLMT(i), MCF_ADC_ADHLMT(i) >> 3);
}
}
else
{
/* If correct number of arguments, get the value of high limit */
hlmt = get_value(argv[2],&success,10);
if (success == 0 || hlmt > 4095 || hlmt < 0)
{
/* If incorrect range for offset, display error message */
printf(INVALUE,argv[2]);
printf(" Value must be between 0 and 4095\n");
return;
}
/* Get channel for the high limit */
i = get_value(argv[1],&success,10);
if (success == 0 || i > 7 || i < 0)
{
/* If incorrect range for channel, display error message */
printf(INVALUE,argv[1]);
printf(" Value must be between 0 and 7\n");
return;
}
/* Set the high limit to the appropriate channel */
MCF_ADC_ADHLMT(i) = MCF_ADC_ADHLMT_HLMT(hlmt);
}
}
/********************************************************************/
/* Set/Show Commands */
/********************************************************************/
/* This command sets the Scan Mode in the ADC control 1 register */
void
setshow_smode (int argc, char **argv)
{
int i, success, smode;
/* Set */
if (argv[2] != NULL)
{
/* Get user input for the smode bits in control 1 register */
smode = get_value(argv[2],&success,2);
if (success == 0)
{
printf(INVALUE,argv[2]);
return;
}
/* Set the SMODE bits in the ADCR1 to the desired configuration */
MCF_ADC_CTRL1 = MCF_ADC_CTRL1_SMODE(smode);
}
/* Show the current value of the smode bits in control 1 register */
else
printf("%03b", (MCF_ADC_CTRL1)&0x7);
}
/********************************************************************/
/* This command changes the Channel Configuration in the ADC
control 1 register. */
void
setshow_chncfg (int argc, char **argv)
{
int i, success, chncfg;
/* Set */
if (argv[2] != NULL)
{
/* Get user input for the chncfg bits in the ADCR1 register */
chncfg = get_value(argv[2],&success,2);
if (success == 0)
{
printf(INVALUE,argv[2]);
return;
}
/* Set the CHNCFG bits in the ADCR1 to the desired configuration */
MCF_ADC_CTRL1 = MCF_ADC_CTRL1_CHNCFG(chncfg);
}
/* Show current value of the chncfg bits in the ADCR1 register */
else
printf("%04b", (MCF_ADC_CTRL1 >>4)&0xF);
}
/********************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -