unsigned char theta1,theta2,theta3,Count2,Count3,datain,intiCom,ready,Thetax=0,Thetay,Thetaz,x,y,z1,reset;
unsigned char Write(char dataout);
void mapInput();
void SysInit();
void sent_data(unsigned char Theta1 ,unsigned char Theta2 , unsigned char Theta3 );
void main(){
SysInit();
PORTB=0xF0;
while(1)
{
sent_data(95,100,70);
delay_ms(150);
sent_data(85,80, 50);
} }
void Write(unsigned char dataout)
{
PORTC.B2=0; // SS select low slave is selected port C pin 2
delay_ms(50); // wait.
SPI1_Write(dataout); // transmit the dataout
while(SSPSTAT.BF); // wait till end of data transmission
datain=SSPBUF;
PORTC.B2=1;
delay_ms(50); // wait.
}
void SysInit()
{
TRISA=0xFF; // configure Port A as input
ADCON1=0x06;// disable ADC and make all the pins of Port A as digital input.
TRISD=0x00; // configure Port D as output
PORTD=0x00; // Clear all pin of Port D
TRISB=0x00; // configure Port B as output
PORTB=0x00; // Clear all pin of Port B
TRISE=0x00; // configure Port B as output
PORTE=0x00; // Clear all pin of Port B
TRISC.B2=0; // Output for Slave select
TRISC.B3=0; // SCK output
TRISC.B4=1; // SDI input
TRISC.B5=0; // SDO output
PORTC.B2=1; // SS select high no slave is select
//Set SPI1 module to master mode, clock = Fosc/4, data sampled at the middle of interval,
//clock idle state low and data transmitted at low to high edge:
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV4, _SPI_DATA_SAMPLE_MIDDLE, _SPI_CLK_IDLE_LOW, _SPI_LOW_2_HIGH); // Setup the spi
}
void sent_data(unsigned char Theta1 ,unsigned char Theta2 , unsigned char Theta3 )
{
PORTC.B2=0; // SS select low slave is selected port C pin 2 f
delay_ms(50);
Write(Theta1);
delay_ms(20);
Write(Theta2);
delay_ms(20);
Write(Theta3);
delay_ms(20);
Write(0x0A);
delay_ms(20);
PORTC.B2=1;
}