18 changed files with 837 additions and 659 deletions
-
3.vscode/settings.json
-
79APP/adc.c
-
28APP/adc.h
-
70APP/main.c
-
1APP/main.h
-
45APP/ozone_work.c
-
3APP/ozone_work.h
-
7APP/port.c
-
5APP/port.h
-
8APP/systick.c
-
1APP/systick.h
-
36PlatForm/irqhandler.c
-
BINdoc/~$项目需求.docx
-
BINdoc/项目需求.docx
-
1050project_ozone/Listings/project_o.map
-
103project_ozone/project_o.uvgui.admin
-
40project_ozone/project_o.uvopt
-
5project_ozone/project_o.uvproj
@ -0,0 +1,79 @@ |
|||||
|
#include "adc.h" |
||||
|
/*********************************************************************************************************************** |
||||
|
* =====================================================adc====================================================== |
||||
|
** |
||||
|
***********************************************************************************************************************/ |
||||
|
// uint16_t meanvalue; |
||||
|
adc_t adc={0}; |
||||
|
void ADCInit(void) { |
||||
|
ADC_InitStruType x; |
||||
|
GPIO_InitSettingType y; |
||||
|
|
||||
|
y.Signal = GPIO_Pin_Signal_Analog; //模拟 |
||||
|
y.Dir = GPIO_Direction_Input; //输入 |
||||
|
y.Func = GPIO_Reuse_Func0; |
||||
|
// GPIO_Init(GPIO_Pin_B9, &y); |
||||
|
GPIO_Init(GPIO_Pin_A2, &y); |
||||
|
|
||||
|
//经过分频以后Tadclk=1/(PCLK/4)约等于0.083us |
||||
|
x.CLKS = ADC_CLKS_PCLK; |
||||
|
x.CLKDIV = ADC_CLKDIV_1_4; /* ADC时钟源预分频 */ |
||||
|
|
||||
|
//采集到的模拟量*3.3/4096=现在的电压 |
||||
|
|
||||
|
x.VREF_SEL = ADC_VREF_SEL_0; /* 内部参考电压2.048v,设置内部参考电压为多少 */ |
||||
|
x.VREFP = ADC_VREFP_VDD; /* 选择芯片的工作电压VDD,adc的参考电压为多少 */ |
||||
|
x.VREFN = ADC_VREFN_VSS; /* 负向参考电压选择 */ |
||||
|
// x.CHS = ADC_CHS_AIN4; |
||||
|
x.CHS = ADC_CHS_AIN8; |
||||
|
x.SMPS = ADC_SMPS_SOFT; /* AD采样模式为软件控制 */ |
||||
|
//采样时间st*2+1(个Tadclk)=1.743us |
||||
|
x.ST = 10; /* AD采样时间选择 */ |
||||
|
x.BITSEL = ADC_BITSEL_12; /* AD分辨率12位 */ |
||||
|
ADC_Init(&x); |
||||
|
} |
||||
|
|
||||
|
uint16_t get_adc_value(void) { |
||||
|
uint16_t adc_value; |
||||
|
ADC_SoftStart(); |
||||
|
while (ADC_GetConvStatus() == SET) |
||||
|
; //正在转换 |
||||
|
adc_value = ADC_GetConvValue(); |
||||
|
ADC_SoftStop(); |
||||
|
return adc_value; |
||||
|
} |
||||
|
|
||||
|
void adc_loop_gather(void) { |
||||
|
static uint32_t adc_ticket = 0; |
||||
|
uint16_t adc_value = 0; |
||||
|
if (port_haspassedms(adc_ticket) > 500) { |
||||
|
adc_ticket = get_sys_ticket(); |
||||
|
adc_value = get_adc_value(); |
||||
|
adc.adc_value_buff[adc.adc_value_count++] = adc_value; |
||||
|
if (adc.adc_value_count == 5) { |
||||
|
adc.past_count_reached_five = true; |
||||
|
adc.adc_value_count = 0; |
||||
|
} |
||||
|
if (adc.past_count_reached_five == true) { |
||||
|
beg_average_value(adc.adc_value_buff); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
void beg_average_value(uint16_t *adc_buff) { |
||||
|
uint16_t adc_average_value = 0; |
||||
|
uint32_t adc_sum_value = 0; |
||||
|
for (int i = 0; i < 5; i++) { |
||||
|
adc_sum_value += *adc_buff; |
||||
|
adc_buff++; |
||||
|
} |
||||
|
adc_average_value = adc_sum_value / 5; |
||||
|
if (adc_average_value <= 16 || adc.abnormal_state == true) { |
||||
|
adc.abnormal_state = true; |
||||
|
process_voltage_abnormal(); |
||||
|
printf("short out%d\r\n", adc_average_value); //短路 |
||||
|
} else { |
||||
|
printf("adc_value_t%d\r\n", |
||||
|
adc_average_value); //例:adc_value=1700则电压=1700/4096=1.369 |
||||
|
} |
||||
|
} |
@ -0,0 +1,28 @@ |
|||||
|
#ifndef _ADC_H_ |
||||
|
#define _ADC_H_ |
||||
|
|
||||
|
#include "port.h" |
||||
|
#include "systick.h" |
||||
|
#include "ozone_work.h" |
||||
|
void ADCInit(void); |
||||
|
uint16_t get_adc_value(void); |
||||
|
void adc_loop_gather(void); |
||||
|
void beg_average_value(uint16_t *adc_buff); |
||||
|
|
||||
|
|
||||
|
typedef struct { |
||||
|
bool abnormal_state; |
||||
|
bool past_count_reached_five; |
||||
|
bool first_detection_abnormal; |
||||
|
uint16_t adc_value_buff[5]; |
||||
|
uint8_t adc_value_count; |
||||
|
}adc_t; |
||||
|
|
||||
|
// typedef struct { |
||||
|
// uint16_t adc_value_buff[5]; |
||||
|
// uint8_t adc_value_count; |
||||
|
// bool abnormal_state; |
||||
|
// bool past_count_reached_five; //Ïû¶¶Ê¹Óà |
||||
|
// } adc_t; |
||||
|
|
||||
|
#endif |
1050
project_ozone/Listings/project_o.map
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
103
project_ozone/project_o.uvgui.admin
File diff suppressed because it is too large
View File
File diff suppressed because it is too large
View File
Write
Preview
Loading…
Cancel
Save
Reference in new issue