最近用AT91SAM7X256的SPI总线,想自己管理器件的CS线,只用芯片提供的MOSI、MISO和SCK进行数据收发,硬件安排为器件片选PA14(SPI0_NPCS2),使用SPI0。程序如下,但是读出的结果总是不对的。
初始化如下: void SPI_init(void) { AT91C_BASE_PMC->PMC_PCER = (1 << AT91C_ID_SPI0); AT91C_BASE_PIOA->PIO_PDR = BSP_GPIOA_SPI0; AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SWRST; AT91C_BASE_SPI0->SPI_MR = AT91C_SPI_MSTR | AT91C_SPI_FDIV | AT91C_SPI_PS_VARIABLE | AT91C_SPI_MODFDIS | AT91C_SPI_DLYBCS; AT91C_BASE_SPI0->SPI_CSR[2] = AT91C_SPI_CPOL | AT91C_SPI_DLYBS | AT91C_SPI_DLYBCT | AT91C_SPI_SCBR | AT91C_SPI_BITS_8; AT91C_BASE_SPI0->SPI_CR = AT91C_SPI_SPIEN;
//片选 AT91C_BASE_PMC->PMC_PCER = 1 << AT91C_ID_PIOA; AT91C_BASE_PIOA->PIO_PER = BSP_GPIOA_TEMP; AT91C_BASE_PIOA->PIO_OER = BSP_GPIOA_TEMP; AT91C_BASE_PIOA->PIO_IDR = BSP_GPIOA_TEMP; AT91C_BASE_PIOA->PIO_SODR = BSP_GPIOA_TEMP; }
//发送字节 void SPI_SendByte(CPU_INT08U data) { CPU_INT32U value_for_cs; value_for_cs = (~(1 << 2)) & 0xF; //Place a zero among a 4 ONEs number AT91C_BASE_SPI0->SPI_TDR = ((CPU_INT32U)data & 0x000000FF) | (value_for_cs << 16); while((AT91C_BASE_SPI0->SPI_SR & AT91C_SPI_TXEMPTY == 0)) { ; } }
//读取字节 CPU_INT32U SPI_RecByte(void) { return((CPU_INT08U)(AT91C_BASE_SPI0->SPI_RDR & 0xFFFF)); }
进行读写操作时,片选我保证了有效。 因为第一次用,所以都摸不着头脑了。恳请有经验的朋友帮忙看看哪里出问题了,谢谢。 |