Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Help interfacing PIC18f4620 w/ LTC1661 DAC through SPI.

Status
Not open for further replies.

TWhilly

New Member
I'm doing project where I need to produce sine waves or triangle waves using a LTC1661 and PIC18f4620. Currently the output of my DAC is just a DC voltage (nearly 4 volts). I have limited microcontroller experience, so I think that may have set up the SPI registers wrongly. At this point, I am just testing the sine waves. I need help making sure the PIC is communicating to the DAC properly.

I have made all the standard power/ground connections for the circuit. I have attached the LT1661 datasheet. My complete code is below:

C:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <delays.h>
#include <p18f4620.h>
int main(void) {
//WaveTables (0-FF)

//Sine table values
char SineWave[32] = {0x80,0x98,0xb0,0xc6,0xda,0xea,0xf5,0xfd,
0xff,0xfd,0xf5,0xea,0xda,0xc6,0xb0,0x98,
0x80,0x67,0x4f,0x39,0x25,0x15,0xa,0x2,
0x0,0x2,0xa,0x15,0x25,0x39,0x4f,0x67};

//Triangle wave table values
char TriWave[32] ={0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,
0x8f,0x9f,0xaf,0xbf,0xcf,0xdf,0xef,0xff,
0xef,0xdf,0xcf,0xbf,0xaf,0x9f,0x8f,0x80,
0x70,0x60,0x50,0x40,0x30,0x20,0x10,0x0};

//Prepare SPI Pins
TRISCbits.TRISC3 = 0; // SCK (output)
TRISCbits.TRISC5 = 0; // SD0 (output)
TRISCbits.TRISC6 = 0; // CS  (output)

//Initialize SPI
OSCTUNE = 0x40;
OSCCON =  0x70; //8MHz (INTOSC drives clock directly)
SSPCON1 = 0x20; //Serial ports enabled idle state is clock low
SSPSTAT = 0x40; //Transmit occurs from active to idle clock transition

while(1){
//SPI Transfer
PORTCbits.RC6 = 0; //Make CS (chip select) low
for(int i = 0;i<31;i++){ // Loop through table

//Upper byte
PIR1bits.SSPIF = 0; // Make Transmission flag low
SSPBUF = SineWave[i]; //Send table data
while(PIR1bits.SSPIF == 0); // Wait until transmission is complete
i++; // Move to next element in table for lower byte

//Lower byte
PIR1bits.SSPIF = 0; // Make Transmission flag low
SSPBUF = SineWave[i]; // Send table data data.
while(PIR1bits.SSPIF == 0); // Wait until transmission is complete
PORTCbits.RC6 = 1; //Make CS high, transfer done
  }
}

}
 

Attachments

  • 1661fa.pdf
    230.5 KB · Views: 212
Last edited by a moderator:
Never mind!!!!.

I was caught out by HTML formatting again....

Using i as an index doesn't post well... Changes the code to Italics..

Not sure how you are communicating with the device..
16 bit s are used only 10 bits are data..

Are you integrating the address in the Sine table??

A3 A2 A1 A0 D9 D8 D7 D6 D5 D4 D3 D2 D1 D0 xx xx
 
Last edited:
The addressing gave me some confusion (probably because of my inexperience) . I realize now that the A3-A0 control the actions of the DAC not the location of information. I assumed the DAC needed 16bits (in 2 transmissions of 10 bits) with 4 unused/don't cares.

If I use a 10bit data table with the correct A3-A0 added in front, I should be able to send 16 bits at a time before the transmission is complete, right? I'm still a little unsure of my code structure.
 
Last edited:
You need to split the DAC data up like this..

assume you start with 512 or middle value... 512 = 0x200 then add the dac control lets say 0x9
so the first data byte will be 0x98 and the second byte will be 0x00..

unsigned char SineWave [32] = { 0x98, 0x00, etc....... }

Edit sorry forgot about the 2 bit shift....
 
Last edited:
C:
char SineWave[30] = {24,0,27,16,29,168,31,100,
           31,255,29,168,27,16,20,240,
           18,88,16,156,16,0,16,156,
           18,88,20,240,24,0};

Trouble here is... The wave is distorted due to the for loop latency!!

Also move the CS to low command into the for loop...
 
Status
Not open for further replies.

Latest threads

Back
Top