267 lines
6.9 KiB
C
267 lines
6.9 KiB
C
|
#include "device_adc_collect.h"
|
|||
|
#include <includes.h>
|
|||
|
#include "gd32f4xx.h"
|
|||
|
#include "comm_types.h"
|
|||
|
#include "ptz_struct.h"
|
|||
|
#include "tmp75.h"
|
|||
|
#include "w25q128.h"
|
|||
|
#include "agent_hyt.h"
|
|||
|
#include "ptz_type_select.h"
|
|||
|
#include "pdebug.h"
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹ
|
|||
|
void ptz_send_voltage(char dev)
|
|||
|
{
|
|||
|
unsigned short int vol = 0;
|
|||
|
unsigned char VData[7]= {0xff,0x00,0x00,0x00,0x00,0x00,0x00};
|
|||
|
vol = ( unsigned short int)(g_ptz.Voltage * 100 + 0.5);
|
|||
|
VData[1] = g_ptz.address;
|
|||
|
VData[2] = 0xcd;
|
|||
|
VData[3] = (unsigned char)(vol >> 8);
|
|||
|
VData[4] = (unsigned char)(vol & 0x00ff);
|
|||
|
VData[6] = MotorCalPelcoDSUM(VData,sizeof(VData));
|
|||
|
ptz_send_data(dev, VData, sizeof(VData));
|
|||
|
//<2F><><EFBFBD>͵<EFBFBD><CDB5><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨<EFBFBD><CCA8><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
void ptz_send_current(char dev)
|
|||
|
{
|
|||
|
unsigned short int current = 0;
|
|||
|
unsigned char IData[7]= {0xff,0x00,0x00,0x00,0x00,0x00,0x00};
|
|||
|
current = (unsigned short int)(g_ptz.electric_current * 100 + 0.5);
|
|||
|
IData[1] = g_ptz.address;
|
|||
|
IData[2] = 0xc8;
|
|||
|
IData[3] = (unsigned char)(current >> 8);
|
|||
|
IData[4] = (unsigned char)(current & 0x00ff);
|
|||
|
IData[6] = MotorCalPelcoDSUM(IData,sizeof(IData));
|
|||
|
|
|||
|
ptz_send_data(dev, IData, sizeof(IData));
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>̨<EFBFBD>¶<EFBFBD>
|
|||
|
void ptz_send_temperature(char dev)
|
|||
|
{
|
|||
|
unsigned short int tem = 0;
|
|||
|
unsigned char TData[7]= {0xff,0x00,0x00,0x00,0x00,0x00,0x00};
|
|||
|
tem = (unsigned short int)(g_ptz.temperature * 100 + 0.5);
|
|||
|
TData[1] = g_ptz.address;
|
|||
|
TData[2] = 0xd6;
|
|||
|
TData[3] = (unsigned char)(tem >> 8);
|
|||
|
TData[4] = (unsigned char)(tem & 0x00ff);
|
|||
|
TData[6] = MotorCalPelcoDSUM(TData,sizeof(TData));
|
|||
|
|
|||
|
ptz_send_data(dev,TData,sizeof(TData));
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
void ADC_Init()
|
|||
|
{
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>
|
|||
|
rcu_periph_clock_enable(RCU_GPIOC);
|
|||
|
//<2F><><EFBFBD>Ÿ<EFBFBD><C5B8>ã<EFBFBD>PC2<43><32><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ѹADC1-CH12<31><32>PC3<43><33><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ADC1-CH13
|
|||
|
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_2);
|
|||
|
gpio_mode_set(GPIOC,GPIO_MODE_ANALOG,GPIO_PUPD_NONE,GPIO_PIN_3);
|
|||
|
|
|||
|
rcu_periph_clock_enable(RCU_ADC1);
|
|||
|
//<2F><><EFBFBD><EFBFBD>ʱ<EFBFBD><CAB1>20<32><30>Ƶ=6MHZ
|
|||
|
adc_clock_config(ADC_ADCCK_PCLK2_DIV4);
|
|||
|
/***********************************************/
|
|||
|
|
|||
|
//<2F><><EFBFBD>뷽ʽ ADC_INSERTED_CHANNEL
|
|||
|
adc_data_alignment_config(ADC1,ADC_DATAALIGN_RIGHT);
|
|||
|
//ɨ<><C9A8>ģʽ
|
|||
|
adc_special_function_config(ADC1,ADC_SCAN_MODE,ENABLE);
|
|||
|
//<2F>ⲿ<EFBFBD><E2B2BF><EFBFBD><EFBFBD><EFBFBD>ر<EFBFBD>
|
|||
|
adc_external_trigger_config(ADC1,ADC_INSERTED_CHANNEL,DISABLE);
|
|||
|
//<2F>ɼ<EFBFBD>ͨ<EFBFBD><CDA8><EFBFBD><EFBFBD>
|
|||
|
adc_channel_length_config(ADC1,ADC_INSERTED_CHANNEL,2);
|
|||
|
|
|||
|
//ͨ<><CDA8>ע<EFBFBD><D7A2>˳<EFBFBD><CBB3>
|
|||
|
adc_inserted_channel_config(ADC1,0,ADC_CHANNEL_13,ADC_SAMPLETIME_480);//<2F><><EFBFBD><EFBFBD>
|
|||
|
adc_inserted_channel_config(ADC1,1,ADC_CHANNEL_12,ADC_SAMPLETIME_480);//<2F><>ѹ
|
|||
|
|
|||
|
|
|||
|
adc_enable(ADC1);
|
|||
|
|
|||
|
adc_calibration_enable(ADC1);
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//<2F><>ѹ<EFBFBD>ɼ<EFBFBD>
|
|||
|
static void ptz_Voltage_collect_adc1_task()
|
|||
|
{
|
|||
|
static float adc1_v[5];
|
|||
|
static unsigned char adc1_num;
|
|||
|
int j,k;
|
|||
|
float tem;
|
|||
|
float curadc1;
|
|||
|
uint16_t value_V;
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʹ<EFBFBD><CAB9>
|
|||
|
adc_software_trigger_enable(ADC1,ADC_INSERTED_CHANNEL);
|
|||
|
value_V = ADC_IDATA1(ADC1);
|
|||
|
|
|||
|
adc1_v[adc1_num] = (float)value_V / 4096.0 * 11 *3.3;//(float)value_V;//
|
|||
|
adc1_num++;
|
|||
|
if(adc1_num >= 5)
|
|||
|
{
|
|||
|
adc1_num = 0;
|
|||
|
for(j = 0; j < 5-1; j++)//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
for(k = 0; k < 5-j-1; k++)
|
|||
|
{
|
|||
|
if(adc1_v[k] > adc1_v[k+1])
|
|||
|
{
|
|||
|
tem = adc1_v[k];
|
|||
|
adc1_v[k] = adc1_v[k+1];
|
|||
|
adc1_v[k+1] = tem;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
for(uint8_t i = 1; i < 5 - 1; i++)
|
|||
|
{
|
|||
|
curadc1 = curadc1 + adc1_v[i];
|
|||
|
}
|
|||
|
curadc1 = curadc1 /((float)(5 - 2));//ȥ<><C8A5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Сֵ<D0A1><D6B5>ƽ<EFBFBD><C6BD>ֵ
|
|||
|
g_ptz.Voltage = curadc1;
|
|||
|
memset(adc1_v,0,sizeof(adc1_v));
|
|||
|
// pdebug(DEBUG_LEVEL_INFO,"get ptz Voltage: %.3fV",g_ptz.Voltage);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//<2F><><EFBFBD><EFBFBD><EFBFBD>ɼ<EFBFBD>
|
|||
|
static void ptz_Current_collect_adc1_task()
|
|||
|
{
|
|||
|
static float adc1_i[5];
|
|||
|
static unsigned char adc0_num;
|
|||
|
int j,k;
|
|||
|
float tem;
|
|||
|
float curadc0;
|
|||
|
uint16_t value_I;
|
|||
|
|
|||
|
adc_software_trigger_enable(ADC1,ADC_INSERTED_CHANNEL);
|
|||
|
value_I = ADC_IDATA0(ADC1);
|
|||
|
|
|||
|
adc1_i[adc0_num] = (((float)value_I / 4096.0 * 3.3)-3.3/2) / 0.132;//(float)value_I;//
|
|||
|
|
|||
|
adc0_num ++;
|
|||
|
if(adc0_num >= 5)
|
|||
|
{
|
|||
|
adc0_num = 0;
|
|||
|
for(j = 0; j < 5-1; j++)//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
for(k = 0; k < 5-j-1; k++)
|
|||
|
{
|
|||
|
if(adc1_i[k] > adc1_i[k+1])
|
|||
|
{
|
|||
|
tem = adc1_i[k];
|
|||
|
adc1_i[k] = adc1_i[k+1];
|
|||
|
adc1_i[k+1] = tem;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
for(uint8_t i = 1; i < 5 - 1; i++)
|
|||
|
{
|
|||
|
curadc0 = curadc0 + adc1_i[i];
|
|||
|
}
|
|||
|
curadc0 = curadc0 /((float)(5 - 2));//ȥ<><C8A5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Сֵ<D0A1><D6B5>ƽ<EFBFBD><C6BD>ֵ
|
|||
|
g_ptz.electric_current = curadc0;
|
|||
|
memset(adc1_i,0,sizeof(adc1_i));
|
|||
|
// pdebug(DEBUG_LEVEL_INFO,"get ptz Electric_Current: %.3fA",g_ptz.electric_current);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
//<2F>¶Ȳɼ<C8B2>
|
|||
|
static void ptz_temperature_collect_tmp75_task()
|
|||
|
{
|
|||
|
static float tmp75[5];
|
|||
|
static unsigned char tmp75_num;
|
|||
|
float curtmp75;
|
|||
|
float tem;
|
|||
|
int j,k;
|
|||
|
tmp75[tmp75_num] = tmp75_read_temp();
|
|||
|
tmp75_num ++;
|
|||
|
if(tmp75_num >= 5)
|
|||
|
{
|
|||
|
tmp75_num = 0;
|
|||
|
for(j = 0; j < 5-1; j++)//<2F><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>С<EFBFBD><D0A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
{
|
|||
|
for(k = 0; k < 5-j-1; k++)
|
|||
|
{
|
|||
|
if(tmp75[k] > tmp75[k+1])
|
|||
|
{
|
|||
|
tem = tmp75[k];
|
|||
|
tmp75[k] = tmp75[k+1];
|
|||
|
tmp75[k+1] = tem;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
for(uint8_t i = 1; i < 5 - 1; i++)
|
|||
|
{
|
|||
|
curtmp75 = curtmp75 + tmp75[i];//ȥ<><C8A5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>һ<EFBFBD><D2BB><EFBFBD><EFBFBD>Сֵ
|
|||
|
}
|
|||
|
curtmp75 = curtmp75 / ((float)(5 - 2));
|
|||
|
g_ptz.temperature = curtmp75;
|
|||
|
memset(tmp75,0,sizeof(tmp75));
|
|||
|
// pdebug(DEBUG_LEVEL_INFO,"get ptz Temperature: %.3f<EFBFBD><EFBFBD>",g_ptz.temperature);
|
|||
|
}
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
//<2F>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
static char ptz_data_collect_task()
|
|||
|
{
|
|||
|
int i=0;
|
|||
|
while(1)
|
|||
|
{
|
|||
|
//<2F><>̨<EFBFBD><CCA8><EFBFBD>Լ<EFBFBD><D4BC>رգ<D8B1><D5A3>ɼ<F2BFAAB2><C9BC><EFBFBD><EFBFBD><EFBFBD>
|
|||
|
#ifndef PTZ_NO_SELF_CHECK
|
|||
|
ptz_Voltage_collect_adc1_task();
|
|||
|
#endif
|
|||
|
OSTimeDlyHMSM(0u, 0u, 0u, 50u);
|
|||
|
ptz_Current_collect_adc1_task();
|
|||
|
OSTimeDlyHMSM(0u, 0u, 0u, 50u);
|
|||
|
if(i >= 30)
|
|||
|
{//<2F><><EFBFBD><EFBFBD><EFBFBD>¶Ȳɼ<C8B2>Ƶ<EFBFBD><C6B5>
|
|||
|
ptz_temperature_collect_tmp75_task();
|
|||
|
i=0;
|
|||
|
}
|
|||
|
i++;
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
|
|||
|
|
|||
|
static OS_STK task_data_collect_stk[TASK_PTZ_DATA_COLLECT_STK_SIZE];
|
|||
|
static void creat_task_data_collect(void)
|
|||
|
{
|
|||
|
CPU_INT08U task_err;
|
|||
|
CPU_INT08U name_err;
|
|||
|
|
|||
|
task_err = OSTaskCreateExt((void (*)(void *)) ptz_data_collect_task,
|
|||
|
(void *) 0,
|
|||
|
(OS_STK *)&task_data_collect_stk[TASK_PTZ_DATA_COLLECT_STK_SIZE - 1],
|
|||
|
(INT8U ) TASK_PTZ_DATA_COLLECT_PRIO,
|
|||
|
(INT16U ) TASK_PTZ_DATA_COLLECT_PRIO,
|
|||
|
(OS_STK *)&task_data_collect_stk[0],
|
|||
|
(INT32U ) TASK_PTZ_DATA_COLLECT_STK_SIZE,
|
|||
|
(void *) 0,
|
|||
|
(INT16U )(OS_TASK_OPT_STK_CHK | OS_TASK_OPT_STK_CLR));
|
|||
|
#if (OS_TASK_NAME_EN > 0)
|
|||
|
OSTaskNameSet(TASK_PTZ_DATA_COLLECT_PRIO, "ptz_data_collect_task", &name_err);
|
|||
|
#endif
|
|||
|
|
|||
|
}
|
|||
|
|
|||
|
|
|||
|
void init_data_collect_module(void)
|
|||
|
{
|
|||
|
ADC_Init();//AD<41>ɼ<EFBFBD><C9BC><EFBFBD><EFBFBD>ų<EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
temp75_gpio_init();//<2F>¶ȴ<C2B6><C8B4><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʼ<EFBFBD><CABC>
|
|||
|
creat_task_data_collect();
|
|||
|
}
|
|||
|
|