df

Dependencies:   mbed

Fork of APP1 by Team APP

Committer:
dupm2216
Date:
Tue Jan 17 06:01:05 2017 +0000
Revision:
16:c77e34bc69bc
Parent:
15:b38d9d210e32
Child:
18:a21199781d20
Fix everything; ; - Change UART0 (USB) to UART1 (pin 13); - Add the pin select instructions; - Reintroduced the tests run; - Reintegrate with the accelerometer

Who changed what in which revision?

UserRevisionLine numberNew 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 16:c77e34bc69bc 72
GaiSensei 14:2f89279586cb 73 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x400FC0C4), 22, 23, 0x03);
dupm2216 16:c77e34bc69bc 74
dupm2216 16:c77e34bc69bc 75 set_lcr();
GaiSensei 14:2f89279586cb 76 set_baud_rate(9600);
dupm2216 15:b38d9d210e32 77 set_fifo();
dupm2216 16:c77e34bc69bc 78
dupm2216 16:c77e34bc69bc 79 //Pins: Select UART pins through PINSEL registers
dupm2216 16:c77e34bc69bc 80 //Pin Function Select Register 1 (TXD1)
dupm2216 16:c77e34bc69bc 81 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4002C000), 30, 31, 0x01);
GaiSensei 14:2f89279586cb 82 }
GaiSensei 14:2f89279586cb 83
GaiSensei 14:2f89279586cb 84 void HomemadeUART::set_baud_rate(const unsigned int baud_rate)
GaiSensei 14:2f89279586cb 85 {
dupm2216 16:c77e34bc69bc 86 const unsigned int m = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 0, 14) + 1;
dupm2216 16:c77e34bc69bc 87 const unsigned int n = homemade_mbed::read_bits(reinterpret_cast<unsigned int*>(0x400FC088), 16, 23) + 1;
GaiSensei 14:2f89279586cb 88 const unsigned int in_frequency = 4000000; //Hz
dupm2216 16:c77e34bc69bc 89 const unsigned int fcco = (2 * m * in_frequency) / n;
dupm2216 15:b38d9d210e32 90 const unsigned int x = fcco / (baud_rate * 16 * 4);
GaiSensei 14:2f89279586cb 91
dupm2216 16:c77e34bc69bc 92 //Baud rate: In register U1LCR (Table 298), set bit DLAB =1
dupm2216 16:c77e34bc69bc 93 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x01);
dupm2216 16:c77e34bc69bc 94 //Set the baud rate
dupm2216 16:c77e34bc69bc 95 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010000), 0, 7, x); //UART1 Divisor Latch LSB Register (U1DLL
dupm2216 16:c77e34bc69bc 96 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010004), 0, 7, 0x00); //UART1 Divisor Latch MSB Register (U1DLM)
dupm2216 16:c77e34bc69bc 97 //Baud rate: In register U1LCR (Table 298), set bit DLAB =0
dupm2216 16:c77e34bc69bc 98 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 7, 7, 0x00);
GaiSensei 14:2f89279586cb 99 }
GaiSensei 14:2f89279586cb 100
GaiSensei 14:2f89279586cb 101 void HomemadeUART::set_fifo()
GaiSensei 14:2f89279586cb 102 {
dupm2216 16:c77e34bc69bc 103 //UART FIFO: Use bit FIFO enable (bit 0) in register U0FCR (Table 297) to enable FIFO
dupm2216 16:c77e34bc69bc 104 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x40010008), 0, 0, 0x01);
GaiSensei 14:2f89279586cb 105 }
GaiSensei 14:2f89279586cb 106
GaiSensei 14:2f89279586cb 107 void HomemadeUART::set_lcr()
GaiSensei 14:2f89279586cb 108 {
dupm2216 16:c77e34bc69bc 109 homemade_mbed::write_bits(reinterpret_cast<unsigned int*>(0x4001000C), 0, 1, 0x03);
GaiSensei 2:b8a20f7e2912 110 }