Analog loopback test p18 - p20 LED Bar Graph Display

## main.cpp@1:0286d548948b, 2010-08-28 (annotated)

- Committer:
- 4180_1
- Date:
- Sat Aug 28 02:44:40 2010 +0000
- Revision:
- 1:0286d548948b
- Parent:
- 0:045a3e5d8f7d

### Who changed what in which revision?

User | Revision | Line number | New contents of line |
---|---|---|---|

4180_1 | 0:045a3e5d8f7d | 1 | #include "mbed.h" |

4180_1 | 0:045a3e5d8f7d | 2 | |

4180_1 | 0:045a3e5d8f7d | 3 | AnalogOut aout(p18); |

4180_1 | 0:045a3e5d8f7d | 4 | AnalogIn ain(p20); |

4180_1 | 0:045a3e5d8f7d | 5 | // Jumper p18 to p20 for analog loopback test |

4180_1 | 0:045a3e5d8f7d | 6 | BusOut led_state(LED1, LED2, LED3, LED4); |

4180_1 | 0:045a3e5d8f7d | 7 | // Analog input connected to analog output will display in LED Bar Graph |

4180_1 | 0:045a3e5d8f7d | 8 | int LED_BarGraph(float max_value, float value); |

4180_1 | 0:045a3e5d8f7d | 9 | |

4180_1 | 0:045a3e5d8f7d | 10 | int main() { |

4180_1 | 0:045a3e5d8f7d | 11 | while (1) { |

4180_1 | 0:045a3e5d8f7d | 12 | for (float i=0.0; i<1.0; i+=0.1) { |

4180_1 | 1:0286d548948b | 13 | aout = (1 + sin(i*2*3.14159265))/2; |

4180_1 | 0:045a3e5d8f7d | 14 | LED_BarGraph(1.0, ain); |

4180_1 | 0:045a3e5d8f7d | 15 | wait(0.1); |

4180_1 | 0:045a3e5d8f7d | 16 | } |

4180_1 | 0:045a3e5d8f7d | 17 | } |

4180_1 | 0:045a3e5d8f7d | 18 | } |

4180_1 | 0:045a3e5d8f7d | 19 | |

4180_1 | 0:045a3e5d8f7d | 20 | int LED_BarGraph(float max_value,float value) { |

4180_1 | 0:045a3e5d8f7d | 21 | int scale; |

4180_1 | 0:045a3e5d8f7d | 22 | scale = (value*4.5)/max_value; |

4180_1 | 0:045a3e5d8f7d | 23 | switch (scale) { |

4180_1 | 0:045a3e5d8f7d | 24 | case 0: |

4180_1 | 0:045a3e5d8f7d | 25 | led_state = 0x00; |

4180_1 | 0:045a3e5d8f7d | 26 | break; |

4180_1 | 0:045a3e5d8f7d | 27 | case 1: |

4180_1 | 0:045a3e5d8f7d | 28 | led_state = 0x01; |

4180_1 | 0:045a3e5d8f7d | 29 | break; |

4180_1 | 0:045a3e5d8f7d | 30 | case 2: |

4180_1 | 0:045a3e5d8f7d | 31 | led_state = 0x03; |

4180_1 | 0:045a3e5d8f7d | 32 | break; |

4180_1 | 0:045a3e5d8f7d | 33 | case 3: |

4180_1 | 0:045a3e5d8f7d | 34 | led_state = 0x07; |

4180_1 | 0:045a3e5d8f7d | 35 | break; |

4180_1 | 0:045a3e5d8f7d | 36 | default: |

4180_1 | 0:045a3e5d8f7d | 37 | led_state = 0x0F; |

4180_1 | 0:045a3e5d8f7d | 38 | break; |

4180_1 | 0:045a3e5d8f7d | 39 | } |

4180_1 | 0:045a3e5d8f7d | 40 | return 0; |

4180_1 | 0:045a3e5d8f7d | 41 | } |