How to adapt a function ?

If you look down in code you will see this commented line:
// WriteData(digitAddr[i], 0b11100101, 8);
If I'm activating it, and comment out some other code, everything is working fine. It is displaying a number "3" on a LCD display in reality. The "3" is encoded by this binary value: 0b11100101 and is used as a test value.

boolean LSB_FIRST = MSB_FORMAT;
void TestSegments3()
{
    for (uint8_t i = 0; i < NUM_DIGITS; i++)
    {
        delayMicroseconds(1);
        digitalWrite(CS, LOW);
        delayMicroseconds(1);
        HT162x_SendBits(0b101, 3);
        HT162x_SendBits(digitAddr[0], 6);
        for (int n = 1; n >= 0; n--)        //<<<--------T
        {
            uint16_t data = (0b11100101 & (0xf) << 4*n) >> (4*n);

//-A
            //__HT162x_SendBits(nib, 4, LSB_FORMAT)__
            uint16_t mask = 0b1 << 4-1;
            for (uint8_t i = 4; i > 0; i--)
            {
              delayMicroseconds(WRITE_DELAY_USECS);
              digitalWrite(WR, LOW);
                data & mask ? digitalWrite(DATA, HIGH) : digitalWrite(DATA, LOW);
              delayMicroseconds(WRITE_DELAY_USECS);
              digitalWrite(WR, HIGH);
              delayMicroseconds(HOLD_DELAY_USECS);
              data <<= 1;
            }
//-B
           // uint16_t nib = (0b11100101 & (0xf) << 4*n) >> (4*n);
           // HT162x_SendBits(nib, 4, LSB_FORMAT);
        }
        delayMicroseconds(SETUP_DELAY_USECS);
        digitalWrite(CS, HIGH);

        int t = 100;
    //  WriteData(digitAddr[i], 0b11100101, 8);
        delay(t);
        WriteData(digitAddr[i], 0b00000000, 8);
        delay(t);  
    }
}

What Im trying to do is to adapt the content of the WriteData() function inside this TestSegments3() function. Between //-A and //-B markings is this WriteData() function content code, but it is adapted already with some values inside this new function. Easy, right?
This specific code that is presented right now, is not throwing any errors in the console, but in reality, instead of "3" I get a "| |" segments opening. I want it to display "3" !!!
The line data <<= 1; if is shifted >>, the display is not showing anything. But in the original function, this should have been worked - if Im not mistaking. But I might be wrong. Is my precarious observation.
Now... Im not new to programming and I know there is a thing in all C like languages and especially in OOP like C#, some order of precedence thing or object like reference, I forget what it is called exactly, but I know this 2 functions MUST work separately and not together like im trying here. Im aware. My target is to get to the bottom of this, to extract the absolute working code, the functional code and apply it my way. But I need some help.
Any ideas are welcomed, no matter how weird or out-high they seem.
Ask me anything and Ill do my best to clarify it.
Thank you.

I moved your topic to an appropriate forum category @q12.

In the future, please take some time to pick the forum category that best suits the subject of your topic. There is an "About the _____ category" topic at the top of each category that explains its purpose.

This is an important part of responsible forum usage, as explained in the "How to get the best out of this forum" guide. The guide contains a lot of other useful information. Please read it.

Thanks in advance for your cooperation.

1 Like

are you looking for something like this ?

const byte PinWr   = 12;
const byte PinData = 13;

#if 0
const unsigned long UsecWr  10
#define Delay()     delayMicroseconds (UsedWr)
#else
#define Delay()     delay(500)
#endif

enum { LsbFirst, MsbFirst };

void
output (
    uint16_t  data,
    int       nBit,
    int       dir )
{
    uint16_t  mask;

    if (LsbFirst)
        mask = 1;
    else
        mask = 1 << (nBit-1);

    for (int i = 0; i < nBit; i++)   {
        digitalWrite (PinWr,   LOW);
        digitalWrite (PinData, data & mask);
        Delay ();

        digitalWrite      (PinWr,   HIGH);
        Delay ();

        if (LsbFirst)
            data >>= 1;
        else
            data <<= 1;
    }
}

// -----------------------------------------------------------------------------
void setup ()
{
    pinMode (PinWr,   OUTPUT);
    pinMode (PinData, OUTPUT);

    output (0xa9, 8, LsbFirst);
    delay (3000);
    output (0xa9, 8, MsbFirst);
}

void loop ()
{
}

hmmm...I will do this. I will give you my entire file for you to modify at your discretion.
What I want to make, and is looking like a mess, inside TestSegments3(), is to expand and unify this line:
// HT162x_WriteData(digitAddr[i], 0b11100101, 8);
So I look inside WriteData() function and I copy its code into my TestSegments3()
Next.. inside WriteData() function, I have SendBits() function inside the for loop.
So I copy-paste that SendBits() content into TestSegments3() as well.
That is what that mess is about inside TestSegments3().

