Go Down

Topic: Tx/Rx as digitals on Mini (Read 1 time) previous topic - next topic

magnethead794

Dec 16, 2011, 12:54 pm Last Edit: Dec 16, 2011, 12:58 pm by magnethead794 Reason: 1
I'm pretty sure the answer is yes, but can I use Tx/Rx as digital0 and digital1? Do I just references them as pins 0 and 1 in code, or do they have a special label?

I'm looking to drive 2 7 segment displays using a pair of decimal->binary encoded switches.

Each segment is 4 LEDs, 2.5V @ 20ma each in series-parallel. To display an 8 would be 280ma, at 40mA per segment (pin). I know this is on the upper bound of the arduino :/ To display 88 would be twice that, 560mA @ 40mA per pin. I guess this pretty much means I'm going to need a pair of ULN2802's?

segment0 pin0
segment1 pin1
segment2 pin2
segment3 pin3
segment4 pin4
segment5 pin5
segment6 pin6
segment7 pin7
segment8 pin8
segment9 pin9
segment10 pin10
segment11 pin11
segment12 pin12
segment13 pin13

SW1,1 pinA0
SW1,2 pinA1
SW1,4 pinA2
SW1,8 pinA3
SW2,1 pinA4
SW2,2 pinA5
SW2,4 pinA6
SW2,8 pinA7

Code: [Select]

pinMode(0, OUTPUT);
pinMode(1, OUTPUT);
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
pinMode(6, OUTPUT);

pinMode(7, OUTPUT);
pinMode(8, OUTPUT);
pinMode(9, OUTPUT);
pinMode(10, OUTPUT);
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);

pinMode(A0, INPUT);
pinMode(A1, INPUT);
pinMode(A2, INPUT);
pinMode(A3, INPUT);

pinMode(A4, INPUT);
pinMode(A5, INPUT);
pinMode(A6, INPUT);
pinMode(A7, INPUT);

digitalWrite(A0, HIGH);
digitalWrite(A1, HIGH);
digitalWrite(A2, HIGH);
digitalWrite(A3, HIGH);

digitalWrite(A4, HIGH);
digitalWrite(A5, HIGH);
digitalWrite(A6, HIGH);
digitalWrite(A7, HIGH);

int digitOne;
int digitTwo;

void setup(){



}

void loop(){

digitOne = 0;
digitTwo = 0;

if (!A0){

digitOne = digitOne + 1;

}
if (!A1){

digitOne = digitOne + 2;

}
if (!A2){

digitOne = digitOne + 4;

}
if (!A3){

digitOne = digitOne + 8;

}


if (!A4){

digitTwo = digitTwo + 1;

}
if (!A5){

digitTwo = digitTwo + 2;

}
if (!A6){

digitTwo = digitTwo + 4;

}
if (!A7){

digitTwo = digitTwo + 8;

}

switch (digitOne){

case 1:

digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);

case 2:

digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);

case 3:

digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, HIGH);

case 4:

digitalWrite(0, LOW);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

case 5:

digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, LOW);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

case 6:

digitalWrite(0, HIGH);
digitalWrite(1, LOW);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

case 7:

digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);

case 8:

digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
digitalWrite(2, HIGH);
digitalWrite(3, HIGH);
digitalWrite(4, HIGH);
digitalWrite(5, HIGH);
digitalWrite(6, HIGH);

default:

digitalWrite(0, LOW);
digitalWrite(1, LOW);
digitalWrite(2, LOW);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(5, LOW);
digitalWrite(6, LOW);

}

switch (digitTwo){

case 1:

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);

case 2:

digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);

case 3:

digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, HIGH);

case 4:

digitalWrite(7, LOW);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);

case 5:

digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, LOW);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);

case 6:

digitalWrite(7, HIGH);
digitalWrite(8, LOW);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);

case 7:

digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);

case 8:

digitalWrite(7, HIGH);
digitalWrite(8, HIGH);
digitalWrite(9, HIGH);
digitalWrite(10, HIGH);
digitalWrite(11, HIGH);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);

default:

digitalWrite(7, LOW);
digitalWrite(8, LOW);
digitalWrite(9, LOW);
digitalWrite(10, LOW);
digitalWrite(11, LOW);
digitalWrite(12, LOW);
digitalWrite(13, LOW);

}

}

KF5RVR

madworm

#1
Dec 16, 2011, 01:02 pm Last Edit: Dec 16, 2011, 01:04 pm by madworm Reason: 1
I recently had a similar need for more I/O pins ;-)

You can do it, but you must disable the UART the 'hard way'. It is activated by default and overrides the pin-direction (input/output) internally in hardware. Once you've disabled it you cannot use any hardware serial communication anymore (in that program).

Insert this at the very beginning of setup()

Code: [Select]
UCSR0A = 0x00;
UCSR0B = 0x00;
UCSR0C = 0x03;


This sets the uart control & status registers to their default values and turns the uart + port override off.
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

MarkT

Yes they are just IO pins, but there is an important problem - the bootloader uses them at reset to check for a new program download.  This limits what hardware you can connect to RX and TX in practice and people avoid using these pins (some boards have them directly connected to the USB/serial chip, BTW, more recent boards have resistors in-line I think.

I suspect these problems can be overcome but you need to be aware of the issues.
[ I won't respond to messages, use the forum please ]

magnethead794

This is a mini, so no onboard USB crap. Once I have it programmed, it won't be getting programmed for a good while. It won't be using serial in application, thus why not use them (in my case, there's an exact number of pins)
KF5RVR

madworm

Well it works just fine on a 'bare' ATmega168 programmed with the Arduino IDE. And a mini is pretty close to that.
• Upload doesn't work? Do a loop-back test.
• There's absolutely NO excuse for not having an ISP!
• Your AVR needs a brain surgery? Use the online FUSE calculator.
My projects: RGB LED matrix, RGB LED ring, various ATtiny gadgets...
• Microsoft is not the answer. It is the question, and the answer is NO!

Go Up