I'm trying to drive a shift register using a PIC12F683, so I wrote this code for a simple test:
#include <pic.h>
#include <pic12f683.h>
#define _XTAL_FREQ 4000000
// GP0 -> data
// GP1 -> latch
// GP2 -> clock
void clear_shift_register() {
GPIObits.GP1 = 0;
GPIObits.GP0 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP2 = 0;
GPIObits.GP1 = 1;
}
void main(void) {
TRISIO = 0x00;
clear_shift_register();
while(1) {
GPIObits.GP1 = 0;
GPIObits.GP0 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP0 = 0;
//GPIObits.GP0 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP0 = 0;
GPIObits.GP0 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP0 = 0;
//GPIObits.GP0 = 1;
GPIObits.GP2 = 0;
GPIObits.GP2 = 1;
GPIObits.GP0 = 0;
GPIObits.GP1 = 1;
__delay_ms(500);
}
}
When I programmed the PIC and powered it, nothing happened. So I decided to hook up some LEDs directly to the PIC pins to know what it was doing:
- GP0 is OFF all the time
- GP1 is ON all the time, but periodically does a one-time flicker very fast
- GP2 is OFF all the time
Why am I getting these weird results and how to correct it?