I am unable to read answer string from modem, stuck in the row WHILE (!(USART0.STATUS & USART_RXCIF_bm)) in USART_0_read(). What's wrong in the code?
/* * File: main.c * ATtiny202, MPLAB X IDE, XC8, Snap Debug Tool */ #define F_CPU 3333333 #include <xc.h> #include <util/delay.h> // _delay_ms() #define USART0_BAUD_RATE(BAUD_RATE) ((float)(3333333 * 64 / (16 * (float)BAUD_RATE)) + 0.5) char AT_Send_OK[] = "AT\r"; // 3x char OK_buff[16]; // 16x char c; // Read char int idx; // Index to OK_buff //Disable digital input buffer on all IO pins void USART_0_write(const uint8_t data) { while (!(USART0.STATUS & USART_DREIF_bm)) ; USART0.TXDATAL = data; } uint8_t USART_0_read() { while (!(USART0.STATUS & USART_RXCIF_bm)) ; return USART0.RXDATAL; } void Send_AT_CMD(char str[], int n_chars) { for (int n = 0; n < n_chars; n++) { USART_0_write(str[n]); while (!(USART0.STATUS & USART_TXCIF_bm)); //wait for USART TX complete USART0.STATUS = USART_TXCIF_bm; //Clear TXCIF flag } } void Read_AT_OK() { idx = 0; do { c = USART_0_read(); asm("nop"); OK_buff[idx++] = c; if (idx > 16) // More than 16 chars ? idx = 0; } while (c != ('\r' || '\n')); do c = USART_0_read(); // Flush '\n' while (c != '\n'); OK_buff[idx++] = '\0'; } int main() { VPORTA.DIR |= PIN6_bm; // Configure USART TX pin as output pin 2 (ATtiny202) VPORTA.DIR &= ~PIN7_bm; // Configure USART RX pin as input pin 3 USART0.BAUD = (uint16_t) USART0_BAUD_RATE(9600); USART0.CTRLB |= USART_TXEN_bm; // Enable Transmitter USART0.CTRLB |= USART_RXEN_bm; // Enable Reciever while (1) { Send_AT_CMD(AT_Send_OK, 3); // Send AT command to modem ("AT\r") _delay_ms(100); // Some delay to finish with send and to get reply from modem Read_AT_OK(); // Input string is 4F 4B 0D 0A ("OK\r\n") asm("nop"); _delay_ms(500); } }