CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with SPI and PIC16f628

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
jcmorales82



Joined: 15 Aug 2012
Posts: 1
Location: Venezuela

View user's profile Send private message

Problem with SPI and PIC16f628
PostPosted: Wed Aug 15, 2012 4:55 pm     Reply with quote

Hi guys! Hope you can help me out with this issue... I'm using CCS 4.109 and I'm trying to implement communication between 2 PIC's 16f628 trough SPI, I'm using PROTEUS to simulate the circuit, and the master works fine, but the slave is getting crappy data... I read somewhere that could be an issue with the compiler, but i'm not sure about it... Here is the code! Thanks a lot!

SLAVE PIC

Code:

#include<16F628A.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#byte puerto_a=5
#byte puerto_b=6
#use delay(clock=4000000)
#use spi(SLAVE,CLK=PIN_A0,DO=PIN_A2,DI=PIN_A1,ENABLE=PIN_A3,MODE=1,BITS=8)

byte dato_rx;

void main()
{
   for(;;){
   dato_rx=spi_xfer(0);
   puerto_b=dato_rx;
   delay_ms(1);
   }
}


MASTER PIC

Code:

#include<16F628.H>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#byte puerto_a=5
#byte puerto_b=6
#use delay(clock=4000000)
#use spi(CLK=PIN_A0,DO=PIN_A1,DI=PIN_A2,ENABLE=PIN_A3,MODE=1,BITS=8)

byte dato_tx;

void main()
{
   for(;;){
   dato_tx=puerto_b;
   spi_xfer(dato_tx);
   delay_ms(1);
   }
}
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Thu Aug 16, 2012 5:13 am     Reply with quote

Proteus related questions are troubled here... Often Proteus has shown to be the problem. Best is to build the circuit with real hardware.
See the first sticky post in this forum!

A few tips though:
1) Get rid of the delay_ms in the slave. The slave will wait for the data to arrive before continuing. Your extra delay can make the slave to be too late for new data to receive.
2) In the master you have to handle the enable line yourself. I don't think the library can do it for you. This would explain your slave to be out of sync with the data sent by the master and you receiving garbage.
3) You don't handle the TRIS registers. I haven't checked the default values, but either the reading of port_b or the writing will fail. Instead of hard coded access to the port addresses you should use the CCS functions input_b() and output_b(). Then the compiler will set the TRIS registers for you automatically, is easier to read and also portable to other processors.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group