lab4 using referenced voltage from knob and generate voltage output from DAC mcp4922

Dependencies:   mbed

Committer:
sippasaeng
Date:
Fri Nov 02 03:13:15 2018 +0000
Revision:
1:b81dc534e7d7
Parent:
0:bdf668d4c05b
new;

Who changed what in which revision?

UserRevisionLine numberNew 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 1:b81dc534e7d7 9 DigitalOut MCP4922_CS(D10);//CS
sippasaeng 0:bdf668d4c05b 10
sippasaeng 1:b81dc534e7d7 11 AnalogIn knob(A1);
sippasaeng 0:bdf668d4c05b 12
sippasaeng 1:b81dc534e7d7 13 DigitalIn mode_s(D8);
sippasaeng 0:bdf668d4c05b 14
sippasaeng 0:bdf668d4c05b 15 //Digital Output
sippasaeng 1:b81dc534e7d7 16 DigitalOut MCP4922_LDAC(D8);
sippasaeng 1:b81dc534e7d7 17 DigitalOut MCP4922_SHDN(D9);
sippasaeng 0:bdf668d4c05b 18 void volt_out(double volt)
sippasaeng 0:bdf668d4c05b 19 {
sippasaeng 0:bdf668d4c05b 20
sippasaeng 0:bdf668d4c05b 21 uint16_t temp=((uint16_t)(volt*4096/3.33))&0x0fff;
sippasaeng 0:bdf668d4c05b 22 uint16_t MCP4922_Sent_Data = 0;
sippasaeng 1:b81dc534e7d7 23 MCP4922_Sent_Data = 0xB000|temp;
sippasaeng 0:bdf668d4c05b 24 //MCP4922_Sent_Data = 0x3fff;
sippasaeng 0:bdf668d4c05b 25 MCP4922_CS = 0;
sippasaeng 0:bdf668d4c05b 26
sippasaeng 0:bdf668d4c05b 27
sippasaeng 0:bdf668d4c05b 28 //sent data buffer to mcp4922
sippasaeng 0:bdf668d4c05b 29 MCP4922_SPI.write(MCP4922_Sent_Data);
sippasaeng 0:bdf668d4c05b 30
sippasaeng 0:bdf668d4c05b 31 //set CS to High to End MCP4922 SPI Interface
sippasaeng 0:bdf668d4c05b 32 MCP4922_CS = 1;
sippasaeng 0:bdf668d4c05b 33
sippasaeng 0:bdf668d4c05b 34 //set LDAC to LOW to Load data to output
sippasaeng 0:bdf668d4c05b 35 MCP4922_LDAC = 0 ;
sippasaeng 0:bdf668d4c05b 36
sippasaeng 0:bdf668d4c05b 37 //wait at least 100ns = 0.1 microsec ..... So set at 1 microsec
sippasaeng 0:bdf668d4c05b 38 wait_us(1);
sippasaeng 0:bdf668d4c05b 39
sippasaeng 0:bdf668d4c05b 40 //set LDAC to High to prepare next output
sippasaeng 0:bdf668d4c05b 41 MCP4922_LDAC = 1 ;
sippasaeng 0:bdf668d4c05b 42 }
sippasaeng 0:bdf668d4c05b 43 int main()
sippasaeng 0:bdf668d4c05b 44 {
sippasaeng 0:bdf668d4c05b 45 /******************************
sippasaeng 0:bdf668d4c05b 46 ***** setup *******
sippasaeng 0:bdf668d4c05b 47 ******************************/
sippasaeng 1:b81dc534e7d7 48 uint16_t volt_temp=0;
sippasaeng 0:bdf668d4c05b 49 /////////setup SPI//////////
sippasaeng 0:bdf668d4c05b 50 //set CS High
sippasaeng 0:bdf668d4c05b 51 MCP4922_CS=1;
sippasaeng 0:bdf668d4c05b 52
sippasaeng 0:bdf668d4c05b 53 //set SPI mode [1,1]=> mode 3 @16bit
sippasaeng 0:bdf668d4c05b 54 MCP4922_SPI.format(16,3);
sippasaeng 0:bdf668d4c05b 55
sippasaeng 0:bdf668d4c05b 56 //set SPI Frequency @20MHz
sippasaeng 1:b81dc534e7d7 57 MCP4922_SPI.frequency(1000000);
sippasaeng 0:bdf668d4c05b 58
sippasaeng 0:bdf668d4c05b 59 //set Load DAC High (inactive)
sippasaeng 0:bdf668d4c05b 60 MCP4922_LDAC = 1;
sippasaeng 0:bdf668d4c05b 61
sippasaeng 0:bdf668d4c05b 62 //set shutdown DAC High (inactive)
sippasaeng 0:bdf668d4c05b 63 MCP4922_SHDN = 1;
sippasaeng 0:bdf668d4c05b 64
sippasaeng 0:bdf668d4c05b 65 //loop
sippasaeng 0:bdf668d4c05b 66 while(1) {
sippasaeng 1:b81dc534e7d7 67 /* if(mode_s)volt_temp=uint16_t(3.33-knob.read()*3.33);
sippasaeng 1:b81dc534e7d7 68 else volt_temp=uint16_t(knob.read()*3.33);
sippasaeng 1:b81dc534e7d7 69 volt_out(volt_temp); */
sippasaeng 1:b81dc534e7d7 70 volt_out(2.5);
sippasaeng 1:b81dc534e7d7 71 }
sippasaeng 0:bdf668d4c05b 72 }