• AVR Freaks

AnsweredHot!74HC595 Shift Register wiith PIC167877A using SPI

Page: 12 > Showing page 1 of 2
Author
Sidhu805
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2017/12/04 00:59:52
  • Location: 0
  • Status: offline
2017/12/27 08:04:28 (permalink)
0

74HC595 Shift Register wiith PIC167877A using SPI

Hi I'm trying to implement simple spi communication to turn on 8 led connected to shift register can amy one help me whats wrong with this code, im getting random output, I want to send 0x02 to turn on led(00000010) .
My connection ST_CP(LATCH) of shift register to PORTC0
SH_CP( CLOCK) to PORTC3(SCK)
DS(Data select) to PORTC5(SDO)
 
 
#include <xc.h>
#include "spi.h"
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled)
#pragma config LVP = OFF // Low-Voltage In-Circuit Serial Programming Enable bit
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit
#pragma config WRT = OFF // Flash Program Memory Write Enable bits
#pragma config CP = OFF // Flash Program Memory Code Protection bit
#define _XTAL_FREQ 8000000
void main()
{
char data = 0x10;
spiInit(SPI_MASTER_OSC_DIV4, SPI_DATA_SAMPLE_END, SPI_CLOCK_IDLE_LOW, SPI_IDLE_2_ACTIVE);
TRISC0 = 0;
RC0 = 1;
RC3 = 0;
RC5 = 0;
while(1)
{
RC0 = 0; //Slave Select
__delay_ms(100);
spiWrite(data);
while(!SSPSTATbits.BF);
__delay_ms(100);
RC0= 1; //Slave Deselect
}
}

 
post edited by Sidhu805 - 2017/12/29 00:56:14
#1
jack@kksound
code tags!
  • Total Posts : 3224
  • Reward points : 0
  • Joined: 2014/05/14 10:03:19
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/27 11:48:56 (permalink)
+1 (1)
The 74HC595 requires a latch clock (RCK) to transfer the shift register data to the output latch, how are you driving that signal?
#2
mbrowning
USNA79
  • Total Posts : 1741
  • Reward points : 0
  • Joined: 2005/03/16 14:32:56
  • Location: Melbourne, FL
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/27 12:59:41 (permalink)
0
 SSPSTATbits.BF = 0;
This is the same as a NOP. BF is a read only bit.
#3
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/27 13:29:47 (permalink)
0
As above.
The only way to clear the BF flag is to read the SSPBUF register.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#4
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3840
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/27 15:42:28 (permalink)
0
You may not need the read but always complete the exchange like this.
 
SSPBUF = dat;
while (!SSPSTATbits.BF);
SSPBUF;
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#5
Gort2015
Klaatu Barada Nikto
  • Total Posts : 3840
  • Reward points : 0
  • Joined: 2015/04/30 10:49:57
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/27 15:49:34 (permalink)
0
What is the advantage of using spi rather than latch pins?
 
I would use interrupts to latch the 74HC595 at timed intervals.
You may need spi for external devices.
 

MPLab X playing up, bug in your code? Nevermind, Star Trek:Discovery will be with us soon.
https://www.youtube.com/watch?v=Iu1qa8N2ID0
+ ST:Continues, "What Ships are Made for", Q's back.
#6
Sidhu805
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2017/12/04 00:59:52
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/27 19:31:54 (permalink)
0
595 DS i connected to SDO(MOSI)
And latch pin to RB5
#7
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/28 20:45:20 (permalink)
+1 (1)
Sidhu805
595 DS i connected to SDO(MOSI)
And latch pin to RB5

Your code sets RB to 0, and leaves it there.
It should be toggling that signal low to high each time you write a new value into the 74HC595.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#8
Sidhu805
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2017/12/04 00:59:52
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 01:05:58 (permalink)
0
Can you please check modified code again
without spi i tried sending data one by one its working perfectly


when i send data to SSPBUF in a same way its not working



#9
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 01:45:53 (permalink) ☼ Best Answerby Sidhu805 2017/12/29 01:48:03
+1 (1)
You only attached the code that does not use SPI, so how can we tell you what is wrong with your SPI code?
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#10
Sidhu805
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2017/12/04 00:59:52
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 01:49:29 (permalink)
0
I uploaded in main thread anyway here is the source code please do check
#11
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 02:13:09 (permalink)
+1 (1)
Sidhu805
I uploaded in main thread

Please do NOT go changing the initial question once people start answering.
That confuses the discussion terribly.
 

anyway here is the source code please do check

