AVR Embedded Tutorial - SPI MCP4922: what changed
Revision 1 to 2. 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 | 70 | <thead> | |
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 71 | + | ||
| 77 | 72 | </thead> | |
| 78 | 73 | <tbody> | |
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 74 | + | ||
| 83 | 75 | <strong>1</strong> = Write to DACB</td> | |
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 76 | + | ||
| 89 | 77 | <strong>1</strong> = Buffered</td> | |
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 78 | + | ||
| 95 | 79 | <strong>1</strong> = 1x (VOUT = VREF * D / 4096)</td> | |
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 80 | + | ||
| 101 | 81 | <strong>1</strong> = Active mode operation. VOUT is available.</td> | |
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 82 | + | ||
| 108 | 83 | </tbody> | |
| 109 | - | ||
| 110 | 84 | ||
| 111 | 85 | ### Send to DAC | |
| 112 | 86 | ||
| 113 | 87 | ``` pascal | |
| 114 | 88 | procedure MCP4922sendValue(Value : UInt16; Channel : Byte); | |
| 115 | 89 | type | |
| 116 | 90 | TADC = bitpacked record | |
| 117 | 91 | Value : 0..4095; // bits 0-11 | |
| 118 | 92 | ShutDown, // bit 12 | |
| 119 | 93 | Gain, // bit 13 | |
| 120 | 94 | VRef, // bit 14 | |
| 121 | 95 | DAC : 0..1; // bit 15 | |
| 122 | 96 | end; | |
| 123 | 97 | ||
| 124 | 98 | begin | |
| 125 | 99 | with TADC(Value) do | |
| 126 | 100 | begin | |
| 127 | 101 | ShutDown := 1; | |
| 128 | 102 | Gain := 1; | |
| 129 | 103 | VRef := 0; | |
| 130 | 104 | Dac := Channel; | |
| 131 | 105 | end; | |
| 132 | 106 | ||
| 133 | 107 | SPIWrite(@Value, 2); | |
| 134 | 108 | end; | |
| 135 | 109 | ``` | |
| 136 | 110 | ||
| 137 | 111 | ## Example program | |
| 138 | 112 | ||
| 139 | 113 | 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. | |
| 140 | 114 | ||
| 141 | 115 | ### Constants and variables | |
| 142 | 116 | ||
| 143 | 117 | A counting variable that counts up to 4095. | |
| 144 | 118 | ||
| 145 | 119 | ``` pascal | |
| 146 | 120 | var | |
| 147 | 121 | z : Int16 ; | |
| 148 | 122 | ``` | |
| 149 | 123 | ||
| 150 | 124 | ### Main program | |
| 151 | 125 | ||
| 152 | 126 | Alternately write a value in DAC 0 & 1. | |
| 153 | 127 | ||
| 154 | 128 | ``` pascal | |
| 155 | 129 | begin | |
| 156 | 130 | // initialize SPI. | |
| 157 | 131 | SPIInit; | |
| 158 | 132 | ||
| 159 | 133 | repeat | |
| 160 | 134 | for z := 0 to 4095 do | |
| 161 | 135 | begin | |
| 162 | 136 | MCP4922sendValue(4095 - z, 0); | |
| 163 | 137 | MCP4922sendValue(z, 1); | |
| 164 | 138 | end; | |
| 165 | 139 | until 1 = 2; | |
| 166 | 140 | end. | |
| 167 | 141 | ``` | |
| 168 | 142 | ||
| 169 | 143 | # See also | |
| 170 | 144 | ||
| 171 | 145 | - Overview page - [[AVR Embedded Tutorials]]. | |
| 172 | 146 | - Use of the hardware SPI interface with an ATmega328 / Arduino - [[AVR Embedded Tutorial - SPI|SPI]]. | |
| 173 | 147 | ||
| 174 | 148 | --- | |
| 175 | 149 | *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).* |