To summarize, I want a VERY simplistic representation of this IC protocol - no matter how repetitive or big it gets. All I want is the basics for it to function. And I know Im already there, but not completely.

I already figure out the IC settings protocol. But this DATA sending protocol... is a bit confusing. I must mention that I am reverse engineering this code, it is not mine. I want to break it down to it's basics- pretty much.
/*
General Functionality for the settings code:
you feed 1bit at a time from right to left and click-clack all
the cs,wr,data pins in order

This is the basic code Im refering and I want to obtain:
//General Functionality for the settings code:
digitalWrite(_cs_p, LOW);
digitalWrite(_wr_p, LOW);
if (data & 0x80) //checs if the most significant bit(MSB) is 0b1000
digitalWrite(_data_p, HIGH);
else
digitalWrite(_data_p, LOW);
digitalWrite(_wr_p, HIGH);
digitalWrite(_cs_p, HIGH);
data <<= 1; //left-shifts by 1 bit
*/
This is a code I get it from a completely other source file. Not from the one attached here.

Thanks !

get the file here: LCD_HT1622_16SegLcd_demo_driver.ino (6.0 KB)

do you I2C when you say IC?

do you I2C when you say IC?

no
it's a multiplexed LCD driver named HT1621

originally I wired IRQ and RD pins of the IC to arduino, thats why you see more wires than 3. In the end, I managed to make it work with 2! arduino libraries and only needed 3 wires: CS,WR, and Data pins of the IC to arduino.


you can see the LCD on the breadboard on the right:

not clear

you've done what i've would have done using SendBits() in command() and writeDate().

not sure what TestSegments3() is intended to do. don't understand why you don't use SendBits() instead of the copy of SendBits() which looks like it sends 4 bits while the data, 0b11100101 is 8 bits.

So...
/*
This is the basic code Im refering and I want to obtain:
//General Functionality for the settings code:
digitalWrite(_cs_p, LOW);
digitalWrite(_wr_p, LOW);
if (data & 0x80) //checs if the most significant bit(MSB) is 0b1000
digitalWrite(_data_p, HIGH);
else
digitalWrite(_data_p, LOW);
digitalWrite(_wr_p, HIGH);
digitalWrite(_cs_p, HIGH);
data <<= 1; //left-shifts by 1 bit
*/
I want something concise and to the point as this "settings code". But for Data !
Thats all I want.
For that, I have to carve down all the unnecesary "things" in the code and leave only the bare bone of the function. It will not even be a function, just some setup() initialization and then the loop() code and thats it. To the limit.

On this IC as well on other IC's or MCU's you have the Settings code and then the Data code.
The settings code deal with internal settings of the IC, like clock source for example (internal RC, crystal osc, external RC oscilators), like some pins to be set Hi or Lo, I/O settings of the GPIO's, and stuff like that.
For this HT1621 IC Im dealing with here, it needs a special serial code to be transmitted to it only to set it's internal settings. He needs a 100 code to expect a write procedure, and then some special code to enable/disable some special functions inside it.
void setup()
...
HT162x_Command(CMD_SYS_EN);
HT162x_Command(CMD_RC_INT);
HT162x_Command(CMD_BIAS_COM); // Only for HT1621
HT162x_Command(CMD_LCD_OFF);
HT162x_Command(CMD_LCD_ON); // Should turn it back on

I explained the settings code to you to familiarize with it. I think I get the right functionality of it already. All I need is the Data code to be "carved down" from that code I send.
If you need the -original- unaltered code, I can send you that as well.
This one you've seen already is already cut some chunks out and tested.

don't understand what you mean by "settings"? do you configuration?
how is it different than command or data?

sounds like you want something you already have like _SendBits(), WriteData() or something similar?

yes

???

I see your point. So you are saying that the same protocol for the settings code should work for data code. Right? I did had this idea all the time, but... it is something else in the mix and is definitely different, and I cant put my finger on it. I do have my suspicions but... I couldn't make it work with less and more direct code so far.

Could anyone please make a video tutorial on it?

Tutorial on what?
The code, discussed in the topic - is quite similar to the code of the arduino shiftOut() function, and explained in detail in any tutorial about using shift registers in arduino

1 Like

i'm not sure what you expect TestSegments3() to do

looks like there are several I/O sequences where there may be some # of writes, # of read and even and additional # of writes. And it looks like the write may be composed of some # of address and # of data bits.

i think with a pair of sub-functions, one that performs a specified # of writes and another a # of reads, there can be a higher level sub-functions that perform the combinations of writes/reads/writes. And above them, sub-functions that call them to write and read 4-bit data from a specified address that is always written.

these layers of sub-functions can easily be tested at low speeds 500 msec cycles and tested with prints, and then run them at full speed to access the chip

1 Like

Or, considering the userId, maybe it's spam?

2 Likes

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.