![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
dac subsystem
DAC/DAC.cpp@1:8056d0c97ee2, 2014-04-01 (annotated)
- Committer:
- dzl5187
- Date:
- Tue Apr 01 04:47:27 2014 +0000
- Revision:
- 1:8056d0c97ee2
- Parent:
- 0:c7adfe979a5b
first commit;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
dzl5187 | 0:c7adfe979a5b | 1 | #include "mbed.h" |
dzl5187 | 0:c7adfe979a5b | 2 | #include "mcp4725.h" |
dzl5187 | 0:c7adfe979a5b | 3 | #include "DAC.h" |
dzl5187 | 0:c7adfe979a5b | 4 | #include "math.h" |
dzl5187 | 0:c7adfe979a5b | 5 | #include "log.h" |
dzl5187 | 0:c7adfe979a5b | 6 | |
dzl5187 | 0:c7adfe979a5b | 7 | #define PI 3.1415926 |
dzl5187 | 0:c7adfe979a5b | 8 | MCP4725 controller(p9, p10, MCP4725::Fast400kHz, DEVICE_ADDR_BIT); |
dzl5187 | 0:c7adfe979a5b | 9 | Timer DAC_timer; |
dzl5187 | 0:c7adfe979a5b | 10 | |
dzl5187 | 0:c7adfe979a5b | 11 | bool volatile DAC_running = false; |
dzl5187 | 0:c7adfe979a5b | 12 | float p_range = 0; |
dzl5187 | 0:c7adfe979a5b | 13 | enum FREQ p_freq = FREQ_10HZ; |
dzl5187 | 0:c7adfe979a5b | 14 | enum FORM p_form = FORM_SQUARE; |
dzl5187 | 0:c7adfe979a5b | 15 | |
dzl5187 | 0:c7adfe979a5b | 16 | void DAC_start(){ |
dzl5187 | 0:c7adfe979a5b | 17 | DAC_running = true; |
dzl5187 | 0:c7adfe979a5b | 18 | } |
dzl5187 | 0:c7adfe979a5b | 19 | |
dzl5187 | 0:c7adfe979a5b | 20 | void DAC_stop(){ |
dzl5187 | 0:c7adfe979a5b | 21 | DAC_running = false; |
dzl5187 | 0:c7adfe979a5b | 22 | } |
dzl5187 | 0:c7adfe979a5b | 23 | void DAC_set(float range, enum FREQ freq, enum FORM form){ |
dzl5187 | 0:c7adfe979a5b | 24 | p_range = range; |
dzl5187 | 0:c7adfe979a5b | 25 | p_freq = freq; |
dzl5187 | 0:c7adfe979a5b | 26 | p_form = form; |
dzl5187 | 0:c7adfe979a5b | 27 | } |
dzl5187 | 0:c7adfe979a5b | 28 | |
dzl5187 | 0:c7adfe979a5b | 29 | void DAC_generate(){ |
dzl5187 | 0:c7adfe979a5b | 30 | float Vout = 0; |
dzl5187 | 0:c7adfe979a5b | 31 | int time_us = 0; |
dzl5187 | 0:c7adfe979a5b | 32 | DAC_timer.start(); |
dzl5187 | 0:c7adfe979a5b | 33 | while( DAC_running ){ |
dzl5187 | 0:c7adfe979a5b | 34 | if( p_form == FORM_SQUARE ){ |
dzl5187 | 0:c7adfe979a5b | 35 | if( p_freq == FREQ_10HZ ){ |
dzl5187 | 0:c7adfe979a5b | 36 | //generate square wave at 10HZ |
dzl5187 | 0:c7adfe979a5b | 37 | if( (int)time_us % 1000000 <= 500000){ // T = 100 ms |
dzl5187 | 0:c7adfe979a5b | 38 | Vout = p_range; |
dzl5187 | 0:c7adfe979a5b | 39 | } |
dzl5187 | 0:c7adfe979a5b | 40 | else |
dzl5187 | 0:c7adfe979a5b | 41 | Vout = 0; |
dzl5187 | 0:c7adfe979a5b | 42 | } |
dzl5187 | 0:c7adfe979a5b | 43 | else{ |
dzl5187 | 0:c7adfe979a5b | 44 | //generate square wave at 1HZ |
dzl5187 | 0:c7adfe979a5b | 45 | if( (int)time_us % 1000000 <= 500000) //T = 1 s |
dzl5187 | 0:c7adfe979a5b | 46 | Vout = p_range; |
dzl5187 | 0:c7adfe979a5b | 47 | else |
dzl5187 | 0:c7adfe979a5b | 48 | Vout = 0; |
dzl5187 | 0:c7adfe979a5b | 49 | } |
dzl5187 | 0:c7adfe979a5b | 50 | } |
dzl5187 | 0:c7adfe979a5b | 51 | else if ( p_form == FORM_SIN){ |
dzl5187 | 0:c7adfe979a5b | 52 | if( p_freq == FREQ_10HZ ){ |
dzl5187 | 0:c7adfe979a5b | 53 | Vout = p_range / 2.0 * (sin( 2.0 * PI * 10.0 * time_us / 1000000.0) + 1); |
dzl5187 | 0:c7adfe979a5b | 54 | } |
dzl5187 | 0:c7adfe979a5b | 55 | else{ |
dzl5187 | 0:c7adfe979a5b | 56 | //generate sin wave at 1HZ |
dzl5187 | 0:c7adfe979a5b | 57 | Vout = p_range / 2.0 * (sin( 2.0 * PI * 1.0 * time_us / 1000000.0) + 1); |
dzl5187 | 0:c7adfe979a5b | 58 | } |
dzl5187 | 0:c7adfe979a5b | 59 | } |
dzl5187 | 0:c7adfe979a5b | 60 | wait_us(1000); //delay 1 ms |
dzl5187 | 0:c7adfe979a5b | 61 | time_us += 1000; |
dzl5187 | 0:c7adfe979a5b | 62 | controller.write(MCP4725::Normal, (0xFFF * (Vout/VDD_VOLTAGE) ), false); |
dzl5187 | 0:c7adfe979a5b | 63 | } |
dzl5187 | 0:c7adfe979a5b | 64 | DAC_timer.stop(); |
dzl5187 | 0:c7adfe979a5b | 65 | DAC_timer.reset(); |
dzl5187 | 0:c7adfe979a5b | 66 | |
dzl5187 | 0:c7adfe979a5b | 67 | } |