mod02 ex02 ok
This commit is contained in:
@@ -118,22 +118,20 @@ void uart_init() {
|
||||
|
||||
UCSR0C |= ASYNCHRONOUS | PARITY_DISABLED | STOP_ONE_BIT | DATA_EIGHT_BIT; // 20.11.4 : set Frame Format
|
||||
|
||||
UCSR0B |= RECEIVER_DISABLED | TRANSMITTER_ENABLED; // 20.11.3 : enable Receiver and/or Transmitter
|
||||
UCSR0B |= RECEIVER_ENABLED | TRANSMITTER_ENABLED; // 20.11.3 : enable Receiver and/or Transmitter
|
||||
}
|
||||
|
||||
void uart_tx(char c) {
|
||||
while (TEST(UCSR0A, UDRE0) == 0); // 20.11.2 : do nothing until UDR emission buffer is empty, if UDREn flag is 1, UCSRnA register is empty
|
||||
while (TEST(UCSR0A, UDRE0) == 0); // 20.11.2 : do nothing until UDRn buffer is empty, (UDREn flag in UCSRnA register set to 1 when buffer empty)
|
||||
UDR0 = (unsigned char) c; // 20.11.1 : Put data into buffer, UDRn – USART I/O Data Register (read and write)
|
||||
}
|
||||
|
||||
void uart_printstr(const char* str) {
|
||||
while (*str) {
|
||||
uart_tx(*str);
|
||||
str++;
|
||||
}
|
||||
char uart_rx(void) {
|
||||
while (TEST(UCSR0A, RXC0) == 0); // 20.11.2 : do nothing until there are unread data in the receive buffer (UDRn), (RXCn flag in UCSRnA register set to 1 when buffer has data)
|
||||
return UDR0; // 20.11.1 : get data in buffer, UDRn – USART I/O Data Register (read and write)
|
||||
}
|
||||
|
||||
// print hello world, on serial port, every 2 seconds, with empty infinite loop
|
||||
// send back caracters received on serial port
|
||||
// `screen /dev/ttyUSB0 115200`
|
||||
int main() {
|
||||
uart_init();
|
||||
@@ -141,17 +139,13 @@ int main() {
|
||||
TCCR1A |= CTC_TOP_OCR1A_IN_TCCR1A; // Table 16-4 : set timer in CTC (Clear Time on Compare) mode
|
||||
TCCR1B |= CTC_TOP_OCR1A_IN_TCCR1B;
|
||||
|
||||
sei(); // enable global interrupts (https://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html#gaad5ebd34cb344c26ac87594f79b06b73)
|
||||
TIMSK1 |= INTERRUPT_ENABLE_CHANNEL_A; // 16.11.8 : enables the Timer1 Compare Match A interrupt, this makes the MCU react when OCR1A == TCNT1 by calling TIMER1_COMPA_vect
|
||||
|
||||
OCR1A = TIME_MS(PERIOD); // Table 16-4 : set CTC compare value on channel A, the counter is cleared to zero when the counter value (TCNT1) matches the OCR1A register
|
||||
|
||||
TCCR1B |= (PRESCALE_SET(PRESCALE_VALUE)); // 16.4 : set timer according to prescale value, in register TCCR1B, table 16-5 : prescale sets
|
||||
|
||||
while(1);
|
||||
}
|
||||
|
||||
// ISR interrupt macro : https://www.nongnu.org/avr-libc/user-manual/group__avr__interrupts.html
|
||||
ISR(TIMER1_COMPA_vect) { // Table 12-7 : we select the code for timer 1 on channel A
|
||||
uart_printstr("Hello World!");
|
||||
|
||||
char received_char;
|
||||
while(1) {
|
||||
received_char = uart_rx();
|
||||
uart_tx(received_char);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user