[$is_have_disable_EA] 1 [$is_need_EA_all_open] 0 [$is_need_EA_all_open1_paichu_uids] [$is_event_jimu] 0 [$group_e] 元件事件库.18b20模块 [$group] 元件库-传感器.$pname$ [$last_ver_update_time] 2021-04-03 [$min_need_ver] 1.23 [$p_list] [$short_show_UIname] [$code_area_type] codes,exe_per_1ms,init_code [$blocks_type] line [$name] 18b20模块的初始化积木 [$引脚占用列表,用于提示冲突] [($_xgm$data1),type=pin]|18b20引脚 [$寄存器占用列表,用于提示冲突,但=>后的值没变则不提示冲突] [$中断函数号占用列表,用于提示冲突] [$interrupt_1] flag_$rnd_varname_pre$=1; [$thread_do] [$cs] [$function_ref_define_in_headfile] @@file=18b20.h extern float ret_value_$rnd_varname_pre$; char DS18B20_Get_Temp_$rnd_varname_pre$(struct pt *process_pt); u8 DS18B20_Init_$rnd_varname_pre$(void); [$init_code_for_define_area_once] @@file=18b20.c #include "protothread.h" #include "app_process.h" #include "sys/timer.h" #include "Extline_Lib.h" #include "Lib_base.h" //考虑PROCESS2_WAIT_UNTIL宏 [$init_code_for_define_area] @@file=18b20.c #define uint8 u8 #define uint16 u16 #define uint32 u32 void DS18B20_IO_OUT_$rnd_varname_pre$(){ #get_P口_to_PxM1_PxM0_set_code_without_init(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$data1),type=pin]","1","18b20信号输入")% //1=推挽 } void DS18B20_IO_IN_$rnd_varname_pre$(){ #get_P口_to_PxM1_PxM0_set_code_without_init(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$data1),type=pin]","0","18b20信号输入")% //0=双向 } struct timer timer_csb2_$rnd_varname_pre$; void delay_us_$rnd_varname_pre$(u16 time) { if((time/10)>0){ timer_set2(&timer_csb2_$rnd_varname_pre$, time/10 ); }else{ timer_set2(&timer_csb2_$rnd_varname_pre$, 1 ); } timer_restart2(&timer_csb2_$rnd_varname_pre$); while(timer_expired2(&timer_csb2_$rnd_varname_pre$)==0){;} return; u16 i=0; while(time--) { i=(#get_10us_delay_num_stm32f10x(group_xml,rand1,block_name,ref_complier,extinfo)% / 2 ); //自己定义 while(i--) ; } } //复位DS18B20 void DS18B20_Rst_$rnd_varname_pre$(void) { DS18B20_IO_OUT_$rnd_varname_pre$(); //SET PG11 OUTPUT IO_Write([($_xgm$data1),type=pin],0); //拉低DQ delay_us_$rnd_varname_pre$(750); //拉低750us IO_Write([($_xgm$data1),type=pin],1); //DQ=1 delay_us_$rnd_varname_pre$(44); //15US } //等待DS18B20的回应 //返回1:未检测到DS18B20的存在 //返回0:存在 u8 DS18B20_Check_$rnd_varname_pre$(void) { u8 retry=0; DS18B20_IO_IN_$rnd_varname_pre$(); //SET PG11 INPUT while (IO_Read( [($_xgm$data1),type=pin] )&&retry<10) { retry++; delay_us_$rnd_varname_pre$(20); }; if(retry>=10)return 1; else retry=0; while (!IO_Read( [($_xgm$data1),type=pin] ) && retry<12) { retry++; delay_us_$rnd_varname_pre$(20); }; if(retry>=12)return 1; return 0; } //从DS18B20读取一个位 //返回值:1/0 u8 DS18B20_Read_Bit_$rnd_varname_pre$(void) { u8 data; DS18B20_IO_OUT_$rnd_varname_pre$(); //SET PG11 OUTPUT __disable_irq(); // 关闭总中断,串口中断会在开启总中断后恢复上次的中断,所以无碍 IO_Write([($_xgm$data1),type=pin],0); IO_Write([($_xgm$data1),type=pin],1); DS18B20_IO_IN_$rnd_varname_pre$(); //SET PG11 INPUT if(IO_Read( [($_xgm$data1),type=pin] ))data=1; else data=0; __enable_irq(); // 开启总中断 delay_us_$rnd_varname_pre$(60); IO_Write([($_xgm$data1),type=pin],1); return data; } //从DS18B20读取一个字节 //返回值:读到的数据 u8 DS18B20_Read_Byte_$rnd_varname_pre$(void) { u8 i,j,dat; dat=0; for (i=0;i<=7;i++) { j=DS18B20_Read_Bit_$rnd_varname_pre$(); //dat=(j<<7)|(dat>>1); dat = dat | (j << i); } return dat; } //写一个字节到DS18B20 //dat:要写入的字节 void DS18B20_Write_Byte_$rnd_varname_pre$(u8 dat) { u8 j; u8 testb; DS18B20_IO_OUT_$rnd_varname_pre$(); //SET PG11 OUTPUT; for (j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if (testb) { IO_Write([($_xgm$data1),type=pin],0); // Write 1 delay_us_$rnd_varname_pre$(10); IO_Write([($_xgm$data1),type=pin],1); delay_us_$rnd_varname_pre$(80); } else { IO_Write([($_xgm$data1),type=pin],0); // Write 0 delay_us_$rnd_varname_pre$(80); IO_Write([($_xgm$data1),type=pin],1); delay_us_$rnd_varname_pre$(10); } } } //开始温度转换 u8 DS18B20_Start_$rnd_varname_pre$(void) { u8 ret1=0; DS18B20_Rst_$rnd_varname_pre$(); ret1=DS18B20_Check_$rnd_varname_pre$(); DS18B20_Write_Byte_$rnd_varname_pre$(0xcc); // skip rom //delay_us_$rnd_varname_pre$(10); DS18B20_Write_Byte_$rnd_varname_pre$(0x44); // convert return ret1; } //初始化DS18B20的IO口 DQ 同时检测DS的存在 //返回1:不存在 //返回0:存在 u8 DS18B20_Init_$rnd_varname_pre$(void) { DS18B20_Rst_$rnd_varname_pre$(); return DS18B20_Check_$rnd_varname_pre$(); } float ret_value_$rnd_varname_pre$; //从ds18b20得到温度值 //精度:0.1C //返回值:温度值 (-550~1250) char DS18B20_Get_Temp_$rnd_varname_pre$(struct pt *process_pt) { PT_BEGIN(process_pt); static u8 temp; static u8 TL,TH; static short tem; static float retv1=0; TT_disable_100us_Task_scan_for_other(1); if(DS18B20_Start_$rnd_varname_pre$()!=0){ TT_disable_100us_Task_scan_for_other(0); return 0; } TT_disable_100us_Task_scan_for_other(0); //delay_us_$rnd_varname_pre$(700); timer_set2(&timer_csb2_$rnd_varname_pre$, 100 );//unit:10us timer_restart2(&timer_csb2_$rnd_varname_pre$); PROCESS2_WAIT_UNTIL(process_pt , timer_expired2(&timer_csb2_$rnd_varname_pre$)!=0); //等待温度转换完成,不要这个:占cpu中断太多: //while(DS18B20_Read_Byte_$rnd_varname_pre$()!=0xFF&&timer_expired2(&timer_csb2_$rnd_varname_pre$)==0){} TT_disable_100us_Task_scan_for_other(1); //rst+Check就是DS18B20_Start_ DS18B20_Rst_$rnd_varname_pre$(); if(DS18B20_Check_$rnd_varname_pre$()!=0){ TT_disable_100us_Task_scan_for_other(0); return 0; } DS18B20_Write_Byte_$rnd_varname_pre$(0xcc); // skip rom //delay_us_$rnd_varname_pre$(10); DS18B20_Write_Byte_$rnd_varname_pre$(0xbe); // convert TL=DS18B20_Read_Byte_$rnd_varname_pre$(); // LSB TH=DS18B20_Read_Byte_$rnd_varname_pre$(); // MSB TT_disable_100us_Task_scan_for_other(0); //return TH; if(TH>0x7f) { TH=(~TH); TL=~TL; temp=0; //温度为负 }else temp=1; //温度为正 tem=TH; //获得高八位 tem<<=8; tem+=TL; //获得低八位 retv1=(float)(((float)tem)*0.0625); //转换 if(temp)ret_value_$rnd_varname_pre$= retv1; //注意:temp不是tem, 返回温度值 else ret_value_$rnd_varname_pre$= (-1.0)*retv1; timer_set2(&timer_csb2_$rnd_varname_pre$, 300 );//unit:10us ,释放cpu占中断3ms给其他积木 timer_restart2(&timer_csb2_$rnd_varname_pre$); PROCESS2_WAIT_UNTIL(process_pt , timer_expired2(&timer_csb2_$rnd_varname_pre$)!=0); PT_END(process_pt); } [$init_code_for_init_val] #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$data1),type=pin]","0","18b20信号输入")% IO_Write([($_xgm$data1),type=pin] ,1) ; //DS18B20_Init_$rnd_varname_pre$(); [$timer0_1ms_code] [$timer0_100us_code] [$toolbox_block_code] [$replace_map] $_xgm$data1 = $field$data1 ,mytype|pin, ptype|TIMES