Bruno Allaire-Lemay
/
APP1test
df
Fork of APP1 by
UARTDisplayer.cpp@18:a21199781d20, 2017-01-17 (annotated)
- Committer:
- dupm2216
- Date:
- Tue Jan 17 19:45:02 2017 +0000
- Revision:
- 18:a21199781d20
- Parent:
- 16:c77e34bc69bc
- Child:
- 20:3b82955f42f7
Add MovingAverageFilter
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 | |
dupm2216 | 16:c77e34bc69bc | 8 | UARTDisplayer::UARTDisplayer() |
GaiSensei | 2:b8a20f7e2912 | 9 | { |
dupm2216 | 16:c77e34bc69bc | 10 | this->device.init(); |
GaiSensei | 2:b8a20f7e2912 | 11 | } |
GaiSensei | 2:b8a20f7e2912 | 12 | |
GaiSensei | 2:b8a20f7e2912 | 13 | void UARTDisplayer::displayAngle(float angle) |
GaiSensei | 2:b8a20f7e2912 | 14 | { |
GaiSensei | 13:bb9669053eb3 | 15 | char digits[6]; |
GaiSensei | 13:bb9669053eb3 | 16 | std::snprintf(digits, sizeof digits, "%f", angle); |
GaiSensei | 2:b8a20f7e2912 | 17 | |
GaiSensei | 2:b8a20f7e2912 | 18 | if(digits[1] == '.') |
GaiSensei | 2:b8a20f7e2912 | 19 | { |
GaiSensei | 2:b8a20f7e2912 | 20 | device.write(0); |
GaiSensei | 2:b8a20f7e2912 | 21 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 22 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 23 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 24 | device.write((int)digits[2] - 48); |
GaiSensei | 2:b8a20f7e2912 | 25 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 26 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 27 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 28 | } |
GaiSensei | 2:b8a20f7e2912 | 29 | else |
GaiSensei | 2:b8a20f7e2912 | 30 | { |
GaiSensei | 2:b8a20f7e2912 | 31 | device.write((int)digits[0] - 48); |
GaiSensei | 2:b8a20f7e2912 | 32 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 33 | device.write((int)digits[1] - 48); |
GaiSensei | 2:b8a20f7e2912 | 34 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 35 | device.write((int)digits[3] - 48); |
GaiSensei | 2:b8a20f7e2912 | 36 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 37 | device.write((int)digits[4] - 48); |
GaiSensei | 2:b8a20f7e2912 | 38 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 39 | } |
GaiSensei | 2:b8a20f7e2912 | 40 | |
GaiSensei | 2:b8a20f7e2912 | 41 | device.write(DECIMAL_CONTROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 42 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 43 | device.write(FLOATING_POINT_DOT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 44 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 45 | } |
GaiSensei | 2:b8a20f7e2912 | 46 | |
GaiSensei | 2:b8a20f7e2912 | 47 | void UARTDisplayer::reset() |
GaiSensei | 2:b8a20f7e2912 | 48 | { |
GaiSensei | 13:bb9669053eb3 | 49 | device.write(CLEAR_DISPLAY_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 50 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 51 | device.write(CURSOR_CONROL_REGISTER); |
GaiSensei | 2:b8a20f7e2912 | 52 | wait(0.001); |
GaiSensei | 2:b8a20f7e2912 | 53 | device.write(MOST_LEFT_DIGIT_POSITION); |
GaiSensei | 2:b8a20f7e2912 | 54 | wait(0.001); |
GaiSensei | 13:bb9669053eb3 | 55 | } |
GaiSensei | 13:bb9669053eb3 | 56 | |
GaiSensei | 14:2f89279586cb | 57 | void HomemadeUART::write(const unsigned char value) |
GaiSensei | 13:bb9669053eb3 | 58 | { |
dupm2216 | 16:c77e34bc69bc | 59 | //UART1 Transmitter Holding Register |
dupm2216 | 16:c77e34bc69bc | 60 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, value); |
GaiSensei | 13:bb9669053eb3 | 61 | } |
GaiSensei | 13:bb9669053eb3 | 62 | |
GaiSensei | 13:bb9669053eb3 | 63 | void HomemadeUART::init() |
GaiSensei | 13:bb9669053eb3 | 64 | { |
dupm2216 | 16:c77e34bc69bc | 65 | //Power: In the PCONP register (Table 46), set bits PCUART1 |
dupm2216 | 16:c77e34bc69bc | 66 | //Default: enabled |
dupm2216 | 16:c77e34bc69bc | 67 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 4, 4, 0x01); |
dupm2216 | 16:c77e34bc69bc | 68 | |
dupm2216 | 16:c77e34bc69bc | 69 | //Peripheral clock: In the PCLKSEL0 register (Table 40), select PCLK_UART1 |
dupm2216 | 16:c77e34bc69bc | 70 | //Default: PCLK_peripheral = CCLK/4 |
dupm2216 | 16:c77e34bc69bc | 71 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC1A8), 8, 9, 0x00); |
dupm2216 | 18:a21199781d20 | 72 | |
dupm2216 | 16:c77e34bc69bc | 73 | set_lcr(); |
GaiSensei | 14:2f89279586cb | 74 | set_baud_rate(9600); |
dupm2216 | 15:b38d9d210e32 | 75 | set_fifo(); |
dupm2216 | 16:c77e34bc69bc | 76 | |
dupm2216 | 16:c77e34bc69bc | 77 | //Pins: Select UART pins through PINSEL registers |
dupm2216 | 16:c77e34bc69bc | 78 | //Pin Function Select Register 1 (TXD1) |
dupm2216 | 16:c77e34bc69bc | 79 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4002C000), 30, 31, 0x01); |
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 | { |
dupm2216 | 16:c77e34bc69bc | 84 | const unsigned int m = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1; |
dupm2216 | 16:c77e34bc69bc | 85 | const unsigned int n = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1; |
GaiSensei | 14:2f89279586cb | 86 | const unsigned int in_frequency = 4000000; //Hz |
dupm2216 | 16:c77e34bc69bc | 87 | const unsigned int fcco = (2 * m * in_frequency) / n; |
dupm2216 | 15:b38d9d210e32 | 88 | const unsigned int x = fcco / (baud_rate * 16 * 4); |
GaiSensei | 14:2f89279586cb | 89 | |
dupm2216 | 16:c77e34bc69bc | 90 | //Baud rate: In register U1LCR (Table 298), set bit DLAB =1 |
dupm2216 | 16:c77e34bc69bc | 91 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x01); |
dupm2216 | 16:c77e34bc69bc | 92 | //Set the baud rate |
dupm2216 | 16:c77e34bc69bc | 93 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, x); //UART1 Divisor Latch LSB Register (U1DLL |
dupm2216 | 16:c77e34bc69bc | 94 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010004), 0, 7, 0x00); //UART1 Divisor Latch MSB Register (U1DLM) |
dupm2216 | 16:c77e34bc69bc | 95 | //Baud rate: In register U1LCR (Table 298), set bit DLAB =0 |
dupm2216 | 16:c77e34bc69bc | 96 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x00); |
GaiSensei | 14:2f89279586cb | 97 | } |
GaiSensei | 14:2f89279586cb | 98 | |
GaiSensei | 14:2f89279586cb | 99 | void HomemadeUART::set_fifo() |
GaiSensei | 14:2f89279586cb | 100 | { |
dupm2216 | 16:c77e34bc69bc | 101 | //UART FIFO: Use bit FIFO enable (bit 0) in register U0FCR (Table 297) to enable FIFO |
dupm2216 | 16:c77e34bc69bc | 102 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010008), 0, 0, 0x01); |
GaiSensei | 14:2f89279586cb | 103 | } |
GaiSensei | 14:2f89279586cb | 104 | |
GaiSensei | 14:2f89279586cb | 105 | void HomemadeUART::set_lcr() |
GaiSensei | 14:2f89279586cb | 106 | { |
dupm2216 | 16:c77e34bc69bc | 107 | homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 0, 1, 0x03); |
GaiSensei | 2:b8a20f7e2912 | 108 | } |