data type bugfix
Fork of SoftMCP4922 by
SoftMCP4922.cpp@2:79d0ecc7375b, 2013-10-31 (annotated)
- Committer:
- doctorura
- Date:
- Thu Oct 31 15:07:08 2013 +0000
- Revision:
- 2:79d0ecc7375b
- Parent:
- 1:1f8a7ae7dc8b
- Child:
- 3:060c219353b8
Vref added
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
magnetone | 0:aeca3abe0194 | 1 | /* |
magnetone | 0:aeca3abe0194 | 2 | * MCP4922 (Soft SPI) Library |
magnetone | 0:aeca3abe0194 | 3 | * 2013 Magnetone |
magnetone | 0:aeca3abe0194 | 4 | */ |
magnetone | 0:aeca3abe0194 | 5 | |
magnetone | 0:aeca3abe0194 | 6 | #include "SoftMCP4922.h" |
magnetone | 0:aeca3abe0194 | 7 | |
magnetone | 0:aeca3abe0194 | 8 | SoftMCP4922::SoftMCP4922( PinName sck, PinName sdi, PinName cs, PinName ldac, PinName shdn, int ch): |
magnetone | 0:aeca3abe0194 | 9 | m_sck( sck ), |
magnetone | 0:aeca3abe0194 | 10 | m_sdi( sdi ), |
magnetone | 0:aeca3abe0194 | 11 | m_cs( cs ), |
magnetone | 0:aeca3abe0194 | 12 | m_ldac( ldac ), |
magnetone | 0:aeca3abe0194 | 13 | m_shdn( shdn ), |
magnetone | 0:aeca3abe0194 | 14 | m_ch( ch ) |
magnetone | 0:aeca3abe0194 | 15 | { |
magnetone | 0:aeca3abe0194 | 16 | |
magnetone | 0:aeca3abe0194 | 17 | m_shdn = 1; // non shutdown |
magnetone | 0:aeca3abe0194 | 18 | m_sck = 0; // init |
magnetone | 0:aeca3abe0194 | 19 | |
magnetone | 0:aeca3abe0194 | 20 | // out vcc/2 |
magnetone | 0:aeca3abe0194 | 21 | write_u12( 2048 ); |
magnetone | 0:aeca3abe0194 | 22 | |
magnetone | 0:aeca3abe0194 | 23 | } |
magnetone | 0:aeca3abe0194 | 24 | |
magnetone | 0:aeca3abe0194 | 25 | void SoftMCP4922::write_u16( unsigned short value ) |
magnetone | 0:aeca3abe0194 | 26 | { |
magnetone | 0:aeca3abe0194 | 27 | dacControl( value >> 4 ); |
magnetone | 0:aeca3abe0194 | 28 | } |
magnetone | 0:aeca3abe0194 | 29 | |
magnetone | 0:aeca3abe0194 | 30 | void SoftMCP4922::write_u12( unsigned short value ) |
magnetone | 0:aeca3abe0194 | 31 | { |
magnetone | 0:aeca3abe0194 | 32 | dacControl( value ); |
magnetone | 0:aeca3abe0194 | 33 | } |
magnetone | 0:aeca3abe0194 | 34 | |
magnetone | 0:aeca3abe0194 | 35 | void SoftMCP4922::write_u10( unsigned short value ) |
magnetone | 0:aeca3abe0194 | 36 | { |
magnetone | 0:aeca3abe0194 | 37 | dacControl( value << 2 ); |
magnetone | 0:aeca3abe0194 | 38 | } |
magnetone | 0:aeca3abe0194 | 39 | |
doctorura | 1:1f8a7ae7dc8b | 40 | void SoftMCP4922::write_u8( unsigned short value ) |
magnetone | 0:aeca3abe0194 | 41 | { |
magnetone | 0:aeca3abe0194 | 42 | dacControl( value << 4 ); |
magnetone | 0:aeca3abe0194 | 43 | } |
magnetone | 0:aeca3abe0194 | 44 | |
magnetone | 0:aeca3abe0194 | 45 | // DAC control function |
doctorura | 1:1f8a7ae7dc8b | 46 | void SoftMCP4922::dacControl( unsigned short value ) |
magnetone | 0:aeca3abe0194 | 47 | { |
magnetone | 0:aeca3abe0194 | 48 | int i ; |
magnetone | 0:aeca3abe0194 | 49 | |
magnetone | 0:aeca3abe0194 | 50 | // dac setting start |
magnetone | 0:aeca3abe0194 | 51 | m_cs = 0; |
magnetone | 0:aeca3abe0194 | 52 | m_ldac = 1; |
magnetone | 0:aeca3abe0194 | 53 | |
magnetone | 0:aeca3abe0194 | 54 | // channel select |
magnetone | 0:aeca3abe0194 | 55 | if( m_ch == 0 ){ |
magnetone | 0:aeca3abe0194 | 56 | m_sdi = 0; |
magnetone | 0:aeca3abe0194 | 57 | }else{ |
magnetone | 0:aeca3abe0194 | 58 | m_sdi = 1; |
magnetone | 0:aeca3abe0194 | 59 | } |
magnetone | 0:aeca3abe0194 | 60 | m_sck = 1; |
magnetone | 0:aeca3abe0194 | 61 | m_sck = 0; |
magnetone | 0:aeca3abe0194 | 62 | |
doctorura | 2:79d0ecc7375b | 63 | // vref buffer on |
doctorura | 2:79d0ecc7375b | 64 | m_sdi = 1; |
magnetone | 0:aeca3abe0194 | 65 | m_sck = 1; |
magnetone | 0:aeca3abe0194 | 66 | m_sck = 0; |
magnetone | 0:aeca3abe0194 | 67 | |
magnetone | 0:aeca3abe0194 | 68 | // output gain = 1 |
magnetone | 0:aeca3abe0194 | 69 | m_sdi = 1; |
magnetone | 0:aeca3abe0194 | 70 | m_sck = 1; |
magnetone | 0:aeca3abe0194 | 71 | m_sck = 0; |
magnetone | 0:aeca3abe0194 | 72 | |
magnetone | 0:aeca3abe0194 | 73 | // analog output enable |
magnetone | 0:aeca3abe0194 | 74 | m_sdi = 1; |
magnetone | 0:aeca3abe0194 | 75 | m_sck = 1; |
magnetone | 0:aeca3abe0194 | 76 | m_sck = 0; |
magnetone | 0:aeca3abe0194 | 77 | |
magnetone | 0:aeca3abe0194 | 78 | // DAC data out |
doctorura | 2:79d0ecc7375b | 79 | for( i=11 ; i>=0 ; i-- ){ |
doctorura | 2:79d0ecc7375b | 80 | if( ( ( value >> i ) & 0x1 ) == 1 ) |
magnetone | 0:aeca3abe0194 | 81 | m_sdi = 1; |
magnetone | 0:aeca3abe0194 | 82 | else |
magnetone | 0:aeca3abe0194 | 83 | m_sdi = 0; |
magnetone | 0:aeca3abe0194 | 84 | m_sck = 1; |
magnetone | 0:aeca3abe0194 | 85 | m_sck = 0; |
magnetone | 0:aeca3abe0194 | 86 | } |
magnetone | 0:aeca3abe0194 | 87 | |
magnetone | 0:aeca3abe0194 | 88 | // analog out |
magnetone | 0:aeca3abe0194 | 89 | m_cs = 1; |
magnetone | 0:aeca3abe0194 | 90 | m_ldac = 0; |
magnetone | 0:aeca3abe0194 | 91 | |
magnetone | 0:aeca3abe0194 | 92 | } |
magnetone | 0:aeca3abe0194 | 93 |