When the digital inputs of an MCU is used up, we can expand the digital inputs using various digital communication ICs such as I2C, SPI etc.
With a simple digital IC 74HC165 8-bit parallel-load shift register, reading digital input is such an easy way.
This IC commonly found in any photocopier machine.
A common SN74HC165 DIP Package. It come with 16 pins DIP, ease of breadboard prototyping. |
The SH/~LD load the 8 parallel inputs from Low to High transition.
The 8-bit parallel input labels from A to H. The internal circuitry start loading these parallel inputs at 4th clock (CLK) pulse, and shift out via QH pin.
A working waveform of 74HC165 simulated in Proteus VSM. |
In this example, I use PIC16F84A to read the digital input from SN74HC165. PortB of PIC16F84A uses to output the reading the data.
PIC16F84A clocks at 4 MHz, reads the digital inputs from 74HC165. PORTB displays the digital output to Bar graph LEDs. The 74HC165 reads the digital inputs from DIP switches, and commanded to shift out the data from PIC16F84A. |
The communication initiated by only three lines from PIC16F84A:
- CLK : clock line feds to SN74HC165
~EN : the Enable line feds to SH/~LD pin of SN74HC165
- DI : the QH (SO) serial data output line from SN74HC165
The program is driven by the 74165.c driver of CCS PICC compiler. We can use more than one 74HC165 IC as we need.
C source code could be downloaded here:
/*
Digital Input expanding Using 74HC165 SPI
*/
#include<16F84A.h>
#fuses XT
#use delay(clock=4M)
//Pre-Define Parameters For 74165.c
#define EXP_IN_ENABLE PIN_A1
#define EXP_IN_CLOCK PIN_A0
#define EXP_IN_DI PIN_A2
#define NUMBER_OF_74165 1
#include<74165.c>
void main(void){
char readInput;
output_b(0x00);
set_tris_b(0x00);
while(1){
read_expanded_inputs(&readInput);
output_b(readInput);
delay_ms(100);
}
}
If you want a standard PCB for ATMega32 micro-controller, you can order my AVR Microcontroller project from PCBWay with a reasonable price. Click here to get a free $5 credit for new account.
ATMega16 ATMega32 Experiment Board PCB from PCBWay |
No comments:
Post a Comment