PIC18 LaurTec Library  3.3.1
Open Source C Library for PIC18 Microcontrollers based on C18 - XC8 Compilers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros
MCP4822.h File Reference
#include <spi.h>
#include <delays.h>

Go to the source code of this file.

Macros

#define USE_OR_MASKS
 
#define MCP4822_PULSE_DELAY   Delay1TCY(1)
 
#define MCP4822_SDO_BIT   5
 
#define MCP4822_SDO_TRIS   TRISC
 
#define MCP4822_SDO_PORT   LATC
 
#define MCP4822_SCK_BIT   3
 
#define MCP4822_SCK_TRIS   TRISC
 
#define MCP4822_SCK_PORT   LATC
 
#define MCP4822_CS_BIT   5
 
#define MCP4822_CS_TRIS   TRISA
 
#define MCP4822_CS_PORT   LATA
 
#define MCP4822_LDAC_BIT   0
 
#define MCP4822_LDAC_TRIS   TRISE
 
#define MCP4822_LDAC_PORT   LATE
 
#define MCP4822_DAC_A   0x00
 
#define MCP4822_DAC_B   0x80
 
#define MCP4822_GAIN_1   0x20
 
#define MCP4822_GAIN_2   0x00
 
#define MCP4822_OUT_DEFAULT_VALUE   0x00
 
#define MCP4822_SHUTDOWN_ON   0x00
 
#define MCP4822_SHUTDOWN_OFF   0x10
 
#define MCP4822_CLOCK_FOSC_64   SPI_FOSC_64
 
#define MCP4822_CLOCK_FOSC_16   SPI_FOSC_16
 
#define MCP4822_CLOCK_FOSC_4   SPI_FOSC_4
 
#define MCP4822_CLOCK_FOSC_TMR2   SPI_FOSC_TMR2
 
#define initialize_MCP4822   MCP4822_initialize
 
#define set_value_MCP4822   MCP4822_set_value
 
#define set_amplitude_MCP4822   MCP4822_set_amplitude
 
#define latch_pulse_MCP4822   MCP4822_latch_pulse
 
#define set_options_MCP4822   MCP4822_set_options
 

Functions

void MCP4822_initialize (unsigned char clock)
 
void MCP4822_set_value (unsigned int value, unsigned char channel)
 
void MCP4822_set_amplitude (unsigned int value, unsigned char channel)
 
void MCP4822_latch_pulse (void)
 
void MCP4822_set_options (unsigned char gain, unsigned char power, unsigned char channel)
 

Macro Definition Documentation

#define initialize_MCP4822   MCP4822_initialize

Definition at line 128 of file MCP4822.h.

#define latch_pulse_MCP4822   MCP4822_latch_pulse

Definition at line 179 of file MCP4822.h.

#define MCP4822_CLOCK_FOSC_16   SPI_FOSC_16

Definition at line 109 of file MCP4822.h.

#define MCP4822_CLOCK_FOSC_4   SPI_FOSC_4

Definition at line 110 of file MCP4822.h.

#define MCP4822_CLOCK_FOSC_64   SPI_FOSC_64

Definition at line 108 of file MCP4822.h.

#define MCP4822_CLOCK_FOSC_TMR2   SPI_FOSC_TMR2

Definition at line 111 of file MCP4822.h.

#define MCP4822_CS_BIT   5

Definition at line 85 of file MCP4822.h.

#define MCP4822_CS_PORT   LATA

Definition at line 87 of file MCP4822.h.

#define MCP4822_CS_TRIS   TRISA

Definition at line 86 of file MCP4822.h.

#define MCP4822_DAC_A   0x00

Definition at line 97 of file MCP4822.h.

#define MCP4822_DAC_B   0x80

Definition at line 98 of file MCP4822.h.

#define MCP4822_GAIN_1   0x20

Definition at line 100 of file MCP4822.h.

#define MCP4822_GAIN_2   0x00

Definition at line 101 of file MCP4822.h.

#define MCP4822_LDAC_BIT   0

Definition at line 89 of file MCP4822.h.

#define MCP4822_LDAC_PORT   LATE

Definition at line 91 of file MCP4822.h.

#define MCP4822_LDAC_TRIS   TRISE

Definition at line 90 of file MCP4822.h.

#define MCP4822_OUT_DEFAULT_VALUE   0x00

Definition at line 103 of file MCP4822.h.

#define MCP4822_PULSE_DELAY   Delay1TCY(1)

Definition at line 65 of file MCP4822.h.

#define MCP4822_SCK_BIT   3

Definition at line 81 of file MCP4822.h.

#define MCP4822_SCK_PORT   LATC

Definition at line 83 of file MCP4822.h.

#define MCP4822_SCK_TRIS   TRISC

Definition at line 82 of file MCP4822.h.

#define MCP4822_SDO_BIT   5

Definition at line 74 of file MCP4822.h.

#define MCP4822_SDO_PORT   LATC

Definition at line 76 of file MCP4822.h.

