Hello Jingjing,
Well that is the problem because one example is not enough. I need to read ADC value every x ms and to send data via UART (using printf () function). Blinking red LED indicates me interrupt frequency.
I am modifying LAB6 - FTM & ADC Module. But I not sure how to configure clock to interrupt every x ms (for example every 1ms).
Questions:
a) I don't understand what frequency is core clock and bus clock?
b) How to make interrupt every 1 ms from FTM2? Please you can explain me how to do it.
c) In a code below I am trying to get 1s interrupt but it is 1,72s...
I think functions FTM2_Init_Output_Cmp(); and FTM2_SetPeriod(0, 187499); are not working properly because I can change value for period but period is not changing.
d) Maybe I should do something with Timer overflow flag ( configure TOF, and TOIE registers) to get precise interrupt every x ms? Any examples?
e) Maybe I should Trigger ADC with FTM ? I not sure which method is best for me. Please advice.
/******************************************************************************
* LAB6 - FTM & ADC Module *
* FRDM+KEA128 Board *
* *
* This lab demonstrates how to use the ADC module to read the analog *
* value of the on-board potentiometer and use FTM to blink LEDs. *
* The application reads the potentiometer, ADC channel 10. *
* LED will change color and speed according to the ADC value. *
* FLEX timer is used for that. *
* Target Board is FRDM+KEA128 *
******************************************************************************/
/**********************************************************************************************
* External objects
**********************************************************************************************/
#include "derivative.h" /* include peripheral declarations */
#include "GPIO.h"
#include "ADC.h"
#include "FTM.h"
#include "uart.h"
#include "ics.h"
#include "printf.h"
/**********************************************************************************************
* Global variables
**********************************************************************************************/
/**********************************************************************************************
* Constants and macros
**********************************************************************************************/
#define PTH0 24 /* For RED LED */
#define LED0_TOGGLE OUTPUT_TOGGLE(H1,PTH0)
#define LED0_OFF OUTPUT_CLEAR(H1,PTH0);
#define LED0_ON OUTPUT_SET(H1,PTH0);
/**********************************************************************************************
* Local types
**********************************************************************************************/
/**********************************************************************************************
* Local function prototypes
*********************************************************************************************/
void Enable_Interrupt(uint8_t vector_number);
void FTM2_Interrupt();
//uint32_t ADC_FTM_ValueFunction();
/**********************************************************************************************
* Local variables
**********************************************************************************************/
uint8_t color=0;
uint16_t adc_value = 0;
uint8_t dec=10;
uint16_t adc_total=0;
uint16_t Result = 0;
/**********************************************************************************************
* Local functions
**********************************************************************************************/
/**********************************************************************************************
* Global functions
**********************************************************************************************/
/***********************************************************************************************
* @brief GPIO_Init - Initialize the LEDs as outputs, SWs as inputs.
* @param none
* @return none
************************************************************************************************/
void GPIO_Init()
{
CONFIG_PIN_AS_GPIO(H1,PTH0,OUTPUT); /* Configure RED LED as an output */
LED0_OFF; /* Turn off RED */
}
/***********************************************************************************************
* @brief main() - Program entry function
* @param none
* @return none
************************************************************************************************/
int main(void)
{
/* Initialize Core Clock to MHz, Bus Clock to MHz */
/* Perform processor initialization */
ICS_ConfigType ICS_set={0}; /* Declaration of ICS_setup structure */
ICS_set.u8ClkMode=ICS_CLK_MODE_FEI; //FLL engaged internal clock//
ICS_set.bdiv=0; // 0= divides by 1. Bus Frequency Divider//
ICS_Init(&ICS_set); /* Initialization of Core clock at 48 MHz, Bus clock at 24 MHz */
/* Initialize UART */
UART_ConfigType UART_Config={{0}};
UART_Config.sctrl1settings.bits.bM=0; /* 8 bit mode*/
UART_Config.sctrl1settings.bits.bPe=0; /* No hardware parity generation or checking*/
UART_Config.bSbns=0; /* One stop bit*/
UART_Config.sctrl2settings.bits.bRe=1; /* Enable Receiver*/
UART_Config.sctrl2settings.bits.bTe=1; /* Enable Transmitter*/
UART_Config.u32SysClkHz = 24000000; /* Bus clock in Hz*/
UART_Config.u32Baudrate = 9600; /* UART baud rate */
UART_Init(UART2,&UART_Config); /*Initialization of UART utilities*/
/* Configure button pins as inputs and LED pins as outputs */
GPIO_Init();
/* Initialize the FTM2 module in Output Compare mode,
* and interrupt at every 46875 counts, equal to every 300 ms */
FTM2_Init_Output_Cmp();
FTM2_SetPeriod(0, 187499);
/* Initialize the ADC module and read at Channel 10 and 8bit conversion mode is imposed */
ADC_Init(10,TWELVE_BIT); /* Configure ADC channel 10 in 8-bit resolution mode */
/* Enable FTM interrupt */
Enable_Interrupt(FTM2_IRQn);
while(1);
return 0;
}
/***********************************************************************************************
* @brief FTM2_Interrupt() - User Function called by the FTM2 module on each timer interrupt.
* @param none
* @return none
************************************************************************************************/
void FTM2_Interrupt()
{
(void)FTM2_C0SC; /* Read to clear flag */
FTM2_C0SC ^= FTM_CnSC_CHF_MASK; /* Clear flag */
adc_value=ADC_Read(10); /* Read ADC value from channel 10 */
adc_total+=adc_value ;
dec--;
if (dec==0)
{
Result=adc_total;
printf("\nPOTENTIOMETER conversion value:%d\n",Result);//IB added
Result=0;
adc_total=0;
dec=10;
LED0_TOGGLE;
}
}
/***********************************************************************************************
* @brief FTM2_IRQHandler - Interrupt for FTM channels, Call user function.
* @param none
* @return none
************************************************************************************************/
void FTM2_IRQHandler()
{
FTM2_Interrupt();
}
//////////////////////////////
/////////////////////////////code copy from FTM.C//////////////////////////////////////
void FTM2_Init_Output_Cmp()
{
/*System Clock Gating Control Register*/
SIM_SCGC |= SIM_SCGC_FTM2_MASK; /* Enable Clock for FTM2 */
FTM2_SC |= FTM_SC_PS(7); /* Select Prescaler in this case 128. 24 Mhz /128 =187.50 Khz. */
/* Counter increase by one every 5,333 us */
/* Enable Channel 0*/
FTM2_C0SC |= FTM_CnSC_CHIE_MASK; /* Enable channel 0 interrupt */
FTM2_C0SC |= FTM_CnSC_MSA_MASK; /* Channel as Output compare mode */
/*Select interrupt frequency*/
FTM2_C0V = 187499; // IB modified
FTM2_SC |= FTM_SC_CLKS(1); /*FTM2 use system clock*/
}
void FTM2_SetPeriod(uint8_t ch, uint32_t period)
{
FTM2_C0V = FTM_CnV_VAL(period) ;
}
Best Regards,
IB