University of Plymouth - Stages 1, 2 and 3
/
PWM-as-DAC
Use a PWM to simulate a DAC (needs additional filter)
main.cpp@0:5d9c5cd32f43, 2017-10-10 (annotated)
- Committer:
- noutram
- Date:
- Tue Oct 10 14:38:52 2017 +0000
- Revision:
- 0:5d9c5cd32f43
PWM as DAC
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
noutram | 0:5d9c5cd32f43 | 1 | #include "mbed.h" |
noutram | 0:5d9c5cd32f43 | 2 | |
noutram | 0:5d9c5cd32f43 | 3 | DigitalOut led1(LED1); |
noutram | 0:5d9c5cd32f43 | 4 | DigitalOut pin(D7); |
noutram | 0:5d9c5cd32f43 | 5 | PwmOut pwm(D6); |
noutram | 0:5d9c5cd32f43 | 6 | |
noutram | 0:5d9c5cd32f43 | 7 | // main() runs in its own thread in the OS |
noutram | 0:5d9c5cd32f43 | 8 | int main() { |
noutram | 0:5d9c5cd32f43 | 9 | pwm.period_us(10); // 1 second period |
noutram | 0:5d9c5cd32f43 | 10 | pwm = 0.5f; // 50% duty cycle, relative to period |
noutram | 0:5d9c5cd32f43 | 11 | while (true) { |
noutram | 0:5d9c5cd32f43 | 12 | led1 = !led1; |
noutram | 0:5d9c5cd32f43 | 13 | pin = !pin; |
noutram | 0:5d9c5cd32f43 | 14 | wait(0.5); |
noutram | 0:5d9c5cd32f43 | 15 | } |
noutram | 0:5d9c5cd32f43 | 16 | } |
noutram | 0:5d9c5cd32f43 | 17 |