Easy way to port bitWrite between ATtiny and ATmega?

Hello,

I'm currently writing code for an ATtiny85 that I am however developing on an ATmega328 using the Arduino Uno, to be able to use the serial port to debug the code before it finally goes onto the ATtiny85. I can't use SoftwareSerial with the ATtiny85, as my code already barely fits on the ATtiny as is. Including the SoftwareSerial library bumps my sketch well above the 100% program space threshold. I also have no free pins left on the ATtiny to run SoftwareSerial on.

Anyway, so for a few reasons, I have to put an LED on a different port on the ATtiny than on the ATmega. It's PORTD on the ATmega and PORTB on the ATtiny.

So this is what I then have in my code:

//PORTD on ATmega, PORTB on ATtiny
//bitWrite(PORTB, PB3, HIGH);
//delay(100);
//bitWrite(PORTB, PB3, LOW);

bitWrite(PORTD, PD4, HIGH);
delay(100);
bitWrite(PORTD, PB4, LOW);

Is there an easier way to do this, so I don't have to comment out the portion that isn't needed each time? Because there are several sections in my code where I need to blink that LED.

Can I do anything with #define at the top of my sketch, so that I effectively just need to change one line or two, instead of half a dozen within the sketch?

I need to use bitWrite, as my code is already pushing the limit of what will fit on the ATtiny.

Easily!
Create a define and use it instead of the port name:

// ATmega
#define CARGUY_OUT_PORT PORTD  // make sure define name is unique
// Attiny
//#define CARGUY_OUT_PORT PORTB

bitWrite(CARGUY_OUT_PORT, 4, HIGH);
delay(100);
bitWrite(CARGUY_OUT_PORT, 4, LOW);

Furthermore, you could use a conditional compilation to automatic changing your port depending on board you use:

#if defined(__AVR_ATmega328P__)
#define CARGUY_OUT_PORT PORTD
#elif defined(__AVR_ATtiny85__)
#define CARGUY_OUT_PORT PORTB
#endif

Thanks for the quick answers. It seems to work now.

Another idea I've had is to blink the LED using hardware timers to circumvent delays in my code that happen because of other things. Because the other parts of my circuit, especially the polling of a DS18B20 temperature sensor, can cause the LED to blink irregularly. I've tried non-blocking code using millisecond comparison, but it doesn't help. If the LED blinks while the temperature sensor is read (once every five seconds), then it becomes irregular.

How would you blink an LED on the ATtiny using hardware timers? Let's say I specifically want the LED to blink five times, at equal high-low intervals of 250 ms. The LED is on pin 3 (PORTB, PB3) on the ATtiny. For practical reasons, I have just now moved the corresponding LED on the ATmega to Pin 7 (PORTD, PD7) while still developing the code.

The ATtiny will run at the default 8 MHz, while I'm using the Uno's standard 16MHz.

Have you read the relevant sections of the processor's datasheet?
https://ww1.microchip.com/downloads/en/devicedoc/atmel-2586-avr-8-bit-microcontroller-attiny25-attiny45-attiny85_datasheet.pdf

It looks like you're using blocking code to access the temperature sensor. Blocking code can't be combined with millis. You need to use non-blocking DS18B20 reading.
Please show your full code so we can help you.

While timers themselves are a good subject to study, they're generally the wrong approach to solving your problem. Most programs require multiple actions to be performed simultaneously, and the controller simply doesn't have enough timers to handle each of them independently.
It's better to learn how to work with millis.

Undoubtedly, the code was incorrect. To learn how to use millis(), I recommend this excellent tutorial.

Also, heed the advice in post #6 above and learn how to read the sensor without using blocking code. Examples are posted in many places on line.

Ah... interesting. I was not even aware that the standard DallasTemperature library uses blocking code.

After a quick Google search, I take it that NonBlockingDallas is the library I will have to install for this? Or ar there other similar libraries?

I will post my code later, at the moment it's still a bit too untidy to share. But maybe the non-blocking Dallas library will do the trick.

You can use this library in either Blocking or Non-Blocking mode. See the "WaitForConversion2" example.

Sorry, I'm not aware of any libraries for this. I don't use libraries. The non-blocking code for reading DS18B20 sensor is, at a first approximation, about 20 lines of code, which can be easily found online.

