Introduction: Getting Started With FRDM Kl46z Part 4 - ADC & DAC

Picture of Getting Started With FRDM Kl46z Part 4 - ADC & DAC

Hi !

In this part I'll show You how to use ADC and DAC using mbed compiler.

In datasheet p.172 we can noticed that ADC0 pin is PTE20 and DAC0 pin is PTE30

Step 1: ADC

Picture of ADC

First of all we have to create object as AnalogIn name(ADC_PIN). I used PTE20 pin. Function to show ADC value is read(), but we have to call it with name.read(). It shows value from 0 - 1, so if we want to measure voltage max 3,3V, we have to multiply the value by 3.3. ADC programme is very simple. The function adc_programme() we call in infinite loop. The ADC running mode signalize Green LED.

void adc_programme(void)
{ if(show_title == 1) // shows only when ADC programme is started { led2 = 1; lcd.clear(); lcd.printf("ADC "); wait(2); show_title = 0; } lcd.printf("%1.2fv", adc0.read()*3.3); led = !led; }

Step 2: DAC

Picture of DAC

As it was in ADC, here in DAC we also have to create the object but in this case it will be AnalogOut name(DAC_PIN). I used PTE30 as DAC0. I decided to integrate the touch slider with it (how to start with touch slider i showed in https://www.instructables.com/id/FRDM-kl46z-part-3-LCD-Slider-PWM/ ). The multimeter shows value from PTE30 according to where i put my finger on touch slider. With DAC we can also generate the sine wave or sawtooth using the tables with appropriate values. To set the value on DAC output we have to use name.write(value). The dac_programme() function is called in the same way as ADC - in infinite loop. The DAC running mode signalize the Red LED blinking.


void dac_programme(void)
{ if(show_title == 2) // shows only when DAC programme is started { led = 1; lcd.clear(); lcd.printf("DAC "); show_title = 0; } dac0.write(slider.readPercentage()); led2 = !led2; }

Step 3: Writing a Programme

At the end I'll show You complete programme. I add switches SW3 for DAC and SW1 for ADC. When nor the ADC or DAC is running on LCD is "on" printed. The flag is used to check which switch is pressed.

/*
for: instructables peripheries: DAC, ADC, TSI, LCD. LEDs, Switches author: kubavit */ #include "mbed.h" #include "SLCD.h" #include "tsi_sensor.h"

AnalogIn adc0(PTE20); AnalogOut dac0(PTE30); SLCD lcd; DigitalOut led(LED1); DigitalOut led2(LED2); DigitalIn keyADC(SW1); DigitalIn keyDAC(SW3); TSIAnalogSlider slider(PTB16, PTB17, 100);

uint8_t flag; uint8_t check_key(void); uint8_t show_title; void adc_programme(void); void dac_programme(void);

int main()

{ led = led2 = 1; // turn off leds lcd.printf(" on "); while (true) { lcd.CharPosition = 0; if(check_key() == 2) // if SW3 pressed { dac_programme(); // run DAC } if(check_key() == 1) // if SW1 pressed { adc_programme(); // run ADC } wait(0.2); // delay 200ms } }

uint8_t check_key(void) { if( !keyADC ) { flag = 1; show_title = 1; } if( !keyDAC ) { flag = 2; show_title = 2; } return flag; }

void adc_programme(void) { if(show_title == 1) // shows only when ADC programme is started { led2 = 1; lcd.clear(); lcd.printf("ADC "); wait(2); show_title = 0; } lcd.printf("%1.2fv", adc0.read()*3.3); //float value x.xx led = !led; }

Comments

About This Instructable

1,528views

5favorites

License:

More by kubavit:STM32F4 Discovery part2 - ADC GraphSTM32F4 Discovery part1 - Touch ButtonsGetting started with FRDM kl46z part 4 - ADC & DAC
Add instructable to: