Laurent Huot
/
SPI
drtgddf
main.cpp@1:2b2be189b4db, 2016-01-11 (annotated)
- Committer:
- larryspaghetti
- Date:
- Mon Jan 11 21:09:20 2016 +0000
- Revision:
- 1:2b2be189b4db
- Parent:
- 0:aab1f335cf6b
f
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
larryspaghetti | 0:aab1f335cf6b | 1 | #include "mbed.h" |
larryspaghetti | 1:2b2be189b4db | 2 | #include "LPC17xx.h" |
larryspaghetti | 0:aab1f335cf6b | 3 | |
larryspaghetti | 0:aab1f335cf6b | 4 | |
larryspaghetti | 0:aab1f335cf6b | 5 | int main() { |
larryspaghetti | 1:2b2be189b4db | 6 | UART3_init(); |
larryspaghetti | 1:2b2be189b4db | 7 | |
larryspaghetti | 1:2b2be189b4db | 8 | UART3_putc('C'); |
larryspaghetti | 1:2b2be189b4db | 9 | } |
larryspaghetti | 1:2b2be189b4db | 10 | |
larryspaghetti | 1:2b2be189b4db | 11 | void UART3_init() |
larryspaghetti | 1:2b2be189b4db | 12 | { |
larryspaghetti | 1:2b2be189b4db | 13 | const uint32_t baudrate = 9600; |
larryspaghetti | 1:2b2be189b4db | 14 | |
larryspaghetti | 1:2b2be189b4db | 15 | //On "power on" le module UART3 |
larryspaghetti | 1:2b2be189b4db | 16 | LPC_SC->PCONP |= (1<<25); |
larryspaghetti | 1:2b2be189b4db | 17 | |
larryspaghetti | 1:2b2be189b4db | 18 | //On met le device clock a CPU CLOCK/1 |
larryspaghetti | 1:2b2be189b4db | 19 | LPC_SC->PCLKSEL1 &= ~(3<<18); |
larryspaghetti | 1:2b2be189b4db | 20 | LPC_SC->PCLKSEL1 |= (1<<18); |
larryspaghetti | 1:2b2be189b4db | 21 | |
larryspaghetti | 1:2b2be189b4db | 22 | //Formule: registreValue = Clock du chip / (16*baudrate) |
larryspaghetti | 1:2b2be189b4db | 23 | uint8_t dllRegValue = 96000000 / (16 * baudrate); |
larryspaghetti | 1:2b2be189b4db | 24 | |
larryspaghetti | 1:2b2be189b4db | 25 | //On set le bit DLAB a 1 |
larryspaghetti | 1:2b2be189b4db | 26 | LPC_UART3->LCR = (1 << 7); |
larryspaghetti | 1:2b2be189b4db | 27 | //On donne la valeur au registre DLL pour que le baudrate soit 9600 |
larryspaghetti | 1:2b2be189b4db | 28 | LPC_UART3->DLL = dllRegValue; |
larryspaghetti | 1:2b2be189b4db | 29 | |
larryspaghetti | 1:2b2be189b4db | 30 | //8 bit de message, 1 bit de stop, pas de bit de parite |
larryspaghetti | 1:2b2be189b4db | 31 | LPC_UART3->LCR = 3; |
larryspaghetti | 1:2b2be189b4db | 32 | |
larryspaghetti | 1:2b2be189b4db | 33 | //On allume le FIFO |
larryspaghetti | 1:2b2be189b4db | 34 | LPC_UART3->FCR = 1; |
larryspaghetti | 1:2b2be189b4db | 35 | |
larryspaghetti | 1:2b2be189b4db | 36 | //Selection des pins pour UART3 |
larryspaghetti | 1:2b2be189b4db | 37 | LPC_PINCON->PINSEL0 |= 0x00000002; |
larryspaghetti | 1:2b2be189b4db | 38 | |
larryspaghetti | 1:2b2be189b4db | 39 | |
larryspaghetti | 1:2b2be189b4db | 40 | } |
larryspaghetti | 1:2b2be189b4db | 41 | |
larryspaghetti | 1:2b2be189b4db | 42 | void UART3_putc(char c) |
larryspaghetti | 1:2b2be189b4db | 43 | { |
larryspaghetti | 1:2b2be189b4db | 44 | //Il faut attendre que le transfert soit fini pour envoyer un deuxieme caractere |
larryspaghetti | 1:2b2be189b4db | 45 | LPC_UART3->THR = c; |
larryspaghetti | 1:2b2be189b4db | 46 | |
larryspaghetti | 1:2b2be189b4db | 47 | //Tant que le registre de transmiter n'est pas vide |
larryspaghetti | 1:2b2be189b4db | 48 | while (true) |
larryspaghetti | 1:2b2be189b4db | 49 | { |
larryspaghetti | 1:2b2be189b4db | 50 | if (LPC_UART3->LSR & (1 << 5)) |
larryspaghetti | 1:2b2be189b4db | 51 | { |
larryspaghetti | 1:2b2be189b4db | 52 | break; |
larryspaghetti | 1:2b2be189b4db | 53 | } |
larryspaghetti | 0:aab1f335cf6b | 54 | } |
larryspaghetti | 1:2b2be189b4db | 55 | } |