#include "spi.h"
/*
** CPOL 0
** CPHA 0
*/
void SPI_Init(void)
{
// 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 |= (1 << 6); // CPHA
U1GCR |= (1 << 7); // CPOL
U1GCR &= ~0xC0; U1GCR |= 0x20; // MSB
}
unsigned char Spi0WrByte(unsigned char indata)
{
unsigned char out;
U1CSR &= 0xF9;
U1DBUF = indata;
while (!(U1CSR&0x02));
//while (!(U1CSR&0x04));
out = U1DBUF;
return out;
}
void DisableSpi0(void)
{
}
//头文件=========================================
#ifndef __SPI_H_
#define __SPI_H_
#include "all.h"
#define ACC_CS_LOW_N() P1_4=0
#define ACC_CS_HIGH_N() P1_4=1
void SPI_Init(void);
unsigned char Spi0WrByte(unsigned char indata);
void DisableSpi0(void);
#endif
//例子================================================
#include "all.h"
#include "uart.h"
#include "delay.h"
#include "lis3dh_driver.h"
typedef unsigned char uint8;
typedef unsigned short int uint16;
char buf[40];
AxesRaw_t axes[32];
GRaw_t gdata[32];
short xt,yt,zt;
char p_buf[]="hello world\r\n\r\n";
unsigned char get_data_item;
unsigned char i;
void main(void)
{
CLKCONCMD &= ~0x40; //设置系统时钟源为32MHZ晶振
while(CLKCONSTA & 0x40); //等待晶振稳定
CLKCONCMD &= ~0x47; //设置系统主时钟频率为32MHZ
Uart_Init(UART_BAUDRATE_115200);
Uart_String(p_buf);
AccSensorConfig();
SetAccPowerOff(false);
while(1)
{
get_data_item=GetData(axes,gdata);
for(i = 0; i < get_data_item; i++)
{
xt=((short)axes[i].AXIS_X)>>4;
yt=((short)axes[i].AXIS_Y)>>4;
zt=((short)axes[i].AXIS_Z)>>4;
sprintf(buf,"%d-%d-%d\r\n",xt,yt,zt);
Uart_String(buf);
}
delay_ms(400);
}
}
我需要硬件来验证我的SPI驱动是否写对了,我选择Li3dh模块,在转动LI3DH时,三个轴的数据不同。

网友评论