//UART->DS18B20 //Uart转1-Wire硬件控制器程序源码例程 //HotPower@126.com 2008.3.2 于雁塔菜地 LPC_INT8U OneWire_Start (void); LPC_INT8U OneWire_ReadWriteByte (LPC_INT8U data); LPC_INT8U OneWire_ReadId (LPC_INT8U *pData);
LPC_INT8U OneWire_Start (void) { LPC_INT8U ch; Uart1Config.BaudRate = BD9600; Uart1Config.WordLenth = WordLength8; Uart1Config.TwoStopBitsSelect = false;//stop=1 UART_Init(UART1); UART_PutCharByPolling(UART1, 0xf0); //ch=0xf0 无应答 ch=0xe0 有应答 ds18b20在线 ch = UART_GetCharByPolling(UART1); Uart1Config.BaudRate = BD115200; Uart1Config.WordLenth = WordLength6; Uart1Config.TwoStopBitsSelect = false;//stop=1 UART_Init(UART1); return ch; }
LPC_INT8U OneWire_ReadId (LPC_INT8U *pData) { LPC_INT8U i, ch; //ch=0xf0 无应答 ch=0xe0 有应答 ds18b20在线 ch = OneWire_Start();//960us OneWire_ReadWriteByte(0x33); for (i = 0; i < 8; i ++) { pData = OneWire_ReadWriteByte(0xff);//读一个字节 } return ch;//返回结果 }
LPC_INT8U OneWire_ReadWriteByte (LPC_INT8U data) { LPC_INT8U i, ch; LPC_INT8U value = 0; for (i = 0; i < 8; i ++) { if (data & 1) { UART_PutCharByPolling(UART1, 0x3f);//1 } else { UART_PutCharByPolling(UART1, 0x00);//0 } ch = UART_GetCharByPolling(UART1); data >>= 1; value >>= 1; if (ch & 0x07)//收到1T+3T个'0'为0 { value |= 0x80; } } return value; }
void main (void) { char RecData, TranBuff[30]; if(SysInit() == 0) { // Start user program __enable_interrupt(); UserStart(); } __disable_interrupt(); OneWire_ReadId(TranBuff);//读18b20的器件ID=28 2D 95 1D 00 00 00 22 while(1); }
1.读DS18B20系列码程序硬件仿真帖图
2.HotCRC计数器计算CRC8过程校验帖图
3.硬件接口电路
|