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.
Fork of TutoElectro_PWM by
Diff: main.cpp
- Revision:
- 1:778a662bafcf
- Parent:
- 0:647746eea42d
- Child:
- 2:be4ea24f1cdd
--- a/main.cpp Sun Jan 04 22:12:46 2015 +0000
+++ b/main.cpp Tue Feb 03 17:10:33 2015 +0000
@@ -1,13 +1,13 @@
#include "mbed.h"
-PwmOut pwm1(PTB0);
-PwmOut pwmLED(LED_RED);
+AnalogOut dac(PTE30);
+
Serial pc(USBTX,USBRX);
-uint16_t periodPWM1 = 1;
-uint16_t periodPWMLED =1;
-float dutyPWM1 = 0;
-float dutyPWMLED = 0;
+uint16_t dacIntValue = 0;
+
+float dacValue = 0;
+float dacVoltage = 0;
void rxInterrupt()
{
@@ -15,58 +15,56 @@
switch(pc.getc()) {
case 'a':
-
- periodPWM1++;
- periodPWMLED++;
-
- pwm1.period_ms(periodPWM1);
- pwmLED.period_ms(periodPWMLED);
+ if(dacValue>0.001){
+ dacValue = dacValue - 0.001;
+ }
+ dacIntValue = (uint16_t)(4095*dacValue);
break;
case 's':
- if (periodPWM1>1) {
- periodPWM1--;
- periodPWMLED--;
+ if(dacValue<1){
+ dacValue = dacValue + 0.001;
}
-
- pwm1.period_ms(periodPWM1);
- pwmLED.period_ms(periodPWMLED);
+
+ dacIntValue = (uint16_t)(4095*dacValue);
break;
case 'f':
-
- if (dutyPWM1<1) {
- dutyPWM1 = dutyPWM1 + 0.05 ;
- dutyPWMLED = dutyPWMLED + 0.05;
+
+ if(dacIntValue> 1){
+ dacIntValue = dacIntValue - 1;
}
-
- pwm1.write(dutyPWM1);
- pwmLED.write(dutyPWMLED);
-
+
+ dacValue = dacIntValue/4095.0;
+
+
break;
case 'g':
-
- if (dutyPWM1>0) {
- dutyPWM1 = dutyPWM1 - 0.05 ;
- dutyPWMLED = dutyPWMLED - 0.05;
+
+ if(dacIntValue< 4095){
+ dacIntValue = dacIntValue + 1;
}
-
- pwm1.write(dutyPWM1);
- pwmLED.write(dutyPWMLED);
-
+
+ dacValue = dacIntValue/4095.0;
+
break;
default: break;
}
- pc.printf("Period (ms): %i, Duty: %.2f \r\n", periodPWM1,dutyPWM1);
+ dacVoltage = 3.3 * dacValue;
+
+ dac.write(dacValue);
+
+
+ pc.printf("DAC Value: %i, DAC Voltage: %.4f \r\n", dacIntValue, dacVoltage);
}
