Without using interrupts produce a rising sawtooth waveform

158 views Asked by At

Here I need to output a waveform on oscilloscope in C which should in a rising sawtooth waveform.I am not sure if my code is correct. Any help or suggestions?

while(1)
{
    for (i = 1; i < 360; i++);

    // Check to see if status.TRDY is 1
    while(*(base+2) & 0x40 != 1);

    // while shift register is not empty
    // Make the sawtooth pattern
    if (saw == 0x1fff){
        saw = 0x1000;
    }
    else {
        saw = saw+1; 
    }
    // transmit sawtooth to the oscilloscope
    *(base+1) = saw;
}
1

There are 1 answers

0
Weather Vane On

This only tidies up the OP's posted code. It does not answer how to program the DAC. OP is using a 16-bit amplitude value but his register addressing suggests 8-bit registers - perhaps two writes are needed.

I suggest you also need function arguments defining the period of the sawtooth wave, and the number of steps. You also need an exit condition. I leave these points to you.

@Chris Stratton also commented that the I/O port should be the correct language type.

#define MINSAW  0x1000
#define MAXSAW  0x1FFF

unsigned *base = (unsigned *)0xD000;  // "insert your value"

int main()  {
    unsigned saw, i;
    while(1) {
        for (i = 0; i < 360; i++) {
            // ratio the waveform amplitude
            saw = MINSAW + i * (MAXSAW - MINSAW) / 359;

            // Check to see if status.TRDY is 1
            while((*(base+2) & 0x40) != 0x40);

            // transmit sawtooth to the oscilloscope
            *(base+1) = saw;
        }
    }
    return 1;
}