Hi,
Apologies if this is in the wrong section. Im writing code for PIC18F45K22 and 5 TPIC6C595D Shift Registers.
I need the shift registers to run through a sequence to turn on and off LED, one by one. Each bit has a corresponding LED.
The PIC is using the SPI function.
The first segment of my code seems to work ok. The first 16 bits are broken into 2 x 8 bit parts.
The first 16 LEDs will work as expected, but the LEDs belonging to the 3rd, 4th and 5th shift registers turn on and off sporadically.
Can someone please point out what im doing wrong.
Mod edit: code tags - JohnInTX
Apologies if this is in the wrong section. Im writing code for PIC18F45K22 and 5 TPIC6C595D Shift Registers.
I need the shift registers to run through a sequence to turn on and off LED, one by one. Each bit has a corresponding LED.
The PIC is using the SPI function.
The first segment of my code seems to work ok. The first 16 bits are broken into 2 x 8 bit parts.
The first 16 LEDs will work as expected, but the LEDs belonging to the 3rd, 4th and 5th shift registers turn on and off sporadically.
Can someone please point out what im doing wrong.
C:
void SPIOut(unsigned int ind_value, unsigned char ctrl_value) {
unsigned char first_byte, second_byte, third_byte, fourth_byte, fifth_byte;
// Split the 16-bit ind_value into 5 bytes
fifth_byte = (unsigned char)(ind_value >> 32);
fourth_byte = (unsigned char)(ind_value >> 24);
third_byte = (unsigned char)(ind_value >> 16);
second_byte = (unsigned char)(ind_value >> 8);
first_byte = (unsigned char)(ind_value >> 0);
// Send the bytes through SPI
SPI1_write(fifth_byte); // Send to the first shift register
SPI1_write(fourth_byte); // Send to the second shift register
SPI1_write(third_byte); // Send to the third shift register
SPI1_write(second_byte); // Send to the fourth shift register
SPI1_write(first_byte); // Send to the first shift register
// Now it's time to pull the latch to transfer the values from the storage register to the value register
LATC4_bit = 0;
asm { nop };
asm { nop };
asm { nop };
LATC4_bit = 1;
// Send the last byte to the fifth shift register
SPI1_write(ctrl_value);
unsigned int led_state;
int i;
//unsigned char bytes [5]; // Array to hold data for 5 shift registers
void main() {
// 16MHz Speed
OSCCON.IRCF0=1;
OSCCON.IRCF1=1;
OSCCON.IRCF2=1;
PLLEN_bit = 0; //Disable PLL
ANSELA = 0; // Configure PORTA pins as digital
ANSELB = 0; // Configure PORTB pins as digital
ANSELC = 0; // Configure PORTC pins as digital
ANSELD = 0; // Configure PORTD pins as digital
ANSELE = 0; // Configure PORTE pins as digital
TRISA = 0b11111111; // PORTA All Inputs
TRISB = 0b11111111; // PORTB All Inputs
TRISC = 0b00000000; // PORTC All Outputs / UART
TRISD = 0b11111111; // PORTD All Inputs
TRISE = 0b000; // PORTE All Outputs
LATC = 0x00; // Initial PORTC value
LATD = 0x00; // Initial PORTD value
LATC0_bit = 0; //clear latch
asm{nop};
asm{nop};
asm{nop};
LATC0_bit = 1;
led_state = 0x0001; // initial value which we will send
SPI1_Init();
Delay_ms(50);
LATC4_bit = 0;
TRISC4_bit = 0; //Use SPI in line as output for RCK
LATC4_bit = 0;
do{
for(i=0;i<32;i++) {
led_state = led_state << 0x01;
SPIOut(led_state,0x00);
delay_ms(300);
}
} while(1);
}
Last edited by a moderator: