Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Easiest way to load a shift register with PIC? (using 18F4550, hitech c 18)

Status
Not open for further replies.

Triode

Well-Known Member
I haven't done anything with serial output yet, and I'm trying to get started. I want to make a PIC18F4550 load a string of 3 shift registers (74HC595), 8 outputs each for a total of 24 bits. Ideally it would load 24 bit variables straight into them so it's easy to get them from arrays. Eventually the goal is to drive an LED matrix, where the rows are set by the shift registers, and the columns are cycled with PNP transistors connected to a decade counter. But for now I'm just looking for the best way to load the shift registers.
 
Assuming that the shift registers are cascaded then you could do something like,
Code:
void ShiftOut(unsigned short long dat){
unsigned char i;
    for(i=0;i<24;i++){
        DataPin=dat & 1;
        dat>>=1;
        ClockPin=1;
        ClockPin=0;
    }
    LatchPin=1;
    LatchPin=0;
}

Mike.
 
Thanks, that's very helpful. This is what I was going to try but then I didn't know how to pick a single bit out of a variable. So when you go "dat & 1" that is just anding with the rightmost bit? Then you shift dat left by one and continue to get then next one. Then clock pin is set high and low to advance, but what exactly does latch pin do?
 
Read the spec on the shift register...you have to 'latch' to xfer the data onto the parallel output of the SR. First you serial clock your 24 bits in....then latch - Output is 'latched' with data in SR, then unlatch - Output unaffected by SR data , clk another 24 bits in.. then latch and so on.
 
Oh, I was wondering how one avoided having the data show up as it was moved when it might not be desired.

I have implemented the code like this
Code:
/*
ShiftOut.c
add to main to use this:
void ShiftOut(unsigned short long dat);
*/

#ifndef __SHIFT_OUT_C
#define __SHIFT_OUT_C

#include "HardwareProfile.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"

#define DataPin RA3
#define ClockPin RA4
#define LatchPin RA5

void ShiftOut(unsigned short long dat){
unsigned char i;
    for(i=0;i<24;i++){
        DataPin=dat & 1;
        dat>>=1;
        ClockPin=1;
        ClockPin=0;
    }
    LatchPin=1;
    LatchPin=0;
}

#endif

It seems like it wants to work except for the part that I was having trouble with before, picking out a bit DataPin=dat & 1; It says it cant generate code for that. I tried
& 0b1
& 0b00000001
& true
& set

It just won't generate the code.
 
Last edited:
ok, it didn't seem to like comparing the string dat directly using a binary operator, so I did this

Code:
/*
ShiftOut.c
add to main to use this:
void ShiftOut(unsigned short long dat);
*/

#ifndef __SHIFT_OUT_C
#define __SHIFT_OUT_C

#include "HardwareProfile.h"
#include "GenericTypeDefs.h"
#include "Compiler.h"

#define DataPin RA3
#define ClockPin RA4
#define LatchPin RA5

void ShiftOut(unsigned short long dat){
unsigned char i;
    for(i=0;i<24;i++){
        [B][U]DataPin = (dat % 2 == 1);[/U][/B]
        dat>>=1;
        ClockPin=1;
        ClockPin=0;
    }
    LatchPin=1;
    LatchPin=0;
}

#endif

it may not be the most efficient but it works. First I tried using

DataPin = dat % 2

but it wouldn't take that, possibly because while it will only return 0 or 1 it isn't technically binary. I tried

DataPin = (dat & 1 == 1);

but it didn't work, same "cant generate code" error
 
Last edited:
It compiles fine in C18. Which compiler are you using?

Edit, just tried compiling in HiTech C and was told my evaluation period had run out. O well, no more HiTech C then.

Mike.
 
Last edited:
Usually I use C 18 but I'm using hitech C for this because I have a USB script that I haven't felt like converting and it would be good if the finished project could work with USB. It makes sense that it would work with one and not the other since they have different rules about converting data types, in general it seems like Hitech C will not allow as many kinds of manipulation of variables.
 
According to the HiTech manual you should be able to do DataPin=dat; as when you assign a char to a bit, it just uses bit 0.

However, it also states that & is a valid operator (it should be as it's standard to C). Maybe it's the assignment it doesn't like, try DataPin=(bit) (dat & 1);

Mike.
 
Last edited:
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top