This program makes a sine wave. This converts 360 degrees into radian every once from 0 and gives it in sine function. The value multiply 0.5 by the result of the calculation and add 0.5 to give an offset voltage. As for the value that calculated, it becomes a sin wave of center level 1.65V, 0V at the minimum, up to 3.3V with a DA converter in this way. The sine wave is stored in a local file. Extension is .CSV. You can make a graph by Excel and confirm a sine wave. See: http://blogs.yahoo.co.jp/jf1vrr_station/19797477.html (Japanese)

Dependencies:   TextLCD mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* This program makes a sine wave. This converts 360 degrees 
00002 into radian every once from 0 and gives it in sine function. 
00003 The value multiply 0.5 by the result of the calculation and add 0.5 
00004 to give an offset voltage. As for the value that calculated, 
00005 it becomes a sin wave of center level 1.65V, 0V at the minimum,
00006 up to 3.3V with a DA converter in this way. 
00007 The sine wave is stored in a local file. Extension is .CSV. 
00008 You can make a graph by Excel and confirm a sine wave.
00009 */
00010 #include "mbed.h"
00011 #include "TextLCD.h"
00012 
00013 TextLCD lcd(p24, p26, p27, p28, p29, p30);
00014 
00015 LocalFileSystem local("local");
00016 DigitalOut busy(LED2);
00017 AnalogOut DA(p18);
00018 AnalogIn AD(p20);
00019 
00020 int main() {
00021     lcd.printf("SinWave\n");
00022     FILE *fp;
00023 
00024     if ( NULL == (fp = fopen( "/local/sinewave.csv", "w" )) )
00025         error( "" );
00026 
00027     busy  = 1;
00028 
00029     for (float i=0; i<360; i++) {
00030         DA = sin(i/180*3.14)*0.5+0.5;
00031         wait(0.05);
00032         lcd.locate(0,1);
00033         lcd.printf("DA out %0.5fV", AD.read()*3.3);
00034         fprintf( fp, "%f\n", (float)AD.read()*3.3 );
00035     }
00036     fclose( fp );
00037     busy   = 0;
00038 }