error 'PORTB' undeclared

I found a serial bit bang program using direct port manipulation which won't compile - code attached below. It complains that PORTB is undeclared. I created a new sketch and included the softUart.h at the top.

I also found a version of 'blink' using direct port manipulation which compiles just fine and also refers to PORTB.

Arduino IDE 1.8.5, Arduino Nano

{softUart.h}

#define UART_SOFT_DDR DDRB
#define UART_SOFT_PORT PORTB
#define UART_SOFT_PIN PB1
#define UART_SOFT_BAUD 4800UL
//----------------------------//

#define UART_SOFT_DELAY_US (int) (1000000.0/((float) UART_SOFT_BAUD)+0.5) 

#define softUartInit()	UART_SOFT_DDR |= (1<<UART_SOFT_PIN); \
						UART_SOFT_PORT |= (1<<UART_SOFT_PIN)
void softUartSendChar (char txData);
void softUartSendString (char *txData);

{Serial_Bit_Bang.c}

#include "softUart.h"

void softUartSendChar (char txData)
{
  	//txData = ~txData;
  	UART_SOFT_PORT &= ~(1<<UART_SOFT_PIN);
  	_delay_us(UART_SOFT_DELAY_US);
	for (char i=0; i<8; i++)
  	{
    	if( txData & 1 )
      		UART_SOFT_PORT |= (1<<UART_SOFT_PIN);
    	else
      		UART_SOFT_PORT &= ~(1<<UART_SOFT_PIN);
    	txData >>= 1;
		_delay_us(UART_SOFT_DELAY_US);
 	}
	UART_SOFT_PORT |= (1<<UART_SOFT_PIN);
	_delay_us(UART_SOFT_DELAY_US);
} 

void softUartSendString (char *txData)
{
	while(*txData != '\0') softUartSendChar(*txData++);
}

You are not using PORTB in your code?

Danois90:
You are not using PORTB in your code?

#define UART_SOFT_PORT PORTB``UART_SOFT_PORT &= ~(1<<UART_SOFT_PIN);

Sorry, didn't see that :-/

Try to put "#include <Arduino.h>" as the first line in "softUart.h" :slight_smile:

Danois90:
Sorry, didn't see that :-/

Try to put "#include <Arduino.h>" as the first line in "softUart.h" :slight_smile:

Aha, that fixed it, thanks.

I'm still bemused that the 'blink' code doesn't need this but never mind, I can get on with the rest of the code.

AlbertHall:
I'm still bemused that the 'blink' code doesn't need this but never mind, I can get on with the rest of the code.

The blink code does probably not access PORTB via a #define. Your code would have worked if you had accessed PORTB directly :slight_smile:

Danois90:
The blink code does probably not access PORTB via a #define. Your code would have worked if you had accessed PORTB directly :slight_smile:

Aha, correct again.
Thanks.