[SOLVED] I want to connect 80 inputs to micro controller

Status
Not open for further replies.
with the suggestion of this threads ,I decided to work with 74hc165 shift register with 8051 as shown the circuits . i am not well in program . i Need to cascade 74hc165 for getting 80 input But i need to study how to read from a single one . My plan is that , which bit is ( ? switch in DIP switch ) will be selected and need to display in LCD with an LED ON (alert) . please guide me .
 
Last edited:

Is this flowchart is correct for reading input by using 74hc165

PL(Pin No. 1)
CP(Pin No. 2)
CE(Pin No.3)

1) CE Makes zero. // dont know well for what /
2) PL makes one // to load the 8 bit value in the shift register
3 is there any need of Delay Dont know ?
4)PL makes zero // to read the data bit at pin 9 of 74hc165
5)read msb bit
6 give a clock
7) read net bit
8 repat 6,7 steps util 8 bit .

pleas guide me
 

The easiest way is use a keyboard scanner type method to sample the input states using row / column scanners ( not the N key rollover chips)

Then sample all inputs with a frame rate faster than the expected rate and debounce if necessary in s/w.

In the late 70's We would use a MC6800 to address a 1 of 16 decoder with a 1 of 8 decoder for row & column to get 128 inputs on an 8 bit bus in 16 scans. This takes (4+3=) 7 data bits to use a matrix x,y addresses and we used the 8th bit to define if the latched address was an input or an output and thus get 128 outputs as well. So for input ports write address, latch then read back results if 8th bit was = 1=in

Many other ways to do this with CPLD's
 



I cannot use matrix keyboard because my switches are placed in different room or place ..

- - - Updated - - -

what is the need of CE Clock enable pin ?
 

CE is for Chip Enable. Only when chip is enabled it will work else it behaves as if it were off.

Why don't you use microchip I2C port expanders ? You can have 8 x 16 channel Port Expander. It will give 16 x 8 = 128 pins. You can have 128 inputs. The I2C port expanders have a INT pin which you can connect it to INT0 or INT1 pin of the controller and if the state of the port expander input pins change then it generates interrupt which can be detected by 8051 (in your case) and the controller can then read the Port Expander port state and find out which input caused the interrupt by comparing the port value with the old port state.
 
Last edited:
So In my case can i use this chip enabled the whole time ?
what is the draw back if i use 74hc165 ?
 

In your case there are more than one 74HC165. You can connect the outputs of different HC165 to the same input pin of controller. You have to enable one HC165 at a time scan its inputs and take action based on the input data. As HC165 is a parallel to serial shift register it will be slow. If you use Port expander then with the interrupt signal you will know that the state of the port expander has changed and you can read that port expander only when the change is setected. In HC165 you have to read the chips continuously.
 
yes thanks , May i get some examples by using port ex pander i need at-least 50 input
 

Use Microchip I2C Port expanders. Use either 5 x 8 bit Port expanders or 4 x 16 bit port expanders. For 16 bit port expanders I can write code using mikroC PRO Compiler. Can you use PIC microcontroller instead of 8051 ?
 


Use Microchip I2C Port expanders. Use either 5 x 8 bit Port expanders or 4 x 16 bit port expanders. For 16 bit port expanders I can write code using mikroC PRO Compiler. Can you use PIC microcontroller instead of 8051?

Please don't confuse the discussion. You got it all mixed up. When using cascaded parallel-in/serial-out shift registers like HC165, you don't need individual selects. Instead all shift registers are loaded simultaneously by a common PL signal. Then the data is shifted through all registers, using a Clock and Serial pin. In total, it's a 3 line interface.

How slow the serial shift register solution might ever be, I2C will be slower. If your processor has a dedicated SPI interface, data can be read with MHz bit rate. If it hasn't (and neither an I2C interface) SPI bit banging method is still simple compared to software I2C. No need to suggest a specific processor.
 
Sorry, I didn't see that the HC165 are cascaded. if you need 50 input lines then use 5 HC165s and cascade them then send 8 clocks at a time and read one byte at a time which will give the state of a HC165. Like this give 8 clocks 5 times and everytime read the data into a byte.
 
So the CE pin can be grounded the whole time right ?
 

So the CE pin can be grounded the whole time right
If you mean the HC165 CLKINH pin, yes. Only SH/nLD, CLK, SER and QH used for the serial interface.

need 50 input lines then use 5 HC165s and cascade them
My pocket calculator keeps insisting 8*5 = 40
 
i am not well in programming so i need to interface with a single one firstly .
 

I believe, serial-in code has been posted a hundred times at Edaboard. But anyway

Code:
int8 sr;
int8 data[NBYTE];
int8 i,j;

SHLD = 0;
// optional delay
SHLD = 1;
for (j = 0, j < NBYTE; j++) {
{
  for (i = 0,  i < 8, i++) {
    sr = (sr << 1) | SI;
    CLK = 1;
    // optional delay    
    CLK = 0; 
  }
  data[j] = sr;
}

Use delays for fast processors as required. Use IO macros or built-in functions according to the used processor and compiler tool.
 
@FvM

My pocket calculator keeps insisting 8*5 = 40

Yes, that was a small mistake.

@thannnara

I will write code for you for single and 7 HC165 interfacing and reading. Just zip and post the Proteus file and mention which controller and Compiler you are using. If you are using 8051 then can you change it to PIC because I mainly use PIC.
 

Here is a similar project with 64 inputs, pcb design and software. You can get many ideas from it..
**broken link removed**
 
I got a program for this as follows
Code:
#define data_pin portf.f0 
#define clk portf.f1 
#define pl portf.f2 

main() 
{ 
trisf = 0b00000001; 
trisb= 0b00000000; 
unsigned char data = 0;

clk = 0 ; // portf.f1 
pl = 0 ; // portf.f2 
//delay()	// may need to call a few microsec.delay here, and other marked places too 
pl = 1; //portf.f2 
//delay()

If(data_pin) // read status of uc pin(which is connected to serial o/p) 
data = data+1; 
for(i = 0; i < 7; i++) 
{ 
data<<1; 
clk = 1; 
//delay()	
clk = 0;
//delay() 
If(data_pin) // read status of uc pin(which is connected to serial o/p) 
data = data+1; 
} 

while(1) 
{ 
portb = data; 
} 

}
From this thread https://www.edaboard.com/threads/124798/ on #10 post

I have a doubt what it mean
if(data_pin){}
 

data_in is the input pin of the microcontroller to which output of HC165 is connected to. It is checking if input is 1 or 0. If it is 1 then 1 is assigned to buffer and it is shifted. If it is 0 then 0 is appended to the byte and shifted.

- - - Updated - - -

Try this code. It reads data from 7 cascaded 74HC165s. This is mikroC PRO 8051 code.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
sbit DATA_IN at P1_1_bit;
sbit CLK at P1_0_bit;
sbit SHLD at P1_2_bit;
 
char byte[7][8];
 
void Read_Keys() {
 
    char i, j;
 
    SHLD = 0;
    Delay_us(500);
    SHLD = 1;
    
    for(i = 0; i < 7; i++) {
        for (j = 0; j < 8; j++) {
          if(DATA_IN)
               byte[i][j] = 1;
          else byte[i][j] = 0;
 
          byte[i][j] <<= 1;
          
          CLK = 1;
          Delay_us(500);
          CLK = 0;
        }
    }
}
 
void main() {
 
     P0 = 0x00;
     P1 = 0x02;
     P2 = 0x00;
     P3 = 0x00;
     
     Delay_ms(200);
     
     while(1) {
     
     
     
     }
}

 

Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…