0% found this document useful (0 votes)
62 views2 pages

Spi mcp4822

This C code defines functions for initializing and communicating with a digital to analog converter (DAC) chip using SPI (Serial Peripheral Interface) protocol on a PIC18F4520 microcontroller. It includes functions for enabling the SPI port, setting the SPI mode and clock frequency, and transmitting and receiving data. The main function uses these to repeatedly send 12-bit DAC output values to the chip at a fixed frequency, incrementing through a lookup table to generate a ramp output voltage.

Uploaded by

kennyfernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
62 views2 pages

Spi mcp4822

This C code defines functions for initializing and communicating with a digital to analog converter (DAC) chip using SPI (Serial Peripheral Interface) protocol on a PIC18F4520 microcontroller. It includes functions for enabling the SPI port, setting the SPI mode and clock frequency, and transmitting and receiving data. The main function uses these to repeatedly send 12-bit DAC output values to the chip at a fixed frequency, incrementing through a lookup table to generate a ramp output voltage.

Uploaded by

kennyfernandez
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

15/3/2018 artico.lma.fi.upm.es/numerico/antonio/blog/spi_mcp4822.

#include <p18F4520.h>
#include <delays.h>

#pragma config OSC=HS


#pragma config PWRT = OFF, BOREN = OFF
#pragma config WDT = OFF, WDTPS = 128
#pragma config PBADEN = OFF, LVP = OFF

#include "..\tipos.h"
#include "..\int_defs_C18.h"

uint8 tabla[256]= {
0x80,0x83,0x86,0x89,0x8C,0x8F,0x92,0x95,0x98,0x9B,0x9E,0xA2,0xA5,0xA7,0xAA,0xAD,
0xB0,0xB3,0xB6,0xB9,0xBC,0xBE,0xC1,0xC4,0xC6,0xC9,0xCB,0xCE,0xD0,0xD3,0xD5,0xD7,
0xDA,0xDC,0xDE,0xE0,0xE2,0xE4,0xE6,0xE8,0xEA,0xEB,0xED,0xEE,0xF0,0xF1,0xF3,0xF4,
0xF5,0xF6,0xF8,0xF9,0xFA,0xFA,0xFB,0xFC,0xFD,0xFD,0xFE,0xFE,0xFE,0xFF,0xFF,0xFF,
0xFF,0xFF,0xFF,0xFF,0xFE,0xFE,0xFE,0xFD,0xFD,0xFC,0xFB,0xFA,0xFA,0xF9,0xF8,0xF6,
0xF5,0xF4,0xF3,0xF1,0xF0,0xEE,0xED,0xEB,0xEA,0xE8,0xE6,0xE4,0xE2,0xE0,0xDE,0xDC,
0xDA,0xD7,0xD5,0xD3,0xD0,0xCE,0xCB,0xC9,0xC6,0xC4,0xC1,0xBE,0xBC,0xB9,0xB6,0xB3,
0xB0,0xAD,0xAA,0xA7,0xA5,0xA2,0x9E,0x9B,0x98,0x95,0x92,0x8F,0x8C,0x89,0x86,0x83,
0x80,0x7C,0x79,0x76,0x73,0x70,0x6D,0x6A,0x67,0x64,0x61,0x5D,0x5A,0x58,0x55,0x52,
0x4F,0x4C,0x49,0x46,0x43,0x41,0x3E,0x3B,0x39,0x36,0x34,0x31,0x2F,0x2C,0x2A,0x28,
0x25,0x23,0x21,0x1F,0x1D,0x1B,0x19,0x17,0x15,0x14,0x12,0x11,0x0F,0x0E,0x0C,0x0B,
0x0A,0x09,0x07,0x06,0x05,0x05,0x04,0x03,0x02,0x02,0x01,0x01,0x01,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x01,0x01,0x01,0x02,0x02,0x03,0x04,0x05,0x05,0x06,0x07,0x09,
0x0A,0x0B,0x0C,0x0E,0x0F,0x11,0x12,0x14,0x15,0x17,0x19,0x1B,0x1D,0x1F,0x21,0x23,
0x25,0x28,0x2A,0x2C,0x2F,0x31,0x34,0x36,0x39,0x3B,0x3E,0x41,0x43,0x46,0x49,0x4C,
0x4F,0x52,0x55,0x58,0x5A,0x5D,0x61,0x64,0x67,0x6A,0x6D,0x70,0x73,0x76,0x79,0x7C};

