DonghunKang
Dependencies: mbed Adafruit_GFX
main.cpp@3:e7cab3f9facb, 2019-11-04 (annotated)
- Committer:
- eins
- Date:
- Mon Nov 04 00:31:15 2019 +0000
- Revision:
- 3:e7cab3f9facb
- Parent:
- 2:3061fe655b7d
- Child:
- 4:a37c4436b6eb
made by H. S. Lee. 2019-10-28; DAC Test for F303RE
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
eins | 3:e7cab3f9facb | 1 | // made by H. S. Lee. 2019-10-28 |
eins | 3:e7cab3f9facb | 2 | // DAC Test for F303RE |
eins | 3:e7cab3f9facb | 3 | |
eins | 0:46005cfeedb5 | 4 | #include "mbed.h" |
eins | 0:46005cfeedb5 | 5 | #include "Adafruit_SSD1306.h" // Adafruit_GFX library |
eins | 0:46005cfeedb5 | 6 | |
eins | 1:ae0b11ca17d1 | 7 | Serial pc(SERIAL_TX, SERIAL_RX); |
eins | 1:ae0b11ca17d1 | 8 | DigitalOut myled(LED1); |
eins | 1:ae0b11ca17d1 | 9 | DigitalOut redLed(PA_12); |
eins | 1:ae0b11ca17d1 | 10 | DigitalIn myButton(PC_13); |
eins | 1:ae0b11ca17d1 | 11 | DigitalIn exButton(PC_11); |
eins | 3:e7cab3f9facb | 12 | AnalogOut myAnalogOut(PA_4); |
eins | 1:ae0b11ca17d1 | 13 | |
eins | 1:ae0b11ca17d1 | 14 | BusOut my7Seg(PA_8, PA_9, PA_10, PC_9, PC_8, PC_7, PC_6, PA_11); // 8bit data |
eins | 1:ae0b11ca17d1 | 15 | // LSB, , MSB |
eins | 1:ae0b11ca17d1 | 16 | char val7Seg[16] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F, |
eins | 1:ae0b11ca17d1 | 17 | 0x77, 0x7C, 0x39, 0x5E, 0x79, 0x71}; |
eins | 1:ae0b11ca17d1 | 18 | |
eins | 1:ae0b11ca17d1 | 19 | char rxData[5]; |
eins | 1:ae0b11ca17d1 | 20 | bool flagRx = 0; |
eins | 0:46005cfeedb5 | 21 | |
eins | 3:e7cab3f9facb | 22 | #define TEST2 |
eins | 2:3061fe655b7d | 23 | |
eins | 2:3061fe655b7d | 24 | #ifdef TEST1 |
eins | 2:3061fe655b7d | 25 | |
eins | 2:3061fe655b7d | 26 | int main(void) |
eins | 2:3061fe655b7d | 27 | { |
eins | 3:e7cab3f9facb | 28 | pc.baud(115200); |
eins | 3:e7cab3f9facb | 29 | pc.puts("\n TESt1: Starting Program \n"); |
eins | 3:e7cab3f9facb | 30 | |
eins | 2:3061fe655b7d | 31 | while (1) { |
eins | 2:3061fe655b7d | 32 | // change the voltage on the digital output pin by 0.1 * VCC |
eins | 2:3061fe655b7d | 33 | // and print what the measured voltage should be (assuming VCC = 3.3v) |
eins | 2:3061fe655b7d | 34 | for (float n = 0.0f; n < 1.0f; n += 0.1f) { |
eins | 2:3061fe655b7d | 35 | myAnalogOut = n; |
eins | 2:3061fe655b7d | 36 | // myAnalogOut.write(n); |
eins | 3:e7cab3f9facb | 37 | |
eins | 3:e7cab3f9facb | 38 | pc.printf("n=%1.2f, output = %1.2f volts\n", n, myAnalogOut.read() * 3.3f); |
eins | 2:3061fe655b7d | 39 | // turn on the led if the voltage is greater than 0.5f * VCC |
eins | 2:3061fe655b7d | 40 | myled = (myAnalogOut > 0.5f) ? 1 : 0; |
eins | 2:3061fe655b7d | 41 | wait(0.1); |
eins | 2:3061fe655b7d | 42 | } |
eins | 2:3061fe655b7d | 43 | } |
eins | 2:3061fe655b7d | 44 | } |
eins | 2:3061fe655b7d | 45 | #endif |
eins | 2:3061fe655b7d | 46 | |
eins | 2:3061fe655b7d | 47 | #ifdef TEST2 |
eins | 2:3061fe655b7d | 48 | Ticker DACtimer; |
eins | 2:3061fe655b7d | 49 | bool flagTimer = 0; |
eins | 2:3061fe655b7d | 50 | |
eins | 2:3061fe655b7d | 51 | void DACInt() |
eins | 2:3061fe655b7d | 52 | { |
eins | 2:3061fe655b7d | 53 | flagTimer = 1; |
eins | 2:3061fe655b7d | 54 | } |
eins | 2:3061fe655b7d | 55 | |
eins | 2:3061fe655b7d | 56 | main() |
eins | 2:3061fe655b7d | 57 | { |
eins | 3:e7cab3f9facb | 58 | pc.baud(115200); |
eins | 3:e7cab3f9facb | 59 | pc.puts("\n Test2: Starting Program \n"); |
eins | 3:e7cab3f9facb | 60 | |
eins | 2:3061fe655b7d | 61 | // for 10kHz sine wave generation, it's needed to have |
eins | 2:3061fe655b7d | 62 | // a sampling frequency in 10 times of that frequency, |
eins | 2:3061fe655b7d | 63 | // or 100kHz -> 10us period |
eins | 3:e7cab3f9facb | 64 | const float samplingTime = 0.00005; |
eins | 3:e7cab3f9facb | 65 | |
eins | 3:e7cab3f9facb | 66 | DACtimer.attach(&DACInt, samplingTime); // 50us is appropriate for F303RE (20kHz) |
eins | 3:e7cab3f9facb | 67 | // DACtimer.attach_us(&DACInt, samplingTime*1000000); // 50us is appropriate for F303RE |
eins | 3:e7cab3f9facb | 68 | |
eins | 3:e7cab3f9facb | 69 | float soundFreq = 1000; // 1kHz = 1000Hz |
eins | 3:e7cab3f9facb | 70 | pc.printf("smapling time = %dus, sound frequency = %dHz\n", (int)(samplingTime*1000000), (int)soundFreq); |
eins | 3:e7cab3f9facb | 71 | |
eins | 3:e7cab3f9facb | 72 | unsigned int n = 0; |
eins | 3:e7cab3f9facb | 73 | float tempVal; |
eins | 0:46005cfeedb5 | 74 | while(1) |
eins | 0:46005cfeedb5 | 75 | { |
eins | 2:3061fe655b7d | 76 | if (1 == flagTimer) |
eins | 2:3061fe655b7d | 77 | { |
eins | 3:e7cab3f9facb | 78 | flagTimer = 0; |
eins | 3:e7cab3f9facb | 79 | tempVal = soundFreq*n*samplingTime; |
eins | 3:e7cab3f9facb | 80 | myAnalogOut = sin(2*3.14159*tempVal)*0.5+0.5; // an angle is expressed in radians. |
eins | 3:e7cab3f9facb | 81 | // myAnalogOut = (sin(2*3.14159*tempVal)+sin(tempVal))*0.25+0.5; // an angle is expressed in radians. |
eins | 3:e7cab3f9facb | 82 | if (tempVal > 1.0f) n = 0; // to prevent a phase shift due to overflow of the unsinged int variable |
eins | 2:3061fe655b7d | 83 | n++; |
eins | 2:3061fe655b7d | 84 | } |
eins | 2:3061fe655b7d | 85 | } |
eins | 2:3061fe655b7d | 86 | } |
eins | 2:3061fe655b7d | 87 | #endif |
eins | 0:46005cfeedb5 | 88 |