#include <avr/io.h>
#include <util/delay.h> //Max delay: 262.14 ms / F_CPU in MHz
//#define F_CPU 4000000// Clock Speed
//#define BAUD 9600
#define MYUBRR 24//F_CPU/16/BAUD-1
///////////////////////////////////////////////USART Init/////////////////////////////////////////////////////////
void USART_Init( unsigned int ubrr)
{
/* Set baud rate */
UCSR0A=(1<<U2X0); //Divisor in BAUD set to 8. USX0=0 divisor=16, USX0=1 divisor=8
UBRR0H = (unsigned char)(ubrr>>8);
UBRR0L = (unsigned char)ubrr;
/* Enable receiver and transmitter */
UCSR0B = (1<<RXEN0)|(1<<TXEN0);
/* Set frame format: 8data, 1stop bit */
UCSR0C = (0<<USBS0)|(3<<UCSZ00);
}
///////////////////////////////////////////////USART Transmit/////////////////////////////////////////////////////
void USART_Transmit( unsigned char data )
{
PORTD|=(1<<PD6); //RS485==Transmit
_delay_ms(1);
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
//_delay_ms(1);
PORTD&=~(1<<PD6); //PE6: RS485==Receive
}
///////////////////////////////////////////////USART Recieve//////////////////////////////////////////////////////
unsigned char USART_Receive( void )
{
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) );
/* Get and return received data from buffer */
return UDR0;
}
///////////////////////////////////////////////Main///////////////////////////////////////////////////////////////
int main(void)
{
USART_Init(MYUBRR);
DDRA=0xFF; //PORTA output. LEDs connected to this port
DDRD=0xFF; //PORTF output
DDRC=0x00; //PORTC input
PORTC=0xFF; //Set PORTC to 'High'
PORTA=0xFF; //Turn OFF all LEDs
PORTD&=~(1<<PD6); //PE6: Receive
unsigned char x=0;
while(1)
{
if ((PINC & 0x01)==0)
{
PORTA=0x00;
USART_Transmit(0x54);
}
if ((PINC & 0x02)==0)
{
PORTA=USART_Receive();
}
}
}
unsigned char USART_Receive( void )
{
volatile uint16_t counter=0;
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) ) {
counter++;
if (counter==20000) break; //change according to needed delay
}
/* Get and return received data from buffer */
if (counter != 20000) return UDR0;
else return 255; // all leds off
}
unsigned char USART_Receive( void )
{
[B]/* Wait for data to be received */[/B]
while ( !(UCSR0A & (1<<RXC0)) );
/* Get and return received data from buffer */
return UDR0;
}
void USART_Transmit( unsigned char data ) {
/* Wait for empty transmit buffer */
while ( !( UCSR0A & (1<<UDRE0)) );
/* Put data into buffer, sends the data */
UDR0 = data;
}
unsigned char USART_Receive( void ) {
/* Wait for data to be received */
while ( !(UCSR0A & (1<<RXC0)) );
/* Get and return received data from buffer */
return UDR0;
}
alexan_e said:I don't see interrupts being used in your code.
alexan_e said:Yes I saw the interrupts functions but no code was written in them, I would expect to see the GetByte and SendByte called from the interrupts but I suppose you are using another interrupt function that is not shown.
#pragma vector = USART1_RX_vect
__interrupt void USART1_Rx_ISR (void)
{
USART1RxInterruptCallback();
}
#pragma vector = USART1_TX_vect
__interrupt void USART1_Tx_ISR (void)
{
USART1TxInterruptCallback();
}
It is the low level implementation
tyassin said:But I am interested in knowing more on how to implement protocols?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?
We use cookies and similar technologies for the following purposes:
Do you accept cookies and these technologies?