hljrzy 发表于 2007-8-9 17:47:43

spi中断服务程序没有执行,请教能是哪里得原因?

我用得是atmega64.想用他得spi取出传感器得数据,按照马老师得程序老是执行不到中断服务程序.那是什么原因呢?能是我硬件方面得原因吗?如果是硬件得原因.硬件要怎么样接呢.请指教

程序也没有动.如下

#define SIZE 100   

unsigned char SPI_rx_buff;   

unsigned char SPI_tx_buff;   

unsigned char rx_wr_index,rx_rd_index,rx_counter,rx_buffer_overflow;   

unsigned char tx_wr_index,tx_rd_index,tx_counter,SPI_ok;   



#pragma interrupt_handler spi_stc_isr:18   

void spi_stc_isr(void)   

{   

SPI_rx_buff = SPDR;    //从ISP口读出收到的字节

SPI_ok = 1;                         // SPI 空闲

if (++rx_wr_index == SIZE) rx_wr_index = 0;    //放入接收缓冲区,并调整队列指针   

if (++rx_counter == SIZE)   

{   

    rx_counter = 0;   

    rx_buffer_overflow = 1;   

}   

if (tx_counter)      //如果发送缓冲区中有待发的数据   

{   

   --tx_counter;   

   SPDR = SPI_tx_buff; //发送一个字节数据,并调整指针   

   if (++tx_rd_index == SIZE) tx_rd_index = 0;   

   SPI_ok = 0;                      // SPI 发送工作

}   

}   



unsigned char getSPIchar(void)   

{   

unsigned char data;   

while (rx_counter == 0);   //无接收数据,等待   

data = SPI_rx_buff;    //从接收缓冲区取出一个SPI收到的数据   

if (++rx_rd_index == SIZE) rx_rd_index = 0;    //调整指针   

CLI();   

--rx_counter;   

SEI();   

return data;   

}   



void putSPIchar(char c)   

{   

while (tx_counter == SIZE);//发送缓冲区满,等待   

CLI();   

if (tx_counter || SPI_ok==0 )      //发送缓冲区已中有待发数据   

{                //或SPI正在发送数据时   

    SPI_tx_buffer = c;    //将数据放入发送缓冲区排队   

    if (++tx_wr_index == SIZE) tx_wr_index = 0;    //调整指针   

    ++tx_counter;   

}   

else

{   

    SPDR = c;      // 发送缓冲区中空且SPI口空闲,直接放入SPDR由SIP口发送   

    SPI_ok = 0;      // SPI 发送工作

}

SEI();   

}   



void spi_init(void)   

{   

unsigned chat temp;

DDRB = 0x07;      //MISO=input and MOSI,SCK,SS = output      

PORTB = 0x08;   //MISO上拉电阻有效   

SPCR = 0xD5;      //SPI允许,主机模式,MSB,允许SPI中断,极性方式01,1/16系统时钟速率   

SPSR = 0x00;   

temp = SPSR;   

temp = SPDR;    //清空SPI,和中断标志,使SPI空闲   

SPI_ok = 1;   // SPI 空闲

}   



void main(void)   

{   

unsigned char I;   

CLI();      //关中断   

spi_init();    //初始化SPI接口   

SEI();      //开中断   

while(1)   

{   

    char temp=0x15;

   putSPIchar(temp);      //

   SPI_Data=getSPIchar();      //

   putSPIchar(0x80);      //

   SPI_Data=getSPIchar();      

}   

}   

中断总是触发不了.郁闷了好几天.请指教谢谢

machao 发表于 2007-8-10 15:37:51

1。传感器是标准的SPI接口吗?它是采用什么工作模式的?



2。你怎么知道没有中断?
页: [1]
查看完整版本: spi中断服务程序没有执行,请教能是哪里得原因?