jcmorales82
Joined: 15 Aug 2012 Posts: 1 Location: Venezuela
|
Problem with SPI and PIC16f628 |
Posted: Wed Aug 15, 2012 4:55 pm |
|
|
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
|
|
Posted: Thu Aug 16, 2012 5:13 am |
|
|
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. |
|