Simon NOWAK
/
RegistreSerial
Project to use Serial with registers
main.cpp@2:1deea3b4119e, 2017-04-05 (annotated)
- Committer:
- SimonNOWAK
- Date:
- Wed Apr 05 14:12:05 2017 +0000
- Revision:
- 2:1deea3b4119e
- Parent:
- 1:2b6896966307
- Child:
- 3:fba5fc24faff
Update
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
SimonNOWAK | 0:25823c5e440a | 1 | #include "mbed.h" |
SimonNOWAK | 0:25823c5e440a | 2 | |
SimonNOWAK | 0:25823c5e440a | 3 | void usartSetup (void) { |
SimonNOWAK | 2:1deea3b4119e | 4 | RCC->IOPENR |= RCC_IOPENR_IOPBEN; |
SimonNOWAK | 0:25823c5e440a | 5 | RCC->APB2ENR |= RCC_APB2ENR_USART1EN; // enable clock for USART1 |
SimonNOWAK | 2:1deea3b4119e | 6 | USART1->BRR = 320000 / 96; // set baudrate |
SimonNOWAK | 2:1deea3b4119e | 7 | GPIOB->MODER &= ~GPIO_MODER_MODE6_0; |
SimonNOWAK | 2:1deea3b4119e | 8 | GPIOB->MODER |= GPIO_MODER_MODE6_1; |
SimonNOWAK | 1:2b6896966307 | 9 | |
SimonNOWAK | 2:1deea3b4119e | 10 | GPIOB->AFR[1] |= 0x40; |
SimonNOWAK | 2:1deea3b4119e | 11 | GPIOB->OTYPER &= ~GPIO_OTYPER_OT_6; |
SimonNOWAK | 1:2b6896966307 | 12 | USART1->CR1 |= (USART_CR1_RE | USART_CR1_TE | USART_CR1_UE); // RX, TX enable |
SimonNOWAK | 0:25823c5e440a | 13 | } |
SimonNOWAK | 0:25823c5e440a | 14 | |
SimonNOWAK | 0:25823c5e440a | 15 | void SendChar(){ |
SimonNOWAK | 1:2b6896966307 | 16 | Serial pc(USBTX, USBRX); |
SimonNOWAK | 1:2b6896966307 | 17 | pc.printf("TXE: %02X\r\n", USART1->ISR); |
SimonNOWAK | 2:1deea3b4119e | 18 | char stringtosend[] = {"Salut ca va ? Oui et toi ? Oh yes ca fonctionne bien ahahahahahahahahahahahah\r\n"}; |
SimonNOWAK | 2:1deea3b4119e | 19 | for(int send = 0; send < sizeof(stringtosend); send++){ |
SimonNOWAK | 2:1deea3b4119e | 20 | //On attend que la transmission soit terminée |
SimonNOWAK | 2:1deea3b4119e | 21 | while((USART1->ISR & USART_ISR_TC) != USART_ISR_TC); |
SimonNOWAK | 2:1deea3b4119e | 22 | |
SimonNOWAK | 2:1deea3b4119e | 23 | //Si on a tout envoyé |
SimonNOWAK | 2:1deea3b4119e | 24 | if(send == sizeof(stringtosend)) |
SimonNOWAK | 2:1deea3b4119e | 25 | { |
SimonNOWAK | 2:1deea3b4119e | 26 | send=0; |
SimonNOWAK | 2:1deea3b4119e | 27 | USART1->ICR = USART_ICR_TCCF; /* Clear transfer complete flag */ |
SimonNOWAK | 2:1deea3b4119e | 28 | } |
SimonNOWAK | 2:1deea3b4119e | 29 | else |
SimonNOWAK | 2:1deea3b4119e | 30 | { |
SimonNOWAK | 2:1deea3b4119e | 31 | //Si le registre de données est vide |
SimonNOWAK | 2:1deea3b4119e | 32 | while (!(USART1->ISR & (USART1->ISR | USART_ISR_TXE))); |
SimonNOWAK | 1:2b6896966307 | 33 | USART1->TDR = stringtosend[send]; |
SimonNOWAK | 1:2b6896966307 | 34 | } |
SimonNOWAK | 2:1deea3b4119e | 35 | |
SimonNOWAK | 0:25823c5e440a | 36 | } |
SimonNOWAK | 0:25823c5e440a | 37 | } |
SimonNOWAK | 0:25823c5e440a | 38 | |
SimonNOWAK | 0:25823c5e440a | 39 | int main() { |
SimonNOWAK | 0:25823c5e440a | 40 | usartSetup(); |
SimonNOWAK | 0:25823c5e440a | 41 | SendChar(); |
SimonNOWAK | 0:25823c5e440a | 42 | } |