Johnny Bravo
/
dac_controller
dac controller for EE300 project
Revision 1:4bd950ab8756, committed 2014-04-04
- Comitter:
- dzl5187
- Date:
- Fri Apr 04 18:48:23 2014 +0000
- Parent:
- 0:ce795bcc930d
- Commit message:
- dac sub-system;
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DAC/DAC.cpp Fri Apr 04 18:48:23 2014 +0000 @@ -0,0 +1,67 @@ +#include "mbed.h" +#include "mcp4725.h" +#include "DAC.h" +#include "math.h" +#include "log.h" + +#define PI 3.1415926 +MCP4725 controller(p9, p10, MCP4725::Fast400kHz, DEVICE_ADDR_BIT); +Timer DAC_timer; + +bool volatile DAC_running = false; +float p_range = 0; +enum FREQ p_freq = FREQ_10HZ; +enum FORM p_form = FORM_SQUARE; + +void DAC_start(){ + DAC_running = true; +} + +void DAC_stop(){ + DAC_running = false; +} +void DAC_set(float range, enum FREQ freq, enum FORM form){ + p_range = range; + p_freq = freq; + p_form = form; +} + +void DAC_generate(){ + float Vout = 0; + int time_us = 0; + DAC_timer.start(); + while( DAC_running ){ + if( p_form == FORM_SQUARE ){ + if( p_freq == FREQ_10HZ ){ + //generate square wave at 10HZ + if( (int)time_us % 1000000 <= 500000){ // T = 100 ms + Vout = p_range; + } + else + Vout = 0; + } + else{ + //generate square wave at 1HZ + if( (int)time_us % 1000000 <= 500000) //T = 1 s + Vout = p_range; + else + Vout = 0; + } + } + else if ( p_form == FORM_SIN){ + if( p_freq == FREQ_10HZ ){ + Vout = p_range / 2.0 * (sin( 2.0 * PI * 10.0 * time_us / 1000000.0) + 1); + } + else{ + //generate sin wave at 1HZ + Vout = p_range / 2.0 * (sin( 2.0 * PI * 1.0 * time_us / 1000000.0) + 1); + } + } + wait_us(1000); //delay 1 ms + time_us += 1000; + controller.write(MCP4725::Normal, (0xFFF * (Vout/VDD_VOLTAGE) ), false); + } + DAC_timer.stop(); + DAC_timer.reset(); + +} \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DAC/DAC.h Fri Apr 04 18:48:23 2014 +0000 @@ -0,0 +1,39 @@ + +#ifndef DAC_H +#define DAC_H + +#include "mbed.h" +#include "mcp4725.h" + +//pins for connection +#define PIN_SDA p9 +#define PIN_SCL p10 +#define DEVICE_ADDR_BIT 0 +//ref voltage +#define VDD_VOLTAGE 5.04 + +enum FREQ { FREQ_10HZ, FREQ_1HZ}; +enum FORM { FORM_SQUARE, FORM_SIN}; + +/** + Funciton: DAC_start + Description: start generating the analog signal +*/ +void DAC_start(); +/** + Funciton: DAC_stop + Description: stop generating the analog signal +*/ +void DAC_stop(); +/** + Funciton: DAC_generate + Description: calculate the Vout accoding to the configuration and send command to DAC chip +*/ +void DAC_generate(); +/** + Funciton: DAC_set + Description: set the configuration( amplitude, frequency and form of the signal) +*/ +void DAC_set(float range, enum FREQ freq, enum FORM form); + +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DAC/MCP4725.lib Fri Apr 04 18:48:23 2014 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/dzl5187/code/MCP4725/#35e3e80b804c
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log/log.cpp Fri Apr 04 18:48:23 2014 +0000 @@ -0,0 +1,27 @@ +#include "log.h" +#include "stdio.h" +#include "mbed.h" +#include "stdarg.h" +Serial pc(USBTX, USBRX); +void vlogMessage(const char *,va_list); +/** + Function: logMessage + Description: output the message(can be formatted string) to PC +*/ +void logMessage(const char *fmt,...){ + va_list args; + va_start(args, fmt); + vlogMessage( fmt, args ); + va_end(args); +} + +void vlogMessage(const char *fmt,va_list args){ + char buffer[255]; + char msg[255]; + //get time stamp + sprintf(buffer, "Time[seconds]: %.3f : ", programTimer.read_ms()/1000.0); + + vsprintf( msg, fmt, args ); + strcat( buffer, msg); + pc.printf("%s \n", buffer); +}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/log/log.h Fri Apr 04 18:48:23 2014 +0000 @@ -0,0 +1,14 @@ + +#ifndef LOG_H +#define LOG_H +#include "mbed.h" + + +extern Timer programTimer; + +/** + Function: logMessage + Description: output the message(can be formatted string) to PC +*/ +void logMessage(const char *fmt,...); +#endif \ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/main.cpp Fri Apr 04 18:48:23 2014 +0000 @@ -0,0 +1,19 @@ +#include "mbed.h" +#include "log.h" +#include "DAC.h" +#include "mcp4725.h" +Timer programTimer; +//Timer DACC_timer; +//DigitalOut testLed(LED1); + +int main() +{ + + DAC_set(3.0, FREQ_1HZ, FORM_SQUARE); + DAC_start(); + while(1) { + DAC_generate(); + } + + +} \ No newline at end of file