Hey I am working on FRDM KL25z board.
I have written some code for ADC and DAC but it is not working. actually it is working on verylow voltage signal which is less than 100mVpp. would you suggest me what is wrong in my code?? I think something in ADC ref volt or in ADC res is wrong but i can't figure out.
#include <MKL25Z4.H>
#include "user_defs.h"
unsigned int h[21] = { 29,70,115,163,211,256,297,331,357,374,379,374,357,331,297,256,211,163,115,70,29}
unsigned int x[21] = {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
void Init_ADC(void) {
SIM->SCGC6 |= (1UL << SIM_SCGC6_ADC0_SHIFT);
ADC0->CFG1 = 0x9C; // Select 16 bit resolution
ADC0->SC2 = 0x00;
}
void Init_DAC(void) {
// Init DAC output
SIM->SCGC6 |= (1UL << SIM_SCGC6_DAC0_SHIFT);
SIM->SCGC5 |= (1UL << SIM_SCGC5_PORTE_SHIFT);
PORTE->PCR[DAC_POS] &= ~(PORT_PCR_MUX(7)); // Select analog
// Disable buffer mode
DAC0->C1 = 0;
DAC0->C2 = 0;
// Enable DAC, select VDDA as reference voltage
DAC0->C0 = (1 << DAC_C0_DACEN_SHIFT) |
(1 << DAC_C0_DACRFS_SHIFT);
}
void Result_Out(volatile unsigned int out_data) {
DAC0->DAT[0].DATH = DAC_DATH_DATA1(out_data >> 8);
DAC0->DAT[0].DATL = DAC_DATL_DATA0(out_data);
}
float Measure_ADC_Value(void) {
volatile unsigned int res=0;
unsigned int i=0,y=0;
ADC0->SC1[0] = 0x00; // start conversion on channel 0
while (!(ADC0->SC1[0] & ADC_SC1_COCO_MASK));
res = ADC0->R[0]
x[0] = res;
for(i=0;i<21;i++)
{
y = y + h[20-i]*x[i];
}
for(i=20;i>0;i--)
{
x[i] = x[i-1];
}
return y/4096;
}
/*----------------------------------------------------------------------------
MAIN function
*----------------------------------------------------------------------------*/
int main (void) {
volatile unsigned int ADC_Res = 0.0;
Init_DAC();
Init_ADC();
while (1) {
ADC_Res = Measure_ADC_Value();
ADC_Res = ADC_Res;
Result_Out(ADC_Res);
}
}
Solved! Go to Solution.
Hi
Please check the attached code, this is a sample code that set the internal bandgap, you just need to set the proper channel for your applycation.
I hope this will help you.
Have a great day,
Vicente
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Hi
Please check the attached code, this is a sample code that set the internal bandgap, you just need to set the proper channel for your applycation.
I hope this will help you.
Have a great day,
Vicente
-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------
Thank You.. Mine code is also working after mode change...