Standard Blink Example

In the good tradition of the Arduino world, the first step is to let the user led blink. The HelvePic32 offers two possibilities:

 

1) Traditional digitalWrite()

 

const uint8_t LEFT=0;
const uint8_t RIGHT=1;
uint8_t nP[2][8] = {{0,17, 9,10,11,12,13,14},{18,17, 1, 2, 3, 6, 7, 8}};

void setup()
{
    pinMode(nP[RIGHT][2],OUTPUT);
}

void loop()
{
    digitalWrite(nP[RIGHT][2], HIGH);
    delay(250);
    digitalWrite(nP[RIGHT][2], LOW);
    delay(250);
}

 

2) Register Manipulation

 

I am usinhg a trick that I saw in the UTFT library code. Instead of determening the Port Register and the bitmap mask by myself I can ask the system to return them to me. This works for the arduino as well as all ChipKit boards. I only have to take into accoun the right value definition as the Arduino is a 8-bit system and the HelvePic32 is a 32-bit system.

 

So I define two arrays, one for the pointers to the Port Register and one for the bitmask. I also define two pre-processor macros to set (SBI) and clear (cbi) a bit:

 

#define cbi(reg, bitmask) *reg &= ~bitmask
#define sbi(reg, bitmask) *reg |= bitmask
const uint8_t LEFT=0;
const uint8_t RIGHT=1;
uint8_t nP[2][8] = {{0,17, 9,10,11,12,13,14},{18,17, 1, 2, 3, 6, 7, 8}};

#if defined(__AVR__)
volatile uint8_t *pP[2][8];
uint8_t           bP[2][8];
#elif defined(__PIC32MX__)
volatile uint32_t *pP[2][8];
uint32_t           bP[2][8];
#endif

void _IO_Init() {
    for (uint8_t i=0; i<2; i++){
        for (uint8_t j=0; j<8; j++){
            pP[i][j] = portOutputRegister(digitalPinToPort(nP[i][j]));
            bP[i][j] = digitalPinToBitMask(nP[i][j]);
        }
    }
}

void setup()
{
    _IO_Init();
    for (uint8_t i=0; i<8; i++){
        pinMode(nP[RIGHT][i],OUTPUT);
    }
}

void loop()
{
    sbi(pP[RIGHT][2], bP[RIGHT][2]);
    delay(500);
    cbi(pP[RIGHT][2], bP[RIGHT][2]);
    delay(100);
}

 

(to differentiate the sketch from the previous one, different delay times were used)

 

What is the difference?

 

On the oscilloscope, I looked at the pulses if I have a digitalWrite(pin,HIGH) directly followed by a digitalWrite(pin,LOW) compared to a sbi() followed by a cbl() call:

The picture shows that the shortest pulse using digitalWrite() is 1.2 μs wheras the sbi/cbi only needs 140 ns, making these kind of actions 8-10 times faster.