0

I am writing a DAC driver for the Freedom KL25z and it does not work. I am testing it with an oscilloscope, but there is no rise in voltage.

My configuration function is using the only DAC channel available for the KL25...

Connected oscilloscope voltage to PORTE30 pin and oscilloscope ground to KL25 GND pin.

UPDATE, forgot to write filenames and actual question

Here is my DACDriver.c file

#include "DACDriver.h"
unsigned char bflgSendingData = 0;
unsigned short* wpSetPattern;
unsigned short wSetPatternSize;

void vfnDACInit()
{
    SIM_SCGC6 |= SIM_SCGC6_DAC0_MASK;
    DAC0_C0 |= DAC_C0_DACRFS_MASK;
    PORTE_PCR30 = PORT_PCR_MUX(0);
    DAC0_DAT0L = 0x00;
    DAC0_DAT0H |= 0x0;
    DAC0_DAT1L = 0xFF;
    DAC0_DAT1H |= 0xF;
    DAC0_C0 |= DAC_C0_DACEN_MASK;
}

void vfnDACSetValue(unsigned short wValue)
{
    DAC0_DAT0L = (unsigned char)(wValue & 0xFF);
    DAC0_DAT0H = (unsigned char)(wValue >> 8);
}

unsigned char bfnDACDriver(void)
{
    vfnDACSetValue(0xFF);
/*This is just a test; in my main, I'm just trying to see an output here.
But the main logic should be like the code below*/
    /*if(bflgSendingData){
        if(wSetPatternSize){
            vfnDACSetValue(wpSetPattern);
            wpSetPattern++;
            wSetPatternSize--;
        }
        return 1;
    }else{
        return 0;
    }
    */
}

unsigned char bfnDACSetPattern(unsigned short* wpPattern, unsigned short wPatternSize)
{
    if(0 == bflgSendingData){
        wpSetPattern = wpPattern;
        wSetPatternSize = wPatternSize;
        bflgSendingData = 1;
        return 1;
    }else{
        return 0;
    }
}

UPDATE, forgot to include this file

Here is my DACDriver.h file

#ifndef __DACDRIVER_H_
#define __DACDRIVER_H_
#include "derivative.h"

void vfnDACInit(void);
unsigned char bfnDACDriver(void);
unsigned char bfnDACSetPattern(unsigned short* wpPattern, unsigned short wPatternSize);

#endif /* __DACDRIVER_H_ */

Here is my main.c file, which right now only has a little test so I can see some output

#include "DACDriver.h"
int main(void)
{
    unsigned char pattern[] = {
            0xFF, 0xEF, 0xDF, 0xCF, 0xBF, 0xAF, 
            0x9F, 0x8F, 0x7F, 0x6F, 0x5F, 0x4F, 0x3F, 0x2F, 0x1F, 
            0x0F, 0x0E, 0x0D, 0x0C, 0x0B, 0x0A, 
            0x09, 0x08, 0x07, 0x06, 0x05, 0x04, 0x03, 0x02, 0x01, 0x00};
    vfnDACInit();
    for(;;) {
        bfnDACDriver();
    }
    return 0;
}

I am trying to be the most autodidact possible, but I have run out of ideas...

My main questions are: Do you see any error here? How do I make it work? Am I missing something?

Thanks a lot!!

porras
  • 21
  • 6
  • 1) Use `stdint.h` fixed bitwidth types for such tasks. The hardware registers should use them, too (otherwise this gives some impression on the quality of the provided headers/source code). 2) What was your question? 3) Use prototype declarators. `void vfnDACInit()` is not (you seem to mix then at will). – too honest for this site Dec 10 '15 at 23:29
  • 1
    What does "no output" mean? I see that `bfnDACDriver()` called from `main()` outputs `0xFF`, ignoring the values in `unsigned char pattern[]` defined in `main()`. – Weather Vane Dec 10 '15 at 23:30
  • Oh, and don't put function or object definitions in headers - except for Inline` functions. – too honest for this site Dec 10 '15 at 23:33
  • What happens when you replace `0xFF` with `0x00`? Do you still get "no output"? – Weather Vane Dec 10 '15 at 23:37
  • @Olaf 1) I am trying not to use libraries, but thank you. 2) How do I make it to work? Do you see any errors? Sorry, I didn't see I did not put any actual question 3) My prototype declaration is inside my DACDriver.h file, which I didn't include. – porras Dec 11 '15 at 00:09
  • 1
    `stdint.h` is not a library, but a header which is provided by all standard compliant C compilers. And you really should use these types, because they are exactly meant for this, while the basic types are not guaranted to provide a specific number of bits or a specific encoding for signed values either. Regarding the subject: as @WeatherVane already stated, you don't provide enough information. SO is not a remote debugging forum, but a Q&A site. It is not even clear if your problem is software or hardware based. – too honest for this site Dec 11 '15 at 00:12
  • "Here is my header file ..." says different. Edit your question. Still, you do not use correct prototype declarators. Also enable all compiler warnings, at least `-Wall -Wextra -Wconversion` (gcc, use the corresponding for your compiler). – too honest for this site Dec 11 '15 at 00:13
  • @Olaf sorry, I am trying to reach experts in C so someone who knows more than me can tell what I am doing wrong, I am not asking anyone to debug for me. And I am doing my best in explaining myself. Thanks for all the replies. – porras Dec 11 '15 at 00:20
  • @Porras: Well, you might not want to, but you actually do. So, last time: please provide the information required to help you. Use a debugger, read [ask], take the [tour]. Unteil then: good luck. – too honest for this site Dec 11 '15 at 00:24
  • @WeatherVane I changed the value from 0xFF to 0x00 and still got no voltage rise in oscilloscope graph, thanks for replying – porras Dec 11 '15 at 00:37
  • this line: `vfnDACSetValue(0xFF);` is only setting a single value, not a series of values. I'm not familiar with the indicated platform, however, on every system I have worked on 1) the DAC had to be enabled 2) each conversion has to be initiated, usually be writing a specific register in the DAC peripheral. 3) must not start another conversion until the prior conversion completes, usually be checking some 'busy' bit in a status register of the DAC peripheral. Could you clarify where/how these operations are being performed. – user3629249 Dec 12 '15 at 11:19
  • You do notice that the table: `pattern[]` is never being used. – user3629249 Dec 12 '15 at 11:20
  • Thanks @user3629249, I was not using my pattern but was expecting any rise in the oscilloscope's voltage instead with my 0xFF. My "state machine" is implemented in my DACDriver.c file – porras Dec 12 '15 at 22:14

1 Answers1

1

So my real problem was not seeing that my oscilloscope didn't have the right voltage/time resolution hence the "null" output so to speak. My driver is fine. Thank you very much to everyone answering and taking an interest in my problem.

porras
  • 21
  • 6