lab4 using referenced voltage from knob and generate voltage output from DAC mcp4922
Dependencies: mbed
main.cpp@0:bdf668d4c05b, 2018-11-02 (annotated)
- Committer:
- sippasaeng
- Date:
- Fri Nov 02 02:15:49 2018 +0000
- Revision:
- 0:bdf668d4c05b
- Child:
- 1:b81dc534e7d7
1st version;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sippasaeng | 0:bdf668d4c05b | 1 | #include "mbed.h" |
sippasaeng | 0:bdf668d4c05b | 2 | #include "math.h" |
sippasaeng | 0:bdf668d4c05b | 3 | /********************** |
sippasaeng | 0:bdf668d4c05b | 4 | * MCP4922 |
sippasaeng | 0:bdf668d4c05b | 5 | **********************/ |
sippasaeng | 0:bdf668d4c05b | 6 | |
sippasaeng | 0:bdf668d4c05b | 7 | ///SPI Interface |
sippasaeng | 0:bdf668d4c05b | 8 | SPI MCP4922_SPI(D11,D12,D13);//MOSI ,MISO ,SCK |
sippasaeng | 0:bdf668d4c05b | 9 | DigitalOut MCP4922_CS(D7);//CS |
sippasaeng | 0:bdf668d4c05b | 10 | |
sippasaeng | 0:bdf668d4c05b | 11 | |
sippasaeng | 0:bdf668d4c05b | 12 | |
sippasaeng | 0:bdf668d4c05b | 13 | //Digital Output |
sippasaeng | 0:bdf668d4c05b | 14 | DigitalOut MCP4922_LDAC(D4); |
sippasaeng | 0:bdf668d4c05b | 15 | DigitalOut MCP4922_SHDN(D6); |
sippasaeng | 0:bdf668d4c05b | 16 | void volt_out(double volt) |
sippasaeng | 0:bdf668d4c05b | 17 | { |
sippasaeng | 0:bdf668d4c05b | 18 | |
sippasaeng | 0:bdf668d4c05b | 19 | uint16_t temp=((uint16_t)(volt*4096/3.33))&0x0fff; |
sippasaeng | 0:bdf668d4c05b | 20 | uint16_t MCP4922_Sent_Data = 0; |
sippasaeng | 0:bdf668d4c05b | 21 | MCP4922_Sent_Data = 0x3000|temp; |
sippasaeng | 0:bdf668d4c05b | 22 | //MCP4922_Sent_Data = 0x3fff; |
sippasaeng | 0:bdf668d4c05b | 23 | MCP4922_CS = 0; |
sippasaeng | 0:bdf668d4c05b | 24 | |
sippasaeng | 0:bdf668d4c05b | 25 | |
sippasaeng | 0:bdf668d4c05b | 26 | //sent data buffer to mcp4922 |
sippasaeng | 0:bdf668d4c05b | 27 | MCP4922_SPI.write(MCP4922_Sent_Data); |
sippasaeng | 0:bdf668d4c05b | 28 | |
sippasaeng | 0:bdf668d4c05b | 29 | //set CS to High to End MCP4922 SPI Interface |
sippasaeng | 0:bdf668d4c05b | 30 | MCP4922_CS = 1; |
sippasaeng | 0:bdf668d4c05b | 31 | |
sippasaeng | 0:bdf668d4c05b | 32 | //set LDAC to LOW to Load data to output |
sippasaeng | 0:bdf668d4c05b | 33 | MCP4922_LDAC = 0 ; |
sippasaeng | 0:bdf668d4c05b | 34 | |
sippasaeng | 0:bdf668d4c05b | 35 | //wait at least 100ns = 0.1 microsec ..... So set at 1 microsec |
sippasaeng | 0:bdf668d4c05b | 36 | wait_us(1); |
sippasaeng | 0:bdf668d4c05b | 37 | |
sippasaeng | 0:bdf668d4c05b | 38 | //set LDAC to High to prepare next output |
sippasaeng | 0:bdf668d4c05b | 39 | MCP4922_LDAC = 1 ; |
sippasaeng | 0:bdf668d4c05b | 40 | } |
sippasaeng | 0:bdf668d4c05b | 41 | int main() |
sippasaeng | 0:bdf668d4c05b | 42 | { |
sippasaeng | 0:bdf668d4c05b | 43 | /****************************** |
sippasaeng | 0:bdf668d4c05b | 44 | ***** setup ******* |
sippasaeng | 0:bdf668d4c05b | 45 | ******************************/ |
sippasaeng | 0:bdf668d4c05b | 46 | |
sippasaeng | 0:bdf668d4c05b | 47 | /////////setup SPI////////// |
sippasaeng | 0:bdf668d4c05b | 48 | //set CS High |
sippasaeng | 0:bdf668d4c05b | 49 | MCP4922_CS=1; |
sippasaeng | 0:bdf668d4c05b | 50 | |
sippasaeng | 0:bdf668d4c05b | 51 | //set SPI mode [1,1]=> mode 3 @16bit |
sippasaeng | 0:bdf668d4c05b | 52 | MCP4922_SPI.format(16,3); |
sippasaeng | 0:bdf668d4c05b | 53 | |
sippasaeng | 0:bdf668d4c05b | 54 | //set SPI Frequency @20MHz |
sippasaeng | 0:bdf668d4c05b | 55 | MCP4922_SPI.frequency(20000000); |
sippasaeng | 0:bdf668d4c05b | 56 | |
sippasaeng | 0:bdf668d4c05b | 57 | //set Load DAC High (inactive) |
sippasaeng | 0:bdf668d4c05b | 58 | MCP4922_LDAC = 1; |
sippasaeng | 0:bdf668d4c05b | 59 | |
sippasaeng | 0:bdf668d4c05b | 60 | //set shutdown DAC High (inactive) |
sippasaeng | 0:bdf668d4c05b | 61 | MCP4922_SHDN = 1; |
sippasaeng | 0:bdf668d4c05b | 62 | |
sippasaeng | 0:bdf668d4c05b | 63 | //loop |
sippasaeng | 0:bdf668d4c05b | 64 | while(1) { |
sippasaeng | 0:bdf668d4c05b | 65 | for(double x=0; x<2*3.141592; x+=0.00001) { |
sippasaeng | 0:bdf668d4c05b | 66 | volt_out((sin(x)+1.00)*3.33/2.00); |
sippasaeng | 0:bdf668d4c05b | 67 | } |
sippasaeng | 0:bdf668d4c05b | 68 | |
sippasaeng | 0:bdf668d4c05b | 69 | } |
sippasaeng | 0:bdf668d4c05b | 70 | } |