1

I have a concern about the library port handling. I have used direct port direction manipulation successfully in previous projects but there seems to be a problem here.

// This code mostly taken from the example
#include "LedControl.h"

LedControl lc=LedControl(11,13,10,1);
//LedControl lc=LedControl(12,11,10,1); // original
// pin 12 is connected to the DIN pin (now MOSI 11)
// pin 11 is connected to the CLK pin (now SCK 13)
// pin 10 is connected to the CS pin  (now SS 10)
// 1 as we are only using 1 MAX7219

// I am actually using all arduino pins except 12 for my project
#define PD_MSK  B11111100 // Data pins PD2..7 (pins2..7)
#define PB_MSK  B00000011 // Data pins PB0,1 (pins8,9)
#define PC_MSK  B00001111 // Data pins PC0..3 (pins 14..17)

void setup()
{
// set pins 2..9 as inputs  
  DDRD = DDRD & !PD_MSK; // this works OK
  DDRB = DDRB & !PB_MSK; // configure ports D,B inputs *** doesnt work with this
 // pinMode(8, INPUT); //*** does work with this
  //pinMode(9, INPUT); //*** does work with this

  DDRC = DDRC | PC_MSK; // configure PortC0..3 as outputs
  PORTC |= 0x0F; // set PortC0..3 high (default)

  // the zero refers to the MAX7219 number, it is zero for 1 chip
  lc.shutdown(0,false);// turn off power saving, enables display
  lc.setIntensity(0,8);// sets brightness (0~15 possible values)
  lc.clearDisplay(0);// clear screen

}
void loop()
{
  //numbers 7 to 0
  for (int a=0; a<8; a++)
  {
    lc.setDigit(0,a,a,false);
  }

  delay(1000);

  //display number 8 on all segments
  for (int a=0; a<8; a++)
  {
    lc.setDigit(0,a,8,false);
  }
  delay(1000);

  // numbers 7 to 0 reverse order
  for (int a=8; a>=0; a--)
  {
    lc.setDigit(0,a,a,false);
    //delay(100);
  }
  delay(1000);

}

The direct port direction setting does not seem to allow the library to work while the pinMode commands doing the same job do. I note that the library uses software SPI but I would like to use hardware if possible. I have certainly used hardware SPI with frequent port direction changes on the remaining pins for other projects. Any insight as to why this problem occurs?

Thanks.

5
  • What is the connection between "the library does not work if I setup ports manually" and "I would like to use hardware SPI"? – the busybee Apr 7 '20 at 6:29
  • 1
    Since most libraries come as Open Source, did you peek into its sources? Is this one open? If so, what did your research reveal? – the busybee Apr 7 '20 at 6:30
  • 2
    The ! is a logic operator. Bitwise variant is ~ – KIIV Apr 7 '20 at 11:44
  • However after the start/restart DDRx are cleared and configured as inputs, so I don't see any point in those &= – KIIV Apr 7 '20 at 11:51
  • What tells "LedControl.h" that you are using hardware SPI vs software SPI? – CrossRoads Apr 7 '20 at 14:07
0

As always I recommend the old RTFM (read the f***ing manual) of the LedControl.h library.
In the FAQ its clearly stated:

Why don't you use the hardware SPI-Bus of the Arduino?

When I wrote the first version of the library I didn't knew much about the Arduino or AVR processors in general and I knew nothing about writing code for the hardware SPI-bus on an ATMega. But I knew how the SPI protocol works and how I could make this work using simple digitalWrite() commands. I turned out the library code is fast enough for common use cases, so there is no need for doing Hi-Speed communications.

Other strong points in favour of the (admittedly trivial) code design are - Use any three free IO-Pins of your board for the hardware - Simple to debug. Much easier than managing 3 differnt SPI devices connected to the same hardware bus - Library is easily portable to other platforms, since it uses only two very basic functions from the Arduino language

So with this library you clearly have never used hardware SPI (although you could use the pins, but only via softwareSPI)

2
  • Answer #2 pointed out that I had used the wrong operator to set the port direction. (! instead of ~ - bitwise.) – Ardathome Apr 10 '20 at 9:50
  • Answer #2 pointed out that I had used the wrong operator to set the port direction. (! instead of ~ - bitwise.) My mistake! I want to use hardware SPI in my eventual project (this was just a test) as it can be faster. I used this library to ensure that the external hardware worked -it does now. Using different pins has no benefit in my case so I will probably write my own (hardware) SPI driver. I already have a port direction control system to create a 8bit IO bus for use with other external hardware - if I copy it correctly this time. Thanks to all who took time and found my error. – Ardathome Apr 10 '20 at 10:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.