新聞中心

EEPW首頁 > 嵌入式系統(tǒng) > 設(shè)計(jì)應(yīng)用 > STM32內(nèi)部溫度傳感器實(shí)驗(yàn)——串口 delphi上位機(jī)

STM32內(nèi)部溫度傳感器實(shí)驗(yàn)——串口 delphi上位機(jī)

作者: 時(shí)間:2016-11-13 來源:網(wǎng)絡(luò) 收藏
家里氣溫15度 我對(duì)著芯片使勁哈氣 把他哈到了 20度



下位機(jī)關(guān)鍵代碼
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(電位器)###### 內(nèi)部溫度傳感器 改為 Channel 16 ###################
ADC_RegularChannelConfig(ADC1, ADC_Channel_16, 1, ADC_SampleTime_55Cycles5);
//內(nèi)部溫度傳感器 添加這一句
/* 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 */ //使用之前一定要校準(zhǔn)
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 rn",ADCConvertedValue);
//v= ADCConvertedValue*3.30/4096;
//printf("The Voltage is %f rn",v );


Value=ADC_Filter();
//printf("The ADC_Filter Value is %d rn",(int)Value);

temp=(1.42 - Value*3.3/4096)*1000/4.35 + 25;
//printf("The Temperature is %f rn",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++);
}
}
-------------------------------------------------------------------
上位機(jī)關(guān)鍵代碼
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( );

RzLEDDisplay.Caption := inttostr(Value)+ ;
Temp:=int(Temp*1000)/1000;
RzLEDDisplay1.Caption := floattostr(Temp);

end;
end;


評(píng)論


技術(shù)專區(qū)

關(guān)閉