Skip to content

Commit

Permalink
modified initailization code for use with ATMEGA8 (mbs38#3)
Browse files Browse the repository at this point in the history
* modified initailization code for use with ATMEGA8

* added #define BAUD
and automatic calculation of UBRR value
  • Loading branch information
mwue authored and mbs38 committed Aug 14, 2017
1 parent 820eb4c commit 42b3c6b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 28 deletions.
15 changes: 9 additions & 6 deletions yaMBSiavr.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,15 @@ ISR(UART_TRANSMIT_COMPLETE_INTERRUPT)

void modbusInit(void)
{
UBRRH = (unsigned char)(Baud>>8);
UBRRL = (unsigned char)Baud;
UART_CONTROL = (1<<RXCIE)|(1<<RXEN)|(1<<TXEN); // USART receiver and transmitter and receive complete interrupt
UART_STATUS |= (1<<U2X); //double speed mode.
UCSRC = (3<<UCSZ0); //Frame Size
UART_CONTROL|=(1<<TXCIE); //Transmit Complete Interrupt an
UBRRH = (unsigned char)(UBRR>>8);
UBRRL = (unsigned char)UBRR;
UART_STATUS = (1<<U2X); //double speed mode.
#ifdef URSEL // if UBRRH and UCSRC share the same I/O location , e.g. ATmega8
UCSRC = (1<<URSEL)|(3<<UCSZ0); //Frame Size
#else
UCSRC = (3<<UCSZ0); //Frame Size
#endif
UART_CONTROL = (1<<TXCIE)|(1<<RXCIE)|(1<<RXEN)|(1<<TXEN); // USART receiver and transmitter and receive complete interrupt
#if PHYSICAL_TYPE == 485
TRANSCEIVER_ENABLE_PORT_DDR|=(1<<TRANSCEIVER_ENABLE_PIN);
transceiver_rxen();
Expand Down
31 changes: 9 additions & 22 deletions yaMBSiavr.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ THE POSSIBILITY OF SUCH DAMAGE.
* @author Max Brueggemann www.maxbrueggemann.de
*/

/* define baudrate of modbus */
#define BAUD 38400L

/*
* Definitions for transceiver enable pin.
*/
Expand All @@ -72,11 +75,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define UART_CONTROL UCSRB
#define UART_DATA UDR
#define UART_UDRIE UDRIE
/*
* Change this value if you are using a different frequency and/or
* different baudrate.
*/
#define Baud 25 //38400@16e6Hz

#elif defined(__AVR_ATmega164P__)
#define UART_TRANSMIT_COMPLETE_INTERRUPT USART1_TX_vect
Expand All @@ -95,11 +93,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define U2X U2X1
#define UBRRH UBRR1H
#define UBRRL UBRR1L
/*
* Change this value if you are using a different frequency and/or
* different baudrate.
*/
#define Baud 64 //38400@20e6Hz

#elif defined(__AVR_ATmega168PA__)|(__AVR_ATmega88PA__)|(__AVR_ATmega328PA__)
#define UART_TRANSMIT_COMPLETE_INTERRUPT USART_TX_vect
Expand Down Expand Up @@ -141,11 +134,6 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define U2X U2X0
#define UBRRH UBRR0H
#define UBRRL UBRR0L
/*
* Change this value if you are using a different frequency and/or
* different baudrate.
*/
#define Baud 64 //38400@20e6Hz

#elif defined(__AVR_ATmega8__)|| defined(__AVR_ATmega16__) || defined(__AVR_ATmega32__) || defined(__AVR_ATmega323__)
#define UART_TRANSMIT_COMPLETE_INTERRUPT USART_TXC_vect
Expand All @@ -156,17 +144,15 @@ THE POSSIBILITY OF SUCH DAMAGE.
#define UART_DATA UDR
#define UART_UDRIE UDRIE

/*
* Change this value if you are using a different frequency and/or
* different baudrate.
*/
#define Baud 25 //38400@8e6Hz double speed mode


#else
#error "no definition available"
#endif

#ifndef F_CPU
#error " F_CPU not defined "
#else
#define UBRR (F_CPU / 8 / BAUD ) -1
#endif /* F_CPU */
/*
* Available address modes.
*/
Expand Down Expand Up @@ -345,3 +331,4 @@ extern uint8_t modbusExchangeBits(volatile uint8_t *ptrToInArray, uint16_t start
*
*/
extern uint8_t modbusExchangeRegisters(volatile uint16_t *ptrToInArray, uint16_t startAddress, uint16_t size);

0 comments on commit 42b3c6b

Please sign in to comment.