@carguy2
1. If you are working with fresh ATTiny85, then its internal operating cock is 1 MHz.

2. To enable 8 Mz clock, you have to program a fuse bit. You may follow the procedures of Section-`19.2 of the attached file.

3. You can comfortable\y work directly with your ATTiny85 using Arduino UNO as a Programmer for which you may follow Section-19.2 of the attached file. After uploading a revised sketch into the ATtiny85, you disconnect three wires from UNO and then connect your IO devices with them.

4. You can use bitSet(arg1, arg2) and bitClear(arg1, arg2) insrutions instead of bitWrite(arg1, arg2, arg3).

5. You can blink your LED connected at any DPin of 8-MHz ATtiny85 at 500 ms interval (250 ms On/Off) by executing te following millisCounter based codes. It is difficult to genertae 250 ms time tick using ATtiny85's 8-bit timers.

int ledState = LOW; 
pinMode(DPin, OUTPUT);    //if using ATTnyCore, then PB0-PB5 are DPin-0 to 5
digitalWrite(DPin, ledState);
unsigned long prTime = millis();
if(millis() - prTime >= 250)
{
   ledState != ledState;
   digitalWrie(DPin, ledState);
   prMillis = millis();
}

Ch-19 ATtiny85 MCULec (5).pdf (902.0 KB)

That's an over simplification given the requirement of working with a OneWire bus. A code that doesn't use any library will be considerably longer than 20 lines. The Arduino-Temperature-Control-Library by milesburton is the standard used by nearly everyone. It supports a non-Blocking mode.

To be honest, there will always be some "blocking" because of the timing requirements of OneWire bus transactions. But, using the non-Block mode means your code can do other things during the DS18B20's A2D conversion time which can be up to 750ms.

Are the following pseudo codes non-blocking? If yes, which option is better?
1.

//after conversion command
if(millis() - prTime >= 750) //max conversion time for 12-bit resolution
{
     //---data ready
}
else
{
     //---do something else when data is not ready
}

or

2.

//after conversion cammand
do
{
     //--- do something else while data is not ready from Ds18B20
}
while(ds.read() != 0xFF); // busStatus = ds.read() gives 0xFF if data ready
//--- data ready

After some brief research, I was able to add the line


    sensors.setWaitForConversion(false);

right before I poll the temperature sensor, and it looks like it did the trick. The LED now blinks evenly, even if the sensor is being polled during blinking.

That's a necessary but perhaps not sufficient step. You're still likely not doing it the most efficient way possible. You don't need to "poll the temperature sensor". The process goes something like:

  • Kick off a temperature conversion with requestTemperatures().
  • Call millisToWaitForConversion() to get an upper bound on how long the conversion will take.
  • Have your code do useful work while using standard millis() techniques to check if the conversion time has expired.
  • Read the temperature result from the sensor.

Also, the getTempCByIndex() and getTempFByIndex() functions are relatively slow as they must determine the sensor's OneWire address everytime they're called. Rather, use the getTempC() or getTempF() functions and supply the OneWire address yourself. The address can be determined in the setup() function and saved in a byte array. See the library examples for this.

Thanks, I will definitely keep your additional suggestions in mind.

At the moment, the code seems to run fine with just that line added; the main issue was that the polling of the DS18B20 was slowing down the LED or making the blink intervals uneven. This no longer seems to be the case now.

Well, there is digitalWrite() :slight_smile:
I don't know how optimized digitalWrite is on ATtiny cores, but I'm pretty sure you could find a digitalWriteFast() implementation that compiles to the same code you have now...

In 2016 I un-delayed a greenhouse automation in Budapest (while in USA) that had a DS18B20 and whatever it did with delays, I replaced with a state machine that used millis.

What are these timing requirements? According to the system log I had it keep, it was waiting 97ms after request to get data and the state machine tended to all that. I took code that worked one piece at a time with delays and left every line that wasn’t delay and built the timer and state machine around them, cut&dry but I didn’t get deep into those copied lines so perhaps the timing part’s in there?

Without bitWrite()….

// set HIGH, write to PINB bit toggles PORTB bit
if (PORTB & PB3 == 0) PINB = PB3;
// set LOW, write to PINB bit toggles PORTB bit
if (PORTB & PB3 == PB3) PINB = PB3;
// when you know just change the pin state
PINB = PB3;