Page 1 of 1

Anything wrong with this shift register function (74HC165)?

PostPosted: Mon Jul 27, 2015 5:06 pm
by Jidis
Microchips forum is refusing to let me post for some reason.

I'm trying to push myself off assembler and learn some C, but can't seem to get a working dump from this PISO shift chip. I tried my best to duplicate the order I used in the assembler version as far as the clock/latch activity goes, but I've obviously done something wrong. I've also rearranged the order to no avail, so I'm guessing the flaw is elsewhere.

The output on PORTB is basically nothing after the flashing intro lights.

Thanks!

- BTW, I'm running Hi-Tech 9.83 under MPLAB 8.92

Code: Select all
#include <htc.h>  //yeah, I probably don't need these ;)
#include <stdint.h>
#include <pic16f84a.h>

__CONFIG(FOSC_XT & CP_OFF & PWRTE_OFF & CP_OFF & WDTE_OFF);

#define _XTAL_FREQ 4000000

#define     data      RA2 
#define     latch     RA0
#define     clk       RA1 

unsigned char myByte;    //should hold the returned 74HC165 data
unsigned char shiftin(void);

void   twoflash(void) //just a flashing light routine
{   RB7 = 1;
        __delay_ms(300);
   RB7 = 0;   
        __delay_ms(300);
   RB7 = 1;
        __delay_ms(300);
   RB7 = 0;   
        __delay_ms(300);
}

unsigned char shiftin()
{
   unsigned char inByte = 0;  //temp holder for the grabbed byte
   unsigned int i = 0;

      latch = 0;  //drops to capture the register
       latch = 1;  //back to default

while(i < 8) {
               //test the serial out
if(data && 1)        // data is one
{    inByte <<= 1;
    inByte += 1; } // rotate left and set bit 0
else                //data is zero
     inByte <<= 1; // rotate left, bit 0 defaults to 0
            i++;

         clk = 1;  //pulse the clock high
         clk = 0; //drop back to default
      }
   return inByte;  // leave with the byte
   
}   

void main()
{
    TRISA = 0b11110000;
   TRISB = 0b00000000;
   PORTB = 0;
   latch = 1;
   clk = 0;

   twoflash(); // just shows that the program started

   while(1)
  {
      myByte = shiftin();
      PORTB = myByte;
   }
}

Re: Anything wrong with this shift register function (74HC16

PostPosted: Mon Jul 27, 2015 6:47 pm
by Jidis
I think my brain's too much of a wreck these days to be doing this stuff. I figured it out when I was in the shower.

I was starting with reused code from my 74HC595 test and forgot I was now working with an 'input' for the data line. Changed my TRISA config and it works. Also had to flip the if/else actions in the shift register function as I forgot all of my "pressed" buttons will be pulled to ground and read zero rather than one.

Take Care