Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: TextLCD mbed dacmodule
Fork of DAC_module by
main.cpp@1:af51ee0c367d, 2016-11-01 (annotated)
- Committer:
- psuMbedHead
- Date:
- Tue Nov 01 18:25:30 2016 +0000
- Revision:
- 1:af51ee0c367d
- Parent:
- 0:c1332385cffa
- Child:
- 2:ee4fdb7b9859
added all/most comments
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| psuMbedHead | 1:af51ee0c367d | 1 | #include "mbed.h" //normal library |
| psuMbedHead | 1:af51ee0c367d | 2 | #include "mcp4725.h" //DAC library |
| psuMbedHead | 1:af51ee0c367d | 3 | #include "TextLCD.h" //16x2 lcd library |
| psuMbedHead | 1:af51ee0c367d | 4 | #include "math.h" //math library |
| psuMbedHead | 0:c1332385cffa | 5 | |
| psuMbedHead | 1:af51ee0c367d | 6 | TextLCD lcd(p14, p13, p21, p22, p23, p24, TextLCD::LCD16x2); //setup for 16x2 lcd (rs, e, d4-d7) |
| psuMbedHead | 1:af51ee0c367d | 7 | Serial pc(USBTX, USBRX); //serial usb communication setup |
| psuMbedHead | 1:af51ee0c367d | 8 | AnalogIn analogIn(p15); //used for testing in original code |
| psuMbedHead | 1:af51ee0c367d | 9 | MCP4725 DAC(p28, p27, MCP4725::Standard100kHz, 0); //init the DAC module |
| psuMbedHead | 0:c1332385cffa | 10 | //define custom characters |
| psuMbedHead | 1:af51ee0c367d | 11 | const char sin_char_top[] = { 0x0, 0xe,0x11,0x11, 0x0, 0x0, 0x0, 0x0}; //makes left part of sin wave char |
| psuMbedHead | 1:af51ee0c367d | 12 | const char sin_char_bot[] = { 0x0, 0x0, 0x0, 0x0,0x11,0x11, 0xe, 0x0}; //makes right part of sin wave char |
| psuMbedHead | 1:af51ee0c367d | 13 | const char sq_char_left[] = {0x1c, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4, 0x7}; //makes left part of square wav char |
| psuMbedHead | 1:af51ee0c367d | 14 | const char sq_char_right[] = { 0x7, 0x4, 0x4, 0x4, 0x4, 0x4, 0x4,0x1c}; //makes right part of square wav char |
| psuMbedHead | 1:af51ee0c367d | 15 | const double pi = 3.14159; //needed constant for sin() |
| psuMbedHead | 0:c1332385cffa | 16 | int main() { |
| psuMbedHead | 1:af51ee0c367d | 17 | //set custom characters to empty char spot |
| psuMbedHead | 1:af51ee0c367d | 18 | lcd.setUDC(0, (char *) sin_char_bot); //set to char 0 |
| psuMbedHead | 1:af51ee0c367d | 19 | lcd.setUDC(1, (char *) sin_char_top); //set to char 1 |
| psuMbedHead | 1:af51ee0c367d | 20 | lcd.setUDC(2, (char *) sq_char_left); //set to char 2 |
| psuMbedHead | 1:af51ee0c367d | 21 | lcd.setUDC(3, (char *) sq_char_right); //set to char 3 |
| psuMbedHead | 0:c1332385cffa | 22 | int RUN = 0; //start = 1, stop = 0 |
| psuMbedHead | 0:c1332385cffa | 23 | int SIGNAL_MODE = 0; //1sin,10ksin,1sq,10ksq = 0,1,2,3 |
| psuMbedHead | 0:c1332385cffa | 24 | |
| psuMbedHead | 1:af51ee0c367d | 25 | while(1) { //start continuous loop |
| psuMbedHead | 1:af51ee0c367d | 26 | int dots = 0; //for waiting loop |
| psuMbedHead | 0:c1332385cffa | 27 | while(RUN == 0) |
| psuMbedHead | 0:c1332385cffa | 28 | { |
| psuMbedHead | 1:af51ee0c367d | 29 | lcd.setCursor(TextLCD::CurOff_BlkOff); //set cursor to invisible |
| psuMbedHead | 1:af51ee0c367d | 30 | lcd.setAddress(0, 0); //place cursor at origin |
| psuMbedHead | 1:af51ee0c367d | 31 | lcd.printf("Waveform: off\n"); //print to lcd that it is in off mode |
| psuMbedHead | 0:c1332385cffa | 32 | if(dots == 0){ |
| psuMbedHead | 0:c1332385cffa | 33 | lcd.printf("waiting"); |
| psuMbedHead | 0:c1332385cffa | 34 | dots++; |
| psuMbedHead | 0:c1332385cffa | 35 | } |
| psuMbedHead | 0:c1332385cffa | 36 | else if(dots == 1){ |
| psuMbedHead | 0:c1332385cffa | 37 | lcd.printf("waiting."); |
| psuMbedHead | 0:c1332385cffa | 38 | dots++; |
| psuMbedHead | 0:c1332385cffa | 39 | } |
| psuMbedHead | 0:c1332385cffa | 40 | else if(dots == 2){ |
| psuMbedHead | 0:c1332385cffa | 41 | lcd.printf("waiting.."); |
| psuMbedHead | 0:c1332385cffa | 42 | dots++; |
| psuMbedHead | 0:c1332385cffa | 43 | } |
| psuMbedHead | 0:c1332385cffa | 44 | else if(dots == 3){ |
| psuMbedHead | 0:c1332385cffa | 45 | lcd.printf("waiting..."); |
| psuMbedHead | 0:c1332385cffa | 46 | dots = 0; |
| psuMbedHead | 1:af51ee0c367d | 47 | } //makes the dots go "." ".." "..." in a loop |
| psuMbedHead | 0:c1332385cffa | 48 | else{ |
| psuMbedHead | 1:af51ee0c367d | 49 | lcd.printf("ERROR CASE"); //print to show error |
| psuMbedHead | 0:c1332385cffa | 50 | } |
| psuMbedHead | 1:af51ee0c367d | 51 | wait_ms(100); //lower frequency loop to cycle dots at 0.1 second interval |
| psuMbedHead | 0:c1332385cffa | 52 | } |
| psuMbedHead | 1:af51ee0c367d | 53 | int i = 0; //resets counter when transitioning from stop to start |
| psuMbedHead | 1:af51ee0c367d | 54 | while(RUN == 1) //waveform is being generated |
| psuMbedHead | 0:c1332385cffa | 55 | { |
| psuMbedHead | 0:c1332385cffa | 56 | //set display on |
| psuMbedHead | 1:af51ee0c367d | 57 | lcd.setCursor(TextLCD::CurOff_BlkOff); //set cursor to invisible |
| psuMbedHead | 1:af51ee0c367d | 58 | lcd.setAddress(0, 0); //place cursor at origin |
| psuMbedHead | 1:af51ee0c367d | 59 | lcd.printf("Waveform: on\n"); //print that the waveform is on |
| psuMbedHead | 0:c1332385cffa | 60 | |
| psuMbedHead | 0:c1332385cffa | 61 | if(SIGNAL_MODE == 0){ |
| psuMbedHead | 0:c1332385cffa | 62 | //sin wave 1Hz or 1s |
| psuMbedHead | 0:c1332385cffa | 63 | lcd.putc(0); lcd.putc(1); lcd.printf("1Hz Sin Wav"); |
| psuMbedHead | 0:c1332385cffa | 64 | |
| psuMbedHead | 1:af51ee0c367d | 65 | double j = 1.65*sin(2*pi*i/100)+1.65; //formula to plot a sin wave from 0 to 3.3V over 100 points |
| psuMbedHead | 1:af51ee0c367d | 66 | DAC.write(MCP4725::Normal, (0xFFF * (j/3.3)), false); //send value to the DAC |
| psuMbedHead | 1:af51ee0c367d | 67 | wait_ms(10); //going from 0 to 2pi over 100 points -> 1 second/100 |
| psuMbedHead | 1:af51ee0c367d | 68 | i++; //increment to the next point |
| psuMbedHead | 0:c1332385cffa | 69 | } //end if case |
| psuMbedHead | 0:c1332385cffa | 70 | else if(SIGNAL_MODE == 1){ |
| psuMbedHead | 0:c1332385cffa | 71 | //sin wave 10kHz or 100us |
| psuMbedHead | 0:c1332385cffa | 72 | lcd.putc(0); lcd.putc(1); lcd.printf("10kHz Sin Wav"); |
| psuMbedHead | 0:c1332385cffa | 73 | |
| psuMbedHead | 1:af51ee0c367d | 74 | double k = 1.65*sin(2*pi*i/100)+1.65; //formula to plot a sin wave from 0 to 3.3V over 100 points |
| psuMbedHead | 1:af51ee0c367d | 75 | DAC.write(MCP4725::Normal, (0xFFF * (k/3.3)), false); //send value to the DAC |
| psuMbedHead | 1:af51ee0c367d | 76 | wait_us(1); //going from 0 to 2pi over 100 points -> 100 usecond/100 |
| psuMbedHead | 1:af51ee0c367d | 77 | i++; //increment to the next point |
| psuMbedHead | 0:c1332385cffa | 78 | } //end if case |
| psuMbedHead | 0:c1332385cffa | 79 | else if(SIGNAL_MODE == 2){ |
| psuMbedHead | 0:c1332385cffa | 80 | //square wave 1Hz or 1s |
| psuMbedHead | 0:c1332385cffa | 81 | lcd.putc(2); lcd.putc(3); lcd.printf("1Hz Sq Wav"); |
| psuMbedHead | 0:c1332385cffa | 82 | |
| psuMbedHead | 1:af51ee0c367d | 83 | DAC.write(MCP4725::Normal, 0xFFF, false); //set to max value |
| psuMbedHead | 1:af51ee0c367d | 84 | wait_ms(500); //wait half the period |
| psuMbedHead | 1:af51ee0c367d | 85 | DAC.write(MCP4725::Normal, 0x000, false); //set to min value |
| psuMbedHead | 1:af51ee0c367d | 86 | wait_ms(500); //wait half the period |
| psuMbedHead | 0:c1332385cffa | 87 | } //end if case |
| psuMbedHead | 0:c1332385cffa | 88 | else if(SIGNAL_MODE == 3){ |
| psuMbedHead | 0:c1332385cffa | 89 | //square wave 10kHz or 100us |
| psuMbedHead | 0:c1332385cffa | 90 | lcd.putc(2); lcd.putc(3); lcd.printf("10kHz Sq Wav"); |
| psuMbedHead | 0:c1332385cffa | 91 | |
| psuMbedHead | 1:af51ee0c367d | 92 | DAC.write(MCP4725::Normal, 0xFFF, false); //set to max value |
| psuMbedHead | 1:af51ee0c367d | 93 | wait_us(50); //wait half the period |
| psuMbedHead | 1:af51ee0c367d | 94 | DAC.write(MCP4725::Normal, 0x000, false); //set to min value |
| psuMbedHead | 1:af51ee0c367d | 95 | wait_us(50); //wait half the period |
| psuMbedHead | 0:c1332385cffa | 96 | } //end if case |
| psuMbedHead | 0:c1332385cffa | 97 | else{ |
| psuMbedHead | 1:af51ee0c367d | 98 | lcd.printf("ERROR CASE"); //print to show error |
| psuMbedHead | 0:c1332385cffa | 99 | //do nothing |
| psuMbedHead | 0:c1332385cffa | 100 | } //end default |
| psuMbedHead | 0:c1332385cffa | 101 | } //end while(on) loop |
| psuMbedHead | 0:c1332385cffa | 102 | } //end while(1) loop |
| psuMbedHead | 0:c1332385cffa | 103 | } //end main |
