MyTetra Share
Делитесь знаниями!
UART
Время создания: 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


#define DS_DDR DDRB

#define DS_PORT PORTB

#define DS 1 //DS18b20 pin

#define DS_Low DS_DDR &= ~(1<<DS)

#define DS_High DS_DDR |= 1<<DS


#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_R_data; //UART received data

unsigned int UART_T_data; //UART data for transmission


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

// INTERNAL INT SECTION

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

//UDR empty

UART_R_data = 0;

UCSRB &= ~(1<<UDRIE); //UDRIE disable

}


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*/)

{

TCNT = 0;

Counter = 1;

}

}


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

// FUNCTION SECTION

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

//byte sending function:

void send_UART(unsigned int 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

}


void send_str_UART(unsigned char *s)

{

//send_str_UART("Hello world")

while (*s != 0) send_UART(*s++);

}


//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

UCSRB = (UCSRB | 1<<RXCIE); //TX and RX interrupts enable

// RXCIE - interrupt after receiving data

// |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

// 11011000 - 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

DS_DDR &= ~(1 <<DS); //clr DDRB 1

DS_PORT &= ~(1 <<DS); //clr PORTB 1

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

// VARIABLE SECTION

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

UART_R_data = 0x00;

Counter = 0x00;

TCNT = 0x00;


sei();


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

// MAIN LOOP CYCLE

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

while(1)

{


if(UART_R_data != 0)

{

LED_Indicator_PORT ^= 1 <<LED_Indicator;

send_UART(UART_R_data);

UCSRB |= 1<<UDRIE;

}

}

}





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