Arduino + High-Low Tech ATTiny mod = ??

Go To Last Post
5 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I've used both the Arduino IDE and just raw avr-gcc to write code for ATTinys. This evening I run up against a conundrum I can't quite wrap my brain around. 

 

First, the hardware. PA4-PA7 are 4 bits going into a BCD-to-7-segment chip. PB0, PB1, PB2 and PA3 are four "digit select" lines being used to mux 4 7-seg displays along with the BCD-7seg chip mentioned earlier. PA0 is AREF, PA1 is being used as an analog read pin, and PA2 is a pushbutton.

#define B0 4
#define B1 5
#define B2 6
#define B3 7

#define DISP1 10
#define DISP2 9
#define DISP3 8
#define DISP4 3

unsigned char bits[] = {B0, B1, B2, B3};
unsigned char display[] = {DISP1, DISP2, DISP3, DISP4};

#define IN 1

#define BUTTON 2

#define SAMPLE_PERIOD 250
#define MUX_PERIOD 2

#define VREF 1800

unsigned long last_sample_time = 0;

unsigned char digits[4];

void setup() {
  analogReference(EXTERNAL);
  
  /*
  for(int i = 0; i < 4; i++) {
    pinMode(bits[i], OUTPUT);
    pinMode(display[i], OUTPUT);
    digitalWrite(bits[i], LOW);
    digitalWrite(display[i], LOW);
  }
  
  pinMode(IN, INPUT);
  
  pinMode(BUTTON, INPUT_PULLUP);
  */
  DDRA = 0xf8;
  DDRB = 0x7;
  PORTA = 4;
  PORTB = 0;
  
}
static void displayWrite(unsigned char value) {
  PORTA &= 0xf;
  PORTA |= value << 4;
  return;
  /*
  digitalWrite(bits[0], (value & 0x1)?HIGH:LOW);
  digitalWrite(bits[1], (value & 0x2)?HIGH:LOW);
  digitalWrite(bits[2], (value & 0x4)?HIGH:LOW);
  digitalWrite(bits[3], (value & 0x8)?HIGH:LOW);
  */
}

void loop() {
  unsigned long now = millis();
  if (last_sample_time == 0 || now - last_sample_time > SAMPLE_PERIOD) {
    last_sample_time = now;
    unsigned long val = analogRead(IN);
    val *= VREF;
    val /= 1024;
    for(int i = 0; i < 4; i++) {
      digits[3 - i] = val % 10;
      val /= 10;
    }
    
  }
  
  for(int i = 0; i < 4; i++) {
    displayWrite(digits[i]);
    digitalWrite(display[i], HIGH);
    delay(MUX_PERIOD);
    digitalWrite(display[i], LOW);
  }
}

That code works. However, I draw your attention to the two commented out blocks of code in setup() and displayWrite(). Those blocks of code do not work. I'm at a loss as to why.

 

It suggests to me that the High-Low Tech mod for the ATTiny84 doesn't wire up the digital pins properly, but it's at least doing it right for PA3 and PB0-PB2, because the code to mux the display uses digitalWrite on the display[] values.

 

As I said, the code is working, so this is just an academic exercise at this point to figure out why the equivalent Arduino style code doesn't work when the direct approach does.

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Do you want this moved to the Arduino forum?

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The official Arduino is written for ATmegaxx8 and ATmegaxxx0 chips.    The port-pin mapping to Digital# or Analog# is clearly defined.

I know that digital#13 on a UNO is the ATmega328P's PB5.

 

Any code for Tinys is written by third parties.    The pin mapping is very arbitrary.    Tell us which "third party Tiny author" you are using.    Ideally a link to the release ZIP that you are using.    

 

If the third-party has followed the conventional Arduino practice,   you might find the pin-mapping in a file like:

C:\Program Files\arduino-1.0.6-windows\hardware\tiny\variants\tinyX4\pins_arduino.h

I find it difficult to understand why anyone would want to use the tiny84.    You can buy a ready-made Nano or Mini for breadboarding.     Any commercial product will use SMD chips.    The size of a TQFP or MLF is not much of a problem.

 

David.

Last Edited: Sun. Dec 14, 2014 - 09:41 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

 

david.prentice wrote:

The official Arduino is written for ATmegaxx8 and ATmegaxxx0 chips.    The port-pin mapping to Digital# or Analog# is clearly defined.

I know that digital#13 on a UNO is the ATmega328P's PB5.

 

Any code for Tinys is written by third parties.    The pin mapping is very arbitrary.    Tell us which "third party Tiny author" you are using.    Ideally a link to the release ZIP that you are using.    

 

If the third-party has followed the conventional Arduino practice,   you might find the pin-mapping in a file like:

C:\Program Files\arduino-1.0.6-windows\hardware\tiny\variants\tinyX4\pins_arduino.h

I find it difficult to understand why anyone would want to use the tiny84.    You can buy a ready-made Nano or Mini for breadboarding.     Any commercial product will use SMD chips.    The size of a TQFP or MLF is not much of a problem.

 

David.

 

I am using, as I said, the High-Low Tech attiny mod. It's at ATtiny master.zip. It does, indeed, have a pins_arduino.h file, and I used that to determine the pin mapping that I set up in bits[] and display[]. In fact, the mapping of display[] is working, since you can see the digitalWrite() operations for those pins are correct.

 

As for why, I make my own boards and the ATTiny chips are half the price of the ATMegas. I get satisfaction from designing for the smallest amount of hardware required to do the job. And, in fact, I do make commercial products (albeit on a small scale - and this board isn't one of them) and they are, indeed, SMD.

Last Edited: Sun. Dec 14, 2014 - 04:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Oh, I think I figured this out. It appears that either B0-B3 or "IN" were overloaded #defines. Changing those to BIT0-BIT3 and SAMPLE fixed everything.