MyTetra Share
Делитесь знаниями!
UART without interrupts
Время создания: 30.11.2017 14:56
Раздел: Electronics - Microcontrollers - AVR8 - AVR GCC - C_PROJ

//#define F_CPU 8000000L

#define LED_Indicator_DDR DDRB

#define LED_Indicator_PORT PORTB

#define LED_Indicator 0 //LED RX and TX UART indication


#include <avr/io.h> //input-output ports library

#include <avr/interrupt.h>


unsigned char TCNT; //timer counter

unsigned char pause; //for pause of LED_Indicator blinking

unsigned char Counter; //for more longer pause

unsigned char UART_data; //blinking counter


//====================

// INTERNAL INT SECTION

//====================

ISR(TIMER0_OVF_vect)

{

//For LED_Indicator blinking

//It will blink if UART sends or receives data packet

TCNT++;

if (TCNT > 0x10 /*&& Blink_count >> 0*/)

{

/* LED_Indicator_PORT ^= 1 << LED_Indicator; //invert LED PORT

TCNT = 0x00;

Blink_count -= 1;

*/

TCNT = 0;

Counter = 1;

pause -= 1;

}

}


//====================

// FUNCTION SECTION

//====================

//byte sending function:

void send_UART(unsigned char x)

{

while (UDRE == 0) //waiting while we have data to transmit

{}

UDR = x; //send data to UDR after UDRE==1

}


//byte receiving function:

unsigned receiv_UART(void)

{

while (RXC == 0) //waiting while receiving isn't ended

{}

return UDR; //return UDR after RXC==1

}


//====================

// MAIN SECTION

//====================

void main(void)

{


//====================

// HARDWARE INIT SECTION

//====================

//Timer0_START

TCCR0 = (TCCR0 | 1<<CS00) | (TCCR0 | 0<<CS01) | (TCCR0 | 1<<CS02); //Prescaler x1024

TIMSK |= 1<<TOIE0; //Enable TIMER0 interrupts

//Timer0_END


//UART_init_START

UBRRH = 0; //UBBR = f/(16*band)-1; f=8000000 Hz, band=9600

UBRRL = 51; //Normal dual asynchronous mode


UCSRA = 0x00;

// RXC - receive ending

// |TXC - transmit ending

// ||UDRE - no data to transmit

// |||FE - frame error

// ||||DOR - buffer overflow error

// |||||PE - parity error

// ||||||U2X - dual speed

// |||||||MPCM - multiprocessor mode

// 76543210

// 00000000 - UCSRA


UCSRB = (UCSRB | 1<<RXEN) | (UCSRB | 1<<TXEN); //TX and RX enable

// RXCIE - receive data interrupt

// |TXCIE - interrupt after transmitting data

// ||UDRIE - interrupt after no data to transmit

// |||RXEN - enable receive

// ||||TXEN - enable transmit

// |||||UCSZ2 - UCSZ0..2 size of data frame

// ||||||RXB8 - 9 bits of received data

// |||||||TXB8 - 9 bits of transmitted data

// 76543210

// 00011000 - UCSRB


UCSRC = (UCSRC | 1<<URSEL) | (UCSRC | 1<<UCSZ0) | (UCSRC | 1<<UCSZ1);

// URSEL - always 1

// |UMSEL - 0 - asynchronous, 1 - synchronous

// ||UPM1 - UPM0..1 - parity

// |||UPM0 - UPM0..1 - parity

// ||||USBS - stop-bits: 0 - 1, 1 - 2

// |||||UCSZ1 - UCSZ0..2 size of data frame

// ||||||UCSZ0 - UCSZ0..2 size of data frame

// |||||||UCPOL- clocking only for synchronous mode

// 76543210

// 10000110 - UCSRC


//UDR - register for reading and writing data

//UART_init_END


LED_Indicator_DDR |= 1 <<LED_Indicator; //set DDRB 0

// LED_Indicator_PORT |= 1 <<LED_Indicator; //set PORTB 0

LED_Indicator_DDR |= 1 <<1; //set DDRB 1

LED_Indicator_PORT |= 1 <<1; //set PORTB 1

//====================

// VARIABLE SECTION

//====================

UART_data = 0x00;

Counter = 0x00;

TCNT = 0x00;


sei();


//====================

// MAIN LOOP CYCLE

//====================

while(1)

{

if(UART_data == 0 && pause <= 0)

{

send_UART(3);

UART_data = receiv_UART() *2;

}


if(UART_data > 0 && Counter == 1)

{

LED_Indicator_PORT ^= 1 <<LED_Indicator; //set PORTB 0

Counter = 0;

UART_data -= 1;

pause = 3;

}

}

}



Второй вариант для передачи инфы через СОМ в консольку:


//#define F_CPU 8000000L

