SPI DAC Output help!

SPI DAC Output help!

Postby lucasc64 » Tue Aug 24, 2021 6:30 pm

Hey everyone, I programmed a PIC16LF19156 to output a sawtooth waveform to a MCP4911 10 bit DAC and I have (unsurprisingly) already ballsed it up. Here is the output. It shows what you would expect to see except it doesn't oscillate between the full power supply range, instead it occupies 1/2VDD - VDD.

[img]Capture.PNG[/img]

All the data is MSB first and here is the series of 16 bits the DAC listens for:

[img]data.PNG[/img]

Here is the code with an outrageously long list of config defines haha

Code: Select all
// CONFIG1
#pragma config FEXTOSC = OFF    // External Oscillator mode selection bits (Oscillator not enabled)
#pragma config RSTOSC = HFINT1  // Power-up default value for COSC bits (HFINTOSC (1MHz))
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config VBATEN = OFF     // VBAT Pin Enable bit (VBAT functionality is disabled)
#pragma config LCDPEN = OFF     // LCD Charge Pump Mode bit (LCD Charge Pump is disabled.)
#pragma config CSWEN = OFF      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (FSCM timer disabled)

// CONFIG2
#pragma config MCLRE = OFF      // Master Clear Enable bit (MCLR pin function is port defined function)
#pragma config PWRTE = OFF      // Power-up Timer selection bits (PWRT disable)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = OFF      // Brown-out reset enable bits (Brown-out reset disabled)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = OFF     // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will not cause a reset)

// CONFIG3
#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF       // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

// CONFIG4
#pragma config BBSIZE = 512     // Boot Block Size Selection bits (Boot Block Size (Words) 512)
#pragma config BBEN = OFF       // Boot Block Enable bit (Boot Block disabled)
#pragma config SAFEN = OFF      // SAF Enable bit (SAF disabled)
#pragma config WRTAPP = OFF     // Application Block Write Protection bit (Application Block NOT write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block NOT write-protected)
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration Words NOT write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM NOT write-protected)
#pragma config WRTSAF = OFF     // Storage Area Flash Write Protection bit (SAF NOT write-protected)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

// CONFIG5
#pragma config CP = OFF         // UserNVM Program memory code protection bit (UserNVM code protection disabled)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>
#include <stdbool.h>
#include <stdint.h>

#define _XTAL_FREQ 1000000              //Clock frequency define

#define DAC_CS      RC1
#define SPI_CLK     RC2
#define SPI_MOSI    RC3


void DACWrite(uint16_t value) {           
    RC1 = 0;                              //CS active
    SSP1BUF = (0b01110000 | (value >> 6));//Send data byte 1
    while(!PIR3bits.SSP1IF);              //Wait for transmission to end
    PIR3bits.SSP1IF = 0;                  //Reset interrupt
   
    SSP1BUF = (value << 2);               //Send data byte 2
    while(!PIR3bits.SSP1IF);              //Wait for transmission to end
    PIR3bits.SSP1IF = 0;                  //Reset interrupt
    RC1 = 1;                              //CS inactive
}

void setup(){
    TRISCbits.TRISC1 = 0;               //DAC CS
    TRISCbits.TRISC2 = 0;               //SPI CLOCK
    TRISCbits.TRISC3 = 0;               //SPI DATA OUT
   
    //SPI SETUP
    SSP1STAT = 0;
    SSP1CON1 = 0b00100010;
   
    RC2PPS = 0x13;                      //RC2 is SPI clock
    RC3PPS = 0x14;                      //RC3 is SPI Data out
   
   
}

void main(void) {
   
    setup();
   
    while(1){
        for(int i = 0; i < 1023; i++) {
            DACWrite(i);
        }
    }
    return;
}


PIC Datasheet: https://ww1.microchip.com/downloads/en/ ... 01923B.pdf
DAC Datasheet: http://ww1.microchip.com/downloads/en/d ... 22248a.pdf

Any ideas would be greatly appreciated because this is the only thing stopping me from getting any further!
Attachments
data.PNG
data.PNG (53.31 KiB) Viewed 1357 times
Capture.PNG
Capture.PNG (23.29 KiB) Viewed 1357 times
lucasc64
 
Posts: 2
Joined: Tue Aug 24, 2021 3:54 pm
PIC experience: Experienced Hobbyist

Re: SPI DAC Output help!

Postby ric » Tue Aug 24, 2021 9:21 pm

This:
Code: Select all
    //SPI SETUP
    SSP1STAT = 0;
    SSP1CON1 = 0b00100010;

gives:
CKP = 0; //clock idles low
CKE = 0 // outgoing data updates on idle to active, so rising edge.

that means the data is changing right when the DAC is sampling it, so it's getting shifted by one bit.
Change CKE to 1, so SSP1STAT = 0b01000000;
That will make the data change on the falling edge of clock.
Latest test project, an LED matrix display made from one reel of addressable LEDs. here
User avatar
ric
Verified identity
 
Posts: 659
Joined: Sat May 24, 2014 2:35 pm
Location: Melbourne, Australia
PIC experience: Professional 5+ years with MCHP products

Re: SPI DAC Output help!

Postby lucasc64 » Wed Aug 25, 2021 11:54 am

That was quick service, no buggering around! It seems silly now you say it, especially since I had the clock and data waveforms on the scope :roll:
Thanks so much!
lucasc64
 
Posts: 2
Joined: Tue Aug 24, 2021 3:54 pm
PIC experience: Experienced Hobbyist


Return to SSP (IIC, SPI)

Who is online

Users browsing this forum: No registered users and 10 guests

cron