使用CC2543的SPI口读HMC5983的数据,通过示波器可以观察到时钟信号正确,输出正确,但是无法得不到接收的数据。
使用硬件调试时,U0DBUF寄存器的数据显示一直为0,如果是发送的话,可以是认为发送太快,所以在使用硬件调试时,看不到U0DBUF赋值过程吗?
但是接收的时候,也没有数据。求帮助。
void main(void)
{
int8 data;
CLKCONCMD = (CLKCONCMD & ~(CLKCON_OSC | CLKCON_CLKSPD)) | CLKCON_CLKSPD_32M;
while (CLKCONSTA & CLKCON_OSC); // Wait until clock source has changed
/***************************************************************************
* Setup I/O ports
* Port and pins used by USART0 operating in SPI-mode are
* MISO (MI): P1_3
* MOSI (MO): P1_4
* SSN (SS) : P1_1
* SCK (C) : P1_2
*/
// Configure USART0 for Alternative 1 => Port P0 (PERCFG.U0CFG = 0).
// Give priority to USART 0 over Timer 1 for port 0 pins.
PERCFG = (PERCFG & ~PERCFG_U0CFG) | PERCFG_U0CFG_ALT2; PPRI &= ~PPRI_PRI0P1;
P1SEL = (P1SEL & ~BIT1) | BIT2 | BIT3 | BIT4;
P1DIR |= BIT1;
U0CSR &= ~(U0CSR_MODE | U0CSR_SLAVE);
U0GCR |= U0GCR_ORDER; //MSB先发
U0GCR |= 0xC0; //CPOL CPHA
U0BAUD |= 216; //baudrate 57600
U0GCR |=10;
P1_1 = 0;
U0DBUF = 0x82;
while(!(U0CSR & U0CSR_TX_BYTE));
U0CSR &= ~U0CSR_TX_BYTE;
//有必要发送一个无效命令,将从器件的值取回来吗?
U0DBUF = 0xFF;
U0CSR &= ~U0CSR_TX_BYTE;
while(!(U0CSR & U0CSR_TX_BYTE));
data = U0DBUF;
P1_1 = 1;
}
HG:
你可以打断点多发几个,但是我觉得U0DBUF里面应该是能看到东西的。
试试附件TI的参考代码,里面有个spi的,确保配置没有问题哈~
gaoyang9992006:
// master #includevoid main() { CLKCONCMD = 0x80; while (CLKCONSTA != 0x80); // 32MHz // SPI Master Mode PERCFG |= 0x02; // map USART1 to its alternative 2 location. P1_4: SSN, P1_5: SCK, P1_6: MOSI, P1_7: MISO P1SEL |= 0xE0; // P1_5, P1_6, and P1_7 are peripherals P1SEL &= ~0x10; // P1_4 is GPIO (SSN) P1DIR |= 0x10; // SSN is set as output U1BAUD = 0x00; U1GCR |= 0x11; // Set baud rate to max (system clock frequency / 8) U1CSR &= ~0xA0; // SPI Master Mode // U1CSR &= ~0x80; U1CSR |= 0x20; // SPI Slave Mode U1GCR &= ~0xC0; U1GCR |= 0x20; // MSB for(;;) { P1_4 = 0; // SSN LOW U1DBUF = 0x55; while (!(U1CSR&0x02)); U1CSR &= 0xFD; U1DBUF = 0x00; while (!(U1CSR&0x02)); U1CSR &= 0xFD; unsigned char temp = U1DBUF; P1_4 = 1; // SSN high } } // slave #includevoid main() { CLKCONCMD = 0x80; while (CLKCONSTA != 0x80); // 32MHz PERCFG |= 0x02; // map USART1 to its alternative 2 location. P1_4: SSN, P1_5: SCK, P1_6: MOSI, P1_7: MISO P1SEL |= 0xF0; // P1_4, P1_5, P1_6, and P1_7 are peripherals U1BAUD = 0x00; U1GCR |= 0x11; // Set baud rate to max (system clock frequency / 8) U1CSR &= ~0x80; U1CSR |= 0x20; // SPI Slave Mode U1GCR &= ~0xC0; U1GCR |= 0x20; // MSB for(;;) { U1DBUF = 0x88; while (!(U1CSR&0x04)); U1CSR &= 0xFB; unsigned char temp = U1DBUF; } } ——————————– 这几个2540,2530,内核都是51,应该配置是一样的。
Kaifei Pu:
回复 HG:
主:CC2543,从:HMC5983磁传感器,CC2543是8051核的,和CC2530、CC2430等51核的器件的SPI口使用方式是相同的。发数据,CLK和MOSI的波形是正确的,SSN使能高低电平也是正确的,但是读数据,一个板子读到的是0xFF,其他板子读到的都是0x00。根据网上的一些信息,说要先写一个无效命令,将从器件寄存器的值取回来,也试过,但是不可以。时钟的相位和极性也改过,但是没有返回的数据。
反复检查了配置,觉得没有问题。程序:void spiInit(void){/*************************************************************************** * Setup I/O ports * Port and pins used by USART0 operating in SPI-mode are * MISO (MI): P1_3 * MOSI (MO): P1_4 * SSN (SS) : P1_1 * SCK (C) : P1_2 */ // Configure USART0 for Alternative 1 => Port P0 (PERCFG.U0CFG = 0). // Give priority to USART 0 over Timer 1 for port 0 pins. PERCFG = (PERCFG & ~PERCFG_U0CFG) | PERCFG_U0CFG_ALT2; PPRI &= ~PPRI_PRI0P1; P1SEL = (P1SEL & ~BIT1) | BIT2 | BIT3 | BIT4; P1DIR |= BIT1; U0BAUD = 216; U0GCR |= 10; U0CSR &= ~(U0CSR_MODE | U0CSR_SLAVE); U0GCR |= 0x80; //CPOL=1; //U0GCR &= ~ 0x80; //CPOL=0; U0GCR |= 0x40; //CPHA=1; //U0GCR &= ~ 0x40; //CPHA=0; U0GCR |= 0x20; //MSB //U0GCR &= ~ 0x20; //LSB}void sendByte(int8 data){ U0DBUF = data; while(!(U0CSR & U0CSR_TX_BYTE)); U0CSR &= ~U0CSR_TX_BYTE;// data = U0DBUF;// data = U0DBUF;}int8 receiveByte(int8 dummyData){ int8 data; U0DBUF = dummyData; while(!(U0CSR & U0CSR_TX_BYTE)); U0CSR &= ~U0CSR_TX_BYTE; data = U0DBUF; return data;}void main(void){ int8 data; CLKCONCMD = (CLKCONCMD & ~(CLKCON_OSC | CLKCON_CLKSPD)) | CLKCON_CLKSPD_32M; while (CLKCONSTA & CLKCON_OSC); // Wait until clock source has changed spiInit(); while(1) { P1_1 = 0; sendByte(0x81); data = receiveByte(0xFF); P1_1 =1; halMcuWaitMs(1); }}
HMC5983关于SPI的时序:
将0xAA数据,写到U0DBUF,并延时的波形:上面的是时钟,下面的是MOSI