AVR Embedded Tutorial - SPI MCP4922: what changed
Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | # Control an MCP4922 12 bit DAC via SPI | |
| 2 | 2 | ||
| 3 | 3 | This code is for an Atmega328 / Arduino with a 16MHz clock and an SPI compatible [MCP4922](https://www.microchip.com/wwwproducts/en/MCP4922) Digital/Analog Converter (DAC). | |
| 4 | 4 | ||
| 5 | 5 | The following functions are described in other tutorials: | |
| 6 | 6 | ||
| 7 | 7 | - [[AVR Embedded Tutorial - Simple GPIO on and off output|GPIO]] - Output/Input - How do I access the GPIO on the AVR. | |
| 8 | 8 | - [[AVR Embedded Tutorial - SPI|SPI]] - use of the hardware SPI interface. | |
| 9 | 9 | ||
| 10 | 10 | This example shows how to control the MCP4922 DAC via SPI. | |
| 11 | 11 | ||
| 12 | 12 | ## Functions for the 74HC595 shift register | |
| 13 | 13 | ||
| 14 | 14 | ### Constants | |
| 15 | 15 | ||
| 16 | 16 | Pins which are required at PORTB for SPI. | |
| 17 | 17 | ||
| 18 | 18 | ``` pascal | |
| 19 | 19 | type | |
| 20 | 20 | TSPIGPIO = bitpacked record | |
| 21 | 21 | p0, p1, SlaveSelect, MOSI, MISO, Clock, p6, p7 : boolean; | |
| 22 | 22 | end; | |
| 23 | 23 | ||
| 24 | 24 | var | |
| 25 | 25 | SPI_PORT : TSPIGPIO absolute PORTB; | |
| 26 | 26 | SPI_DDR : TSPIGPIO absolute DDRB; | |
| 27 | 27 | ``` | |
| 28 | 28 | ||
| 29 | 29 | ### Initialize SPI | |
| 30 | 30 | ||
| 31 | 31 | ``` pascal | |
| 32 | 32 | procedure SPIInit; | |
| 33 | 33 | begin | |
| 34 | 34 | // Setup SPI port for output | |
| 35 | 35 | SPI_DDR.MOSI := True; | |
| 36 | 36 | SPI_DDR.Clock := True; | |
| 37 | 37 | SPI_DDR.SlaveSelect := True; | |
| 38 | 38 | ||
| 39 | 39 | SPCR := (1 shl SPE) or (0 shl SPIE) or (0 shl DORD) or (1 shl MSTR) or (0 shl CPOL) or (0 shl CPHA) or (%01 shl SPR); | |
| 40 | 40 | SPSR := 1 shl SPI2X; // SCK x 2 on 1 MHz | |
| 41 | 41 | end; | |
| 42 | 42 | ``` | |
| 43 | 43 | ||
| 44 | 44 | ### Write SPI | |
| 45 | 45 | ||
| 46 | 46 | ``` pascal | |
| 47 | 47 | procedure SPIWrite(p : PByte; len : byte); | |
| 48 | 48 | var | |
| 49 | 49 | i : byte; | |
| 50 | 50 | begin | |
| 51 | 51 | SPI_Port.SlaveSelect := False; | |
| 52 | 52 | ||
| 53 | 53 | for i := len - 1 downto 0 do | |
| 54 | 54 | begin | |
| 55 | 55 | SPDR := p[i]; | |
| 56 | 56 | ||
| 57 | 57 | while(SPSR and (1 shl SPIF)) = 0 do | |
| 58 | 58 | begin | |
| 59 | 59 | end; | |
| 60 | 60 | end; | |
| 61 | 61 | ||
| 62 | 62 | SPI_Port.SlaveSelect := True; | |
| 63 | 63 | end; | |
| 64 | 64 | ``` | |
| 65 | 65 | ||
| 66 | 66 | ## MCP4922 functions | |
| 67 | 67 | ||
| 68 | 68 | Bit assignments for the MCP4922: | |
| 69 | 69 | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 70 | + | | Bit | Name | Description | | |
| 71 | + | |---|---|---| | |
| 72 | + | | 15 | **A/B:** DACA or DACB Selection bit | **0** = Write to DACA · **1** = Write to DACB | | |
| 73 | + | | 14 | **BUF:** Vref Input Buffer Controlbit | **0** = Unbuffered · **1** = Buffered | | |
| 74 | + | | 13 | **GA:**Output Gain Selection bit | **0** = 2x (VOUT = 2 * VREF * D / 4096) · **1** = 1x (VOUT = VREF * D / 4096) | | |
| 75 | + | | 12 | **SHDN:** Output Shutdown Control bit | **0** = Shutdown the selected DAC channel. Analog output is not available at the channel that was shut down. VOUT pin is connected to 500 kOhm(typical) · **1** = Active mode operation. VOUT is available. | | |
| 76 | + | | 11:0 | **D11:D0:** DAC Input Data bits. Bit x is ignored. | | | |
| 84 | 77 | ||
| 85 | 78 | ### Send to DAC | |
| 86 | 79 | ||
| 87 | 80 | ``` pascal | |
| 88 | 81 | procedure MCP4922sendValue(Value : UInt16; Channel : Byte); | |
| 89 | 82 | type | |
| 90 | 83 | TADC = bitpacked record | |
| 91 | 84 | Value : 0..4095; // bits 0-11 | |
| 92 | 85 | ShutDown, // bit 12 | |
| 93 | 86 | Gain, // bit 13 | |
| 94 | 87 | VRef, // bit 14 | |
| 95 | 88 | DAC : 0..1; // bit 15 | |
| 96 | 89 | end; | |
| 97 | 90 | ||
| 98 | 91 | begin | |
| 99 | 92 | with TADC(Value) do | |
| 100 | 93 | begin | |
| 101 | 94 | ShutDown := 1; | |
| 102 | 95 | Gain := 1; | |
| 103 | 96 | VRef := 0; | |
| 104 | 97 | Dac := Channel; | |
| 105 | 98 | end; | |
| 106 | 99 | ||
| 107 | 100 | SPIWrite(@Value, 2); | |
| 108 | 101 | end; | |
| 109 | 102 | ``` | |
| 110 | 103 | ||
| 111 | 104 | ## Example program | |
| 112 | 105 | ||
| 113 | 106 | This example shows the output of two analog values from the DAC. Two LEDs connected to the DAC flash alternately with a very smooth transition. | |
| 114 | 107 | ||
| 115 | 108 | ### Constants and variables | |
| 116 | 109 | ||
| 117 | 110 | A counting variable that counts up to 4095. | |
| 118 | 111 | ||
| 119 | 112 | ``` pascal | |
| 120 | 113 | var | |
| 121 | 114 | z : Int16 ; | |
| 122 | 115 | ``` | |
| 123 | 116 | ||
| 124 | 117 | ### Main program | |
| 125 | 118 | ||
| 126 | 119 | Alternately write a value in DAC 0 & 1. | |
| 127 | 120 | ||
| 128 | 121 | ``` pascal | |
| 129 | 122 | begin | |
| 130 | 123 | // initialize SPI. | |
| 131 | 124 | SPIInit; | |
| 132 | 125 | ||
| 133 | 126 | repeat | |
| 134 | 127 | for z := 0 to 4095 do | |
| 135 | 128 | begin | |
| 136 | 129 | MCP4922sendValue(4095 - z, 0); | |
| 137 | 130 | MCP4922sendValue(z, 1); | |
| 138 | 131 | end; | |
| 139 | 132 | until 1 = 2; | |
| 140 | 133 | end. | |
| 141 | 134 | ``` | |
| 142 | 135 | ||
| 143 | 136 | # See also | |
| 144 | 137 | ||
| 145 | 138 | - Overview page - [[AVR Embedded Tutorials]]. | |
| 146 | 139 | - Use of the hardware SPI interface with an ATmega328 / Arduino - [[AVR Embedded Tutorial - SPI|SPI]]. | |
| 147 | 140 | ||
| 148 | 141 | --- | |
| 149 | 142 | *From the Free Pascal wiki, page "AVR Embedded Tutorial - SPI MCP4922", as it stood on 2020-03-08 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* |
