1

我正在 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);

            }

   }
   }

////////结尾///////

4

1 回答 1

0

我不确定这部分

lcd_init(&PORTB);

lcd_cmd(lcd_clear);

Lcd_cmd(Lcd_cursor_off);

尝试像这样声明您的 lcd 端口。我使用端口 d

 //lcd
sbit LCD_RS at RD0_bit;
sbit LCD_RW at RD1_bit;
sbit LCD_EN at RD2_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D4 at RD4_bit;
// Pin direction
sbit LCD_RS_Direction at TRISD0_bit;
sbit LCD_RW_Direction at TRISD1_bit;
sbit LCD_EN_Direction at TRISD2_bit;
sbit LCD_D7_Direction at TRISD7_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D4_Direction at TRISD4_bit;

并用于初始化和清除

LCD_Init(); // Initialize LCD
DELAY_MS(100);
Lcd_Cmd(_LCD_CLEAR);                      // Clear display

延迟部分有时可能会派上用场。

于 2013-07-01T05:29:53.247 回答