Kaynağa Gözat

压力传感器IIC通信异常检测,如果一直不恢复就绕过, 调整红绿指示灯,调整定时器回调接口

guoqiang 10 ay önce
ebeveyn
işleme
77393a8f1b

+ 83 - 30
Device/PressureSensor.c

@@ -6,6 +6,15 @@
 
 
 #define CPS135B_ADDR     (0x6D)
+
+static uint8_t  iic_error_count = 0;
+static uint8_t  iic_exception = 0;
+static uint8_t  iic_exception2 = 0;
+static float   cps_pressure = 0;
+static float   cps_temperature = 0;
+static uint32_t ret_status = 0;
+
+
 /**
 * @prototype I2C_ParaInit(void)
 *
@@ -19,8 +28,8 @@ void I2C0_ParaInit(void)
 	I2C_ConfigType i2cConfig;
 	
 	/*初始化引脚功能为I2C.*/
-	//GPIO_SetFunc(I2C0_SCL_PORT, I2C0_SCL_PIN, GPIO_FUN3);
-	//GPIO_SetFunc(I2C0_SDA_PORT, I2C0_SDA_PIN, GPIO_FUN3);
+	GPIO_SetFunc(GPIOA, GPIO_PIN0, GPIO_FUN3);//I2C0_SCL
+	GPIO_SetFunc(GPIOA, GPIO_PIN1, GPIO_FUN3);//I2C0_sda
 	
 	/*清零结构体变量.*/
 	memset(&i2cConfig, 0x00, sizeof(i2cConfig));
@@ -138,18 +147,28 @@ uint8_t cps135b_gd(float* pressure, float* temperature)
 void PressureSensor_Init(void)
 {
 	I2C0_ParaInit();
+	iic_error_count = 0;
+	cps_pressure = 0;
+	cps_temperature = 0;
+	
 }
 
 uint8_t PressureSensor_MR(void)
 {
-	uint32_t status = I2C_HW_STATUS_ERROR_NULL;
+	
 	uint8_t buffer[2];
 	buffer[0] = 0x30;
 	buffer[1] = 0x0A;
 	
-	status = I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, buffer, 2, ENABLE);//发送读取地址
+	//反复几次出现问题,直接返回异常
+	if(iic_exception2 > 3){
+		return 1;
+	}
 	
-	if(I2C_HW_STATUS_ERROR_NULL == status){
+	ret_status = I2C_HW_STATUS_ERROR_NULL;
+	ret_status = I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, buffer, 2, ENABLE);//发送读取地址
+	
+	if(I2C_HW_STATUS_ERROR_NULL == ret_status){
 		return 0;
 	}else{
 		return 1;
@@ -157,43 +176,73 @@ uint8_t PressureSensor_MR(void)
 	
 }
 
-
-
-uint8_t PressureSensor_Getvalue(float* pressure)
+uint8_t PressureSensor_Read(void)
 {
-		uint32_t status = I2C_HW_STATUS_ERROR_NULL;
 	uint32_t  tmp = 0;
 	uint8_t reg_addr = 0x06;
-	uint8_t data[3];
+	uint8_t data[5];
 	
-	status |= I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, &reg_addr, 1, DISABLE);//发送读取地址
-	status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 3);//读取数据
+	if(iic_exception2 > 3){
+		//反复几次出现问题,直接返回异常
+		return 1;
+	}
 	
-	if(I2C_HW_STATUS_ERROR_NULL == status){
+	ret_status = I2C_HW_STATUS_ERROR_NULL;
+	ret_status |= I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, &reg_addr, 1, DISABLE);//发送读取地址
+	ret_status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 5);//读取数据
+	
+	if(I2C_HW_STATUS_ERROR_NULL == ret_status){
 	
 		tmp = (uint32_t)data[0]<<16;
 		tmp += (uint32_t)data[1]<<8;
 		tmp += data[2];
 		
-		*pressure = (1.0*tmp)/256;
+		cps_pressure = (1.0*tmp)/256/1000;  //kPa 
 
-		return 0;
+		tmp = 0;
+		tmp += (uint32_t)data[3]<<8;
+		tmp += data[4];
+		
+		
+		if(data[3] <= 128){
+			cps_temperature = 1.0*tmp/256;
+		}else{
+			cps_temperature = 1.0*(tmp-65535)/256;
+		}
+		
+		iic_error_count = 0;
+		iic_exception =0;
 		
 	}else{
-		return 1;
+		
+		iic_error_count++;
 	}
 	
+	if(iic_error_count >= 3){
+		iic_exception =1;
+		
+		PressureSensor_DeInit();
+		PressureSensor_Init();
+		
+		iic_exception2++;
+		
+	}
+	
+	return iic_exception;
+	
 }
 
