我正在 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);
}
}
}
////////结尾///////