S32K144------ADC
环境
1、软件使用S32DS
2、使用库3.0
3、模块是ADC
4、硬件条件:开发板
5、实现功能:采集电位器电压值
第一步、添加ADC模块
第二步、在ADC中进行设置参数设置
1、填写采样时间
2、选择触发方式
3、选择输入端口
第三步、对LPIT模块设置时钟
第四步、在PIN_MUX设置对应的通道
第五步、拖出相应的初始化函数
第六步、具体函数实现
/* ###################################################################
** Filename : main.c
** Processor : S32K1xx
** Abstract :
** Main module.
** This module contains user's application code.
** Settings :
** Contents :
** No public methods
**
** ###################################################################*/
/*!
** @file main.c
** @version 01.00
** @brief
** Main module.
** This module contains user's application code.
*/
/*!
** @addtogroup main_module main module documentation
** @{
*/
/* MODULE main */
/* Including necessary module. Cpu.h contains other modules needed for compiling.*/
#include "Cpu.h"
#include "rtcTimer1.h"
volatile int exit_code = 0;
uint16_t adcRawValue;
uint16_t num;
uint32_t duty = 0;
/* User includes (#include below this line is not maintained by Processor Expert) */
void delay(volatile int cycles)
{
/* Delay function - do nothing for a number of cycles */
while(cycles--);
}
/*
* @brief Function to measure the value of Vref using the
* internal Vbg (bandgap reference) as known voltage.
*
* Warning !
* This function resets the ADC configuration.
*
* @return Measured value
*/
uint16_t getVrefValue(void)
{
/* clean driver */
// ADC_DRV_Reset(INST_ADCONV1);
/* Configure ADC converter to use software trigger and to have
* a resolution of 12 bits
*/
ADC_DRV_ConfigConverter(INST_ADCONV1, &adConv1_ChnConfig0);
/* Configure the ADC channel and disable the conversion complete
* interrupt.
*/
ADC_DRV_ConfigChan(INST_ADCONV1, /*uint8_t chanIndex*/0, &adConv1_ChnConfig0);
/* Wait for the conversion to be done */
ADC_DRV_WaitConvDone(INST_ADCONV1);
num++;
ADC_DRV_GetChanResult(INST_ADCONV1,0U, &adcRawValue);
}
void LPIT_ISR(void)
{
LPIT_DRV_ClearInterruptFlagTimerChannels(INST_LPIT1,(1<<0));
PINS_DRV_TogglePins(PTE,(1<<8));
}
/*!
\brief The main function for the project.
\details The startup initialization sequence is the following:
* - startup asm routine
* - main()
*/
int main(void)
{
bool increaseDuty = true;
// uint8_t channel1 = pwm_pal1Configs.pwmChannels[0].channel;
// uint8_t channel2 = pwm_pal2Configs.pwmChannels[0].channel;
uint8_t channel3 = pwm_palgreenConfigs.pwmChannels[0].channel;
uint8_t channel4 = pwm_palgreenConfigs.pwmChannels[1].channel;
/* Write your local variable definition here */
/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
#ifdef PEX_RTOS_INIT
PEX_RTOS_INIT(); /* Initialization of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of Processor Expert internal initialization. ***/
CLOCK_DRV_Init(&clockMan1_InitConfig0);
PINS_DRV_Init(NUM_OF_CONFIGURED_PINS, g_pin_mux_InitConfigArr);
INT_SYS_InstallHandler(LPIT0_Ch0_IRQn,&LPIT_ISR,(isr_t *)0);
LPIT_DRV_Init(INST_LPIT1, &lpit1_InitConfig);
LPIT_DRV_InitChannel(INST_LPIT1, /*uint32_t channel*/0, &lpit1_ChnConfig0);
LPIT_DRV_StartTimerChannels(INST_LPIT1,1<<0);
/* Call the init function */
RTC_DRV_Init(RTCTIMER1, &rtcTimer1_Config0);
ADC_DRV_ConfigConverter(INST_ADCONV1, &adConv1_ConvConfig0);//锟斤拷始锟斤拷
num++;
// ADC_DRV_AutoCalibration(INST_ADCONV1);//锟斤拷校准锟斤拷锟斤拷锟斤拷锟斤拷锟斤拷校锟斤拷锟斤拷锟斤拷陆锟?
/* Configure ADC channel and software trigger a conversion */
// ADC_DRV_ConfigChan(INST_ADCONV1, /*uint8_t chanIndex*/0, &adConv1_ChnConfig0);
num++;
/* Wait for the conversion to be done */
// ADC_DRV_WaitConvDone(INST_ADCONV1);
num++;
// ADC_DRV_GetChanResult(INST_ADCONV1,0U, &adcRawValue);
num++;
/* Set the time and date */
// RTC_DRV_SetTimeDate(RTCTIMER1, &rtcTimer1_StartTime0);
/* Configure RTC Time Seconds Interrupt */
// RTC_DRV_ConfigureSecondsInt(RTCTIMER1, &rtcTimer1_SecIntConfig0);
/* Start RTC counter */
// RTC_DRV_StartCounter(RTCTIMER1);
/* Configure an alarm every 3 seconds */
// RTC_DRV_ConfigureAlarm(RTCTIMER1, &rtcTimer1_AlarmConfig0);
// CLOCK_SYS_Init(g_clockManConfigsArr, CLOCK_MANAGER_CONFIG_CNT,
// g_clockManCallbacksArr, CLOCK_MANAGER_CALLBACK_CNT);
// CLOCK_SYS_UpdateConfiguration(0U, CLOCK_MANAGER_POLICY_AGREEMENT);
// PWM_Init(&pwm_pal1Instance,&pwm_pal1Configs);
// PWM_UpdateDuty(&pwm_pal1Instance,pwm_pal1Configs.pwmChannels->channel,500);
// PWM_Init(&pwm_pal2Instance, &pwm_pal2Configs);
// PWM_Init(&pwm_palgreenInstance, &pwm_palgreenConfigs);
while(1)
{
// PWM_UpdateDuty(&pwm_pal1Instance, channel1, duty);
// PWM_UpdateDuty(&pwm_palgreenInstance, channel3, duty);
// PWM_UpdateDuty(&pwm_palgreenInstance, channel4, duty);
// if (increaseDuty == true)
// {
// duty++;
// if (duty > 2500U)
// increaseDuty = false;
getVrefValue();
// }
// else
// {
// duty--;
// if (duty < 1U)
// increaseDuty = true;
// }
//delay(15000U);
}
/* Write your code here */
/* For example: for(;;) { } */
/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** RTOS startup code. Macro PEX_RTOS_START is defined by the RTOS component. DON'T MODIFY THIS CODE!!! ***/
#ifdef PEX_RTOS_START
PEX_RTOS_START(); /* Startup of the selected RTOS. Macro is defined by the RTOS component. */
#endif
/*** End of RTOS startup code. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;) {
if(exit_code != 0) {
break;
}
}
return exit_code;
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/
/* END main */
/*!
** @}
*/
/*
** ###################################################################
**
** This file was created by Processor Expert 10.1 [05.21]
** for the NXP S32K series of microcontrollers.
**
** ###################################################################
*/
第七步、验证ADC采样结果
1、改变电位器位置,adcRawValue相应的值。