Hi everybody
I need to expand input ports with 74HC165 and output ports with 74hc595 on my 18F4550. I'm using shift register 74HC165 and 74hc595
I want to know how to write sample code for reading inputs. Also i would like to know some schematics how to connect shift register to 18F4550. I am working in mikro c for pic
and there is the schematic diagram.
please help me
//////////////////////////////////////////////////////////////////////////////// READING 74HC165 AND WRITING 74HC595 SHIFT REGISTERS WITH PIC18F4550////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////// NUMBER OF 74HC165s IN YOUR CIRCUIT (change to whatever you want)//////////////////////////////////////////////////////////////////////////////constint number_of_74hc165s =1;#define latch_pin LATA.F5 // 74HC165 PIN #1#define clock_pin LATA.F4 // 74HC165 PIN #2#define data_pin PORTA.F3 // 74HC165 PIN #9#define shift_register PORTA.F2 // 74HC595 PIN #11#define storage_register PORTA.F1 // 74HC595 PIN #12#define serial_data PORTA.F0 // 74HC595 PIN #14unsignedchar buttons[number_of_74hc165s *8];void write_outputs_595(char* value){unsignedint i =0;while(i <8){if(value[i]=='1')
serial_data =1;else
serial_data =0;
shift_register =1;
shift_register =0;
i++;}
storage_register =1;
storage_register =0;}void read_inputs_165(int number_of_shift_registers){unsignedint m =0;
latch_pin =0;
latch_pin =1;while(m < number_of_shift_registers *8){
clock_pin =0;if(data_pin) buttons[m]='0';else buttons[m]='1';
clock_pin =1;
m++;}}void main(){
ADCON1 |=0x0F;
CMCON |=7;
TRISA =0x00;// define all ports as outputs
TRISA.F3=1;// data_pin of 74HC165 must be defined as input
PORTA =0x00;// clear PORTAwhile(1){// Example how to use 74HC595. First four ports has HIGH value and last four has LOW value
write_outputs_595("010101010");// Example how to read inputs from shift register 74HC165
read_inputs_165(number_of_74hc165s);// Check Button 1if(buttons[0]=='1'){ write_outputs_595("00000001");delay_ms(2000);}if(buttons[1]=='1'){ write_outputs_595("00000010");delay_ms(2000);}if(buttons[3]=='1'){ write_outputs_595("00000100");delay_ms(2000);}
..............
// Button 1 is pressed .....// Do whatever you want ..}}
thiscode has work well for one shift reg (74hc164) and one shift reg(74hc595)
but when I uesd it with two shift reg (74hc164) and two shift reg(74hc595)
it didnt work in spite of I change the//// const int number_of_74hc165s =2 ///
what is the wrong?
The schematic looks right at first sight, the read part is supposed to work with m=2, you however don't show code for accessing multiple output registers.
In any case, if you don't see what's wrong, check the code execution in real hardware by single-stepping with a debugger.
I NOTICED that the16 buttons(inputs) have work well with (74hc165)
but the outputs (the leds) by (74hc595 ) only one shift reg (74hc595 ) has work well
but the another 74hc595 didnot work
only 8 leds have work well
what is the problem?