我正在mikroC中的两个pic18F452微控制器之间进行spi接口,但我的程序没有给出任何输出。我从主站发送数据,从站必须在LCD上显示。
这是我的主从代码:
主代码:
unsigned char key= a ;
void main(void)
{
TRISC.F2=0; //output for Slave select
TRISC.F3=0; //SCK output
TRISC.F4=1; //SDI input
TRISC.F5=0; //SDO output
Spi_Init_Advanced(Master_OSC_div64, Data_SAMPLE_MIDDLE, CLK_Idle_HIGH, HIGH_2_LOW);
SSPCON1.SSPEN=1; //Synchronus serial port enable
SSPSTAT.SMP=0; //sampled at middle of data output time
while(1)
{
PORTC.F2=0;
SSPBUF=key;
while(!SSPSTAT.bf);
spi_write(key);
}
}
终止
从属代码:
#define LCD PORTB
unsigned char key;
void main(void)
{
TRISB=0; //LCD output
TRISC.F4=1; //SDI input
TRISC.F3=1; //SCK input from master
TRISC.F5=0; //SDO output
TRISA.F5=1; //Slave select input from master
SSPSTAT.SMP=0; //input data sampled at middle
SSPSTAT.CKE=0; // transition from idle to active
SSPCON1.=0x22;
Spi_Init_Advanced(SLAVE_SS_ENABLE, DATA_SAMPLE_MIDDLE, CLK_IDLE_LOW, LOW_2_HIGH);
Lcd_init(&PORTB);
Lcd_cmd(Lcd_clear);
Lcd_cmd(Lcd_cursor_off);
while(1)
{
if(SSPSTAT.BF)
{
key=spi_read(SSPBUF);
}
}
}
/终止/