当前位置:网站首页>Skill sorting [email protected]+adxl345+ Motor vibration + serial port output
Skill sorting [email protected]+adxl345+ Motor vibration + serial port output
2022-06-30 10:32:00 【Sky_ Lannister】
be based on f103zet6 Single chip microcomputer , Use adxl345 Check the motor vibration frequency , Serial printing , Program templates are punctual atoms
1、 Project brief introduction

2、 Implementation logic
# Yes adxl345 initialization
# Detect motor vibration ( The acceleration ), Print through serial port
3、 Application scenarios
# Detect the vibration of motor or other objects
4、 Sort out the core code
#define SlaveAddress 0xA6 // Define the device in IIC Slave address in the bus , according to ALT ADDRESS Address pins are modified differently //ALT ADDRESS When the pin is grounded, the address is 0xA6, When connected to the power supply, the address is 0x3A
unsigned char BUF[8]; // Receive data buffer
unsigned char ge,shi,bai,qian,wan; // Show variable
unsigned char err;
float temp_X,temp_Y,temp_Z;
float Acc_X,Acc_Y,Acc_Z,q;
short Frequency;
void SCL_Set_Output(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SCL_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SCL_GPIO, &GPIO_InitStructure);
}
void SDA_Set_Output(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SDA_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SDA_GPIO, &GPIO_InitStructure);
}
void SDA_Set_Input(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = SDA_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(SDA_GPIO, &GPIO_InitStructure);
}
/************************************** Start signal **************************************/
void ADXL345_Start(void)
{
SCL_OUT();
SDA_OUT();
SET_SDA();//SDA = 1; // Pull up the data line
SET_SCL();//SCL = 1; // Pull up the clock line
delay_us(2);//Delay5us(); // Time delay
CLE_SDA();//SDA = 0; // Pull down the data line
delay_us(2);//Delay5us(); // Time delay
CLE_SCL();//SCL = 0; // Pull down the clock line
}
/************************************** Stop signal **************************************/
void ADXL345_Stop(void)
{
SCL_OUT();
SDA_OUT();
CLE_SDA();//SDA = 0; // Pull down the data line
SET_SCL();//SCL = 1; // Pull up the clock line
delay_us(2);//Delay5us(); // Time delay
SET_SDA();//SDA = 1; // To produce a rising edge
delay_us(2);//Delay5us(); // Time delay
CLE_SCL();
}
/************************************** Send reply signal Entrance parameters :ack (0:ACK 1:NAK) **************************************/
void ADXL345_SendACK(uchar ack)
{
SCL_OUT();
SDA_OUT();
if(ack==0)//SDA = ack; // Write reply signal
{
CLE_SDA();
}
else
{
SET_SDA();
}
SET_SCL();//SCL = 1; // Pull up the clock line
delay_us(2);//Delay5us(); // Time delay
CLE_SCL();//SCL = 0; // Pull down the clock line
delay_us(5);//Delay5us(); // Time delay
}
/************************************** Receive a reply signal **************************************/
uchar ADXL345_RecvACK(void)
{
SDA_INT();
SCL_OUT();
SET_SCL();//SCL = 1; // Pull up the clock line
delay_us(2);// Delay5us(); // Time delay
SET_SCL();
if(SDA_VAL()== Bit_SET) //CY = SDA; // Read the answer signal
{
err = 1;
}
else
{
err = 0;
}
CLE_SCL() ;//SCL = 0; // Pull down the clock line
delay_us(5);// Delay5us(); // Time delay
SDA_OUT();
return err;
}
/************************************** towards IIC The bus sends a byte of data **************************************/
void ADXL345_SendByte(unsigned char dat)
{
unsigned char i;
SCL_OUT();
SDA_OUT();
for (i=0; i<8; i++) //8 Bit counter
{
delay_us(5); // Time delay
if(dat&0x80) //SDA = CY; // Send data port
{
SET_SDA();}
else
{
CLE_SDA();}
delay_us(5); // Time delay
SET_SCL();//SCL = 1; // Pull up the clock line
delay_us(5); // Time delay
CLE_SCL();//SCL = 0; // Pull down the clock line
dat <<= 1; // Remove the highest bit of data
}
ADXL345_RecvACK();
}
/************************************** from IIC The bus receives a byte of data **************************************/
unsigned char ADXL345_RecvByte(void)
{
unsigned char i;
unsigned char Mid;
unsigned char dat = 0;
SDA_INT();
SCL_OUT();
for (i=0; i<8; i++) //8 Bit counter
{
dat <<= 1;
delay_us(5); // Time delay
SET_SCL();
if(SDA_VAL()== Bit_SET) //CY = SDA; // Read the answer signal
{
Mid = 1;
}
else
{
Mid = 0;
}
if(Mid) dat++;
delay_us(5);
CLE_SCL();//SCL = 0; // Pull down the clock line
}
return dat;
}
//****** Single byte write *******************************************
void Single_Write_ADXL345(uchar REG_Address,uchar REG_data)
{
ADXL345_Start(); // Start signal
ADXL345_SendByte(SlaveAddress); // Send device address + Write the signal
ADXL345_SendByte(REG_Address); // Internal address , Please refer to Chinese pdf22 page
ADXL345_SendByte(REG_data); // Internal register data , Please refer to Chinese pdf22 page
ADXL345_Stop(); // Send stop signal
}
//******** Single byte read *****************************************
uchar Single_Read_ADXL345(uchar REG_Address)
{
uchar REG_data;
ADXL345_Start(); // Start signal
ADXL345_SendByte(SlaveAddress); // Send device address + Write the signal
ADXL345_SendByte(REG_Address); // Send storage location address , from 0 Start
ADXL345_Start(); // Start signal
ADXL345_SendByte(SlaveAddress+1); // Send device address + Read the signal
REG_data=ADXL345_RecvByte(); // Read the register data
ADXL345_SendACK(1);
ADXL345_Stop(); // Stop signal
return REG_data;
}
//*********************************************************
//
// Continuous reading ADXL345 Internal acceleration data , Address range 0x32~0x37
//
//*********************************************************
void Multiple_Read_ADXL345(void)
{
uchar i;
ADXL345_Start(); // Start signal
ADXL345_SendByte(SlaveAddress); // Send device address + Write the signal
ADXL345_SendByte(0x32); // Send storage location address , from 0x32 Start
ADXL345_Start(); // Start signal
ADXL345_SendByte(SlaveAddress+1); // Send device address + Read the signal
for (i=0; i<6; i++) // Read continuously 6 Address data , In storage BUF
{
BUF[i] = ADXL345_RecvByte(); //BUF[0] Storage 0x32 The data in the address
if (i == 5)
{
ADXL345_SendACK(1); // The last data needs to be returned to NOACK
}
else
{
ADXL345_SendACK(0); // Respond ACK
}
}
ADXL345_Stop(); // Stop signal
delay_us(5);
}
//*****************************************************************
// initialization ADXL345, Please refer to pdf Make changes ************************
void Init_ADXL345(void)
{
//
Single_Write_ADXL345(0x31,0x0B); // measuring range , Plus or minus 16g,13 Bit mode
Single_Write_ADXL345(0x2C,0x08); // The rate is set to 25 Reference resources pdf13 page
Single_Write_ADXL345(0x2D,0x08); // Select the power mode Reference resources pdf24 page // Reference resources 14 A page table 7 Make changes
Single_Write_ADXL345(0x2E,0x80); // Can make DATA_READY interrupt
Single_Write_ADXL345(0x38,0x00);
Single_Write_ADXL345(0x2F,0x00);
Single_Write_ADXL345(0x1E,0x00); //X Offset Write... According to the status of the test sensor pdf29 page
Single_Write_ADXL345(0x1F,0x00); //Y Offset Write... According to the status of the test sensor pdf29 page
Single_Write_ADXL345(0x20,0x05); //Z Offset Write... According to the status of the test sensor pdf29 page
}
//***********************************************************************
// Show x Axis
void ReadData_x(void)
{
int Acc_dis_data,dis_data,Acc_speed,Dis; // Variable
Multiple_Read_ADXL345(); // Read data continuously , Stored in BUF in
/* obtain syz Axial acceleration */
Acc_dis_data=(BUF[1]<<8)+BUF[0]; // Synthetic data
Acc_X=(float)Acc_dis_data*3.9/1000*9.8; // Calculate data and display , Investigation ADXL345 Quick start No 4 page
Acc_dis_data=(BUF[3]<<8)+BUF[2]; // Synthetic data
Acc_Y=(float)Acc_dis_data*3.9/1000*9.8; // Calculate data and display , Investigation ADXL345 Quick start No 4 page
Acc_dis_data=(BUF[5]<<8)+BUF[4]; // Synthetic data
Acc_Z=(float)Acc_dis_data*3.9/1000*9.8; // Calculate data and display , Investigation ADXL345 Quick start No 4 page
/* obtain syz Axis coordinates */
dis_data=(BUF[1]<<8)+BUF[0]; // Synthetic data
temp_X=(float)dis_data*3.9; // Calculate data and display , Investigation ADXL345 Quick start No 4 page
dis_data=(BUF[3]<<8)+BUF[2]; // Synthetic data
temp_Y=(float)dis_data*3.9; // Calculate data and display , Investigation ADXL345 Quick start No 4 page
dis_data=(BUF[5]<<8)+BUF[4]; // Synthetic data
temp_Z=(float)dis_data*3.9; // Calculate data and display , Investigation ADXL345 Quick start No 4 page
}
int main(void)
{
#define ACC_X_FIL 500
#define TEMP_X_FIL 50
#define LINE_FIL_N 10
u8 time=0,j;
u8 fil_count;
uint16_t value_buf[LINE_FIL_N];
u8 i;
u8 t=0;
uint16_t Acc_X_hex, TEMP_X_hex, Acc_X_hex_last, TEMP_X_hex_last;
uint32_t count_num = 0, fre = 0, fre_sum_fil = 0, fre_sum[10] = {
0}, fre_sum_num = 0, fil_sum = 0;
uart_init(115200); // The serial port is initialized to 115200
delay_init(); // Delay initialization
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); // Set up NVIC Interrupt grouping 2:2 Bit preemption priority ,2 Bit response priority
LED_Init(); // Initialization and LED Connected hardware interface
usart3_init(115200); // Initialize serial port 3
KEY_Init();
BEEP_Init();
OLED_Clear();
TIM2_Int_Init(4999,7199);
TIM6_Int_Init(2499,7199);
OLED_Init(); // initialization OLED
Init_ADXL345(); //adxl345 initialization
DS18B20_Init(); //DS18B20 initialization
printf("start !!!\r\n");
OLED_ShowString(0,0,"Electric machinery",12);
OLED_ShowString(0,52,"ASCII:",12);
OLED_ShowString(64,52,"CODE:",12);
OLED_Refresh_Gram(); // The update is displayed to OLED
u8 ret;
ret=atk_8266_wifista_test();
delay_ms(1500);
if(ret==2)
{
printf("wifi init ok !!!\r\n");
_mqtt.Init(rxbuf,0,txbuf,0);//MQTT initialization
//MQTT Connect
ret=_mqtt.Connect("gl8dRlBcJhj.ESP8266|securemode=2,signmethod=hmacsha256,timestamp=2524608000000|",
"ESP8266&gl8dRlBcJhj",
"223d122ec212698603b5c2af5b73c9d0936e1c1e6327c566ea938089e56452ff");
delay_ms(1500);
delay_ms(1500);// These two delays can be appropriately reduced , But deleting it seems to be a problem
}
_mqtt.Init(rxbuf,0,txbuf,0);//MQTT initialization
ret=_mqtt.SubscribeTopic("/sys/a1e8HLT2lBT/test/thing/service/property/set",0,1);
delay_ms(3000);// You can delete it and try
char * string=NULL;
if(Single_Read_ADXL345(0X00)==0xe5)
{
delay_ms(5);
}
else
{
delay_ms(3);
}
j=' ';
while(1)
{
count_num++;
OLED_ShowChar(48,48,j,16,1);// Show ASCII character
OLED_Refresh_Gram();
j++;
if(j>'~')j=' ';
OLED_ShowNum(103,48,j,3,16);// Show ASCII The code value of a character
if(time%100==0){
temperature=DS18B20_Get_Temp();
ReadData_x(); // Triaxial detection function
}
//printf("\r\nTemp:%d.%d .C\n",temperature/10,temperature%10);
// printf(" X=%4.3f m/s2 Y=%4.3f m/s2 Z=%4.3f m/s2\r\n",Acc_X,Acc_Y,Acc_Z);
// printf("x The value of the shaft :%.1f y The value of the shaft :%.1f Z The value of the shaft :%.1f\r\n",temp_X,temp_Y,temp_Z);
// printf("%4.3f\r\n",Acc_X);
Acc_X_hex = Acc_X * 1000;
TEMP_X_hex = temp_X;
if((abs(Acc_X_hex - Acc_X_hex_last) > ACC_X_FIL))// wave filtering
{
fre++;
}
if((abs(TEMP_X_hex - TEMP_X_hex_last) > TEMP_X_FIL))
{
fre++;
}
Acc_X_hex_last = Acc_X_hex;
TEMP_X_hex_last = TEMP_X_hex;
if(count_num % 100 == 0)// Calibrated at this time 1s
{
fre_sum[(fre_sum_num++) % 10] = fre;
for(i=0; i<10; i++)
{
fre_sum_fil += fre_sum[i];
}
fre_sum_fil = fre_sum_fil/10;
printf("\r\nTemp:%d.%d .C\n",temperature/10,temperature%10);
printf("fre is: %d HZ\r\n",fre_sum_fil);
Fre_sum_fil = fre_sum_fil;
fre_sum_fil = 0;
fre = 0;
LED0=!LED0;
}
if(count_num % 10 == 0)
{
printf("%c",0XFF);
printf("%c",0XFF);
printf("%c",(uint16_t)(Acc_X_hex/255));
printf("%c",(uint16_t)Acc_X_hex);
value_buf[fil_count++] = Acc_X_hex;
if(fil_count == LINE_FIL_N)
{
fil_count = 0;
}
for (u8 count=0; count<LINE_FIL_N; count++)
{
fil_sum += value_buf[count];
}
fil_sum = fil_sum / LINE_FIL_N;
printf("%c",(uint16_t)(fil_sum/255));
printf("%c",(uint16_t)fil_sum);
fil_sum = 0;
}
if(alarm_is_free == 10)//alarm_is_free=10, The initial value is 10
{
if(temperature/10 < 25 && Fre_sum_fil < 40)alarmFlag=0;// Buzzer alarm
else alarmFlag =1;
}
if(alarm_is_free<10)
{
alarm_is_free++;
}
t=KEY_Scan(0); // Get the key value
if(t == KEY0_PRES)LED0=!LED0;
else if(t == KEY1_PRES){
alarmFlag=!alarmFlag;
delay_ms(20000);
alarm_is_free=0;
}
delay_ms(10);
_mqtt.Init(rxbuf,0,txbuf,0);//MQTT initialization
string=MyCreatJson( );
_mqtt.PublishData("/sys/gl8dRlBcJhj/ESP8266/thing/event/property/post", // With publishing permission TOPIC
string, // accord with json Message string in data format
0); // Message level
free(string);//!!!! Release space after publishing ?
}
}
5、 Some references
6、 matters needing attention
# The accuracy of this module is not particularly accurate , It can almost be considered as a vibration sensor , In order to figure out a motor, other data will not suddenly become larger , An accumulated value used
Full operational project address
边栏推荐
- train_ de.py: error: argument --save_ steps: invalid int value: ‘$[$[889580/128/4]*10/2]‘
- Memorize the text and remember the words. Read the text and remember the words. Read the article and remember the words; 40 articles with 3500 words; 71 articles broke through the words in the middle
- Curl --- the request fails when the post request parameter is too long (more than 1024b)
- Leetcode question brushing (IV) -- greedy thought (go Implementation)
- Automated stock trading ensemble strategy based on Reinforcement Learning
- ‘Failed to fetch current robot state‘ when using the ‘plan_kinematic_path‘ service #868
- What is the real performance of CK5, the king machine of CKB?
- WGet -- 404 not found due to spaces in URL
- 跳跃表介绍
- 逸仙電商發布一季報:堅持研發及品牌投入,實現可持續高質量發展
猜你喜欢

