Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Adding ShiftOut functions
20-03-2014, 04:50 AM, (This post was last modified: 20-03-2014, 04:54 AM by Callmea.)
#1
Adding ShiftOut functions
Hi,

I'm porting libraries from Arduino, and there is a function, shiftOut that i often saw in Arduino sources.

I think i will be nice to add it to the commomn pinguino functions too. it works very well on pinguino, and it s a short function

Description of the function :
Shifts out a byte of data one bit at a time. Starts from either the most (i.e. the leftmost) or least (rightmost) significant bit. Each bit is written in turn to a data pin, after which a clock pin is pulsed (taken high, then low) to indicate that the bit is available. (taken [url]from http://arduino.cc/en/Reference/shiftOut[/url])

Here is the code :
Code:
void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val)
{
    uint8_t i;

    for (i = 0; i < 8; i++)  {
        if (bitOrder == LSBFIRST)
            digitalWrite(dataPin, !!(val & (1 << i)));
        else    
            digitalWrite(dataPin, !!(val & (1 << (7 - i))));
            
        digitalWrite(clockPin, HIGH);
        digitalWrite(clockPin, LOW);        
    }
}

I thank you. Have a nice Day Smile
Reply
20-03-2014, 08:46 AM,
#2
RE: Adding ShiftOut functions
Hi

I done something like this in \p32\include\pinguino\libraries\SpiLoc.c which is an SPI emulator, the main difference is which do not have the bitOrder parameter but is easy add it.


Bye Bye, Moreno
Reply
20-03-2014, 11:12 AM,
#3
RE: Adding ShiftOut functions
And I made another version Big Grin in another lib. :
Code:
void shiftOut(u8 dataPin, u8 clockPin, u8 bitOrder, u8 val)
{
    u8 i;
    u8 bitMask;

    for (i = 0; i < 8; i++)
    {
        if (bitOrder == LSBFIRST)
            bitMask = (1 << i);
        else
            bitMask = (1 << (7 - i));

        digitalwrite(dataPin, (val & bitMask) ? HIGH : LOW);
        digitalwrite(clockPin, HIGH);
        digitalwrite(clockPin, LOW);            
    }
}

(20-03-2014, 08:46 AM)moreno Wrote: I done something like this in \p32\include\pinguino\libraries\SpiLoc.c which is an SPI emulator, the main difference is which do not have the bitOrder parameter but is easy add it.

Time to add the Moreno's Spiloc library to the 8-bit (with the bitOrder parameter) !
But I would also add shiftIn and shiftOut keywords to the spiloc.pdl (or pdl32) file.

Any volunteer ? Callmea ? Moreno ?
It is easier to complain than it is to do, but it is better to do than it is to complain.
Reply
21-03-2014, 04:19 PM,
#4
RE: Adding ShiftOut functions
Good idea Smile

I m new to pinguino but why not, but i don't know anything about 32 bit files yet (And i m not an expert in pdl too Big Grin)
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)