Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
UARTDisplayer.cpp@15:b38d9d210e32, 2017-01-17 (annotated)
- Committer:
- dupm2216
- Date:
- Tue Jan 17 00:06:38 2017 +0000
- Revision:
- 15:b38d9d210e32
- Parent:
- 14:2f89279586cb
- Child:
- 16:c77e34bc69bc
Fini?
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
GaiSensei | 2:b8a20f7e2912 | 1 | #include "UARTDisplayer.hpp" |
GaiSensei | 13:bb9669053eb3 | 2 | #include <cstdio> |
GaiSensei | 13:bb9669053eb3 | 3 | #include "Utility.hpp" |
GaiSensei | 14:2f89279586cb | 4 | #include "HomemadeMbed.hpp" |
GaiSensei | 13:bb9669053eb3 | 5 | #include "mbed.h" //Needed for function wait |
GaiSensei | 14:2f89279586cb | 6 | #include <cassert> |
GaiSensei | 2:b8a20f7e2912 | 7 | |
GaiSensei | 13:bb9669053eb3 | 8 | UARTDisplayer::UARTDisplayer(PinName tx_pin) : |
GaiSensei | 13:bb9669053eb3 | 9 | device(tx_pin) |
GaiSensei | 2:b8a20f7e2912 | 10 | { |
GaiSensei | 2:b8a20f7e2912 | 11 | |
GaiSensei | 2:b8a20f7e2912 | 12 | } |
GaiSensei | 2:b8a20f7e2912 | 13 | |
GaiSensei | 2:b8a20f7e2912 | 14 | void UARTDisplayer::displayAngle(float angle) |
GaiSensei | 2:b8a20f7e2912 | 15 | { |
GaiSensei | 13:bb9669053eb3 | 16 | char digits[6]; |
GaiSensei | 13:bb9669053eb3 | 17 | std::snprintf(digits, sizeof digits, "%f", angle); |
GaiSensei | 2:b8a20f7e2912 | 18 | |
GaiSensei | 2:b8a20f7e2912 | 19 | if(digits[1] == '.') |
GaiSensei | 2:b8a20f7e2912 | 20 | { |
GaiSensei | 2:b8a20f7e2912 | 21 | device.write(0); |
GaiSensei | 2:b8a20f7e2912 | 22 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 23 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 24 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 25 | device.write((int)digits[2] - 48); |
GaiSensei | 2:b8a20f7e2912 | 26 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 27 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 28 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 29 | } |
GaiSensei | 2:b8a20f7e2912 | 30 | else |
GaiSensei | 2:b8a20f7e2912 | 31 | { |
GaiSensei | 2:b8a20f7e2912 | 32 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 33 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 34 | device.write((int)digits[1] - 48); |
GaiSensei | 2:b8a20f7e2912 | 35 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 36 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 37 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 38 | device.write((int)digits[4] - 48); |
GaiSensei | 2:b8a20f7e2912 | 39 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 40 | } |
GaiSensei | 2:b8a20f7e2912 | 41 | |
GaiSensei | 2:b8a20f7e2912 | 42 | device.write(DECIMAL_CONTROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 43 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 44 | device.write(FLOATING_POINT_DOT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 45 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 46 | } |
GaiSensei | 2:b8a20f7e2912 | 47 | |
GaiSensei | 2:b8a20f7e2912 | 48 | void UARTDisplayer::reset() |
GaiSensei | 2:b8a20f7e2912 | 49 | { |
GaiSensei | 13:bb9669053eb3 | 50 | device.write(CLEAR_DISPLAY_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 51 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 52 | device.write(CURSOR_CONROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 53 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 54 | device.write(MOST_LEFT_DIGIT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 55 | wait(0.001); |
GaiSensei | 13:bb9669053eb3 | 56 | } |
GaiSensei | 13:bb9669053eb3 | 57 | |
GaiSensei | 13:bb9669053eb3 | 58 | HomemadeUART::HomemadeUART(PinName tx_pin) : |
GaiSensei | 13:bb9669053eb3 | 59 | tx_pin(tx_pin) |
GaiSensei | 13:bb9669053eb3 | 60 | { |
GaiSensei | 13:bb9669053eb3 | 61 | } |
GaiSensei | 13:bb9669053eb3 | 62 | |
GaiSensei | 14:2f89279586cb | 63 | void HomemadeUART::write(const unsigned char value) |
GaiSensei | 13:bb9669053eb3 | 64 | { |
GaiSensei | 14:2f89279586cb | 65 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, value); |
dupm2216 | 15:b38d9d210e32 | 66 | /*homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, value); |
dupm2216 | 15:b38d9d210e32 | 67 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, value);*/ |
GaiSensei | 13:bb9669053eb3 | 68 | } |
GaiSensei | 13:bb9669053eb3 | 69 | |
GaiSensei | 13:bb9669053eb3 | 70 | void HomemadeUART::init() |
GaiSensei | 13:bb9669053eb3 | 71 | { |
GaiSensei | 14:2f89279586cb | 72 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23, 0x03); |
dupm2216 | 15:b38d9d210e32 | 73 | //printf("set_baud_rate\r\n"); |
dupm2216 | 15:b38d9d210e32 | 74 | //printf("set_baud_rate\rmarde"); |
GaiSensei | 14:2f89279586cb | 75 | set_baud_rate(9600); |
dupm2216 | 15:b38d9d210e32 | 76 | //printf("fifo\r\n"); |
dupm2216 | 15:b38d9d210e32 | 77 | set_fifo(); |
dupm2216 | 15:b38d9d210e32 | 78 | //printf("lcr\r\n"); |
dupm2216 | 15:b38d9d210e32 | 79 | set_lcr(); |
GaiSensei | 14:2f89279586cb | 80 | } |
GaiSensei | 14:2f89279586cb | 81 | |
GaiSensei | 14:2f89279586cb | 82 | void HomemadeUART::set_baud_rate(const unsigned int baud_rate) |
GaiSensei | 14:2f89279586cb | 83 | { |
GaiSensei | 14:2f89279586cb | 84 | const unsigned int m_value = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1; |
GaiSensei | 14:2f89279586cb | 85 | const unsigned int n_value = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1; |
GaiSensei | 14:2f89279586cb | 86 | const unsigned int in_frequency = 4000000; //Hz |
GaiSensei | 14:2f89279586cb | 87 | const unsigned int fcco = (2 * m_value * in_frequency) / n_value; |
dupm2216 | 15:b38d9d210e32 | 88 | const unsigned int x = fcco / (baud_rate * 16 * 4); |
GaiSensei | 14:2f89279586cb | 89 | |
dupm2216 | 15:b38d9d210e32 | 90 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 7, 7, 0x01); |
GaiSensei | 14:2f89279586cb | 91 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C000), 0, 7, x); |
dupm2216 | 15:b38d9d210e32 | 92 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 7, 7, 0x00); |
dupm2216 | 15:b38d9d210e32 | 93 | |
dupm2216 | 15:b38d9d210e32 | 94 | /*homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 7, 7, 0x01); |
GaiSensei | 14:2f89279586cb | 95 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098000), 0, 7, x); |
dupm2216 | 15:b38d9d210e32 | 96 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 7, 7, 0x00); |
dupm2216 | 15:b38d9d210e32 | 97 | |
dupm2216 | 15:b38d9d210e32 | 98 | utility::blink(); |
dupm2216 | 15:b38d9d210e32 | 99 | |
dupm2216 | 15:b38d9d210e32 | 100 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 7, 7, 0x01); |
GaiSensei | 14:2f89279586cb | 101 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C000), 0, 7, x); |
dupm2216 | 15:b38d9d210e32 | 102 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 7, 7, 0x00);*/ |
dupm2216 | 15:b38d9d210e32 | 103 | |
dupm2216 | 15:b38d9d210e32 | 104 | |
dupm2216 | 15:b38d9d210e32 | 105 | //const unsigned char x_char = x; |
dupm2216 | 15:b38d9d210e32 | 106 | //assert(x == x_char); |
dupm2216 | 15:b38d9d210e32 | 107 | //printf("allo\r\n"); |
dupm2216 | 15:b38d9d210e32 | 108 | |
dupm2216 | 15:b38d9d210e32 | 109 | // utility::blink(); |
GaiSensei | 14:2f89279586cb | 110 | } |
GaiSensei | 14:2f89279586cb | 111 | |
GaiSensei | 14:2f89279586cb | 112 | void HomemadeUART::set_fifo() |
GaiSensei | 14:2f89279586cb | 113 | { |
dupm2216 | 15:b38d9d210e32 | 114 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C008), 0, 0, 0x01); |
dupm2216 | 15:b38d9d210e32 | 115 | /*homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40098008), 0, 0, 0x01); |
dupm2216 | 15:b38d9d210e32 | 116 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4007C008), 0, 0, 0x01);*/ |
GaiSensei | 14:2f89279586cb | 117 | } |
GaiSensei | 14:2f89279586cb | 118 | |
GaiSensei | 14:2f89279586cb | 119 | void HomemadeUART::set_lcr() |
GaiSensei | 14:2f89279586cb | 120 | { |
dupm2216 | 15:b38d9d210e32 | 121 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4000C00C), 0, 1, 0x03); |
dupm2216 | 15:b38d9d210e32 | 122 | /* homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009800C), 0, 1, 0x03); |
dupm2216 | 15:b38d9d210e32 | 123 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4009C00C), 0, 1, 0x03);*/ |
GaiSensei | 2:b8a20f7e2912 | 124 | } |