STM32F103控制ds18b20溫度采集,帶電腦端上位機源碼。
vs源碼與上位機的執行文件都有
0.png (37.06 KB, 下載次數: 68)
下載附件
2018-3-23 21:12 上傳
0.png (37.88 KB, 下載次數: 51)
下載附件
2018-3-23 21:11 上傳
stm32單片機下位機源程序如下:
- /*************************************************
- Template:遠程傳輸設備
- author:ncy
- date:2017-6-26
- complete data:2017-6-27
- **************************************************/
- #include "sys.h"
- #include "delay.h"
- #include "led.h"
- #include "rtc.h"
- #include "time.h"
- #include "stmflash.h"
- #include "exti.h"
- #include "adc.h"
- #include "key.h"
- #include "timer.h"
- #include "ds18b20.h"
- #include "DS18B20_2.h"
- #include "24cxx.h"
- #include "usart.h"
- #include "mlx90614.h"
- #define Write_Base_Address 0x0807F800//保存參數
- u8 Set_Cal_Time=10;
- u16 data_cnt=0x00;
- u8 Send_Led_Flag=0x00;
- u8 Recv_Led_Flag=0x00;
- float tm_mlx=0.0;
- int main(void)
- {
- float AD_values;
- u16 year;
- u16 write_address=50;//start address 讀寫的基地址
- u8 month,day,hour,min,sec;
- u8 time_buf[8];
- u8 data_buf[10];
- u8 tm1_buf[4],tm2_buf[4];
- u8 ad_buf[6];
- u8 write_buf[41];//暫時100個字
- u8 read_buf[40];
- u8 i=0;
- u8 set_time_ok[2]={0x4F,0x4B};
- u8 set_cj_ok[2]={0x1c,0xc1};
- float tm1,tm2;
- delay_init();
- LED_Init();
- RTC_Init();
- uart_init(115200);
- TIM2_Int_Init(999,7199);//定時器2初始化
- TIM3_Int_Init(999,7199);
- TIM4_Int_Init(999,7199);
- EXTIX_Init();
- Adc_Init();
- DS18B20_Init();
- DS18B20_2_Init();
- AT24CXX_Init();
- SMBus_Init();
- while(AT24CXX_Check())//cheak 24c02 is valid
- {
- //AT24C02 IS NOT
- }
- //讀取參數配置信息
- //1.當前配置的定時器讀取時間參數
- AT24CXX_WriteOneByte(0x00,data_cnt);
- data_cnt=AT24CXX_ReadOneByte(0X00);
- Set_Cal_Time=AT24CXX_ReadOneByte(0X01);
- //當前存取的條目數字
- //record
-
- while(1)
- {
- /****************設置定時時長和日期******************************************************************/
-
- if(USART_RX_STA&0X4000)
- {
- if(USART_RX_STA&0X1000)
- {
- Set_Cal_Time=USART_RX_BUF[2];
- USART1_SendNums(set_cj_ok,2);
- AT24CXX_WriteOneByte(0x01,Set_Cal_Time);
- }
- if(USART_RX_STA&0X0400)
- {
- //設置的格式
- u16 year;
- u8 month,day,hour,min,sec,temp;
- year=2000+10*(USART_RX_BUF[2]/16)+USART_RX_BUF[2]%16;
- month=10*(USART_RX_BUF[3]/16)+USART_RX_BUF[3]%16;
- day=10*(USART_RX_BUF[4]/16)+USART_RX_BUF[4]%16;
- hour=10*(USART_RX_BUF[5]/16)+USART_RX_BUF[5]%16;
- min=10*(USART_RX_BUF[6]/16)+USART_RX_BUF[6]%16;
- sec=10*(USART_RX_BUF[7]/16)+USART_RX_BUF[7]%16;
- RTC_Set(year,month,day,hour,min,sec);//set local time
- USART1_SendNums(set_time_ok,2);
- }
- count=0;
- USART_RX_STA=0;
- }
- /*****************獲取各個物理量*************************************************************/
- tm1=DS18B20_Get_Temp(); //get tm1
-
- tm_mlx=SMBus_ReadTemp();
- //讀取ADvalues
- AD_values=Get_Adc_Average(0x01,10);
-
- year=calendar.w_year; //
- month=calendar.w_month;
- day=calendar.w_date;
- //AD values
- ad_buf[0]='0';//暫時不上傳
- ad_buf[1]='.';
- ad_buf[2]=(u8)AD_values*10%10+'0';
- ad_buf[3]=(u8)AD_values*100%10+'0';
- ad_buf[4]=(u8)AD_values*1000%10+'0';
- ad_buf[5]=(u8)AD_values*10000%10+'0';
- //報文格式
- write_buf[0]=0xee;
- for(i=0;i<6;i++)
- {
- write_buf[1+i]=ad_buf[i];
- }
-
- write_buf[7]=0x20;
- //tm1
- tm1_buf[0]=(u8)tm1/10+'0';
- tm1_buf[1]=(u8)tm1%10+'0';
- tm1_buf[2]='.';
- tm1_buf[3]=(u8)tm1*10%10+'0';
- for(i=0;i<4;i++)
- {
- write_buf[8+i]=tm1_buf[i];
- }
- write_buf[12]=0x20;
- //tm2
- tm2_buf[0]=(u8)tm_mlx/10+'0';
- tm2_buf[1]=(u8)tm_mlx%10+'0';
- tm2_buf[2]='.';
- tm2_buf[3]=(u8)(tm_mlx*10)%10+'0';
- for(i=0;i<4;i++)
- {
- write_buf[13+i]=tm2_buf[i];
- }
- write_buf[17]=0x20;
-
- //date
- data_buf[0]=year/1000+'0';
- data_buf[1]=year/100%10+'0';
- data_buf[2]=year/10%10+'0';
- data_buf[3]=year%10+'0';
- data_buf[4]='-';
- data_buf[5]=month/10+'0';
- data_buf[6]=month%10+'0';
- data_buf[7]='-';
- data_buf[8]=day/10+'0';
- data_buf[9]=day%10+'0';
- for(i=0;i<10;i++)
- {
- write_buf[18+i]=data_buf[i];
- }
- write_buf[28]=0x20;
- //time
- hour=calendar.hour;
- min=calendar.min;
- sec=calendar.sec;
- time_buf[0]=hour/10+'0';
- time_buf[1]=hour%10+'0';
- time_buf[2]=':';
- time_buf[3]=min/10+'0';
- time_buf[4]=min%10+'0';
- time_buf[5]=':';
- time_buf[6]=sec/10+'0';
- time_buf[7]=sec%10+'0';
- for(i=0;i<8;i++)
- {
- write_buf[29+i]=time_buf[i];
- }
- write_buf[37]=0xff;
- write_buf[38]=0xfc;
- write_buf[39]=0xff;
- write_buf[40]=0xff;
- //USART1_SendNums(write_buf,41);//發送偏移的計算
- if(read_flag==1&&write_flag==0)
- {
- TIM_Cmd(TIM3, DISABLE);//OPEN
- TIM_Cmd(TIM4, ENABLE);//OPEN
- if((time4_flag==1)&&(data_cnt>0))
- {
- data_cnt--;
- AT24CXX_Read((data_cnt*41+write_address),(u8*)read_buf,41);//讀取read_buf
- USART1_SendNums(read_buf,41);//發送偏移的計算
- AT24CXX_WriteOneByte(0x00,data_cnt);//write one byte to 24Cxx
- time4_flag=0;
- Recv_Led_Flag=0X00;
- Send_Led_Flag=0x01;
- }
- if(data_cnt==0)
- {
- Send_Led_Flag=0x00;
- TIM_Cmd(TIM4,DISABLE);//定時器3close
- }
- }
- if(read_flag==0&&write_flag==1)
- {
- TIM_Cmd(TIM3, ENABLE); //OPEN
- TIM_Cmd(TIM4, DISABLE); //OPEN
- if(time3_flag==1)
- {
- AT24CXX_Write((write_address+data_cnt*41),(u8*)write_buf,41);
- data_cnt++;
- if(data_cnt==100)//存儲了100條,表示空間已滿
- {
- data_cnt=0;//歸位
- }
- //保存基地址
- AT24CXX_WriteOneByte(0x00,data_cnt);//write one byte to 24Cxx
- Send_Led_Flag=0x00;
- Recv_Led_Flag=0X01;
- time3_flag=0;
- }
- }
- }
- }
-
復制代碼
所有資料51hei提供下載:
遠程溫濕度采集.rar
(2.5 MB, 下載次數: 146)
2018-3-23 21:16 上傳
點擊文件名下載附件
STM32F103 下載積分: 黑幣 -5
|