hoangkhuong
Joined: 16 Mar 2012 Posts: 31
|
Problem with SPI communication between 16f877a and 30f4011 |
Posted: Fri Mar 16, 2012 11:08 am |
|
|
Hi everyone,
I am trying to communicate a pic16f877a with a dsPic30f4011 but still failed. I hope some of you can figure out my problem. Thank you in advance.
Here is my code:
For Master (dsPic30f4011)
Code: |
#include <30F4011.h>
#DEVICE ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOWRT
#FUSES XT_PLL8
#use delay(clock=4000000)
#use rs232(baud=4800, parity=N,xmit=PIN_F5,rcv=PIN_F4,bits=8))
#define SDI PIN_F2
#define SDO PIN_F3
#define SCK PIN_F6
#define SS PIN_B2
#INT_SPI1
void spi()
{
clear_interrupt(INT_SPI1);
}
void main()
{
int8 a = 8;
set_tris_b(0x00);
set_tris_f(0b00000100);
set_tris_e(0x00);
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16|SPI_XMIT_L_TO_H);
enable_interrupts(INT_SPI1);
enable_interrupts(INTR_GLOBAL);
output_high(SCK);
output_high(SS);
while(1)
{
delay_ms(100);
output_low(SS);
delay_ms(10);
spi_write(a);
output_e(a);
a++;
delay_ms(100);
output_high(SS);
delay_ms(3000);
if (a >= 200 ) a = 0;
}
} |
For Slave:
Code: |
#include <16f877a.h>
#device *=16
#FUSES NOWDT, HS, NOPUT, NOPROTECT
#use delay(clock=4000000)
#include "LCD_4bit.h"
#define SDI PIN_C4
#define SDO PIN_C5
#define SCK PIN_C3
#define SS PIN_A5
int8 in_data = 0;
#INT_SSP
void spi()
{
in_data = spi_read();
output_c(in_data);
lcd_gotoxy(1,1);
printf(lcd_putc,"%u",in_data);
clear_interrupt(INT_SSP);
}
void main()
{
set_tris_a(0x00100000);
set_tris_c(0b00010000);
output_c(0);
setup_spi(SPI_SLAVE|SPI_L_TO_H|SPI_CLK_DIV_16|SPI_XMIT_L_TO_H);
enable_interrupts(INT_SSP);
enable_interrupts(GLOBAL);
lcd_init();
delay_ms(20);
while(1)
{
}
}
|
Thanks again |
|