void spi_enable(void) // Enable SSP port and set TRIS register


{
TRISCbits.TRISC3=0; // SCL, out
TRISCbits.TRISC5=0; // SDO, out
TRISCbits.TRISC4=1; // SDI in
SSPCON1bits.SSPEN=1; // Enable SPI port
}

// Sets SPI mode (CPOL,CPHA,SMP)


void spi_mode(uint8 CPOL,uint8 CPHA,uint8 sample)
{
SSPCON1bits.CKP=CPOL; SSPSTATbits.CKE=1-CPHA;
SSPSTATbits.SMP=sample;
}

// Sets clock frequency for SPI in master mode


// clock =3 (TMR2/2) =2 (Fosc/64) =1 (Fosc/16) =0 (Fosc/4)
void spi_master(uint8 clock)
{
SSPCON1 = (SSPCON1 & 0xF0) | clock;
}

// Set SPI port as slave.


// ss=0 -> no dedicated SS pin,
// ss=1 -> dedicated SS pin (RA5 in PIC18F4520)
void spi_slave(uint8 ss)
{
ss=1-ss; ss = ss+4;
SSPCON1 = (SSPCON1 & 0xF0) | ss;
}

uint8 spi_transfer(uint8 x) // basic SPI transfer


{
SSPBUF = x; while(SSPSTATbits.BF==0); return(SSPBUF);
}

http://artico.lma.fi.upm.es/numerico/antonio/blog/spi_mcp4822.c 1/2
15/3/2018 artico.lma.fi.upm.es/numerico/antonio/blog/spi_mcp4822.c

// SPI functions aliases


#define spi_tx(x) spi_transfer(x) // sends TX data, ignores return value.
#define spi_rx() spi_transfer(0xFF) // sends dummy data, returns RX data.
#define spi_clock() spi_transfer(0xFF) // send 8 clocks (nobody cares about the data)

// CS line
#define DAC_CS LATCbits.LATC1
#define DAC_CS_dir TRISCbits.TRISC1
#define select_DAC DAC_CS=0
#define deselect_DAC DAC_CS=1

void DAC_spi_init(byte clock)


{
DAC_CS_dir=0; deselect_DAC; // Configure CS line as output and set it low.
spi_master(clock); // Configure SPI as master and set clock
spi_mode(0,0,0); // SPI mode (0,0). Third arg doesnt matter
spi_enable(); // Enable SPI

// SSPCON1bits.SSPEN=1; TRISC=0b00010000;
// SSPCON1bits.CKP=0; SSPSTATbits.CKE=1; // SPI mode (0,0)
// SSPSTATbits.SMP=0;
// SSPCON1 = (SSPCON1 & 0xF0) | speed;
}

#define send_msg(msg) { select_DAC; spi_tx(msg>>8); spi_tx(msg&0xFF); deselect_DAC; }

void main()
{
uint16 d=0;
uint16 msg;
//uint8 b0,b1;
uint16 v;

// TRISC=0;

DAC_CS_dir=0; deselect_DAC; // Configure CS line as output and set it low.


DAC_spi_init(0); Delay1KTCYx(10); // Configure SPI @ Fosc/4

while(1)
{
msg = d + 0x1000; //b0=msg&0xFF; b1=msg>>8;
// select_DAC; spi_tx(b1); spi_tx(b0); deselect_DAC;
send_msg(msg);

v = tabla[d&0xFF]; v<<=4;
msg = v + 0x9000; //b0=msg&0xFF; b1=msg>>8;
//select_DAC; spi_tx(b1); spi_tx(b0); deselect_DAC;
send_msg(msg);

d++; d&=0x0FFF; //PORTB=tabla[d&0xFF];

http://artico.lma.fi.upm.es/numerico/antonio/blog/spi_mcp4822.c 2/2

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy