AVR spi通讯问题(atmega64/16之间)
主机用 Mega16 (晶振:8MHZ), 向从机 Mega64 (晶振:8MHZ) 每秒发送数据0X60,
现在遇到的问题是:
我是用AVR仿真器设定断点跟踪,从机接收的数据是 F9 (有时是E9),并是主机发送的值0X60,好象是从机本身SPDR中的固定值, 而主机的SPI数据寄存器SPDR中却收到0X60,
这样以来,主从机的SPDR中都接收的是自己本身的值.不知道 是什麽原因.?请老师指点迷津.
主从机的连接如下:
/SS --- /SS ; MOSI – MOSI ; MISO – MISO ; SCK – SCK;
以下是主机(Mega16)程序:
#include <mega16.h>
#include <delay.h>
#include <stdio.h>
#include <math.h>
unsigned char rx_wr_index,rx_counter;
// This flag is set on USART Receiver buffer overflow
bit rx_buffer_overflow;
// SPI interrupt service routine
interrupt [SPI_STC] void spi_isr(void)
{
unsigned char data;
data=SPDR;
if (1)
{
rx_buffer[rx_wr_index]=data;
if (++rx_wr_index == RX_BUFFER_SIZE) rx_wr_index=0;
if (++rx_counter == RX_BUFFER_SIZE)
{
rx_counter=0;
rx_buffer_overflow=1;
};
};
}
//transmit SPI data
void SPITransmion(unsigned char data)
{
/* Put data into buffer, sends the data */
SPDR = data;
/* Wait for empty transmit buffer */
while ( !( SPSR & (1 < <SPIF)) )
;
}
//main program begain
void main(void)
PORTB=0xe0;
DDRB=0xB0;//PB7 SCK – out, PB6 MISO – in, PB5 MOSI ---out, PB4 /SS – out
SPCR = 0xD2; //允许SPI中断,SPI允许,MSB,主机模式,极性方式00,1/64系统时钟
#asm("sei")
while (1)
{
delay_ms(1000);
SPITransmion(0x60); //Transmion 0x60 to slaver per 1 second
}
}
从机(Mega64)程序:
;******************************
; SPI Transfer Complete Handler
;******************************
SPI_STC:
…..
lds r16, SPDR
call UART0Transmion ;从机接收到主机的命令之后从串口发送到上位机
……
RETI
……
RESET:
cli
init_port:
ldi r16, 0x08
out DDRB, r16 ;PB3 MISO--- out, PB2 MOSI-- in, PB1 SCK – in, PB0 /SS -- in
ldi r16, 0x0f;
out PORTB, r16 ;
init_spi:
ldi r16,0xc2;
out SPCR,r16 ;// 允许SPI中断,SPI允许,MSB,从机模式,极性方式00,1/64系统时钟
sei
main_loop:
nop;
nop;
rjmp main_loop
发表时间:2007年6月6日22:03:24