-uint8_t PressureSensor_Getvalue2(float* pressure, float* temperature)
+
+
+uint8_t PressureSensor_Getvalue(float* pressure)
 {
 	uint32_t status = I2C_HW_STATUS_ERROR_NULL;
 	uint32_t  tmp = 0;
 	uint8_t reg_addr = 0x06;
-	uint8_t data[5];
+	uint8_t data[3];
 	
 	status |= I2C_MasterTransmitPoll(I2C0, CPS135B_ADDR, &reg_addr, 1, DISABLE);//发送读取地址
-	status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 5);//读取数据
+	status |= I2C_MasterReceivePoll(I2C0, CPS135B_ADDR, data, 3);//读取数据
 	
 	if(I2C_HW_STATUS_ERROR_NULL == status){
 	
@@ -203,22 +252,20 @@ uint8_t PressureSensor_Getvalue2(float* pressure, float* temperature)
 		
 		*pressure = (1.0*tmp)/256;
 
-		tmp = 0;
-		tmp += (uint32_t)data[3]<<8;
-		tmp += data[4];
-		
-		
-		if(data[3] <= 128){
-			*temperature = 1.0*tmp/256;
-		}else{
-			*temperature = 1.0*(tmp-65535)/256;
-		}
-		
 		return 0;
 		
 	}else{
 		return 1;
 	}
+	
+}
+
+uint8_t PressureSensor_Getvalue2(float* pressure, float* temperature)
+{
+	*pressure = cps_pressure;
+	*temperature = cps_temperature;
+	
+	return iic_exception;
 
 }
 
@@ -239,5 +286,11 @@ uint8_t PressureSensor_Print(void)
 void PressureSensor_DeInit(void)
 {
 
+	I2C_DeInit(I2C0);
+}
+
+void PressureSensor_Release(void)
+{
 
+	I2C_DeInit(I2C0);
 }

+ 2 - 0
Device/PressureSensor.h

@@ -47,6 +47,8 @@ extern "C" {
 void PressureSensor_Init(void);
 
 uint8_t PressureSensor_MR(void);
+uint8_t PressureSensor_Read(void);
+
 uint8_t PressureSensor_Getvalue(float* pressure);
 uint8_t PressureSensor_Getvalue2(float* pressure, float* temperature);
 

+ 2 - 2
Device/gpio.c

@@ -82,8 +82,8 @@ void GPIO_PortInit(void)
 		GPIO_SetDir(REDLED_PORT, REDLED_PIN, GPIO_OUT);
 		
 		/*³õʼ»¯Pressure Sensor iic0 */
-		GPIO_SetFunc(GPIOA, GPIO_PIN0, GPIO_FUN3);//I2C0_SCL
-		GPIO_SetFunc(GPIOA, GPIO_PIN1, GPIO_FUN3);//I2C0_sda
+		//GPIO_SetFunc(GPIOA, GPIO_PIN0, GPIO_FUN3);//I2C0_SCL
+		//GPIO_SetFunc(GPIOA, GPIO_PIN1, GPIO_FUN3);//I2C0_sda
 	
 		//adc0 ch7 ch8
 		GPIO_SetFunc(GPIOA, GPIO_PIN2, GPIO_FUN2); 		// adc IN8 hall_2

+ 4 - 4
Device/gpio.h

@@ -53,11 +53,11 @@ extern "C" {
 #define RS485_TX_EN				do{GPIO_SetPinLevel(RS485CTRL_PORT, RS485CTRL_PIN, GPIO_LEVEL_HIGH);}while(0)
 #define RS485_RX_EN				do{GPIO_SetPinLevel(RS485CTRL_PORT, RS485CTRL_PIN, GPIO_LEVEL_LOW);}while(0)
 
-#define GREENLED_PORT			(GPIOA)
-#define GREENLED_PIN			(GPIO_PIN10)
+#define GREENLED_PORT			(GPIOB)
+#define GREENLED_PIN			(GPIO_PIN8)
 
-#define REDLED_PORT			(GPIOB)
-#define REDLED_PIN			(GPIO_PIN8)
+#define REDLED_PORT			(GPIOA)
+#define REDLED_PIN			(GPIO_PIN10)
 
 /*RUNLED¶¯×÷¶¨Òå.*/	
 #define GREENLED_ON				do{GPIO_SetPinLevel(GREENLED_PORT, GREENLED_PIN, GPIO_LEVEL_HIGH);}while(0)

+ 4 - 41
Device/timer.c

@@ -38,8 +38,6 @@
 /*************<include>****************/
 #include "string.h"
 //#include "gpio.h"
-#include "process.h"
-#include "uart.h"
 #include "timer.h"
 
 /*************<macro>******************/
@@ -60,6 +58,8 @@
 /*************<prototype>**************/
 void Timer_Callback(void *device, uint32_t wpara, uint32_t lpara);
 
+extern void Process_Timer1_Callback(void);
+extern void rs485_Timer1_Callback(void);
 
 /**
 * @prototype Timer0_Init(void)
@@ -134,45 +134,8 @@ void Timer_Callback(void *device, uint32_t wpara, uint32_t lpara)
 				//g_timer0Cnt++;
 		}else if(TIMER_CHANNEL1 == device){
 			
-			if(g_blinkLedTime < 0xFFFF)
-			{
-					g_blinkLedTime++;
-			}
-			
-			if(rs485_info.dmasend_count < 0xFF){
-					rs485_info.dmasend_count++;
-			}
-			
-			if(g_mr_Interval < 0xFF){
-					g_mr_Interval++;
-			}
-			
-			if(g_print_interval < 0xFFFF){
-					g_print_interval++;
-			}
-			
-			
-			/*
-			
-			if(g_detectTime < 0xFF){
-					g_detectTime++;
-			}
-			
-			if (g_flashWrRdRdy)
-			{
-				g_flashWrRdTime++;
-			}
-			
-			if(g_runReady){
-				g_runTime++;
-			}
-			
-			if(g_period1000ms < 0xFFFF)
-			{
-				g_period1000ms++;
-			}
-	
-			*/
+			Process_Timer1_Callback();
+			rs485_Timer1_Callback();
 	
 		}
 

+ 6 - 0
Device/uart.c

@@ -236,4 +236,10 @@ void rs485_RecvData(void)
 		UART_ReceiveDMA(UART485, DMA0_CHANNEL1, rs485_info.recv_buffer, UART_RECV_DATA_POOL_COUNT, UartRxDMAEventCallback); 
 }
 
