|
|
View previous topic :: View next topic |
Author |
Message |
Breno
Joined: 20 Jul 2006 Posts: 5 Location: Belo Horizonte, Brazil
|
Two PICs communicating in SPI |
Posted: Fri Sep 08, 2006 1:25 pm |
|
|
Hi everybody,
I was simulating something in Proteus and it worked perfectly. But when I was going to test it for real it didn't work very well: there is a 4MHz crystal in the OSC1 and OSC2 pins but the osciploscope can't see 4MHz there, there is nothing showing in the LCD display but some LEDs are working. Can someone tell me what's wrong?
This system works communicating the two PIC's through SPI and using a LCD. I will post the scheme of the simulation later.
Here it is the code for the master one:
Code: | // Configure chip and compiler
#include "16F877A.h"
#fuses XT,NOWDT,PUT,NOPROTECT,NOBROWNOUT,NOLVP
#use delay(clock=4000000)
#zero_ram
// #byte PORTA = 5
// PORTA is a variable mapped to the 8 physical pins of port A of the PIC
#define CS PIN_D2
#define DI PIN_C5
#define DOUT PIN_C4
#define CLK PIN_C3
void escreve(void);
int x=0;
int dado[8];
void main(void)
{
// Variables
// Configure pin direction
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
set_tris_a(0b11011111);
set_tris_b(0b11111111);
set_tris_c(0b10010111);
set_tris_d(0b11111011);
port_b_pullups(1);
output_high(PIN_C6);
output_high(PIN_A5);
// Setup the SPI port
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4);
// spi_write(0x00);
// spi_write(0x00);
while(1)
{
if(input(PIN_B0) == 0)
{
strcpy(dado, "01003355");
escreve();
while(input(PIN_B0) == 0);
}
else if(input(PIN_B1) == 0)
{
strcpy(dado, "02002715");
escreve();
while(input(PIN_B1) == 0);
}
}
}
void escreve()
{
for(x=0;x<8;x++)
{
output_low(PIN_A5);
delay_us(10000);
spi_write(dado[x]);
delay_us(10000);
output_high(PIN_A5);
}
}
|
And this is for the Slave one:
Code: |
// Configure chip and compiler
#include "16F877A.h"
#use delay(clock=4000000)
#include "LCD.c" // ENTRE "" OU <> ? CONFIRMAR!!!
#fuses XT,NOWDT,PUT,NOPROTECT,NOBROWNOUT,NOLVP,NOWRT // NOMCLR e INTRC_IO nao compilam!
#zero_ram
// #byte PORTA = 5
// PORTA is a variable mapped to the 8 physical pins of port A of the PIC
int data[8];
int bus1[8];
int bus2[8];
char x=0;
int c=0;
void ext_isr(void);
void escreve_horario(void);
void main(void)
{
// Configure pin direction
#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
set_tris_a(0b11111111);
set_tris_b(0b11111011);
set_tris_c(0b11011111);
output_high(PIN_B2);
// Setup the SPI port
setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_CLK_DIV_4);
// init interrupts
ext_int_edge( H_TO_L ); // seta a int ext para borda de descida
enable_interrupts(INT_EXT);
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);
// Inicializa LCD
lcd_init();
for(;;)
{
if(x==8)
{
x=0;
c=data[1];
if(c=='1')
{
// strcpy(bus1, data);
lcd_putc('\f');
lcd_putc("4111: ");
escreve_horario();
}
else if(c=='2')
{
// strcpy(bus2, data);
lcd_putc('\f');
lcd_putc("4501: ");
escreve_horario();
}
}
}
}
#INT_EXT
void ext_isr(void)
{
data[x]=spi_read(0);
x++;
}
void escreve_horario(void)
{
lcd_putc(data[2]);
lcd_putc(data[3]);
lcd_putc(':');
lcd_putc(data[4]);
lcd_putc(data[5]);
lcd_putc(':');
lcd_putc(data[6]);
lcd_putc(data[7]);
}
|
|
|
|
Breno
Joined: 20 Jul 2006 Posts: 5 Location: Belo Horizonte, Brazil
|
|
Posted: Fri Sep 08, 2006 1:46 pm |
|
|
Here it is the schematic:
Each of those two buttons, when pressed, will make the Master send a different message via SPI to the Slave PIC. The Slave PIC will then show the message in the display.
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
|
|
|
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
|