Microchip Studio#研究UART接收与同步,求程序流程框图

/*

  • GccApplication7.c
  • Experiment on entering the synchronization when transmitting data via UART
  • After starting the program, the Rx input of the microcontroller receives a continuous
  • A data stream at a given rate, consisting of repeating bytes according to the assignment
  • The program receives bytes, checks for errors via standard UART flags,
  • if there are reception errors, then the '.' symbol is transmitted, if there are no reception errors,
  • it transmits the received symbol.
  • After 10 bytes, there is a reception ban, pause, then the reception is allowed,
  • there is a synchronization and then 10 bytes are transmitted again.
  • To fix the results in real execution, the terminal program on the bench is used.
  • Stimulus files are used to record results at home,
  • and recording the transmitted bytes in the log file through port C
  • Created : 18.03.2021 13:51:48
  • Author : SBushuev
  • /

#define F_CPU 8000000UL
#define myUBRR 51 // 9600 b/s
#define myU2X 0
#include <avr/io.h>
#include <avr/interrupt.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>

void USART_Init( unsigned int speed)//initialization USART by given value of UBRR register, which should be calculated by BoudRate
/* if U2X=0
UBRR=Fosc/(16BaudRate)-1
BaudRate=Fosc/16(UBRR+1)
at U2X=1
UBRR=Fosc/(8
BaudRate)-1
BaudRate=Fosc/8(UBRR+1)
*/
{
UBRRH = (unsigned char)(speed>>8);
UBRRL = (unsigned char)speed;

UCSRB=(1<<RXEN)|( 1<TXEN); //transmit and receive USART
#if (myU2X==1) //the following line will only be compiled if myU2X=1
    UCSRA |= (1<<U2X); //transmit rate doublet
#endif
UCSRC = (1<<URSEL)|(1<USBS)|(1<<UCSZ1)|(1<UCSZ0);// 8-bit transmission, 1 stop bit 

}

void USART_Transmit( unsigned char data ) //Transmit one byte to USART channel
{
while ( ((UCSRA & (1<<UDRE)) ); //Checking for data register clearing (readiness for the next byte loading)
UDR = data; //loading data into the register for transmitting
UCSRA |= (1<TXC); //Reset transmission end bit
}

int main (void)
{
char ch='O';
int i;
DDRC=0xFF;
USART_Init(myUBRR);
while (true) {
i=0;
do {
PORTC=i;//put loop number to C port, in order to prepare fixation of a new received symbol in the log-file
if (UCSRA & (1<RXC)){ //check USART channel information
ch=UDR;//read the receiver buffer to prepare receiving the next byte
if (UCSRA & ((1<FE)|(1<DOR))){ //check for a reception error
ch='.';//receive byte with error and replace with '.'
PORTC=0xFF;//for easy identification of the situation in the log file
}
//no reception errors
PORTC=ch;//write the received byte in the C port to be fixed in the log-file
USART_Transmit(ch);
i++;
}
}
while (i<10);

    while ( ((UCSRA & (1<TXC)) ); //Check if the shift register is cleared (transfer completed)
    //release and re-enable uart, 
    //to induce the necessity of a new receiving synchronization (search for the start bit)
    UCSRB &= ~((1<<RXEN)|( 1<<TXEN));// disable UART receive-transfer, this resets the accumulated received bits in the buffer.
    PORTC=0; // for fixing the moment of prohibiting UART in the log-file
    for (i=0;i<(3*myUBRR);i++);//take the time equal to the time of transmission of some bits
    UCSRB|=((1<<RXEN)|( 1<TXEN));//transmit/receive UART, start bit search 
    PORTC=1;// for fixing the moment of UART enable in the log file
}

}