|
刚学430,用的是MSP430F6638,想用6638的硬件SPI控制MCP2515实现CAN通信,现在连读写MCP2515都做不到啊,下面是程序,不长,还希望大家帮忙指导一下,读出来的结果一直是0xFF,谢谢大家啊
#include
#include
#include "spitocan.h"
unsigned char test;
//延时
void delay(unsigned char num)
{
unsigned char i;
for (i = 0; i < num; i++);
}
void UCA1_SPI_Init(void)
{
WDTCTL = WDTPW+WDTHOLD; // Stop watchdog timer
P8SEL|=BIT1+BIT2+BIT3+BIT4;//选择8.1:UCA1CLK,8.2UCA1SIMO,8.3:UCA1SOMI,8.4:CS
P8DIR|=BIT1+BIT2+BIT4;//设定8.1,8.2,8.4端口输出,8.3输入
P2SEL|=BIT0;//2.0:INT
P2DIR&=~BIT0;//设定2.0端口输入
UCA1CTL1|=UCSWRST;
UCA1CTL0|=UCMST+UCSYNC+UCCKPL+UCMSB;//3线,8位SPI主机,同步
UCA1CTL1|=UCSSEL_2;//SMCLK
UCA1BR0=0x02;//波特率
UCA1BR1=0;//波特率
UCA1CTL1&=~UCSWRST;
UCA1IE=UCRXIE;//使能中断
//UCA1IE=UCTXIE;
__delay_cycles(100);
}
unsigned char UCA1_SPI_WriteByte(unsigned char data)
{
while(!(UCA1IFG&UCTXIFG));//等待发送缓冲器空
UCA1TXBUF = data;
while(!(UCA1IFG&UCRXIFG));//等待移位寄存器中断标志
UCA1RXBUF;
return UCA1RXBUF;
}
unsigned char UCA1_SPI_ReadByte(void)
{
unsigned char r_data;
r_data = UCA1_SPI_WriteByte(0X00);
return r_data;
}
void mcp2515_write_register(unsigned char addr, unsigned char data)
{
MCP2515_CS_LOW;
UCA1_SPI_WriteByte(MCP_WRITE); // 字节修改命令
UCA1_SPI_WriteByte(addr); // 被修改的地址
UCA1_SPI_WriteByte(data); // 数据
MCP2515_CS_HIGH;
}
unsigned char mcp2515_read_register(unsigned char addr)
{
unsigned char data;
MCP2515_CS_LOW;
UCA1_SPI_WriteByte(MCP_READ); // 读
UCA1_SPI_WriteByte(addr); // 被读寄存器的地址
data = UCA1_SPI_ReadByte();
MCP2515_CS_HIGH;
return data;
}
void MCP2515_RESET(void)
{
MCP2515_CS_LOW;
UCA1_SPI_WriteByte(MCP_RESET);//复位指令
//delay(1000);
MCP2515_CS_HIGH;
//delay(1000);
}
void main(void)
{
while(1)
{
UCA1_SPI_Init();
MCP2515_RESET();//复位
test=mcp2515_read_register(0x0E);//读CANSTAT状态
}
}
|
|