#define LED_Indicator_DDR DDRB

#define LED_Indicator_PORT PORTB

#define LED_Indicator 0 //LED RX and TX UART indication


#include <avr/io.h> //input-output ports library

#include <avr/interrupt.h>


unsigned char TCNT; //timer counter

unsigned char pause; //for pause of LED_Indicator blinking

unsigned char Counter; //for more longer pause

unsigned int UART_data; //blinking counter


//====================

// INTERNAL INT SECTION

//====================

ISR(TIMER0_OVF_vect)

{

//For LED_Indicator blinking

//It will blink if UART sends or receives data packet

TCNT++;

if (TCNT > 0x10 /*&& Blink_count >> 0*/)

{

/* LED_Indicator_PORT ^= 1 << LED_Indicator; //invert LED PORT

TCNT = 0x00;

Blink_count -= 1;

*/

TCNT = 0;

Counter = 1;

pause -= 1;

}

}


//====================

// FUNCTION SECTION

//====================

//byte sending function:

void send_UART(unsigned char x)

{

//"while ( !(UCSRA & (1<<UDRE)) )" - taken from datasheet (USART - Data transmission)

while ( !(UCSRA & (1<<UDRE)) ) //waiting while we have data to transmit

{}

UDR = x; //send data to UDR after UDRE==1

}


//byte receiving function:

unsigned receiv_UART(void)

{

//"while ( !(UCSRA & (1<<RXC)) )" - taken from datasheet (USART - Data transmission)

while ( !(UCSRA & (1<<RXC)) ) //waiting while receiving isn't ended

{}

return UDR; //return UDR after RXC==1

}


//====================

// MAIN SECTION

//====================

void main(void)

{


//====================

// HARDWARE INIT SECTION

//====================

//Timer0_START

TCCR0 = (TCCR0 | 1<<CS00) | (TCCR0 | 0<<CS01) | (TCCR0 | 1<<CS02); //Prescaler x1024

TIMSK |= 1<<TOIE0; //Enable TIMER0 interrupts

//Timer0_END


//UART_init_START

UBRRH = 0; //UBBR = f/(16*band)-1; f=8000000 Hz, band=9600

UBRRL = 51; //Normal dual asynchronous mode


UCSRA = 0x00;

// RXC - receive ending

// |TXC - transmit ending

// ||UDRE - no data to transmit

// |||FE - frame error

// ||||DOR - buffer overflow error

// |||||PE - parity error

// ||||||U2X - dual speed

// |||||||MPCM - multiprocessor mode

// 76543210

// 00000000 - UCSRA


UCSRB = (UCSRB | 1<<RXEN) | (UCSRB | 1<<TXEN); //TX and RX enable

// RXCIE - receive data interrupt

// |TXCIE - interrupt after transmitting data

// ||UDRIE - interrupt after no data to transmit

// |||RXEN - enable receive

// ||||TXEN - enable transmit

// |||||UCSZ2 - UCSZ0..2 size of data frame

// ||||||RXB8 - 9 bits of received data

// |||||||TXB8 - 9 bits of transmitted data

// 76543210

// 00011000 - UCSRB


UCSRC = (UCSRC | 1<<URSEL) | (UCSRC | 1<<UCSZ0) | (UCSRC | 1<<UCSZ1);

// URSEL - always 1

// |UMSEL - 0 - asynchronous, 1 - synchronous

// ||UPM1 - UPM0..1 - parity

// |||UPM0 - UPM0..1 - parity

// ||||USBS - stop-bits: 0 - 1, 1 - 2

// |||||UCSZ1 - UCSZ0..2 size of data frame

// ||||||UCSZ0 - UCSZ0..2 size of data frame

// |||||||UCPOL- clocking only for synchronous mode

// 76543210

// 10000110 - UCSRC


//UDR - register for reading and writing data

//UART_init_END


LED_Indicator_DDR |= 1 <<LED_Indicator; //set DDRB 0

LED_Indicator_PORT |= 1 <<LED_Indicator; //set PORTB 0

LED_Indicator_DDR |= 1 <<1; //set DDRB 1

LED_Indicator_PORT |= 1 <<1; //set PORTB 1

//====================

// VARIABLE SECTION

//====================

UART_data = 0x00;

Counter = 0x00;

TCNT = 0x00;


sei();


//====================

// MAIN LOOP CYCLE

//====================

while(1)

{

UART_data = receiv_UART();


if(UART_data == 0x0D)

{

send_UART(10); //New line (0x0A) = 10 = LF

send_UART(13); //Carriage return (0x0D) = 13 = CR

}

if(UART_data != 0)

{

send_UART(UART_data);

UART_data = 0;

}

}

}

Так же в этом разделе:
 
MyTetra Share v.0.53
Яндекс индекс цитирования