PWM fun:)
Dependencies: mbed
Fork of LAB07_Oppgave4_Template by
main.cpp@1:9aeabcc7f4a7, 2015-10-15 (annotated)
- Committer:
- madmonkeyman82
- Date:
- Thu Oct 15 15:34:01 2015 +0000
- Revision:
- 1:9aeabcc7f4a7
- Parent:
- 0:ce0d1043321f
First
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlanghbv | 0:ce0d1043321f | 1 | #include "mbed.h" |
rlanghbv | 0:ce0d1043321f | 2 | |
madmonkeyman82 | 1:9aeabcc7f4a7 | 3 | //------------------------------------ |
madmonkeyman82 | 1:9aeabcc7f4a7 | 4 | // Hyperterminal configuration |
madmonkeyman82 | 1:9aeabcc7f4a7 | 5 | // 9600 bauds, 8-bit data, no parity |
madmonkeyman82 | 1:9aeabcc7f4a7 | 6 | //------------------------------------ |
madmonkeyman82 | 1:9aeabcc7f4a7 | 7 | |
madmonkeyman82 | 1:9aeabcc7f4a7 | 8 | Serial pc(SERIAL_TX, SERIAL_RX); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 9 | |
rlanghbv | 0:ce0d1043321f | 10 | Timeout onTime; |
rlanghbv | 0:ce0d1043321f | 11 | Ticker periodTimer; |
madmonkeyman82 | 1:9aeabcc7f4a7 | 12 | Timeout timedown; |
madmonkeyman82 | 1:9aeabcc7f4a7 | 13 | |
rlanghbv | 0:ce0d1043321f | 14 | DigitalOut led(PA_9); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 15 | InterruptIn SW7(PB_3); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 16 | InterruptIn SW6(PA_10); |
rlanghbv | 0:ce0d1043321f | 17 | |
rlanghbv | 0:ce0d1043321f | 18 | float periodTime=0.020; // 20ms period time (50Hz) |
madmonkeyman82 | 1:9aeabcc7f4a7 | 19 | float dutyCycle=.50; |
madmonkeyman82 | 1:9aeabcc7f4a7 | 20 | |
madmonkeyman82 | 1:9aeabcc7f4a7 | 21 | void timeouttick() |
madmonkeyman82 | 1:9aeabcc7f4a7 | 22 | { |
madmonkeyman82 | 1:9aeabcc7f4a7 | 23 | SW7.enable_irq(); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 24 | SW6.enable_irq(); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 25 | } |
madmonkeyman82 | 1:9aeabcc7f4a7 | 26 | |
madmonkeyman82 | 1:9aeabcc7f4a7 | 27 | void interruptSW7(){ |
madmonkeyman82 | 1:9aeabcc7f4a7 | 28 | SW7.disable_irq(); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 29 | timedown.attach(&timeouttick, 0.03); //debounce |
madmonkeyman82 | 1:9aeabcc7f4a7 | 30 | if(dutyCycle < 1.00f) |
madmonkeyman82 | 1:9aeabcc7f4a7 | 31 | { |
madmonkeyman82 | 1:9aeabcc7f4a7 | 32 | dutyCycle += 0.05f; |
madmonkeyman82 | 1:9aeabcc7f4a7 | 33 | } |
madmonkeyman82 | 1:9aeabcc7f4a7 | 34 | } |
madmonkeyman82 | 1:9aeabcc7f4a7 | 35 | |
madmonkeyman82 | 1:9aeabcc7f4a7 | 36 | void interruptSW6(){ |
madmonkeyman82 | 1:9aeabcc7f4a7 | 37 | SW6.disable_irq(); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 38 | timedown.attach(&timeouttick, 0.03); //debounce |
madmonkeyman82 | 1:9aeabcc7f4a7 | 39 | if(dutyCycle > 0.10f) |
madmonkeyman82 | 1:9aeabcc7f4a7 | 40 | { |
madmonkeyman82 | 1:9aeabcc7f4a7 | 41 | dutyCycle -= 0.05f; |
madmonkeyman82 | 1:9aeabcc7f4a7 | 42 | } |
madmonkeyman82 | 1:9aeabcc7f4a7 | 43 | } |
rlanghbv | 0:ce0d1043321f | 44 | |
rlanghbv | 0:ce0d1043321f | 45 | void TurnLightOff() |
rlanghbv | 0:ce0d1043321f | 46 | { led=1; |
rlanghbv | 0:ce0d1043321f | 47 | } |
rlanghbv | 0:ce0d1043321f | 48 | |
rlanghbv | 0:ce0d1043321f | 49 | void TurnLightOn() |
rlanghbv | 0:ce0d1043321f | 50 | { led=0; |
rlanghbv | 0:ce0d1043321f | 51 | onTime.attach(&TurnLightOff,(float)periodTime*dutyCycle); |
rlanghbv | 0:ce0d1043321f | 52 | } |
rlanghbv | 0:ce0d1043321f | 53 | |
rlanghbv | 0:ce0d1043321f | 54 | int main() |
rlanghbv | 0:ce0d1043321f | 55 | { |
madmonkeyman82 | 1:9aeabcc7f4a7 | 56 | pc.printf("Velkommen\r\n"); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 57 | //attach interrup to faling edge |
madmonkeyman82 | 1:9aeabcc7f4a7 | 58 | SW7.fall(&interruptSW7); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 59 | SW6.fall(&interruptSW6); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 60 | |
rlanghbv | 0:ce0d1043321f | 61 | periodTimer.attach(&TurnLightOn,periodTime); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 62 | |
madmonkeyman82 | 1:9aeabcc7f4a7 | 63 | float dutyCycleprv = 0.00f; |
rlanghbv | 0:ce0d1043321f | 64 | |
rlanghbv | 0:ce0d1043321f | 65 | while(1) { |
madmonkeyman82 | 1:9aeabcc7f4a7 | 66 | if(dutyCycleprv != dutyCycle) |
madmonkeyman82 | 1:9aeabcc7f4a7 | 67 | { |
madmonkeyman82 | 1:9aeabcc7f4a7 | 68 | pc.printf("DutyCycle: %.0f%%\r\n", dutyCycle*100); |
madmonkeyman82 | 1:9aeabcc7f4a7 | 69 | dutyCycleprv =dutyCycle; |
madmonkeyman82 | 1:9aeabcc7f4a7 | 70 | } |
rlanghbv | 0:ce0d1043321f | 71 | } |
rlanghbv | 0:ce0d1043321f | 72 | } |