# daisy chain two 74hc165 to read paralell data ,serialy.

Status
Not open for further replies.

#### tony64

##### New Member
My problem is how to read these bits which comes to 16 bits(2 bytes)?then send these bytes serially out to parallel out puts.To explain more suppose we have 16 push button that is input to two shift register and on the out put we will get them serially,but to keep the push buttons pressed alive and active ,we have to some how keep them on (say if they are led) ,what I have done is sending back the bytes we get serialy through SIPO(595)and a driver (ULN) to the paralell inputs.
I have made some codes but do not work.

#### tony64

##### New Member
I am using atmega32 and C in codevision.

#### wkrug

##### Active Member
I think that could not work.
When hold down the level of the switch input with the ULN Chip, the controller always will read a low level in spite of the switch isn't pressed.
I would seperate the input circuit from the output circuit.
One shift register bank will read the switches ( input ), another Bank will handle the Outputs.
The Controller read the switches and handle the outputs depending of the readed switch positions.

Another possible way is to switch the output off for a short time, then read the input shift register and set the output again.
But that make a short interruption of output supply.

Could You post a schematic please?

Last edited:

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
we have to some how keep them on (say if they are led) ,what I have done is sending back the bytes we get serialy through SIPO(595)and a driver (ULN) to the paralell inputs.
This is unclear.... Do you mean outputs...

Scenario

16 switched bits ->two chained 165 -> AVR.... PROCESS ... AVR -> two chained 595 -> 16 LED's

#### tony64

##### New Member
Scenario

16 switched bits ->two chained 165 -> AVR.... PROCESS ... AVR -> two chained 595 -> 16 LED's[/QUOTE]
Yes this is in fact the scenario :When we press the key in the inputs(by connecting them to grnd.)the led in the input dose not stay on,once you release the push button ,it goes off ,So what we like to do is to keep them on and wait for the next instruction.we are using uln because this is the hardware we have on hand and can not chage it. we have to write the program for this hardware and these uln should switch other things.
I have included an schematic (rough).for you.I have also some code written and it works up to a stage which reads the inputs ,but to send the read switches back to input to keep the leds on is not working.

#### Attachments

• 1.4 MB Views: 147

#### Ian Rogers

##### User Extraordinaire
Forum Supporter
Normally you read the switches and if there is a button press you can the latch the LED in software... The button needs to go through each state....

Off-> On...or On -> Off...

Only when it goes off to on set/reset a latch.. This means the switch ahas to be released and re=pressed for the LED to be changed.. I normally save the switch state and add in the new state to see if the correct transition has occurred.. Take a switch...
ON or OFF.. = 1 or 0
move the switch to the second bit in a byte( shift left ) and add the new switch.

we get

0:0 was off... still off
0:1 was off... now on
1:1 was on... still on
1:0 was on now off.

so if we take the last two bits 01 is the only combo we need..
C:
state<<=1;  // Shift the old switch pos
state += sw  & 0x3;  // add the new switch pos and clear all but the last two digits
if(state==1)  LED = ~LED; // only if the state was 1, toggle the LED..

#### tony64

##### New Member
I am thinking about what you said..
but the problem is we have two bytes,not one byte.
and the other point is this software instruction should go thorough 595 ,should it not?
Let me send you a part of my code sorry don,t know how to put it in code box).
C:
 void HC165_readSerialData(uint8_t serial_data[])
{
uint8_t buttons[2] = {0, 0};
uint8_t i,j;
uint8_t _byte;

/* Pulse LD low and then high to load parallel data */
PORTC.3=0;//LD_LOW;
delay_ms(1);
PORTC.3=1;//LD_HIGH;

/* For each '165... */
for (i = 0; i <= 2; i++)
{
_byte = 0;
/* ...read the serial out data */
for (j =0; j <= 7; j++) {
_byte <<= 1;    //shifts a reasonably expensive,so we only do one shift per loop
if (PIND.5 & (1<<QH_PIN))
{
_byte |=1;  //then we conditionally set the lsb

}
PORTD.7=0;//CLKINH_LOW;
delay_ms(1);
PORTD.7=1;//CLKINH_HIGH;
if(PIND.5==0)
{
LCDprint("0");
delay_ms(10);

}
else if(PIND.5==1)
{

LCDprint("1");
delay_ms(10);

}
}
serial_data[I] = _byte;

}

/* Disable clocking now */
CLKINH_HIGH;    //got me beat why you need to inhibit the clock - I'd just tie it low and save a port pin
}

uint8_t buttons[2] = {0, 0};

int i=0;unsigned char out;unsigned char data; // uint8_t register_value;
void main(void)
{
DDRA=0xff;
DDRB=0xff;
DDRC=0b00001100;
DDRD=0b11011111;
// PORTB.4=0; //buzer
// delay_ms(200);
PORTB.4=1;
PORTA.7=1;
PORTA.1=0;
PORTC.2=0;
SET_HC165_DDR;
SET_QH_PULLUP;

LCDinit();
delay_ms(10);

LCDcmd(0x01);

while(1)
{

delay_ms(1000);

LCDcmd(0x01);
delay_ms(1000);
}
}
[/I]

Last edited by a moderator:

#### tony64

##### New Member
unfortunately I could not succeed in transferring back the data I get serially.

Status
Not open for further replies.