[$group] 元件库-储存器.$pname$ [$last_ver_update_time] 2021-04-03 [$min_need_ver] 2.1 [$p_list] [$short_show_UIname] [$code_area_type] codes,exe_per_1ms,init_code [$blocks_type] line [$name] iic通信器 [$引脚占用列表,用于提示冲突] [($_xgm$sda),type=pin]|i2c通信积木里已占用SDA引脚 [($_xgm$scl),type=pin]|i2c通信积木里已占用SCL引脚 [$寄存器占用列表,用于提示冲突,但=>后的值没变则不提示冲突] [$中断函数号占用列表,用于提示冲突] [$interrupt_1] [$thread_do] [$run] [$cs] [$function_ref_define_in_headfile] @@file=iic_at24cxx.h void I2c_init_$rnd_varname_pre$(); void I2c_start_$rnd_varname_pre$(); void I2c_stop_$rnd_varname_pre$(); u8 I2c_wait_ack_$rnd_varname_pre$(); void I2c_ack_$rnd_varname_pre$(); void I2c_nack_$rnd_varname_pre$(); void I2c_send_byte_$rnd_varname_pre$(u8 IIC_Byte); u8 I2c_read_byte_$rnd_varname_pre$(); char AT24CXX_WriteOneByte_$rnd_varname_pre$(struct pt *process_pt,u16 WriteAddr,u8 DataToWrite); u8 AT24CXX_ReadOneByte_$rnd_varname_pre$(u16 ReadAddr); #define I2c_SCL_$rnd_varname_pre$ [($_xgm$scl),type=pin] #define I2c_SDA_$rnd_varname_pre$ [($_xgm$sda),type=pin] [$init_code_for_define_area_once] @@file=iic_at24cxx.c [$init_code_for_define_area] @@file=iic_at24cxx.c #define AT24C01 127 #define AT24C02 255 #define AT24C04 511 #define AT24C08 1023 #define AT24C16 2047 #define AT24C32 4095 #define AT24C64 8191 #define AT24C128 16383 #define AT24C256 32767 #include "protothread.h" #include "app_process.h" #include "sys/timer.h" #include "Extline_Lib.h" #include "Lib_base.h" //考虑PROCESS2_WAIT_UNTIL宏 struct timer timer_at_$rnd_varname_pre$; #define I2c_SCL_$rnd_varname_pre$ [($_xgm$scl),type=pin] #define I2c_SDA_$rnd_varname_pre$ [($_xgm$sda),type=pin] void I2c_SDA_OUT_$rnd_varname_pre$() { #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$sda),type=pin]","1","i2c通信的SDA信号")% } void I2c_SDA_IN_$rnd_varname_pre$() { #get_P口_to_PxM1_PxM0_set_code(group_xml,rand1,block_name,ref_complier,extinfo,"[($_xgm$sda),type=pin]","0","i2c通信的SDA信号")% } void I2c_init_$rnd_varname_pre$() { I2c_SDA_OUT_$rnd_varname_pre$();//推挽输出 IO_Write(I2c_SDA_$rnd_varname_pre$ ,1) ; IO_Write(I2c_SCL_$rnd_varname_pre$ ,1) ; } void Delay10us_$rnd_varname_pre$() { u16 a; u8 b; for(b=1;b>0;b--) for(a=#get_10us_delay_num_stm32f10x(group_xml,rand1,block_name,ref_complier,extinfo)%;a>0;a--); } void delay_10us_$rnd_varname_pre$(u8 time) { do { Delay10us_$rnd_varname_pre$() ;} while (--time); } void I2c_start_$rnd_varname_pre$() { I2c_SDA_OUT_$rnd_varname_pre$(); IO_Write(I2c_SDA_$rnd_varname_pre$ ,1) ; IO_Write(I2c_SCL_$rnd_varname_pre$ ,1) ; delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SDA_$rnd_varname_pre$ ,0) ; delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,0) ; delay_10us_$rnd_varname_pre$(1); //ruan added 20220515 } void I2c_stop_$rnd_varname_pre$() { I2c_SDA_OUT_$rnd_varname_pre$(); IO_Write(I2c_SCL_$rnd_varname_pre$ ,0) ; IO_Write(I2c_SDA_$rnd_varname_pre$ ,0) ; delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,1) ; delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SDA_$rnd_varname_pre$ ,1) ; delay_10us_$rnd_varname_pre$(1); } u8 I2c_wait_ack_$rnd_varname_pre$() { u32 ucErrTime=0; I2c_SDA_IN_$rnd_varname_pre$(); //设置为输入 IO_Write(I2c_SDA_$rnd_varname_pre$ ,1); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,1);delay_10us_$rnd_varname_pre$(1); while(IO_Read(I2c_SDA_$rnd_varname_pre$)==1) { ucErrTime++; if(ucErrTime>9500) { I2c_stop_$rnd_varname_pre$(); return 1; } } IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); return 0; } void I2c_ack_$rnd_varname_pre$() { IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); I2c_SDA_OUT_$rnd_varname_pre$(); IO_Write(I2c_SDA_$rnd_varname_pre$ ,0); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,1); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); } void I2c_nack_$rnd_varname_pre$() { IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); I2c_SDA_OUT_$rnd_varname_pre$(); IO_Write(I2c_SDA_$rnd_varname_pre$ ,1); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,1); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); } void I2c_send_byte_$rnd_varname_pre$(u8 IIC_Byte) { u8 t; I2c_SDA_OUT_$rnd_varname_pre$(); IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); for(t=0;t<8;t++) { if(((IIC_Byte&0x80)>>7)!=0){ IO_Write(I2c_SDA_$rnd_varname_pre$ ,1); }else{ IO_Write(I2c_SDA_$rnd_varname_pre$ ,0); } IIC_Byte<<=1; delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,1); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); delay_10us_$rnd_varname_pre$(1); } } u8 I2c_read_byte_$rnd_varname_pre$() { u8 i; u8 receive=0; I2c_SDA_IN_$rnd_varname_pre$(); //设置为输入 for(i=0;i<8;i++ ) { IO_Write(I2c_SCL_$rnd_varname_pre$ ,0); delay_10us_$rnd_varname_pre$(1); IO_Write(I2c_SCL_$rnd_varname_pre$ ,1); receive<<=1; if(IO_Read(I2c_SDA_$rnd_varname_pre$)==1)receive++; delay_10us_$rnd_varname_pre$(1); } return receive; } u8 AT24CXX_ReadOneByte_$rnd_varname_pre$(u16 ReadAddr) { u8 temp=0; TT_disable_100us_Task_scan_for_other(1); I2c_start_$rnd_varname_pre$(); if([($_xgm$at_type),type=num]>AT24C16) { I2c_send_byte_$rnd_varname_pre$(0xA0); //发送写命令 I2c_wait_ack_$rnd_varname_pre$(); I2c_send_byte_$rnd_varname_pre$(ReadAddr>>8);//发送高地址 I2c_wait_ack_$rnd_varname_pre$(); } else { I2c_send_byte_$rnd_varname_pre$(0xA0+((ReadAddr/256)<<1)); //发送器件地址0XA0,写数据 I2c_wait_ack_$rnd_varname_pre$(); } I2c_send_byte_$rnd_varname_pre$(ReadAddr%256); //发送低地址 I2c_wait_ack_$rnd_varname_pre$(); I2c_start_$rnd_varname_pre$(); I2c_send_byte_$rnd_varname_pre$(0xA1); //进入接收模式 I2c_wait_ack_$rnd_varname_pre$(); temp=I2c_read_byte_$rnd_varname_pre$(); I2c_nack_$rnd_varname_pre$();//nack I2c_stop_$rnd_varname_pre$();//产生一个停止条件 TT_disable_100us_Task_scan_for_other(0); delay_10us_$rnd_varname_pre$(5); return temp; } char AT24CXX_WriteOneByte_$rnd_varname_pre$(struct pt *process_pt,u16 WriteAddr,u8 DataToWrite) { PT_BEGIN(process_pt); TT_disable_100us_Task_scan_for_other(1); I2c_start_$rnd_varname_pre$(); if([($_xgm$at_type),type=num]>AT24C16) { I2c_send_byte_$rnd_varname_pre$(0xA0); //发送写命令 I2c_wait_ack_$rnd_varname_pre$(); I2c_send_byte_$rnd_varname_pre$(WriteAddr>>8);//发送高地址 } else { I2c_send_byte_$rnd_varname_pre$(0xA0+((WriteAddr/256)<<1)); //发送器件地址0XA0,写数据 } I2c_wait_ack_$rnd_varname_pre$(); I2c_send_byte_$rnd_varname_pre$(WriteAddr%256); //发送低地址 I2c_wait_ack_$rnd_varname_pre$(); I2c_send_byte_$rnd_varname_pre$(DataToWrite); //发送字节 I2c_wait_ack_$rnd_varname_pre$(); I2c_stop_$rnd_varname_pre$();//产生一个停止条件 TT_disable_100us_Task_scan_for_other(0); timer_set(&timer_at_$rnd_varname_pre$, 50 ); timer_restart(&timer_at_$rnd_varname_pre$); PROCESS2_WAIT_UNTIL(process_pt , timer_expired(&timer_at_$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$scl),type=pin]","1","i2c通信的SCL信号")% I2c_init_$rnd_varname_pre$(); [$timer0_1ms_code] [$toolbox_block_code] [$replace_map] $_xgm$sda = $field$sda,mytype|pin, ptype|TIMES $_xgm$scl = $field$scl,mytype|pin, ptype|TIMES $_xgm$at_type = $field$at_type,mytype|num, ptype|TIMES