egg86
Joined: 25 Aug 2009 Posts: 3
|
problem of one master controlling two slaves in SPI |
Posted: Sun Mar 28, 2010 1:01 pm |
|
|
Hi all, i am facing serious deadline problem and hope someone can offer me help.
I am doing a three PIC16F877A system with one master controlling two slaves, the master will send data to slaves as one way communication to control the DC motor. The master will also read input sensor data as analog value. I am using SPI to communicate between PIC.
Although i refer to PIC data sheet a lot, but i do feel i got problem on the SPI setup. The slaves seems not receiving from master PIC.
Attached is my master code:
Code: | //master program in spi to control two motors with two ccp
#include <16f877a.h>
#device adc=8
#use delay(clock=20000000)
#fuses HS, NOWDT, NOPROTECT, NOLVP
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h> //for floating abs function
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9
#byte sspcon1=0x14
#byte sspstat=0x94
#define MASTER_DEVICE
int8 junk[2];
int i;
float val1; //val1 = x-axis sensor(ADC Value)
float val2; //val2 = y-axis sensor(ADC Value)
void slave_1()
{
output_high(PIN_A5);
for(i=0;i<2;i++) //to trasmit motor speed to slave 1
{
spi_write(255); //left motors rotate
junk[i]=spi_read();
}
}
void slave_2()
{
output_low(PIN_A5);
for(i=0;i<2;i++) //to trasmit motor speed to slave 2
{
spi_write(255); //right motors rotate
junk[i]=spi_read();
}
}
void main()
{
//declaration
set_tris_A(0xDF); //set ss as input 0b 1101 1111
set_tris_B(0xFF);
set_tris_C(0xD1); //config spi on portc (0xD1 for master) 1101 0001
set_tris_D(0x00); //slave select as output 1101_1111
setup_timer_2(t2_div_by_4,100,1);//Define use of Timer2 and postscalar value
setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16/*|SPI_SS_DISABLED*/);
setup_adc(adc_clock_internal); //set ADC clock
setup_adc_ports(AN0_AN1_AN3); //pin0,1,3 as analog input
//setup_adc_ports(ALL_ANALOG); //enable port a as ADC port
setup_ccp1(ccp_pwm); //for ccp1
setup_ccp2(ccp_pwm); //for ccp2
port_b_pullups(TRUE); //internal pull ups active on port b
sspcon1=0b00100001; //config spi reg
sspstat=0b10000001;
//program starts
portd=0x00;
delay_ms(10);
portd=0x09;
set_pwm1_duty(255);
set_pwm2_duty(255);
while(TRUE)
{
set_adc_channel(0);
delay_us(10);
val1=read_adc(); //read the adc value from X-axis
delay_us(10);
set_adc_channel(1);
delay_us(10);
val2=read_adc(); //read the adc value from Y-axis
delay_us(10);
if(fabs(val1)>fabs(val2)) //if value x > value y, run motor 1
slave_1();
else //if value x < value y, run motor 2
slave_2();
}
} |
Attached is my slave_1 code:
Code: | //slave1 program in spi to get two value of ccp
#include <16f877a.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOPROTECT, NOLVP
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9
#byte sspcon1=14
#byte sspstat=94
int8 speed[2]; //array of 2 for variable speed
int i;
void main()
{
set_tris_A(0xFF); //input for ss' pin
set_tris_D(0x00);
set_tris_C(0xD9); //config spi (D9 for slave) 1101 1001
setup_timer_2(t2_div_by_4,100,1);//Define use of Timer2 and postscalar value
setup_spi(SPI_SLAVE|SPI_L_TO_H|SPI_CLK_DIV_16/*|SPI_SS_DISABLED*/);
setup_ccp1(ccp_pwm); //use ccp1
setup_ccp2(ccp_pwm); //use ccp2
sspcon1=0b01100100; //config reg of spi
sspstat=0b00000001;
port_b_pullups(TRUE); //internal pull ups active on port b
while(TRUE)
{
portd=0x00; //initialize motors to zero
if((input(pin_A5)) && spi_data_is_in()) //high active motor
{
for(i=0;i<2;i++) //loop 2times for two ccp values
{
portd=0x09; //0b 0000 0101
speed[i]=spi_read();
spi_write(speed[i]);
}//end of for
set_pwm1_duty(speed[0]);
set_pwm2_duty(speed[1]);
}//end of if
}//end of while
}//end of main |
Attached is my slave_2 code:
Code: | //slave2 program in spi to get two value of ccp
#include <16f877a.h>
#use delay(clock=20000000)
#fuses HS, NOWDT, NOPROTECT, NOLVP
#byte porta=5
#byte portb=6
#byte portc=7
#byte portd=8
#byte porte=9
#byte sspcon1=14
#byte sspstat=94
int8 speed[2]; //array of 2 for variable speed
int i;
void main()
{
set_tris_A(0xFF); //input for ss' pin
set_tris_D(0x00);
set_tris_C(0xD9); //config spi (D9 for slave) 1101 1001
setup_timer_2(t2_div_by_4,100,1);//Define use of Timer2 and postscalar value
setup_spi(SPI_SLAVE|SPI_L_TO_H|SPI_CLK_DIV_16/*|SPI_SS_DISABLED*/);
setup_ccp1(ccp_pwm); //use ccp1
setup_ccp2(ccp_pwm); //use ccp2
sspcon1=0b01100100; //config reg of spi
sspstat=0b00000001;
port_b_pullups(TRUE); //internal pull ups active on port b
while(TRUE)
{
portd=0x00; //initialize motors to zero
if(!(input(pin_A5)) && spi_data_is_in()) //low active motor
{
for(i=0;i<2;i++) //loop 2times for two ccp values
{
portd=0x09; //0b 0000 0101
speed[i]=spi_read();
spi_write(speed[i]);
}//end of for
set_pwm1_duty(speed[0]);
set_pwm2_duty(speed[1]);
}//end of if
}//end of while
}//end of main |
Are the codes wrong? Hope for your help asap, thank you very much. |
|