戴森设计大奖,以可持续化设计改变世界

Compétences Comb 27 @ Body sense Manipulator
[email protected]体感机械臂"/>技能梳理[email protected]体感机械臂

CSDN博客运营团队2022年H1总结

MySQL index, transaction and storage engine of database (2)

机器人系统动力学——惯性参数
[email protected]基于51系列单片机的智能仪器教具"/>技能梳理[email protected]基于51系列单片机的智能仪器教具

A brief introduction to database mysql

The programmer was beaten.

WGet -- 404 not found due to spaces in URL
随机推荐
Robot system dynamics - inertia parameters
打通供应链 深圳礼品展助跨境电商寻破局之道
那个程序员,被打了。
孙安民作品《莲花净心》数字藏品上线长城数艺
ModuleNotFoundError: No module named ‘_ swigfaiss‘
[ark UI] implementation of the startup page of harmoniyos ETS
Leetcode question brushing (III) -- binary search (go Implementation)
mysql数据库基础:TCL事务控制语言
Robotframework learning notes: environment installation and robotframework browser plug-in installation
[C language quick start] let you know C language and get started with zero basics ③
【Rust日报】2021-01-22 首份Rust月刊杂志邀请大家一起参与
WGet -- 404 not found due to spaces in URL
ArcGIS Pro脚本工具(6)——修复CAD图层数据源
NLopt--非线性优化--原理介绍及使用方法
I found a wave of "alchemy artifact" in the goose factory. The developer should pack it quickly
Musk has more than 100 million twitter fans, but he has been lost online for a week
KOREANO ESSENTIAL打造气质职场范
Open source! Wenxin large model Ernie tiny lightweight technology, accurate and fast, full effect
JS obtient la chaîne spécifiée spécifiant la position du caractère & sous - chaîne spécifiant la plage de position du caractère 【 détails simples 】
Detailed explanation of SolidWorks mass characteristics (inertia tensor, moment of inertia, inertia spindle)