Hardware circuit
LM393 voltage comparator pin follows
a photoresistor
analog acquisition stm32 on the use of the ADC 12 acquisition channels to capture accuracy, seen from the resource distribution expansion board:
Program
//ADC -AO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_2MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AIN;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//ADC -DO
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_Init(GPIOA, &GPIO_InitStructure);
void ADC_Configuration(void)
{
ADC_InitTypeDef ADC_InitStructure;
// ADC1 工作模式配置
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = DISABLE;
ADC_InitStructure.ADC_ContinuousConvMode = DISABLE; //单次转换 两者的区别在于连续转换直到所有的数据转换完成后才停止转换,而单次转换则只转换一次数据就停止,要再次触发转换才可以
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);
ADC_Cmd(ADC1, ENABLE);
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));
}
u16 Read_ADC(void)
{
u16 temp;
ADC_RegularChannelConfig(ADC1, ADC_Channel_4, 1, ADC_SampleTime_1Cycles5); //ADC_AO对应的是通道4
ADC_SoftwareStartConvCmd(ADC1,ENABLE);
while(ADC_GetFlagStatus(ADC1, ADC_FLAG_EOC) == RESET);
temp = ADC_GetConversionValue(ADC1);
ADC_SoftwareStartConvCmd(ADC1,DISABLE);
return temp;
}
The main function
1. The digital output DO: switch signal output high and low level values and photosensitive resistor divider potentiometer divider value output by the comparator.
2. The analog output AO: analog output value of the photoresistor voltage dividing resistors and the value
while(1)
{
temp_AO = Read_ADC();
sprintf((char *)str, " R-P:%.1fK ", temp_AO/(4096.-temp_AO)*10);
LCD_DisplayStringLine(Line6, str);
if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_3) == 1)
{
LCD_DisplayStringLine(Line7, (u8*)" DO:High ");
}
else
{
LCD_DisplayStringLine(Line7, (u8*)" DO:Low ");
}
Note:
Because RP7 potentiometer resistance R45 and the resistance difference of 500 times, and the resistor R46 and the light resistance may vary within 1-50 times, the following phenomena can be obtained:
1. When such adjustment potentiometer RP7 DO output hign time: let photoresistor strong light source, according to the light-sensitive resistor is reduced, so that the DO output Low (adjusting the jump value is preferably just enough to RP7 can be output at hign photoresistor normally, if the potential value is too small, then the resistance may be encountered as a photosensitizing light is still greater than the resistance value of the potentiometer)
2. When the adjustment potentiometer RP7 so low when the output DO: Since the same voltage is input to the inverted input and the value is relatively large, this time may be large regardless of the photoresistor, which is still the same ratio to a voltage input terminal of the inverting input terminal voltage is small, DO output has been so low