1ADXL345硬件接口使用的是SPI端口进行通信,这样读取数据比较快且后续也可以转化为IIC通信接口。
在网上找一些发现IIC接口的比较多,所以本人就DIY做SPI的通信。
2STM32F103T系列单片机作为MCU 资源比较丰富、本人比较熟悉开发速度较快
硬件电路首先是为了实现功能,所以设计比较简单。后续小编想做无线蓝牙的数据传输,所以硬件上也留了蓝牙串口通信的硬件接口和33V电源管理。
暂且将硬件这样设计,设计为双层PCB 这样减少了空间。电容、电阻为0805的易焊接。器件都选为贴片。
二、单片机驱动代码
1ADXL345的端口配置函数
#define ADXL345_FLAG_TIMEOUT ((uint32_t)0x1000)
#define ADXL345_SPI SPI1
#define ADXL345_SPI_CLK RCC_APB2Periph_SPI1
#define ADXL345_SPI_SCK_PIN GPIO_Pin_5
#define ADXL345_SPI_SCK_GPIO_PORT GPIOA
#define ADXL345_SPI_SCK_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADXL345_SPI_SCK_SOURCE GPIO_PinSource5
#define ADXL345_SPI_SCK_AF GPIO_AF_5
#define ADXL345_SPI_MISO_PIN GPIO_Pin_6
#define ADXL345_SPI_MISO_GPIO_PORT GPIOA
#define ADXL345_SPI_MISO_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADXL345_SPI_MISO_SOURCE GPIO_PinSource6
#define ADXL345_SPI_MISO_AF GPIO_AF_5
#define ADXL345_SPI_MOSI_PIN GPIO_Pin_7
#define ADXL345_SPI_MOSI_GPIO_PORT GPIOA
#define ADXL345_SPI_MOSI_GPIO_CLK RCC_APB2Periph_GPIOA
#define ADXL345_SPI_MOSI_SOURCE GPIO_PinSource7
#define ADXL345_SPI_MOSI_AF GPIO_AF_5
#define ADXL345_SPI_CS_PIN GPIO_Pin_2
#define ADXL345_SPI_CS_GPIO_PORT GPIOB
#define ADXL345_SPI_CS_GPIO_CLK RCC_APB2Periph_GPIOB
#define ADXL345_SPI_INT1_PIN GPIO_Pin_0
#define ADXL345_SPI_INT1_GPIO_PORT GPIOB
#define ADXL345_SPI_INT1_GPIO_CLK RCC_APB2Periph_GPIOB
#define ADXL345_SPI_INT1_EXTI_LINE EXTI_Line0
#define ADXL345_SPI_INT1_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
#define ADXL345_SPI_INT1_EXTI_PIN_SOURCE EXTI_PinSource0
#define ADXL345_SPI_INT1_EXTI_IRQn EXTI0_IRQn
#define ADXL345_SPI_INT2_PIN GPIO_Pin_1
#define ADXL345_SPI_INT2_GPIO_PORT GPIOB
#define ADXL345_SPI_INT2_GPIO_CLK RCC_APB2Periph_GPIOB
#define ADXL345_SPI_INT2_EXTI_LINE EXTI_Line1
#define ADXL345_SPI_INT2_EXTI_PORT_SOURCE EXTI_PortSourceGPIOB
#define ADXL345_SPI_INT2_EXTI_PIN_SOURCE EXTI_PinSource1
#define ADXL345_SPI_INT2_EXTI_IRQn EXTI1_IRQn
#define ADXL345_WHO_AM_I_ADDR 0x0F
#define ADXL345_CTRL_REG1_ADDR 0x20
#define ADXL345_CTRL_REG2_ADDR 0x21
#define ADXL345_CTRL_REG3_ADDR 0x22
#define ADXL345_CTRL_REG4_ADDR 0x23
#define ADXL345_CTRL_REG5_ADDR 0x24
#define ADXL345_REFERENCE_REG_ADDR 0x25
#define ADXL345_OUT_TEMP_ADDR 0x26
#define ADXL345_STATUS_REG_ADDR 0x27
#define ADXL345_OUT_X_L_ADDR 0x28
#define ADXL345_OUT_X_H_ADDR 0x29
#define ADXL345_OUT_Y_L_ADDR 0x2A
#define ADXL345_OUT_Y_H_ADDR 0x2B
#define ADXL345_OUT_Z_L_ADDR 0x2C
#define ADXL345_OUT_Z_H_ADDR 0x2D
#define ADXL345_FIFO_CTRL_REG_ADDR 0x2E
#define ADXL345_FIFO_SRC_REG_ADDR 0x2F
#define ADXL345_INT1_CFG_ADDR 0x30
#define ADXL345_INT1_SRC_ADDR 0x31
#define ADXL345_INT1_TSH_XH_ADDR 0x32
#define ADXL345_INT1_TSH_XL_ADDR 0x33
#define ADXL345_INT1_TSH_YH_ADDR 0x34
#define ADXL345_INT1_TSH_YL_ADDR 0x35
#define ADXL345_INT1_TSH_ZH_ADDR 0x36
#define ADXL345_INT1_TSH_ZL_ADDR 0x37
#define ADXL345_INT1_DURATION_ADDR 0x38
#define I_AM_ADXL345 ((uint8_t)0xD4)
#define ADXL345_MODE_POWERDOWN ((uint8_t)0x00)
#define ADXL345_MODE_ACTIVE ((uint8_t)0x08)
#define ADXL345_OUTPUT_DATARATE_1 ((uint8_t)0x00)
#define ADXL345_OUTPUT_DATARATE_2 ((uint8_t)0x40)
#define ADXL345_OUTPUT_DATARATE_3 ((uint8_t)0x80)
#define ADXL345_OUTPUT_DATARATE_4 ((uint8_t)0xC0)
#define ADXL345_X_ENABLE ((uint8_t)0x02)
#define ADXL345_Y_ENABLE ((uint8_t)0x01)
#define ADXL345_Z_ENABLE ((uint8_t)0x04)
#define ADXL345_AXES_ENABLE ((uint8_t)0x07)
#define ADXL345_AXES_DISABLE ((uint8_t)0x00)
#define ADXL345_BANDWIDTH_1 ((uint8_t)0x00)
#define ADXL345_BANDWIDTH_2 ((uint8_t)0x10)
#define ADXL345_BANDWIDTH_3 ((uint8_t)0x20)
#define ADXL345_BANDWIDTH_4 ((uint8_t)0x30)
#define ADXL345_FULLSCALE_250 ((uint8_t)0x00)
#define ADXL345_FULLSCALE_500 ((uint8_t)0x10)
#define ADXL345_FULLSCALE_2000 ((uint8_t)0x20)
#define ADXL345_BlockDataUpdate_Continous ((uint8_t)0x00)
#define ADXL345_BlockDataUpdate_Single ((uint8_t)0x80)
#define ADXL345_BLE_LSB ((uint8_t)0x00)
#define ADXL345_BLE_MSB ((uint8_t)0x40)
#define ADXL345_HIGHPASSFILTER_DISABLE ((uint8_t)0x00)
#define ADXL345_HIGHPASSFILTER_ENABLE ((uint8_t)0x10)
#define ADXL345_INT1INTERRUPT_DISABLE ((uint8_t)0x00)
#define ADXL345_INT1INTERRUPT_ENABLE ((uint8_t)0x80)
#define ADXL345_INT2INTERRUPT_DISABLE ((uint8_t)0x00)
#define ADXL345_INT2INTERRUPT_ENABLE ((uint8_t)0x08)
#define ADXL345_INT1INTERRUPT_LOW_EDGE ((uint8_t)0x20)
#define ADXL345_INT1INTERRUPT_HIGH_EDGE ((uint8_t)0x00)
#define ADXL345_BOOT_NORMALMODE ((uint8_t)0x00)
#define ADXL345_BOOT_REBOOTMEMORY ((uint8_t)0x80)
#define ADXL345_HPM_NORMAL_MODE_RES ((uint8_t)0x00)
#define ADXL345_HPM_REF_SIGNAL ((uint8_t)0x10)
#define ADXL345_HPM_NORMAL_MODE ((uint8_t)0x20)
#define ADXL345_HPM_AUTORESET_INT ((uint8_t)0x30)
#define ADXL345_HPFCF_0 0x00
#define ADXL345_HPFCF_1 0x01
#define ADXL345_HPFCF_2 0x02
#define ADXL345_HPFCF_3 0x03
#define ADXL345_HPFCF_4 0x04
#define ADXL345_HPFCF_5 0x05
#define ADXL345_HPFCF_6 0x06
#define ADXL345_HPFCF_7 0x07
#define ADXL345_HPFCF_8 0x08
#define ADXL345_HPFCF_9 0x09
#define ADXL345_CS_LOW() GPIO_ResetBits(ADXL345_SPI_CS_GPIO_PORT, ADXL345_SPI_CS_PIN)
#define ADXL345_CS_HIGH() GPIO_SetBits(ADXL345_SPI_CS_GPIO_PORT, ADXL345_SPI_CS_PIN)
void ADXL345_Init(ADXL345_InitTypeDef ADXL345_InitStruct);
void ADXL345_RebootCmd(void);
void ADXL345_INT1InterruptCmd(uint8_t InterruptState);
void ADXL345_INT2InterruptCmd(uint8_t InterruptState);
void ADXL345_INT1InterruptConfig(ADXL345_InterruptConfigTypeDef ADXL345_IntConfigStruct);
uint8_t ADXL345_GetDataStatus(void);
void ADXL345_FilterConfig(ADXL345_FilterConfigTypeDef ADXL345_FilterStruct);
void ADXL345_FilterCmd(uint8_t HighPassFilterState);
void ADXL345_Write(uint8_t pBuffer, uint8_t WriteAddr, uint16_t NumByteToWrite);
void ADXL345_Read(uint8_t pBuffer, uint8_t ReadAddr, uint16_t NumByteToRead);
2ADXL345的SPI配置函数
void SPI_init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
SPI_InitTypeDef SPI_InitStructure;
R
自己去看看吧,不要着急。
//
//名称:单片机串口输出ADXL335角度值
//使用单片机:stc12c5a60s2
//供电:电压5v
//晶振:110592m
//X轴->(ADC0)P10
//Y轴->(ADC1)P11
//Z轴->(ADC1)P12
//用pc串口助手显示时,选择字符显示
//参考宏晶网址例程
//
#include "reg51h"
#include "intrinsh"
#include <mathh> //Keil library
#include <stdioh> //Keil library
//#define FOSC 18432000L
#define FOSC 11059200L //晶振110592M
#define BAUD 9600
typedef unsigned char BYTE;
typedef unsigned int WORD;
#define uchar unsigned char
#define uint unsigned int
/Declare SFR associated with the ADC /
sfr AUXR1 = 0XA2;
sfr ADC_CONTR = 0xBC; //ADC control register
sfr ADC_RES = 0xBD; //ADC high 8-bit result register
sfr ADC_RESL = 0xBE;
sfr ADC_LOW2 = 0xBE; //ADC low 2-bit result register
sfr P1ASF = 0x9D; //P1 secondary function control register
/Define ADC operation const for ADC_CONTR/
#define ADC_POWER 0x80 //ADC power control bit
#define ADC_FLAG 0x10 //ADC complete flag
#define ADC_START 0x08 //ADC start control bit
#define ADC_SPEEDLL 0x00 //420 clocks
#define ADC_SPEEDL 0x20 //280 clocks
#define ADC_SPEEDH 0x40 //140 clocks
#define ADC_SPEEDHH 0x60 //70 clocks
void InitUart();
void InitADC();
void SendData(BYTE dat);
int GetADCResult(BYTE ch);
void Delay(WORD n);
uchar ge,shi,bai,qian,wan; //显示变量
void conversion(uint temp_data)
{
wan=temp_data/10000+0x30 ;
temp_data=temp_data%10000; //取余运算
qian=temp_data/1000+0x30 ;
temp_data=temp_data%1000; //取余运算
bai=temp_data/100+0x30 ;
temp_data=temp_data%100; //取余运算
shi=temp_data/10+0x30 ;
temp_data=temp_data%10; //取余运算
ge=temp_data+0x30;
}
/----------------------------
Get ADC result
----------------------------/
int GetADCResult(BYTE ch)
{ int AD10bitResult;
ADC_CONTR = ADC_POWER | ADC_SPEEDLL | ch | ADC_START;
_nop_(); //Must wait before inquiry
_nop_();
_nop_();
_nop_();
while (!(ADC_CONTR & ADC_FLAG));//Wait complete flag
ADC_CONTR &= ~ADC_FLAG; //Close ADC
AD10bitResult=ADC_RES;
AD10bitResult<<=8;
AD10bitResult|=ADC_RESL;
return AD10bitResult; //Return 10BIT ADC result
}
/----------------------------
Initial UART
----------------------------/
void InitUart()
{
SCON = 0x5a; //8 bit data ,no parity bit
TMOD = 0x20; //T1 as 8-bit auto reload
TH1 = TL1 = -(FOSC/12/32/BAUD); //Set Uart baudrate
TR1 = 1; //T1 start running
}
/----------------------------
Initial ADC sfr
----------------------------/
void InitADC()
{
P1ASF = 0xff; //Open 8 channels ADC function
AUXR1=AUXR1|0X04; //ad值左对齐
ADC_RES = 0; //Clear previous result
ADC_CONTR = ADC_POWER | ADC_SPEEDLL;
Delay(2); //ADC power-on and delay
}
/----------------------------
Send one byte data to PC
Input: dat (UART data)
Output:-
----------------------------/
void SendData(BYTE dat)
{
while (!TI); //Wait for the previous data is sent
TI = 0; //Clear TI flag
SBUF = dat; //Send current data
}
/----------------------------
Software delay function
----------------------------/
void Delay(WORD n)
{
WORD x;
while (n--)
{
x = 5000;
while (x--);
}
}
//
void main()
{ int x,y,z;
float Q,T,K,Roll,Pitch;
InitUart(); //Init UART, use to show ADC result
InitADC(); //Init ADC sfr
while (1)
{
x=GetADCResult(0);
y=GetADCResult(1);
z=GetADCResult(2);
// 0-5v 对应 0-1024
// 33v/2 = 165v(0g时候值)
// 165v ->= 338 (AD值即0X0152)
x-=0X0152; //
y-=0X0152;
z-=0X0152;
Q=x;
T=y;
K=z;
Q=-Q;
Roll=(float)(((atan2(K,Q)180)/314159265)+180);
Pitch=(float)(((atan2(K,T)180)/314159265)+180);
conversion(Roll); //横滚角0-360度
//conversion(Pitch); //俯仰角0-360度
SendData(bai);
SendData(shi);
SendData(ge);
SendData(0x0d);
SendData(0);//换行,回车
Delay(30);
}
}
以上就是关于基于STM32的ADXL335传感器的程序,谁能发一份给我呀,谢谢!全部的内容,包括:基于STM32的ADXL335传感器的程序,谁能发一份给我呀,谢谢!、求基于STC单片机ADXL335角度传感器测角度的C语言程序、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)