#define MCP4822_SDO_TRIS   TRISC

Definition at line 75 of file MCP4822.h.

#define MCP4822_SHUTDOWN_OFF   0x10

Definition at line 106 of file MCP4822.h.

#define MCP4822_SHUTDOWN_ON   0x00

Definition at line 105 of file MCP4822.h.

#define set_amplitude_MCP4822   MCP4822_set_amplitude

Definition at line 165 of file MCP4822.h.

#define set_options_MCP4822   MCP4822_set_options

Definition at line 199 of file MCP4822.h.

#define set_value_MCP4822   MCP4822_set_value

Definition at line 147 of file MCP4822.h.

#define USE_OR_MASKS

Definition at line 47 of file MCP4822.h.

Function Documentation

void MCP4822_initialize ( unsigned char  clock)

This function initializes the MCP4822 and PIC signals. SDO, SS, SCK from the PIC side are automatically set by this function.

Parameters
clockIt sets the clock source for the SPI module. Use the following constants [MCP4822_CLOCK_FOSC_64, MCP4822_CLOCK_FOSC_16, MCP4822_CLOCK_FOSC_4, MCP4822_CLOCK_FOSC_TMR2]
Returns
void
Note
By default the DAC outputs are set to high impedance.
Warning
You must properly set the Analog inputs which are muxed with the SPI lines.

Definition at line 61 of file MCP4822.c.

References MCP4822_CS_BIT, MCP4822_CS_PORT, MCP4822_CS_TRIS, MCP4822_DAC_A, MCP4822_DAC_B, MCP4822_GAIN_2, MCP4822_LDAC_BIT, MCP4822_LDAC_PORT, MCP4822_LDAC_TRIS, MCP4822_OUT_DEFAULT_VALUE, MCP4822_SCK_BIT, MCP4822_SCK_PORT, MCP4822_SCK_TRIS, MCP4822_SDO_BIT, MCP4822_SDO_PORT, MCP4822_SDO_TRIS, MCP4822_set_amplitude(), MCP4822_set_options(), and MCP4822_SHUTDOWN_ON.

void MCP4822_latch_pulse ( void  )

This function generates the LATCC pulse required to load the value to the DAC. It is required only in case the MSP4822_set_value is used. The function MSP4822_set_amplitude already include a call to this function.

Parameters
void
Returns
void

Definition at line 136 of file MCP4822.c.

References MCP4822_LDAC_BIT, MCP4822_LDAC_PORT, and MCP4822_PULSE_DELAY.

void MCP4822_set_amplitude ( unsigned int  value,
unsigned char  channel 
)

This function sets the output value of the DAC. By Gain 2 the value is expressed in mV, while by Gain 1 is 1/2 mV.

Parameters
valueIt sets the output value of the DAC [12 bits].
channelIt sets the channel to which the value refers to [MCP4822_DAC_A, MCP4822_DAC_B].
Returns
void
Note
This function does generate the LATCH pulse to load the DAC value right after the SPI writing.

Definition at line 125 of file MCP4822.c.

References MCP4822_latch_pulse(), and MCP4822_set_value().

void MCP4822_set_options ( unsigned char  gain,
unsigned char  power,
unsigned char  channel 
)

This function sets the DAC Options. The options which are set in the function, take place after the next writing to the corresponding DAC Channel.

Parameters
gainIt sets the output gain [MCP4822_GAIN_1, MCP4822_GAIN_2]
powerIt sets the channel power mode [MCP4822_SHUTDOWN_ON, MCP4822_SHUTDOWN_OFF]
channelIt sets the channel to which the options refer to [MCP4822_DAC_A, MCP4822_DAC_B]
Returns
void
Note
This function does NOT generate the LATCH pulse to load the DAC value right after the writing. The settings take place by the next amplitude writing.

Definition at line 149 of file MCP4822.c.

References gain_value_DAC_A_MCP4822, gain_value_DAC_B_MCP4822, MCP4822_DAC_A, MCP4822_DAC_B, power_mode_DAC_A_MCP4822, and power_mode_DAC_B_MCP4822.

void MCP4822_set_value ( unsigned int  value,
unsigned char  channel 
)

This function sets the output value of the DAC. By Gain 2 the value is expressed in mV, while by Gain 1 is 1/2 mV.

Parameters
valueIt sets the output value of the DAC [12 bits].
channelIt sets the channel to which the value refers to [MCP4822_DAC_A, MCP4822_DAC_B].
Returns
void
Note
This function does not generate the LATCH pulse to load the DAC values. This it means that the pulse latch pulse must be generated separately. This option is used in case both DAC must be set before loading the output value.

Definition at line 94 of file MCP4822.c.

References gain_value_DAC_A_MCP4822, gain_value_DAC_B_MCP4822, MCP4822_CS_BIT, MCP4822_CS_PORT, MCP4822_DAC_A, power_mode_DAC_A_MCP4822, and power_mode_DAC_B_MCP4822.