u8 DS18B20_Reset(void) { u8 i = 0; Set18b20IOout(); //主机端口推挽输出模式 Write18b20IO(1); Delay_US(1); Write18b20IO(0); //拉低总线480us~240us Delay_US(500); //>480US延时 Write18b20IO(1); Delay_US(2); //复位完成 Set18b20IOin(); //主机端口浮空输入模式 while(Read18b20IO()) //等待低电平应答信号 { i ++; Delay_US(1); if(i > 100) { // uart_printf("DS18B20 error!\r\n"); return FALSE; //等待超时,初始化失败,返回FALSE; } } // Delay_US(250); //跳过回复信号 Delay_US(410); //跳过回复信号 return TRUE; //检测到DS18B20,并且初始化成功 }
void DS18B20_WriteBit(u8 bit) { Set18b20IOout(); //主机端口推挽输出模式 Write18b20IO(0); //拉低总线10-15us Delay_US(12); Write18b20IO(bit & 0x01);// //写入数据位,保持20-45us Delay_US(30); Write18b20IO(1); //释放总线 Delay_US(5); }
void main(void) { unsigned int channel = CHANNEL; unsigned char data = 0x07; // DBGU output configuration TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); // Configuration PIT (Periodic Interrupt Timer) ConfigurePit(); // Configuration TC (Timer Counter) ConfigureTc(); // Configuration PIO (Paralell In and Out port), Init Interrupt on PIO ConfigureButtons(); ConfigureLeds(); // Configuration Radio Module nRF24L (PIO and SPI), ConfigureButtons must be executed before ConfigureNRF24L(); ConfigureUSART0(); ConfigureUSART1(); //initialize proximity sensor ir_init(); Global_Variable_Init(); while(Timer0Tick<2); // wait until NRF24L01 power up nrf24l01_power_up(True); while(Timer0Tick<4); // wait until NRF24L01 stand by Timer0Tick = 0; //initialize the 24L01 to the debug configuration as RX and auto-ack disabled nrf24l01_initialize_debug(True, nrf_TX_RX_SIZE, False); nrf24l01_write_register(0x06, &data, 1); nrf24l01_set_as_rx(True); Delay_US(130); nrf24l01_set_rf_ch(channel); nrf24l01_flush_rx(); Delay_US(300); while (1) { if(Timer0Tick!=0){ Timer0Tick = 0; Check_Battery(0); odometry(0); ProxRead_m(); Send_Coord(); Delay_US(10000);//give time for the coming message feedbackController(goalx, goaly, goaldist); } Check_Wireless(); }//while }//main
//powers up the 24L01 with all necessary delays //this function takes the existing contents of the CONFIG register and sets the PWR_UP //the argument rx_active_mode is only used if the user is setting up the // 24L01 as a receiver. If the argument is false, the receiver will remain in // standby mode and not monitor for packets. If the argument is true, the CE // pin will be set and the 24L01 will monitor for packets. In TX mode, the value // of this argument is insignificant. //note: if the read value of the CONFIG register already has the PWR_UP bit set, this function // exits in order to not make an unecessary register write. void nrf24l01_power_up(bool rx_active_mode) { unsigned char config; nrf24l01_read_register(nrf24l01_CONFIG, &config, 1); if((config & nrf24l01_CONFIG_PWR_UP) != 0) return; config |= nrf24l01_CONFIG_PWR_UP; nrf24l01_write_register(nrf24l01_CONFIG, &config, 1); Delay_US(1500); if((config & nrf24l01_CONFIG_PRIM_RX) == 0) nrf24l01_clear_ce(); else { if(rx_active_mode != false) nrf24l01_set_ce(); else nrf24l01_clear_ce(); } }
s16 DS18B20_ReadDesignateTemper( u8 pID[8]) { u8 th, tl; s16 data; if(DS18B20_Reset() == FALSE) { return 0xffff; //返回错误 } DS18B20_WriteData(0xcc); //跳过读序列号 DS18B20_WriteData(0x44); //启动温度转换 DS18B20_Reset(); DS18B20_WriteData(0xcc); //跳过读序列号 #if 0 DS18B20_WriteData(0x55); //发送序列号匹配命令 for(data = 0;data < 8;data ++) //发送8byte的序列号 { DS18B20_WriteData(pID[data]); } Delay_US(10); #endif DS18B20_WriteData(0xbe); //读取温度 tl = DS18B20_ReadData(); //读取低八位 th = DS18B20_ReadData(); //读取高八位 data = th; data <<= 8; data |= tl; data *= 6.25; //温度值扩大100倍,精确到2位小数 return data; }
/******** Transceiver_Receive ********************************************** // Hardware trigger on IRQ occurred, change the state for transceiver state // machine in order to read message into microcontroller // Input: none // Output: none // ------------------------------------------------------------------------*/ void Transceiver_Receive ( void ) { portBASE_TYPE xHigherPriorityTaskWoken; unsigned char rx[TRANSCEIVER_MAX_PAYLOAD]; if ( !( nrf24l01_irq_pin_active() && nrf24l01_irq_rx_dr_active() ) ) { // ERROR: we got interupt but the interupt is not packet received Debug_Printf ( "Transceiver_Receive: Error 0x%x\n", nrf24l01_get_status()&nrf24l01_STATUS_INTERRUPT ); goto exit; } //get the payload into data nrf24l01_read_rx_payload ( rx, TRANSCEIVER_MAX_PAYLOAD ); Debug_NetworkTransceiver_PrintPayload(rx); if ( pdTRUE != xQueueSendFromISR(Transceiver_RX_Queue, rx, &xHigherPriorityTaskWoken) ) { // ERROR: Queue is full Debug_Printf ("Transceiver_Receive: Error 0x%x\n", ERROR_QUEUE_FULL); if ( FullQueueCallBack != NULL ) xTaskCreate( Transceiver_FullQueueCallBack, ( signed portCHAR * ) "Transceiver_FullQueueCallBack", DEFAULT_STACK_SIZE, NULL, DEFAULT_PRIORITY, NULL ); goto exit; } exit: nrf24l01_irq_clear_all(); Delay_US(130); }
__inline u8 DS18B20_ReadBit(void) { u8 data = 0; Set18b20IOout(); //主机端口推挽输出模式 Write18b20IO(0); //拉低总线10-15us Delay_US(12); Write18b20IO(1); //释放总线 Set18b20IOin(); //主机端口浮空输入模式 Delay_US(10); if(Read18b20IO()) //读取数据,读取后大约延时40-45us data = 0x01; Delay_US(40); return data; }
/******** Transceiver_SendMessage ******************************************* // unpack transceiver packet and send out // Input: // pkt - transceiver packet to be sent // Output: status // ------------------------------------------------------------------------*/ unsigned char Transceiver_SendMessage ( TransceiverPacket pkt ) { unsigned char tx[TRANSCEIVER_MAX_PAYLOAD]; unsigned char index, length; unsigned char status = SUCCESS; // validate packet Debug_NetworkTransceiver_PrintPacket(&pkt); if ( MAX_DATA_SIZE < pkt.dataSize ) { status = ERROR_INVALID_PAKCET; Debug_Printf ("Transceiver_SendMessage: Error 0x%x\n", status); goto exit; } // unpack transceiver packet tx[SOURCE_ID_INDEX] = pkt.srcID; tx[DEST_ID_INDEX] = pkt.destID; tx[MSG_ID_INDEX] = pkt.msgID; tx[DATA_SIZE_INDEX] = pkt.dataSize; length = DATA_INDEX; for ( index = 0; index < pkt.dataSize; index++,length++ ) { tx[length] = pkt.data[index]; } Debug_NetworkTransceiver_PrintPayload(tx); // lock transceiver while ( xSemaphoreTake(Transceiver_Mutex, portMAX_DELAY) != pdTRUE ); GPIOPinIntDisable ( nrf24l01_IRQ_IOREGISTER, nrf24l01_IRQ_PIN ); nrf24l01_set_as_tx(); nrf24l01_write_tx_payload ( tx, TRANSCEIVER_MAX_PAYLOAD, true ); //wait until the packet has been sent or the maximum number of retries has been active while( !( nrf24l01_irq_pin_active() && (nrf24l01_irq_tx_ds_active()||nrf24l01_irq_max_rt_active()) ) ); if ( nrf24l01_irq_max_rt_active() ) { // hit maximum number of retries nrf24l01_flush_tx(); status = ERROR_MAX_RETRIES; Debug_Printf ("Transceiver_SendMessage: Error 0x%x\n", status); } // reset transceiver nrf24l01_irq_clear_all(); nrf24l01_set_as_rx(true); Delay_US(130); //unlock transceiver GPIOPinIntEnable ( nrf24l01_IRQ_IOREGISTER, nrf24l01_IRQ_PIN ); while ( xSemaphoreGive(Transceiver_Mutex) != pdTRUE ); exit: return status; }
/**********读取字节*********************************/ UINT8 EEPROMReadByte(UINT16 addr) { EEPROMEnable(); // EA =0; ISP_CMD = 0x01;//读模式 ISP_ADDRH = (UINT8)( (addr + EEPROM_START_ADDRESS)>>8 ); ISP_ADDRL = (UINT8) addr; EEPROMStart(); Delay_US(11); return (ISP_DATA); // EA = 1; }
/**********写入一个字节****************************/ void EEPROMWriteByte(UINT16 addr,UINT8 byte) { EEPROMEnable(); // EA =0; ISP_CMD = 0x02;//写模式 ISP_ADDRH = (UINT8)( (addr + EEPROM_START_ADDRESS)>>8 ); ISP_ADDRL = (UINT8) addr; ISP_DATA = byte;//写入数据,(一个字节) EEPROMStart(); Delay_US(60);//写字节的等待时间 EEPROMDisable(); // EA = 1; }
/**********擦除扇形区*****************************/ void EEPROMSectorErase(UINT16 addr) { EEPROMEnable(); // EA =0; ISP_CMD = 0x03;//触发命令,擦除扇区 ISP_ADDRH = (UINT8)( (addr + EEPROM_START_ADDRESS)>>8 ); ISP_ADDRL = (UINT8) addr; EEPROMStart(); Delay_MS(10);//擦除等待时间,约10毫秒 Delay_US(900);//万一10毫秒延时等待不够,增加900微秒的延时等待时间,确保扇区擦除完成。 EEPROMDisable(); // EA = 1; }
//transmits the current tx payload void nrf24l01_transmit() { nrf24l01_set_ce(); Delay_US(10); nrf24l01_clear_ce(); }
/*********毫秒级延时*****************************/ void Delay_MS(UINT16 ms) { for(;ms>0;ms--){ Delay_US(1000); } }
void main(void) { unsigned int channel = CHANNEL; unsigned char data = 0x07; unsigned char t1; unsigned char t2; unsigned int tmpcount = 0; unsigned char wl_data[10]; unsigned char rs_line[20]; unsigned char rs_data[10]; unsigned char tmp_data[50]; // DBGU output configuration TRACE_CONFIGURE(DBGU_STANDARD, 115200, BOARD_MCK); // Configuration PIT (Periodic Interrupt Timer) ConfigurePit(); // Configuration TC (Timer Counter) ConfigureTc(); // Configuration PIO (Paralell In and Out port), Init Interrupt on PIO ConfigureButtons(); ConfigureLeds(); // Configuration Radio Module nRF24L (PIO and SPI), ConfigureButtons must be executed before ConfigureNRF24L(); ConfigureUSART0(); ConfigureUSART1(); while(Timer1Tick<2); // wait until NRF24L01 power up nrf24l01_power_up(True); while(Timer1Tick<4); // wait until NRF24L01 stand by Timer1Tick = 0; //initialize the 24L01 to the debug configuration as RX and auto-ack disabled nrf24l01_initialize_debug(True, nrf_TX_RX_SIZE, False); nrf24l01_write_register(0x06, &data, 1); nrf24l01_set_as_rx(True); Delay_US(130); nrf24l01_set_rf_ch(channel); nrf24l01_flush_rx(); Delay_US(300); reset_wl = 1; while (1) { if( nrf_Data > 0 ) { nrf_Data = 0; for( t1 = 0; t1<8; t1++ ) { wl_data[t1] = nrfRxMessage.Data[t1]; } LED_Toggle(LED_Green); writeByteSequence_8(wl_data); } if(messageUSART1){ messageUSART1 = 0; pmsgRead(tmp_data); while (tmp_data[tmpcount]!='\n'){ t1 = tmp_data[tmpcount]; tmpcount++; if( t1 >= '0' && t1 <= '9' ) { // If character is 0-9 convert it to num if( count < 20) { rs_line[count] = t1-'0'; count++; } } if( t1 >= 'A' && t1 <= 'F' ) { // If character A-F convert to 10-15 if( count < 20) { rs_line[count] = t1-'A'+10; count++; } } } // If character is a line break send packet for( count = 0; count <10; count++ ) { // Convert from 16*4 to 8*8 t1 = (rs_line[count*2])<<4; t2 = rs_line[count*2+1]; rs_data[count] = t1 | t2; } count = 0; tmpcount = 0; if( nrf_Transmission_Done == 1 ) { TX_packet_BASE(rs_data); // Send packet. LED_Toggle(LED_Yellow); } }//if msg flag has been raised }//while }//main