/*********************************************************************** * @brief Initialize the I2C interface for AT88SC102 * @param None * @retval None ************************************************************************/ uint8_t SLE4442_I2C_ReadByte(void) { uint8_t i; uint8_t temp = 0; uint8_t value = 0; #ifdef SLE4442_DEBUG //printf("[IC] SLE4442_I2C_ReadByte...\r\n"); #endif for(i = 8;i > 0;i --) { temp >>= 1; GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); value = GPIO_ReadInputDataBit(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); if(value) { temp |= 0x80; } else { temp &= 0x7f; } delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); } return (temp); }
/************************************ Function Initialization ***************************************************/ void initialize_lcd(void) { int i; for (i=0; i<15; i++) { // wait 15 ms delay1ms(); } write_command4_lcd(0x03); // write SF_D<11:8> =0x3 and pulse lcd_E high for 12 clock cycles for (i=0; i<5; i++) { // wait 5 ms delay1ms(); } write_command4_lcd(0x03); // write SF_D<11:8> =0x3 and pulse lcd_E high for 12 clock cycles for (i=0; i<100; i++) { // wait 100 us delay1us(); }; write_command4_lcd(0x03); // write SF_D<11:8> =0x3 and pulse lcd_E high for 12 clock cycles for (i=0; i<40; i++) { // wait 40 us delay1us(); }; write_command4_lcd(0x02); // write SF_D<11:8> =0x2 and pulse lcd_E high for 12 clock cycles for (i=0; i<40; i++) { // wait 40 us delay1us(); } }
/*********************************************************************** * @brief Initialize the I2C interface for AT88SC102 * @param None * @retval None ************************************************************************/ void SLE4442_I2C_WriteByte(uint8 * pSrcBuffer) { uint8_t i,value; #ifdef SLE4442_DEBUG //printf("[IC] SLE4442_I2C_WriteByte...\r\n"); #endif value = *pSrcBuffer; for(i = 8;i > 0;i --) { GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); if(value & 0x01) { GPIO_SetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); } else { GPIO_ResetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); } delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(10); value >>= 1; } }
/******************************发送一个字节******************************** * 函 数:void Write_Byte_18B20(u8 data); * 功 能:从DS18B20发送一个字节 * 参 数:给DS18B20发送的字节 * 返回值:无 * 更 新:无 * 备 注:无 ****************************************************************************/ static void Write_Byte_18B20(u8 data) { u8 i; for(i = 0;i < 8;i++) { GPIO_18B20_Confing(DATA_OUT); DS18B20_Pin_L; delay1us(15); if( data & 0x01) { DS18B20_Pin_H; delay1us(45); } else { DS18B20_Pin_L; delay1us(45); } data>>=1; DS18B20_Pin_H; delay1us(40); } }
/************************************18B20初始化******************************** * 函 数:extern ErrorStatus Init_18B20(void) * 功 能:DS18B20初始化 * 参 数:无 * 返回值:ErrorStatus判断是否初始化成功success 成功 error失败 ****************************************************************************/ static ErrorStatus Init_18B20(void) { ErrorStatus Flag = ERROR; GPIO_18B20_Confing(DATA_OUT);//配置为输出 DS18B20_Pin_H; delay1us(10); DS18B20_Pin_L; delay1us(600);//至少480us的拉低 DS18B20_Pin_H;//拉高以便接收所返回的低电平 delay1us(100);//15--65us后回复60--240us的低电平 GPIO_18B20_Confing(DATA_IN); if(GPIO_ReadInputDataBit(GPIO_18B20, GPIO_18B20_Pin)) { //握手失败 Flag = ERROR; GPIO_18B20_Confing(DATA_OUT); } else { Flag = SUCCESS; GPIO_18B20_Confing(DATA_OUT); DS18B20_Pin_H; } delay1us(400); return Flag; }
/******************************接收一个字节******************************** * 函 数:u8 Read_Byte_18B20(void); * 功 能:从DS18B20接收一个字节 * 参 数:无 * 返回值:DS18B20返回的一个字节 * 更 新:无 * 备 注:无 ****************************************************************************/ static u8 Read_Byte_18B20(void) { u8 data = 0 , i = 0; GPIO_18B20_Confing(DATA_OUT);//引脚配置成输出 for(i=0; i<8; i++) { GPIO_18B20_Confing(DATA_OUT); DS18B20_Pin_H; delay1us(5);//延时7US DS18B20_Pin_L; delay1us(5);//拉低7US表示要读数据 DS18B20_Pin_H; delay1us(5);//延时7US GPIO_18B20_Confing(DATA_IN);//引脚配置成输入 data >>= 1; if(GPIO_ReadInputDataBit(GPIO_18B20, GPIO_18B20_Pin)) { data |= 0x80; } GPIO_18B20_Confing(DATA_OUT);//释放总线 DS18B20_Pin_H; delay1us(60);//延时60US保证整个位的读取时间60-120uS } return (data); }
/*********************************************************************** * @brief Read date from AT88SC102 * @param None * @retval None ************************************************************************/ void SLE4442_Break(void) { GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); GPIO_ResetBits(GPIO_PORT_IC_CARD_RST, GPIO_PIN_IC_CARD_RST); GPIO_ResetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_RST, GPIO_PIN_IC_CARD_RST); GPIO_SetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); delay1us(5); GPIO_ResetBits(GPIO_PORT_IC_CARD_RST, GPIO_PIN_IC_CARD_RST); delay1us(5); }
/*********************************************************************** * @brief Initialize the I2C interface for AT88SC102 * @param None * @retval None ************************************************************************/ void SLE4442_I2C_Stop(void) { #ifdef SLE4442_DEBUG //printf("[IC] SLE4442_I2C_Stop...\r\n"); #endif GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); GPIO_ResetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); delay1us(10); }
/*********************************************************************** * @brief Read date from AT88SC102 * @param None * @retval None ************************************************************************/ void SLE4442_ProcessMode(void) { uint8_t i; #ifdef SLE4442_DEBUG printf("\r\n[IC] SLE4442_ProcessMode...\r\n"); #endif GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); GPIO_ResetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); for (i = 255; i > 0; i--) { GPIO_SetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); } GPIO_SetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); }
unsigned char wait_interrupt() { /* 主机端等待操作完成, 返回操作状态 */ unsigned short i; // while( CH375_INT_WIRE ); /* 查询等待CH375操作完成中断(INT#低电平) */ for ( i = 0; CH375_INT_WIRE != 0; i ++ ) { /* 如果CH375的中断引脚输出高电平则等待,通过计数防止超时 */ delay1us(); if ( i == 0xF000 ) CH375_WR_CMD_PORT( CMD_ABORT_NAK ); /* 如果超时达61mS以上则强行终止NAK重试,中断返回USB_INT_RET_NAK */ } CH375_WR_CMD_PORT( CMD_GET_STATUS ); /* 产生操作完成中断, 获取中断状态 */ return( CH375_RD_DAT_PORT() ); }
/*********************************************************************** * @brief Read date from AT88SC102 * @param None * @retval None ************************************************************************/ void SLE4442_ReadMode(uint8_t * pt,uint8_t count) { #ifdef SLE4442_DEBUG printf("\r\n[IC] SLE4442_ReadMode...\r\n"); #endif GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); do{ *pt++ = SLE4442_I2C_ReadByte(); //读入一个字,指针加一 }while(--count); //计数器减一,判断 }
/*********************************************************************** * @brief Read date from AT88SC102 * @param None * @retval None ************************************************************************/ void SLE4442_Reset(void) { uint8_t temp[4] = {0}; #ifdef SLE4442_DEBUG //printf("\r\n[IC] SLE4442_Reset...\r\n"); #endif delay1us(5); GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); GPIO_ResetBits(GPIO_PORT_IC_CARD_RST, GPIO_PIN_IC_CARD_RST); GPIO_SetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_RST, GPIO_PIN_IC_CARD_RST); delay1us(5); GPIO_SetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(40); GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); delay1us(5); GPIO_ResetBits(GPIO_PORT_IC_CARD_RST, GPIO_PIN_IC_CARD_RST); delay1us(10); //复位和复位应答时序 // ___ //RST _| |_______________________ // _ _ __ __ //CLK __|0|_|1|_ ..._|31|_|32|_____ // _ __ __ ___ _____ // \/ \/D0\ ... \/D31\/ //I/O _/\__/\__/ /\___/ temp[0] = SLE4442_I2C_ReadByte(); temp[1] = SLE4442_I2C_ReadByte(); temp[2] = SLE4442_I2C_ReadByte(); temp[3] = SLE4442_I2C_ReadByte(); //空读 32Bit (4Byte) GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); // __ __ IC sets I/O to state H delay1us(5); //CLK_|31|_|32|______________ GPIO_SetBits(GPIO_PORT_IC_CARD_IO, GPIO_PIN_IC_CARD_IO); // __ ___ ____________ nop(); // 30 \/ 31\/ GPIO_ResetBits(GPIO_PORT_IC_CARD_CLK, GPIO_PIN_IC_CARD_CLK); //I/O __ /\___/ delay1us(5); #ifdef SLE4442_DEBUG /*for(uint8_t i = 0;i < sizeof(temp);i ++) { printf("0x%02x, ",temp[i]); } printf("\r\n");*/ #endif }
/*** write command for configuration ***/ void write_command8_lcd(unsigned char ch) // ex: ch=0x28 { int lcd_output; int i; //upper nibble lcd_output = (ch & 0xf0) << (LCD_D_SHIFT-4); // lcd_output= 0x1000 (shift_left=4) MemoryWrite(GPIO0_OUT, lcd_output); // write upper nibble for(i=0;i<2;i++){} // hold LCD_RS LCD_RW SF_D stable for 40ns before LCD_E goes high HoldPulse_e12CC(); // LCD_E high for 12 Clock cycles for(i=0;i<1;i++){} MemoryWrite(GPIO0_CLEAR,lcd_output); delay1us(); //lower nibble lcd_output = (ch & 0x0f) << LCD_D_SHIFT; // lcd_output= 0x0600 (shift_left=11) MemoryWrite(GPIO0_OUT, lcd_output); // write lower nibble for(i=0;i<2;i++){} // hold LCD_RS LCD_RW SF_D stable for 40ns before LCD_E goes high HoldPulse_e12CC(); // LCD_E high for 12 Clock cycles for(i=0;i<1;i++){} MemoryWrite(GPIO0_CLEAR,lcd_output); delay40us(); // delay 40us for the next lcd write command }
/*** write characters ***/ void write_char_lcd(unsigned char ch) // ex: char=0x6c { int lcd_output; int i; // upper nibble lcd_output = (ch & 0xf0) << (LCD_D_SHIFT - 4); // lcd_output= 0x3000 (shift_left=7) lcd_output |= LCD_RS; // lcd_output= 0x3400 --> LCD_RS =1 LCD_RW=0 SF_D=0110 MemoryWrite(GPIO0_OUT,lcd_output); for(i=0;i<2;i++){} // hold LCD_RS LCD_RW SF_D stable for 40ns before LCD_E goes high HoldPulse_e12CC(); // LCD_E high for 12 Clock cycles for(i=0;i<1;i++){} MemoryWrite(GPIO0_CLEAR,lcd_output); // LCD_RS=0 LCD_RW=0 delay1us(); // lower nibble lcd_output = (ch & 0x0f) << LCD_D_SHIFT; // lcd_output= 0x6000 (shift_left=11) lcd_output |= LCD_RS; // lcd_output= 0x6400 --> LCD_RS =1 LCD_RW=0 SF_D=1010 MemoryWrite(GPIO0_OUT, lcd_output); // write lower nibble for(i=0;i<2;i++){} // hold LCD_RS LCD_RW SF_D stable for 40ns before LCD_E goes high HoldPulse_e12CC(); // LCD_E high for 12 Clock cycles for(i=0;i<1;i++){} MemoryWrite(GPIO0_CLEAR,lcd_output); // LCD_RS=0 LCD_RW=0 delay40us(); // delay 40us for the next write char lcd }
void CH375_WR_DAT_PORT( unsigned char dat ) { /* 向CH375的数据端口写入数据,周期不小于1.5uS,如果单片机较快则延时 */ CH375_DAT_PORT=dat; delay1us(); /* 因为MCS51单片机较慢所以实际上无需延时 */ }
int main(int argc, char* argv[]) { int r; // set up strcpy(i2c_bus,"/dev/i2c-1"); // print header fprintf(stderr, "AM2321 reader application by saper_2 v.0.1\n"); process_argv(argc, argv); fprintf(stderr, "Using i2c bus device: %s\n",i2c_bus); if (info_mode) { uint8_t data[16]; memset(data,0xff,16); r = am2321_read_info(data, i2c_bus); fprintf(stderr,"Info read result %i\n",r); fprintf(stderr,"Info data: "); for(int i=0;i<8;i++) fprintf(stderr, "%02X ",data[i]); fprintf(stderr,"\n"); uint16_t model = data[0]<<8 | data[1]; uint8_t version = data[2]; uint32_t devid = data[3]<<24 | data[4]<<16 | data[5]<<8 | data[6]; uint8_t stat = data[7]; fprintf(stderr, "Model: 0x%04X (%iu)\n",model,model); fprintf(stderr, "Version: 0x%02X (%iu)\n",version,version); fprintf(stderr, "Device ID: 0x%08X (%iu)\n",devid,devid); fprintf(stderr, "Status: 0x%02X (%iu)\n",stat,stat); // format: i:[res_code];[model];[version];[dev_id];[status] printf("i:%i;%04x;%02x;%08x;%02x\n", r, model, version, devid, stat); if (raw_mode || val_mode) { //printf("\n"); fprintf(stderr,"\nDelay 1sec before reading values...\n"); delay1us(1*1000*1000); } } if (val_mode || raw_mode) { am2321_t val; time_t tt; unsigned long ts = (unsigned)time(NULL); time(&tt); memset(&val,0,sizeof(val)); r = am2321_read_raw(&val, i2c_bus); fprintf(stderr,"Read values result %i\n",r); fprintf(stderr,"Read values data: \n"); fprintf(stderr, " humi = 0x%04X (%i , %3.1f %%)\n",val.humi,val.humi, (val.humi*1.0f)/10.0f); fprintf(stderr, " temp = 0x%04X (%i , %3.1f degC)\n",val.temp,val.temp, (val.temp*1.0f)/10.0f); struct tm *tti = localtime(&tt); fprintf(stderr, "Current time is: %lu - %s\n",ts,asctime(tti)); fprintf(stderr,"\n"); if (val_mode>0 && raw_mode==0) { // val mode // format: v:[unix_time_stamp];[mode];[result];[humi];[temp] printf("v:%lu;r;%i;%3.1f;%3.1f\n", ts, r, (val.humi*1.0f)/10.0f, (val.temp*1.0f)/10.0f); } else if (val_mode==0 && raw_mode>0) { // raw mode // format: v:[unix_time_stamp];[mode];[result];[hex humi];[hex temp] printf("v:%lu;v;%i;%04x;%04x\n", ts, r, val.humi, val.temp); } else if (val_mode>0 && raw_mode>0) { // raw + val // format: v:[unix_time_stamp];[mode];[result];[hex humi];[hex temp];[humi];[temp] printf("v:%lu;rv;%i;%04x;%04x;%3.1f;%3.1f\n", ts, r, val.humi, val.temp, (val.humi*1.0f)/10.0f, (val.temp*1.0f)/10.0f); } else { // no format ? // format: x:[unix_time_stamp];[mode];9;FFFF;FFFF;65535;32767 printf("x:%lu;x;9;FFFF;FFFF;65535;32767\n", ts); } } if (val_mode==0 && raw_mode==0 && info_mode==0) { print_help(); fprintf(stderr,"No read mode selected.\n"); // format: x:[unix_time_stamp=0];[mode=E (=error)];9;FFFF;FFFF;65535;32767 printf("x:0;E;9;FFFF;FFFF;65535;32767\n"); } return 0; }
unsigned char CH375_RD_DAT_PORT() { /* 从CH375的数据端口读出数据,周期不小于1.5uS,如果单片机较快则延时 */ delay1us(); /* 因为MCS51单片机较慢所以实际上无需延时 */ return( CH375_DAT_PORT ); }