MCP4726 sample

Dependencies:   AQM0802 MCP4726 mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //**********************
00002 // DDS with DAC 12bits
00003 // MCP4726 sample for mbed
00004 //
00005 // output
00006 // (1)sin wave with DDS, 1Hz-1KHz
00007 //
00008 // (C)Copyright 2014 All rights reserved by Y.Onodera
00009 // http://einstlab.web.fc2.com
00010 //**********************
00011 #include "mbed.h"
00012 #include "AQM0802.h"
00013 #include "MCP4726.h"
00014 
00015 #define PI 3.14159265
00016 // sampling frequency for DDS
00017 // 400KHz
00018 #define S   8400
00019 // 1MHz
00020 //#define S   12000
00021 
00022 #if defined(TARGET_LPC1768)
00023 I2C i2c(p28,p27);
00024 #endif
00025 // for TG-LPC11U35-501
00026 #if defined(TARGET_LPC11U35_501)
00027 I2C i2c(P0_5,P0_4);
00028 #endif
00029 // for Nucleo
00030 #if defined(TARGET_NUCLEO_F401RE)
00031 I2C i2c(D14,D15);
00032 #endif
00033 AQM0802 lcd(i2c);
00034 MCP4726 mcp4726(i2c);
00035 Ticker dds;
00036 
00037 unsigned short f;
00038 unsigned short sin_table[360];
00039 
00040 
00041 void sampling()
00042 {
00043     static int t=0;
00044     unsigned short x;
00045     float a;
00046 
00047     x=360*modff((float)f*t/S,&a);
00048     x = sin_table[x];
00049     if(++t>=S)t=0;
00050 
00051     // D/A
00052     mcp4726.put(x);
00053 }
00054 
00055 
00056 int main() {
00057     
00058     char msg[10];
00059     int i;
00060     
00061     // to make 12bits sine table
00062     for(i=0;i<360;i++){
00063         sin_table[i]=0x7ff*sin(2*PI/360*i)+0x7ff;
00064     }
00065 
00066     // start signal with sampling
00067     dds.attach_us(&sampling,1000000/S);    // for us
00068 
00069     while(1){
00070         // Set f, test signal
00071         // 10-1KHz
00072         for(f=10;f<=1000;f+=10){
00073             sprintf(msg, "%5d Hz", f );
00074             __disable_irq();
00075             lcd.locate(0,0);
00076             lcd.print(msg);
00077             __enable_irq();
00078             wait(1);
00079         }
00080     }
00081 
00082 }
00083 
00084