DonghunKang
Dependencies: mbed Adafruit_GFX
main.cpp@4:a37c4436b6eb, 2019-11-04 (annotated)
- Committer:
- Donghun
- Date:
- Mon Nov 04 13:44:19 2019 +0000
- Revision:
- 4:a37c4436b6eb
- Parent:
- 3:e7cab3f9facb
submission_donghunKang
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 |
Donghun | 4:a37c4436b6eb | 6 | Ticker DACtimer; |
eins | 1:ae0b11ca17d1 | 7 | Serial pc(SERIAL_TX, SERIAL_RX); |
Donghun | 4:a37c4436b6eb | 8 | DigitalOut Board_led(LED1); |
Donghun | 4:a37c4436b6eb | 9 | DigitalOut Out_led(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); |
Donghun | 4:a37c4436b6eb | 13 | bool flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 14 | BusOut My7segment_pin(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 | char rxData[5]; |
eins | 1:ae0b11ca17d1 | 19 | bool flagRx = 0; |
Donghun | 4:a37c4436b6eb | 20 | int delay =0; |
eins | 0:46005cfeedb5 | 21 | |
Donghun | 4:a37c4436b6eb | 22 | #define Pr_2_3 |
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 |
Donghun | 4:a37c4436b6eb | 40 | Board_led = (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 | |
Donghun | 4:a37c4436b6eb | 89 | |
Donghun | 4:a37c4436b6eb | 90 | #ifdef Pr_1 |
Donghun | 4:a37c4436b6eb | 91 | void DACInt() |
Donghun | 4:a37c4436b6eb | 92 | { |
Donghun | 4:a37c4436b6eb | 93 | flagTimer = 1; |
Donghun | 4:a37c4436b6eb | 94 | } |
Donghun | 4:a37c4436b6eb | 95 | |
Donghun | 4:a37c4436b6eb | 96 | main() |
Donghun | 4:a37c4436b6eb | 97 | { |
Donghun | 4:a37c4436b6eb | 98 | pc.baud(115200); |
Donghun | 4:a37c4436b6eb | 99 | pc.puts("\n Test2: Starting Program \n"); |
Donghun | 4:a37c4436b6eb | 100 | |
Donghun | 4:a37c4436b6eb | 101 | const float samplingTime = 0.00005; |
Donghun | 4:a37c4436b6eb | 102 | DACtimer. attach(&DACInt, samplingTime); |
Donghun | 4:a37c4436b6eb | 103 | float soundFreq = 400.0f; |
Donghun | 4:a37c4436b6eb | 104 | float soundFreq_count = 0.0275f; |
Donghun | 4:a37c4436b6eb | 105 | unsigned int n = 0; |
Donghun | 4:a37c4436b6eb | 106 | float tempVal; |
Donghun | 4:a37c4436b6eb | 107 | while(1) |
Donghun | 4:a37c4436b6eb | 108 | { |
Donghun | 4:a37c4436b6eb | 109 | if (1==flagTimer) |
Donghun | 4:a37c4436b6eb | 110 | { |
Donghun | 4:a37c4436b6eb | 111 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 112 | tempVal = soundFreq*n*samplingTime; |
Donghun | 4:a37c4436b6eb | 113 | myAnalogOut = sin(2 * 3.14159 * tempVal) * 0.5 + 0.5; |
Donghun | 4:a37c4436b6eb | 114 | // myAnalogOut = (sin(2*3.14159*tempVal)+sin(tempVal))*0.25+0.5; // an angle is expressed in radians.Q |
Donghun | 4:a37c4436b6eb | 115 | soundFreq = soundFreq + soundFreq_count; |
Donghun | 4:a37c4436b6eb | 116 | if(2000<soundFreq) soundFreq_count = -soundFreq_count; |
Donghun | 4:a37c4436b6eb | 117 | if(400>soundFreq) soundFreq_count = -soundFreq_count; |
Donghun | 4:a37c4436b6eb | 118 | if(tempVal>1.0f)n=0; |
Donghun | 4:a37c4436b6eb | 119 | n++; |
Donghun | 4:a37c4436b6eb | 120 | // if(12000.0f < n) n = 0; |
Donghun | 4:a37c4436b6eb | 121 | |
Donghun | 4:a37c4436b6eb | 122 | } |
Donghun | 4:a37c4436b6eb | 123 | |
Donghun | 4:a37c4436b6eb | 124 | } |
Donghun | 4:a37c4436b6eb | 125 | } |
Donghun | 4:a37c4436b6eb | 126 | #endif |
Donghun | 4:a37c4436b6eb | 127 | |
Donghun | 4:a37c4436b6eb | 128 | |
Donghun | 4:a37c4436b6eb | 129 | #ifdef Pr_2_3 |
Donghun | 4:a37c4436b6eb | 130 | void ReceiveInt() |
Donghun | 4:a37c4436b6eb | 131 | { |
Donghun | 4:a37c4436b6eb | 132 | char inChar; |
Donghun | 4:a37c4436b6eb | 133 | static char rxCount = 0; |
Donghun | 4:a37c4436b6eb | 134 | static char rxBuf[5]; |
Donghun | 4:a37c4436b6eb | 135 | |
Donghun | 4:a37c4436b6eb | 136 | while(1 == pc.readable()) |
Donghun | 4:a37c4436b6eb | 137 | { |
Donghun | 4:a37c4436b6eb | 138 | inChar=pc.getc(); |
Donghun | 4:a37c4436b6eb | 139 | pc.putc(inChar); |
Donghun | 4:a37c4436b6eb | 140 | if ('<' == inChar) |
Donghun | 4:a37c4436b6eb | 141 | { |
Donghun | 4:a37c4436b6eb | 142 | rxCount = 1; |
Donghun | 4:a37c4436b6eb | 143 | } |
Donghun | 4:a37c4436b6eb | 144 | else if (rxCount > 0 && rxCount < 5) |
Donghun | 4:a37c4436b6eb | 145 | { |
Donghun | 4:a37c4436b6eb | 146 | rxBuf[rxCount - 1] = inChar; |
Donghun | 4:a37c4436b6eb | 147 | rxCount++; |
Donghun | 4:a37c4436b6eb | 148 | } |
Donghun | 4:a37c4436b6eb | 149 | else if (rxCount == 5 && '>' == inChar) |
Donghun | 4:a37c4436b6eb | 150 | { |
Donghun | 4:a37c4436b6eb | 151 | rxCount = 0; |
Donghun | 4:a37c4436b6eb | 152 | flagRx = 1; |
Donghun | 4:a37c4436b6eb | 153 | memcpy(rxData,rxBuf,rxCount-1); |
Donghun | 4:a37c4436b6eb | 154 | } |
Donghun | 4:a37c4436b6eb | 155 | else |
Donghun | 4:a37c4436b6eb | 156 | { |
Donghun | 4:a37c4436b6eb | 157 | rxCount = 0; |
Donghun | 4:a37c4436b6eb | 158 | } |
Donghun | 4:a37c4436b6eb | 159 | } |
Donghun | 4:a37c4436b6eb | 160 | } |
Donghun | 4:a37c4436b6eb | 161 | |
Donghun | 4:a37c4436b6eb | 162 | void DACInt() |
Donghun | 4:a37c4436b6eb | 163 | { |
Donghun | 4:a37c4436b6eb | 164 | flagTimer = 1; |
Donghun | 4:a37c4436b6eb | 165 | } |
Donghun | 4:a37c4436b6eb | 166 | |
Donghun | 4:a37c4436b6eb | 167 | main() |
Donghun | 4:a37c4436b6eb | 168 | { |
Donghun | 4:a37c4436b6eb | 169 | pc.baud(115200); |
Donghun | 4:a37c4436b6eb | 170 | pc.puts("\n Practice2: Starting Program \n"); |
Donghun | 4:a37c4436b6eb | 171 | pc.attach(&ReceiveInt,Serial::RxIrq); |
Donghun | 4:a37c4436b6eb | 172 | |
Donghun | 4:a37c4436b6eb | 173 | char tmpCommand[3]; |
Donghun | 4:a37c4436b6eb | 174 | int rxVal; |
Donghun | 4:a37c4436b6eb | 175 | Board_led=1; |
Donghun | 4:a37c4436b6eb | 176 | const float samplingTime = 0.00005; |
Donghun | 4:a37c4436b6eb | 177 | DACtimer. attach(&DACInt, samplingTime); |
Donghun | 4:a37c4436b6eb | 178 | float soundFreq = 400.0f; |
Donghun | 4:a37c4436b6eb | 179 | unsigned int n = 0; |
Donghun | 4:a37c4436b6eb | 180 | float tempVal; |
Donghun | 4:a37c4436b6eb | 181 | double soundVolume = 2; |
Donghun | 4:a37c4436b6eb | 182 | double squaremaker = 0.0005; |
Donghun | 4:a37c4436b6eb | 183 | |
Donghun | 4:a37c4436b6eb | 184 | while(1) |
Donghun | 4:a37c4436b6eb | 185 | { |
Donghun | 4:a37c4436b6eb | 186 | if(1 == flagRx) |
Donghun | 4:a37c4436b6eb | 187 | { |
Donghun | 4:a37c4436b6eb | 188 | flagRx=0; |
Donghun | 4:a37c4436b6eb | 189 | tmpCommand[0]=rxData[0]; |
Donghun | 4:a37c4436b6eb | 190 | tmpCommand[1]=rxData[1]; |
Donghun | 4:a37c4436b6eb | 191 | tmpCommand[2]=0; |
Donghun | 4:a37c4436b6eb | 192 | rxVal = atoi(rxData + 2); |
Donghun | 4:a37c4436b6eb | 193 | if(!strcmp(tmpCommand,"SI")) |
Donghun | 4:a37c4436b6eb | 194 | { |
Donghun | 4:a37c4436b6eb | 195 | pc.printf("Sine Wave: %d\n",rxVal); |
Donghun | 4:a37c4436b6eb | 196 | soundFreq = rxVal*10.0f; |
Donghun | 4:a37c4436b6eb | 197 | flagTimer = 2; |
Donghun | 4:a37c4436b6eb | 198 | } |
Donghun | 4:a37c4436b6eb | 199 | else if(!strcmp(tmpCommand,"SQ")) |
Donghun | 4:a37c4436b6eb | 200 | { |
Donghun | 4:a37c4436b6eb | 201 | pc.printf("Square Wave: %d\n",rxVal); |
Donghun | 4:a37c4436b6eb | 202 | flagTimer = 3; |
Donghun | 4:a37c4436b6eb | 203 | } |
Donghun | 4:a37c4436b6eb | 204 | else if(!strcmp(tmpCommand,"PL")) |
Donghun | 4:a37c4436b6eb | 205 | { |
Donghun | 4:a37c4436b6eb | 206 | pc.printf("Play Limit: %d\n",rxVal); |
Donghun | 4:a37c4436b6eb | 207 | soundVolume = 0; |
Donghun | 4:a37c4436b6eb | 208 | flagTimer = 4; |
Donghun | 4:a37c4436b6eb | 209 | } |
Donghun | 4:a37c4436b6eb | 210 | else if(!strcmp(tmpCommand,"VL")) |
Donghun | 4:a37c4436b6eb | 211 | { |
Donghun | 4:a37c4436b6eb | 212 | pc.printf("Volume Limit: %d\n",rxVal); |
Donghun | 4:a37c4436b6eb | 213 | soundVolume = rxVal*1.0f; |
Donghun | 4:a37c4436b6eb | 214 | flagTimer = 5; |
Donghun | 4:a37c4436b6eb | 215 | } |
Donghun | 4:a37c4436b6eb | 216 | else if(!strcmp(tmpCommand,"TR")) |
Donghun | 4:a37c4436b6eb | 217 | { |
Donghun | 4:a37c4436b6eb | 218 | pc.printf("Triangle Wave: %ds\n",rxVal); |
Donghun | 4:a37c4436b6eb | 219 | flagTimer = 6; |
Donghun | 4:a37c4436b6eb | 220 | } |
Donghun | 4:a37c4436b6eb | 221 | else if(!strcmp(tmpCommand,"SA")) |
Donghun | 4:a37c4436b6eb | 222 | { |
Donghun | 4:a37c4436b6eb | 223 | pc.printf("Sawtooth Wave: %d\n",rxVal); |
Donghun | 4:a37c4436b6eb | 224 | flagTimer = 7; |
Donghun | 4:a37c4436b6eb | 225 | } |
Donghun | 4:a37c4436b6eb | 226 | } |
Donghun | 4:a37c4436b6eb | 227 | |
Donghun | 4:a37c4436b6eb | 228 | if (2==flagTimer) |
Donghun | 4:a37c4436b6eb | 229 | { |
Donghun | 4:a37c4436b6eb | 230 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 231 | tempVal = soundFreq*n*samplingTime; |
Donghun | 4:a37c4436b6eb | 232 | myAnalogOut = sin(soundVolume*3.14159*tempVal)*0.5+0.5; |
Donghun | 4:a37c4436b6eb | 233 | if (tempVal > 1.0f) n = 0; |
Donghun | 4:a37c4436b6eb | 234 | n++; |
Donghun | 4:a37c4436b6eb | 235 | } |
Donghun | 4:a37c4436b6eb | 236 | else if (3==flagTimer) |
Donghun | 4:a37c4436b6eb | 237 | { |
Donghun | 4:a37c4436b6eb | 238 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 239 | tempVal = soundFreq*n*squaremaker; |
Donghun | 4:a37c4436b6eb | 240 | myAnalogOut = sin(soundVolume*3.14159*tempVal)*0.5+0.5; |
Donghun | 4:a37c4436b6eb | 241 | if (tempVal > 1.0f) n = 0; |
Donghun | 4:a37c4436b6eb | 242 | n++; |
Donghun | 4:a37c4436b6eb | 243 | } |
Donghun | 4:a37c4436b6eb | 244 | else if (4==flagTimer) |
Donghun | 4:a37c4436b6eb | 245 | { |
Donghun | 4:a37c4436b6eb | 246 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 247 | tempVal = soundFreq*n*samplingTime; |
Donghun | 4:a37c4436b6eb | 248 | myAnalogOut = sin(soundVolume*3.14159*tempVal)*0.5+0.5; |
Donghun | 4:a37c4436b6eb | 249 | if (tempVal > 1.0f) n = 0; |
Donghun | 4:a37c4436b6eb | 250 | n++; |
Donghun | 4:a37c4436b6eb | 251 | } |
Donghun | 4:a37c4436b6eb | 252 | else if (5==flagTimer) |
Donghun | 4:a37c4436b6eb | 253 | { |
Donghun | 4:a37c4436b6eb | 254 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 255 | tempVal = soundFreq*n*samplingTime; |
Donghun | 4:a37c4436b6eb | 256 | myAnalogOut = sin(soundVolume*3.14159*tempVal)*0.5+0.5; |
Donghun | 4:a37c4436b6eb | 257 | if (tempVal > 1.0f) n = 0; |
Donghun | 4:a37c4436b6eb | 258 | n++; |
Donghun | 4:a37c4436b6eb | 259 | } |
Donghun | 4:a37c4436b6eb | 260 | else if (6==flagTimer) |
Donghun | 4:a37c4436b6eb | 261 | { |
Donghun | 4:a37c4436b6eb | 262 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 263 | tempVal = soundFreq*n*samplingTime; |
Donghun | 4:a37c4436b6eb | 264 | myAnalogOut = sin(soundVolume*3.14159*tempVal)*0.5 + 0.5 + 0.5 * sin((soundVolume * 3.14159 * tempVal)*2) * 0.5 + 0.33 * sin((soundVolume + 3.14159 + tempVal)*3) * 0.5 + 0.5 + 0.25 * sin((soundVolume + 3.14159 + tempVal)*4) * 0.5 + 0.5 + 0.2 * sin((soundVolume + 3.14159 + tempVal)*5) * 0.5 + 0.5 + 0.166 * sin((soundVolume + 3.14159 + tempVal)*6) * 0.5 + 0.5; |
Donghun | 4:a37c4436b6eb | 265 | if (tempVal > 1.0f) n = 0; |
Donghun | 4:a37c4436b6eb | 266 | n++; |
Donghun | 4:a37c4436b6eb | 267 | } |
Donghun | 4:a37c4436b6eb | 268 | else if (7==flagTimer) |
Donghun | 4:a37c4436b6eb | 269 | { |
Donghun | 4:a37c4436b6eb | 270 | flagTimer = 0; |
Donghun | 4:a37c4436b6eb | 271 | tempVal = soundFreq*n*samplingTime; |
Donghun | 4:a37c4436b6eb | 272 | myAnalogOut = sin(soundVolume*3.14159*tempVal)*0.5 + 0.5 + 0.33 * sin((soundVolume * 3.14159 * tempVal)*3 ) * 0.5 + 0.2 * sin((soundVolume + 3.14159 + tempVal)*5) * 0.5 + 0.5 + 0.144 * sin((soundVolume + 3.14159 + tempVal)*7) * 0.5 + 0.5 + 0.111 * sin((soundVolume + 3.14159 + tempVal)*9) * 0.5 + 0.5 + 0.0999 * sin((soundVolume + 3.14159 + tempVal)*11) * 0.5 + 0.5; |
Donghun | 4:a37c4436b6eb | 273 | if (tempVal > 1.0f) n = 0; |
Donghun | 4:a37c4436b6eb | 274 | n++; |
Donghun | 4:a37c4436b6eb | 275 | } |
Donghun | 4:a37c4436b6eb | 276 | |
Donghun | 4:a37c4436b6eb | 277 | } |
Donghun | 4:a37c4436b6eb | 278 | } |
Donghun | 4:a37c4436b6eb | 279 | #endif |