微波EDA网,见证研发工程师的成长!
首页 > 硬件设计 > 嵌入式设计 > STM32内部温度传感器实验——串口 delphi上位机

STM32内部温度传感器实验——串口 delphi上位机

时间:11-13 来源:互联网 点击:
家里气温15度 我对着芯片使劲哈气 把他哈到了 20度



下位机关键代码
int main(void)
{
double v,temp,Value;

/* System clocks configuration ---------------------------------------------*/
RCC_Configuration();

/* GPIO configuration ------------------------------------------------------*/
GPIO_Configuration();

/* USARTx configured as follow:
- BaudRate = 9600 baud
- Word Length = 8 Bits
- One Stop Bit
- No parity
- Hardware flow control disabled (RTS and CTS signals)
- Receive and transmit enabled
*/
USART_InitStructure.USART_BaudRate = 9600;
USART_InitStructure.USART_WordLength = USART_WordLength_8b;
USART_InitStructure.USART_StopBits = USART_StopBits_1;
USART_InitStructure.USART_Parity = USART_Parity_No;
USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
USART_InitStructure.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;

USART_Init(USART1, &USART_InitStructure);
/* Enable USART1 */
USART_Cmd(USART1, ENABLE);

/* DMA1 channel1 configuration ----------------------------------------------*/
DMA_DeInit(DMA1_Channel1);
DMA_InitStructure.DMA_PeripheralBaseAddr = ADC1_DR_Address;
DMA_InitStructure.DMA_MemoryBaseAddr = (uint32_t)&ADCConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralSRC;
DMA_InitStructure.DMA_BufferSize = 1;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Disable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_M2M = DMA_M2M_Disable;
DMA_Init(DMA1_Channel1, &DMA_InitStructure);

/* Enable DMA1 channel 1 */
DMA_Cmd(DMA1_Channel1, ENABLE);

/* ADC1 configuration ------------------------------------------------------*/
ADC_InitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConv = ADC_ExternalTrigConv_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfChannel = 1;
ADC_Init(ADC1, &ADC_InitStructure);

/* ADC1 regular channel 14 configuration */
//################改为 Channel 10(电位器)###### 内部温度传感器 改为 Channel 16 ###################
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_55Cycles5);
//内部温度传感器 添加这一句
/* Enable the temperature sensor and vref internal channel */
ADC_TempSensorVrefintCmd(ENABLE);

/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);

/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);

/* Enable ADC1 reset calibaration register */ //使用之前一定要校准
ADC_ResetCalibration(ADC1);
/* Check the end of ADC1 reset calibration register */
while(ADC_GetResetCalibrationStatus(ADC1));

/* Start ADC1 calibaration */
ADC_StartCalibration(ADC1);
/* Check the end of ADC1 calibration */
while(ADC_GetCalibrationStatus(ADC1));

/* Start ADC1 Software Conversion */
ADC_SoftwareStartConvCmd(ADC1, ENABLE);

while (1)
{

//printf("The ADC1 Converted Value is %d \r\n",ADCConvertedValue);
//v= ADCConvertedValue*3.30/4096;
//printf("The Voltage is %f \r\n",v );

Value=ADC_Filter();
//printf("The ADC_Filter Value is %d \r\n",(int)Value);

temp=(1.42 - Value*3.3/4096)*1000/4.35 + 25;
//printf("The Temperature is %f \r\n",temp );
printf("%s%c%c%c%c%c%s","#**",(int)Value/256,(int)Value%256,&,(int)((temp-(int)temp)*100),(int)temp,"**%");

for(v=0;v<5000;v++);for(v=0;v<5000;v++);for(v=0;v<5000;v++);
}
}
-------------------------------------------------------------------
上位机关键代码
procedure TMainForm.Comm1ReceiveData(Sender: TObject; Buffer: Pointer;
BufferLength: Word);
var
//iq :Integer;
p :Pchar;

begin
viewstring:= ;

move(buffer^,pchar(@rbuf)^,bufferlength);

p := StrPos(pchar(@rbuf), #**);

//if (p <> nil) and (p[9]=*) and ( p[10]=*) and (p[11]=%) then //
if (p <> nil) and (p[5]=&) and (p[8]=*) and ( p[9]=*) and (p[10]=%) then
begin

{
printf("%s%c%c%c%c%c%s",
"#**",
(int)Value/256,(int)Value%256,
&,
(int)((temp-(int)temp)*100),(int)temp,
"**%");
}

Value :=Ord(p[3])*256 + Ord(p[4]) ; //Ord() 字符-->ASCII码

//Temp:= Float( Ord(p[7]) ) +Float( Ord(p[6]) ) /Float(100.0) ;
Temp:=(1.42 - (Value)*3.3/4096)*1000/4.35 + 25;

viewstring:=The ADC_Filter Value is:;
viewstring:=viewstring+IntToStr(Value);
viewstring:=viewstring+ +The Temperature is:;
viewstring:=viewstring+FloatToStr(Temp);

memo1.lines.add(viewstring);
memo1.lines.add( );

Rz

Copyright © 2017-2020 微波EDA网 版权所有

网站地图

Top