您尚未登录。

楼主 # 2022-01-13 01:08:30

缥缈九哥
会员
注册时间: 2020-05-09
已发帖子: 73
积分: 104

基于华芯微特SWM181CB的ADXL357加速度计的SPI驱动程序

直接上代码:

/********************************************************************************/
/* @file	adxl357.c															*/
/* @version	V1.00																*/
/* $author:	yuanxihua@21cn.com													*/
/* $Date: 	2022/01/13 02:54 $ 													*/
/* @brief 	SWM181CBT6 Series ADXL357 Driver Sample Code						*/
/*																				*/
/* @note																		*/
/* Copyright (C) 2010-2022 PM9GZY Technology Corp. All rights reserved.			*/
/*																				*/
/********************************************************************************/ 
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "adxl537.h"
/*
________________________________________________
|	SWM181CBT6						ADXL357		|
|												|
|	PA12	NSS0		---->		CS/SCL		|
|	PA13	MISO0		<----		MISO		|
|	PA14	MOSI0		---->		MOSI		|
|	PA15	SCLK0		---->		SCLK		|
|_______________________________________________|
*/
/* Private typedef -----------------------------------------------------------*/
/* Private define ------------------------------------------------------------*/
#define ADXL357_CS_L()		GPIO_ClrBit(GPIOA, PIN12)
#define ADXL357_CS_H()		GPIO_SetBit(GPIOA, PIN12)
/* Private macro -------------------------------------------------------------*/
/* Private variables ---------------------------------------------------------*/
/* Private function prototypes -----------------------------------------------*/
void ADXL357_Initial(void);
void ADXL357_Reset(void);
/* Private functions ---------------------------------------------------------*/
void SPI0Init(void)
{
	SPI_InitStructure SPI_initStruct;

	GPIO_Init(GPIOA, PIN12, 1, 0, 0, 0);ADXL357_CS_H();					//GPIOC.4 -> IOSPI.SSEL
//	PORT_Init(PORTA, PIN12, PORTA_PIN12_SPI0_SSEL, 0);
	PORT_Init(PORTA, PIN13, PORTA_PIN13_SPI0_MISO, 1);
	PORT_Init(PORTA, PIN14, PORTA_PIN14_SPI0_MOSI, 0);
	PORT_Init(PORTA, PIN15, PORTA_PIN15_SPI0_SCLK, 0);
	
	SPI_initStruct.clkDiv = SPI_CLKDIV_4;	//80NS , 12500KHZ 
	SPI_initStruct.FrameFormat = SPI_FORMAT_SPI;
	SPI_initStruct.SampleEdge = SPI_SECOND_EDGE;
	SPI_initStruct.IdleLevel = SPI_HIGH_LEVEL;
	SPI_initStruct.WordSize = 8;
	SPI_initStruct.Master = 1;
	SPI_initStruct.RXHFullIEn = 0;
	SPI_initStruct.TXEmptyIEn = 0;
	SPI_initStruct.TXCompleteIEn = 0;
	SPI_Init(SPI0, &SPI_initStruct);
	SPI_Open(SPI0);
	
	ADXL357_Reset();
	ADXL357_Initial();
}
uint8_t ADXL357_Read(uint8_t addr,uint8_t *data,uint32_t len)
{
	uint8_t cmd=(addr<<1)|0x01;		// D0=R/W=1,读取模式
	ADXL357_CS_L();
	SPI_ReadWrite(SPI0,cmd);while(len--){*data++ =SPI_ReadWrite(SPI0,0x55)&0xff;}
	ADXL357_CS_H();
}
uint8_t ADXL357_Write(uint8_t addr,uint8_t *data,uint32_t len)
{
	uint8_t cmd=(addr<<1)&0xFE;		// D0=R/W=0,写入模式
	ADXL357_CS_L();
	SPI_ReadWrite(SPI0,cmd);while(len--){SPI_ReadWrite(SPI0,*data++);}
	ADXL357_CS_H();
}
uint8_t ADXL357_Read_Register(uint8_t addr){uint8_t val;ADXL357_Read(addr,&val,1);return val;}
void ADXL357_Write_Register(uint8_t addr,uint8_t data){ADXL357_Write(addr,&data,1);}
void ADXL357_Reset(void){ADXL357_Write_Register(ADXL357_Reset_REG,0x52);delay_ms(10);}
void ADXL357_Initial(void){}
float ADXL357_Read_TemperatureRead(void)
{
    uint8_t tmp[2];ADXL357_Read(ADXL357_TEMP2,tmp,2);uint16_t tempH=tmp[0]&0x0F,tempL=tmp[1];
	float Temperature = ((float)(tempH<<8)) + ((float)tempL);
	return (25-((Temperature-1885)/9.05));
}
	
void ADXL357_Test(void)
{
	uint8_t data[47];ADXL357_Read(ADXL357_DEVID_AD,data,sizeof(data));printf("\n\r");for(uint8_t i=0;i<sizeof(data);i++){printf("0x%02x ",data[i]);}printf("\n\r");
	printf("ADXL357_DEVID_AD  	= 0x%02x \n\r",ADXL357_Read_Register(ADXL357_DEVID_AD));
	printf("ADXL357_DEVID_MST 	= 0x%02x \n\r",ADXL357_Read_Register(ADXL357_DEVID_MST));
	printf("ADXL357_PARTID    	= 0x%02x \n\r",ADXL357_Read_Register(ADXL357_PARTID));
	printf("ADXL357_REVID     	= 0x%02x \n\r",ADXL357_Read_Register(ADXL357_REVID));	
	printf("ADXL357_Temperature	= %f \n\r",ADXL357_Read_TemperatureRead());
}

//代码完毕

离线

页脚

工信部备案:粤ICP备20025096号 Powered by FluxBB

感谢为中文互联网持续输出优质内容的各位老铁们。 QQ: 516333132, 微信(wechat): whycan_cn (哇酷网/挖坑网/填坑网) service@whycan.cn