I told you yesterday, the only way to clear the BF flag is to read the SSPBUF register.
Your new code NEVER reads the SSPBUF register, so BF stays set, so the wait loops won't work correctly.
 
I don't know if you wrote that SPI code, but it is rubbish. Everyone makes the mistake of trying to treat SPI like a USART when it is not.
SPI is always a simultaneous TRANSFER between Master and Slave.
You must do a write and read for every transfer, even if you don't care about the data.
 

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#12
Sidhu805
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2017/12/04 00:59:52
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 03:11:50 (permalink)
0
Ok sorry I'm completely new to this fourm, I promise i'll not make this mistake again by confusing main thread
anyway thank you for responding i'll try ur solution
#13
DavidBLit
Super Member
  • Total Posts : 1579
  • Reward points : 0
  • Joined: 2012/02/18 13:08:48
  • Location: The Land of Confusion
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 07:31:22 (permalink)
0
qɥb
...
SPI is always a simultaneous TRANSFER between Master and Slave.
You must do a write and read for every transfer, even if you don't care about the data.

It might be more clear to emphasize that it's a simultaneous exchange of data, master-to-slave at the same time as slave-to-master.
 
They still won't get it, though...because reading+thinking is hard.  wink: wink

Yeah, "//Code and stuff".
#14
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 14:06:02 (permalink)
0
Agreed, "Exchange" gives a clearer indication than "Transfer".

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#15
laurie21
Junior Member
  • Total Posts : 116
  • Reward points : 0
  • Joined: 2009/07/21 12:59:44
  • Location: Vancouver B.C. Canada
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 18:04:30 (permalink)
+1 (1)
The 595 needs 3 output pins from the PIC. Data, Clock, and Shift.
I see you have made the RC0=1. That will make it an input. If you are using it as the 595's latch, it should be RC0=0.
 In execution place the byte into the SSPBUF, This will start the auto shift-out, then pole the BF bit to make sure the 8 bits are all out of the buffer. Then, toggle the Shift line which in your case is the RC0.
  FYI When a pin is an output it is called and referred to as lata,b,c,etc. Only if a pin is an <input> do you call it a port. If one looks at the boot-log at setup there is porta,b,c,etc. (input) , lata,b,c,etc. (output) , and trisa,b,c,etc. (direction). In the program context you must use the proper terms.
post edited by laurie21 - 2017/12/29 18:20:54
#16
Liem Dung Phan
New Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2017/09/14 21:09:39
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 18:19:20 (permalink)
0
You can see here: https://youtu.be/2cY9Vf_6Us4
#17
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 18:41:10 (permalink)
0
laurie21
I see you have made the RC0=1. That will make it an input. If you are using it as the 595's latch, it should be RC0=0.

Wrong.
You are thinking of the TRISC0 bit. The code is correctly making the pin an output with "    TRISC0 = 0;"
 

 In execution place the byte into the SSPBUF, This will start the auto shift-out, then pole the BF bit to make sure the 8 bits are all out of the buffer. Then, toggle the Shift line which in your case is the RC0.

The code is raising RC0 after the transfer. That part is correct.
 
  FYI When a pin is an output it is called and referred to as lata,b,c,etc. Only if a pin is an <input> do you call it a port. If one looks at the boot-log at setup there is porta,b,c,etc. (input) , lata,b,c,etc. (output) , and trisa,b,c,etc. (direction). In the program context you must use the proper terms.

Wrong.
You are thinking of PIC18F chips and PIC16F1xxx chip.
The PIC16F877 does not have any LAT registers. None of the "standard" midrange chips do.
 
post edited by qɥb - 2017/12/29 18:43:59

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#18
qɥb
Monolothic Member
  • Total Posts : 3332
  • Reward points : 0
  • Joined: 2017/09/09 05:07:30
  • Location: Jupiter
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 19:10:56 (permalink)
0
liem.phan
You can see here: https://youtu.be/2cY9Vf_6Us4

That shows MCC being used.
MCC does not support the PIC16F877A

This forum is mis-configured so it only works correctly if you access it via https protocol.
The Microchip website links to it using http protocol. Will they ever catch on?
PicForum "it just works"
#19
Sidhu805
New Member
  • Total Posts : 12
  • Reward points : 0
  • Joined: 2017/12/04 00:59:52
  • Location: 0
  • Status: offline
Re: 74HC595 Shift Register wiith PIC167877A using SPI 2017/12/29 19:12:13 (permalink)
0
Can you please send source code
#20
Page: 12 > Showing page 1 of 2
Jump to:
© 2020 APG vNext Commercial Version 4.5