Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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

Status
Not open for further replies.
I wrote just is it correct
Code:
void main(){



               CLK = 0;
               PL = 0;
               delay(50);
               PL = 1;
	if(SI)
	{
	 shiftdata = shiftdata + 1;
	 for(q=0;q<7; q++)
	  { 
	   shiftdata << q;
	   CLK = 1;
	   delay(30);
	   CLK = 0;
	   if(SI)
	   shiftdata= shiftdata + 1;
		}		
		}
 

Just use my code. This is wrong.

Code:
shiftdata << q;
You shift only once when a bit is read. You read in 8 bits and shift it in to a buffer. You only have to do 7 or 8 shifts based on your code.
 

thanks , but i didnt get it well can you explain ,

byte[j] = 1;
else byte[j] = 0;

byte[j] <<= 1;

how do i get the final data ?
 

The shift registers data will be stored in 7 elements of the 2D array. Each row of the array contains data of one shift register. The 1st row contains the data of last shift register and the 7th row contains the data of the 1st shift register. There will be total 7 74HC165s cascaded. Based on the data in each row of the 2D array you decide which button was pressed. For buttons do like this, if button pressed then input goes high else it will be low. This will be easy to check. Use switch statement for each row of 2D array. There will be 8 cases in each switch statement and there will be 7 switch statements.

in the for loops if i = 0 and j = 0 then byte[j] will be byte[0][0] that is 0th row and 0th column. There will be 8 columns as there are 8 bits out from a shift register.

If input to MCU is 1 (coming from last HC165) then that bit should be stored as 1 in the byte[0][0] else it should be stored as 0 and then the byte should be shifted once to left as we as reading MSB first. Hence after 7 shifts the MSB will be in the proper place in the byte[][] array. The outer for() loop counts from 0 to 6 that is 7 loops for getting data of 7 74HC595s. The intter for() loop loops 8 times (0 to 7) and reads the 8 bits of the shift register with MSB read first.
 
Last edited:

wow thanks that is very good idea thank-you , let me try
 

In a short, all codes posted by milan.rajik and thannara123 up to now have some errors in deserializer routine. A good way to find it yourself is to run the code in a simulator. I'm sure you'll figure it out.
 
Thanks FvM for finding out the bug.

Here is the fix.


Code C - [expand]
1
2
3
if(DATA_IN)
               byte[i][j] |= 1;
          else byte[i][j] |= 0;




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
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;
        }
    }
}



For the inputs to 74HC165s are you using Tact switches or SPST switches ?
 
You need 1d array only. The 2nd dimension is implemented by the 8bits of each byte in the 1d array.
FvM in this link:
https://www.edaboard.com/threads/332224/#post1448018
posted the correct code.

You should do something like that (althow it is not the way I would write it):

Code:
    for(i = 0; i < 7; i++) {
        for (j = 0; j < 8; j++) {

          byte[i] <<= 1;

          if(DATA_IN)
               byte[i] |= 1;
          else byte[i] |= 0; 
          
          CLK = 1;
          Delay_us(500);
          CLK = 0;
        }
    }
 
Last edited:
You should keep reading the 74HC165s all the time. What is the actual device you are designing ? What it does other than reading the 50 inputs ?
 

The switchs are located and diffrent location or rooms . Whenever press the button i need to identyfy the swictch on display.
 

if the rooms are different location,it is good to use RS485 protocol,it only use 2 wires.
i have already tested with MODBUS protocol using AT89C51 as master and AT89C2051 as slave.
so the wiring complications are avoided.
 
if the rooms are different location,it is good to use RS485 protocol,it only use 2 wires.
i have already tested with MODBUS protocol using AT89C51 as master and AT89C2051 as slave.
so the wiring complications are avoided.

I am not well in programm . Eventhough may i see the detail of your project
 

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 ?

Distance only demands "signal conditioners" or RC filters and reduced MUX switch speed , which I suspect won't impose any restrictions.

First define what type of switches are used. gold plated I presume.. otherwise wetting current must be provided.
Second define use and toggle rate.
Third desired max latency.
 
if the rooms are different location,it is good to use RS485 protocol,it only use 2 wires.
i have already tested with MODBUS protocol using AT89C51 as master and AT89C2051 as slave.
so the wiring complications are avoided.
I agree that extending an "interchip" interface like SPI or I2C over many meters to a different room means asking for trouble. It's not completely impossible, but without well considered signal filtering, ESD events or RF interferences are likely to cause communication errors and e.g. ghost key presses. The solution suggested by matthai is straightforward and doesn't require much additional resources, except for a second µC.
 
if programming a second uC is an issue, my solution can be done in CPLD or discrete logic with RC filters to suppress noise, crosstalk and ESD transients, if done to low bandwidth in the presence of AC current nearby.

What is the transition frequency ? 0.0001 Hz ? 10 kHz?
 
You need 1d array only. The 2nd dimension is implemented by the 8bits of each byte in the 1d array.
FvM in this link:
https://www.edaboard.com/threads/332224/#post1448018
posted the correct code.

You should do something like that (althow it is not the way I would write it):

Code:
    for(i = 0; i < 7; i++) {
        for (j = 0; j < 8; j++) {

          byte[i] <<= 1;

          if(DATA_IN)
               byte[i] |= 1;
          else byte[i] |= 0; 
          
          CLK = 1;
          Delay_us(500);
          CLK = 0;
        }
    }

Here byte <<= 1; what does it means
 

If you have 7 74HC165s and if the 2nd button of the 7th 74HC165 is pressed then the 1st byte coming into the buffer is 0x02. So, one bit at a time has to be read and shifted into the byte array.

So, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 has to be shifted into the 2D array.

when i = 0, j can take 8 values (0 to 7).

byte[0][0] = 0x02;

So, to shift one bit at a time into the buffer at location bit 0 we are using OR operator and shift the shift the bit one bit to left. So, if 0x02 has to be shifted into byte[0][0] then consider that value of byte[0][0] was 0x00. then you have to read in the 7th bit from 74HC595 which will be 1 and OR this with byte[0][0] will result in 0x00 OR 1 = 0x01 then you have to make place for the 8th bit that will be read next which will be placed at bit 0 of the byte array. So, you left shift the contents of the byte array once. this will result in 0x02. Then you read the last bit which is 0 and then OR it with contents of byte[0][0]. So, 0x02 OR 0 = 0x02.

After reading all the 74HC165s the contents of the array will be as below.

byte[0][] = 0x02
byte[1][] = 0x00
byte[2][] = 0x00
byte[3][] = 0x00
byte[4][] = 0x00
byte[5][] = 0x00
byte[6][] = 0x00


0b00000001 << 1 = 0b00000010
0b00000001 << 7 = 0b10000000


<< is a bitwise operator for left shift. It shifts the bits x times to left.

if byte[0][0] = 0x01 = 0b0000 0001 then byte[0][0] << 2 will shift the bits inside byte[0][0] 2 bits to left. So, it becomes 0b0000 0100 = 0x40.
 
Last edited:
Sorry for the delay i will back soon , I am nothing in C programming ,I am learning c programming ...
 

is there any logic to scan the input (as mentioned above) without saving all the input state into an array .
Only saving the ON state or High state of the DATAPIN ?
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top