+void rs485_Timer1_Callback(void)
+{
+		if(rs485_info.dmasend_count < 0xFF){
+				rs485_info.dmasend_count++;
+		}
+}
 

Dosya farkı çok büyük olduğundan ihmal edildi
+ 166 - 94
Project/AirControlValve.uvguix.JL200


+ 2 - 2
Project/AirControlValve.uvoptx

@@ -120,7 +120,7 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>DLGUARM</Key>
-          <Name>d</Name>
+          <Name></Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>
@@ -140,7 +140,7 @@
         <SetRegEntry>
           <Number>0</Number>
           <Key>JL2CM3</Key>
-          <Name>-U59518874 -O78 -S5 -ZTIFSpeedSel1000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST1 -N00("ARM CoreSight SW-DP") -D00(0BC11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8001 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO15 -FD20000000 -FC1000 -FN1 -FF0AC780x_128KB.FLM -FS08000000 -FL020000 -FP0($$Device:AC78013MDQA$Flash\AC780x_128KB.FLM)</Name>
+          <Name>-U59518874 -O78 -S5 -ZTIFSpeedSel1000 -A0 -C0 -JU1 -JI127.0.0.1 -JP0 -RST1 -N00("ARM CoreSight SW-DP") -D00(0BC11477) -L00(0) -TO18 -TC10000000 -TP21 -TDS8001 -TDT0 -TDC1F -TIEFFFFFFFF -TIP8 -TB1 -TFE0 -FO31 -FD20000000 -FC1000 -FN1 -FF0AC780x_128KB.FLM -FS08000000 -FL020000 -FP0($$Device:AC78013MDQA$Flash\AC780x_128KB.FLM)</Name>
         </SetRegEntry>
         <SetRegEntry>
           <Number>0</Number>

+ 1 - 1
Project/AirControlValve.uvprojx

@@ -49,7 +49,7 @@
             <InvalidFlash>1</InvalidFlash>
           </TargetStatus>
           <OutputDirectory>.\Objects\</OutputDirectory>
-          <OutputName>SmartSeaValve_20240816</OutputName>
+          <OutputName>SmartSeaValve_20241015</OutputName>
           <CreateExecutable>1</CreateExecutable>
           <CreateLib>0</CreateLib>
           <CreateHexFile>1</CreateHexFile>

Dosya farkı çok büyük olduğundan ihmal edildi
+ 89 - 5191
Project/JLinkLog.txt


+ 46 - 22
User/process.c

@@ -8,7 +8,8 @@
 uint16_t	g_blinkLedTime;		/*LED闪烁频率控制时间*/
 uint16_t	g_blinkLedTgtTime;	/*LED目标闪烁频率*/
 
-uint8_t		g_mr_Interval = 0;			/*  压力传感器更新周期  */
+uint16_t		g_mr_count = 0;			/*  压力传感器更新计数  */
+uint16_t		g_stateupdate_count = 0;			/*  状态更新计数  */
 uint16_t  g_print_interval = 0;   /* 打印周期 */
 
 #define STATUS_DETECTINTERVAL  (40)  /*60ms * 8 == 320ms 去抖动 */
@@ -21,22 +22,12 @@ uint8_t close_count =0;
 uint8_t open_count=0;
 
 
-static void update_state()
+static void update_state(void)
 {
 //		//判定阀开状态
-//	if(config->valvecolse_base <= 0){
-//		
-//		if(getHalldiff() > (config->valvecolse_base + config->threshold)){
-//				//g_state = STATUS_OPEN;
-//				if(open_count < 0xFF) open_count++;
-//				close_count=0;
-//		}else{
-//				//g_state = STATUS_CLOSE;
-//				open_count=0;
-//				if(close_count < 0xFF) close_count++;
-//		}
-//	
-//	}else{
+	if(config->valvecolse_base == 0){ // 未标定状态 
+		g_state = STATUS_UNKNOW;
+	}else{
 		
 		if(getHalldiff() >= (config->valvecolse_base + config->threshold)){
 			//g_state = STATUS_CLOSE;
@@ -48,20 +39,24 @@ static void update_state()
 			close_count=0;
 		}
 	
-//	}
+	}
 	
 	if(close_count >= 10){
 		g_state = STATUS_CLOSE;
 	}else if(open_count >= 10){
 		g_state = STATUS_OPEN;
 	}
+	
 		
 	if(STATUS_OPEN == g_state){
 		g_runstate = STATUS_OPEN;
 	}else if(STATUS_CLOSE == g_state){
 		g_runstate = STATUS_CLOSE;
+	}else {
+		g_runstate = STATUS_UNKNOW;
 	}
 	
+	
 	if(1 == g_exception){
 		g_runstate = STATUS_EXCEPTION;
 	}
@@ -74,16 +69,37 @@ void Process_Init(void)
 		g_blinkLedTime 	  = 0;
 		g_blinkLedTgtTime = BLINK_LED_DFTT;
 	
-		g_mr_Interval = 0;
+		g_mr_count = 0;
 		g_exception = 0;
 	
-		g_state = STATUS_CLOSE;
+		g_state = STATUS_UNKNOW;
 
 		update_state();
 	
 	  printf("process init \r\n");
 }
 
+void Process_Timer1_Callback(void)
+{
+		if(g_blinkLedTime < 0xFFFF)
+		{
+				g_blinkLedTime++;
+		}
+
+		if(g_mr_count < 0xFFFF){
+				g_mr_count++;
+		}
+		
+		if(g_stateupdate_count < 0xFFFF){
+			g_stateupdate_count++;
+		}
+		
+		if(g_print_interval < 0xFFFF){
+				g_print_interval++;
+		}
+		
+}
+
 void Process_RunPeriod(void)
 {
 	/*周期性地检查LED闪烁,LED2和LED3同时闪烁.*/
@@ -116,12 +132,20 @@ void Process_RunPeriod(void)
 	
 	
 	//压力传感器测量请求  
-	if(g_mr_Interval >= 50){
-		g_mr_Interval=0;
-		g_exception = PressureSensor_MR();
+	if(g_mr_count >= 500){
+		g_mr_count=0;
+		g_exception = PressureSensor_Read();
+		//PressureSensor_Read();
 		
-		update_state();
+		//update_state();
 		
+	}else if(g_mr_count == 480){
+		PressureSensor_MR();
+		//update_state();
+	}
+	
+	if(g_stateupdate_count >= 100){
+		 update_state();
 	}
 	
 	

+ 1 - 1
User/process.h

@@ -45,7 +45,7 @@ extern "C" {
 /*************<extern>*****************/	
 extern uint16_t		g_blinkLedTime;		/*LED闪烁频率控制时间*/
 extern uint16_t		g_blinkLedTgtTime;	/*LED目标闪烁频率*/
-extern uint8_t		g_mr_Interval;			/*  压力传感器更新周期  */
+extern uint16_t		g_mr_Interval;			/*  压力传感器更新周期  */
 extern uint16_t  	g_print_interval;   /* 打印周期 */
 
 

+ 2 - 2
User/protocol.c

@@ -6,9 +6,9 @@
 
 
 #ifdef IS_BOOTLOADER
-uint32_t Firmware_Version[4] = {1, 0, 0, 20240816};
+uint32_t Firmware_Version[4] = {1, 0, 0, 20241015};
 #else
-uint32_t Firmware_Version[4] = {1, 1, 1, 20240816};
+uint32_t Firmware_Version[4] = {1, 1, 2, 20241015};
 #endif