/************ Solid PWM ************/ void sol_pwm(DigitalOut pwm, int r_turn, int l_turn) { for(i=0; i<10; i++) { pwm = 1; wait_us(r_turn); pwm = 0; wait_ms(26); } for(i=0; i<10; i++) { pwm = 1; wait_us(1525); pwm = 0; wait_ms(26); } for(i=0; i<10; i++) { pwm = 1; wait_us(l_turn); pwm = 0; wait_ms(26); } for(i=0; i<10; i++) { pwm = 1; wait_us(1525); pwm = 0; wait_ms(26); } }
/************ Liquid PWM ************/ void liq_pwm(DigitalOut pwm, int drop) { for(i=1000; i<drop; i=i+25) { pwm = 1; wait_us(i); pwm = 0; wait_ms(26); } wait(3); for(i=0; i<40; i++) { pwm= 1; wait_us(1300); pwm = 0; wait_ms(26); } wait(3); for(i=1300; i>1000; i=i-25) { pwm= 1; wait_us(i); pwm = 0; wait_ms(26); } wait(7); }
void reset_rn42() { P3OUT &= ~BIT0; wait_us(BLUETOOTH_RESET_HOLD_TIME); P3OUT |= BIT0; wait_us(BLUETOOTH_STARTUP_TIME); }
void VNH5019::clear_fault() { // if ENDIAG is high, then there is no fault if (ENDIAG.read()) return; // toggle the inputs INA = 0; INB = 0; wait_us(250); INA = 1; INB = 1; wait_us(250); // pull low all inputs and wait 1600us for t_DEL INA = 0; INB = 0; PWM = 0; ENDIAG.output(); ENDIAG = 0; wait_us(1600); // and finally re-enable the motor ENDIAG.input(); }
// Based on http://arduinodev.woofex.net/2012/12/01/standalone-sharp-dust-sensor/ float GP2Y10::read() { float voMeasured = 0; float calcVoltage = 0; float dustDensity = 0; led = 0; wait_us(SAMPLING_TIME); voMeasured = analog; // read the dust value wait_us(DELTA_TIME); led = 1; // turn the LED off wait_us(SLEEP_TIME); // 0 - 3.3V mapped to 0 - 1023 integer values // recover voltage calcVoltage = voMeasured * 3.3; // linear eqaution taken from http://www.howmuchsnow.com/arduino/airquality/ // Chris Nafis (c) 2012 dustDensity = 0.17 * calcVoltage - 0.1; return dustDensity; }
uint8_t matrix_scan(void) { if (mcp23018_status) { // if there was an error if (++mcp23018_reset_loop == 0) { // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans // this will be approx bit more frequent than once per second print("trying to reset mcp23018\n"); mcp23018_status = init_mcp23018(); if (mcp23018_status) { print("left side not responding\n"); } else { print("left side attached\n"); frenchdev_blink_all_leds(); } } } #ifdef DEBUG_MATRIX_SCAN_RATE matrix_scan_count++; uint32_t timer_now = timer_read32(); if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) { print("matrix scan frequency: "); pdec(matrix_scan_count); print("\n"); matrix_timer = timer_now; matrix_scan_count = 0; } #endif for (uint8_t i = 0; i < MATRIX_ROWS; i++) { select_row(i); wait_us(30); // without this wait read unstable value. matrix_row_t cols = read_cols(i); if (matrix_debouncing[i] != cols) { matrix_debouncing[i] = cols; if (debouncing) { debug("bounce!: "); debug_hex(debouncing); debug("\n"); } debouncing = DEBOUNCE; } unselect_rows(); } if (debouncing) { if (--debouncing) { wait_us(1); // this should be wait_ms(1) but has been left as-is at EZ's request } else { for (uint8_t i = 0; i < MATRIX_ROWS; i++) { matrix[i] = matrix_debouncing[i]; } } } matrix_scan_quantum(); return 1; }
int main() { float duration = 0.0; float distance = 0.0; status = false; while(1) { triggerpin = 0; wait_us(1); // Wait for 1us for clean low pulse triggerpin = 1; wait_us(10); // Trigger Pulse of 10us triggerpin = 0; while(!echopin); //Poll echo pin for High timer.start(); //start Timer while(echopin); //Poll echo pin for low timer.stop(); //Stop timer duration = (float)(timer.read_us()); //Read Timer Value = Echo High Time //terminal.printf("\n Timer Reading cm : %f \n", duration); distance = cal_distance(duration); terminal.printf("\n Object Distance cm : %f \n", distance); timer.reset(); //Reset Timer wait(1); // Repeat Trigger after 1sec } }
int usbSendBytes(uchar *data,int size) { if(size==0) wait_us(140); // 0byte packet対策. /* Last transmission hasn't finished, abort */ if (countTx) { return 0; } if( GetEPTxStatus(ENDP1) == EP_TX_VALID) return 0; if( size >= MAX_SEND_BYTES_CDC) { size = MAX_SEND_BYTES_CDC; } UserToPMABufferCopy(data, ENDP1_TXADDR, size); SetEPTxCount(ENDP1, size); countTx += size; if(size==0) countTx++; // 0byte packet対策. SetEPTxValid(ENDP1); if(size==0) wait_us(140); // 0byte packet対策. return size; }
int CHARMA::lireCharma() { int i = 0, j = 0, fin = 0; for(i = 0; i < 64; ++i){ buf[i] = '\0'; } while (!sepI || !sigI){ wait_ms(delai_ms / 2); } wait_us(delai_ms * 1000 / 4); while (sepI && sigI){ wait_ms(delai_ms / 2); } wait_us(delai_ms * 1000 / 4); for(i = 0; i < 64 && fin == 0; ++i){ for(j = 0; j < 8; ++j){ buf[i] = buf[i] | (sigI << j); while(!sepI){ wait_ms(delai_ms/2); } while (sepI){ wait_ms(delai_ms/2); } wait_us(delai_ms * 1000 / 2); } if (buf[i] == '~'){ fin = 1; } } return 0; }
int main() { async_portTX.baud(9600); async_portRX.baud(9600); while (1) { switch_wordTX=0xA0; if (switch1==1) switch_wordTX=switch_wordTX|0x01; if (switch2==1) switch_wordTX=switch_wordTX|0x02; strobe=1; wait_us(10); strobe=0; async_portTX.putc(switch_wordTX); if (async_portTX.readable()==1) recd_valTX=async_portTX.getc(); myled1 = 0; myled2 = 0; if (recd_valTX&0x01) myled1=1; if (recd_valTX&0x02) myled2=1; switch_wordRX=0xA0; if (switch3==1) switch_wordRX=switch_wordRX|0x01; if (switch4==1) switch_wordRX=switch_wordRX|0x02; strobe=1; wait_us(10); strobe=0; async_portRX.putc(switch_wordRX); if (async_portRX.readable()==1) recd_valRX=async_portRX.getc(); myled3 = 0; myled4 = 0; if (recd_valRX&0x01) myled3=1; if (recd_valRX&0x02) myled4=1; } }
/********************************************************************* * TBLPTR から1バイト読み出し. TBLPTRはポスト・インクリメントされる. ********************************************************************* */ static uchar GetData8(void) { uchar i,data8=0; SetCmdN(b_1001,4); SetCmdN(0 , 8); // return pic18_GetData8b(); /********************************************************************* * 8 bit 受信. LSBファースト. ********************************************************************* static uchar pic18_GetData8b(void) { */ SetPGDDir(0); // PGD=in // for(i=0;i<8;i++,mask<<=1) { // if( GetCmdb1() ) { // data8 |= mask; // } i=8; do { set_bit(PGC,1); data8>>=1; wait_us(1); // if(inPGD) { if(digitalRead(inPGD)) { data8 |= 0x80; } set_bit(PGC,0); wait_us(1); } while(--i); SetPGDDir(1); // PGD=out return data8; }
//写一个字节到DS18B20 //dat:要写入的字节 void DS18B20_Write_Byte(u8 dat) { u8 j; u8 testb; DS18B20_IO_OUT();//SET PA0 OUTPUT; for (j=1;j<=8;j++) { testb=dat&0x01; dat=dat>>1; if (testb) { DS18B20_DQ_OUT=0;// Write 1 wait_us(2); DS18B20_DQ_OUT=1; wait_us(60); } else { DS18B20_DQ_OUT=0;// Write 0 wait_us(60); DS18B20_DQ_OUT=1; wait_us(2); } } }
/********************************************************************* * 4 bit 送信 , 最後の PGC_H時間を指定. ********************************************************************* */ void SetCmdNLong(uchar cmdn,uchar len,uchar ms) { uchar i; //,b; for(i=0; i<(len-1); i++) { // b = cmdn & 1; // SetCmdb1(b); // cmdn >>=1; { if(cmdn&1) set_bit(PGD,1); else set_bit(PGD,0); set_bit(PGC,1); wait_us(1); cmdn >>=1; set_bit(PGC,0); } } { // SetCmdb1Long(cmdn & 1,ms); { if(cmdn&1) set_bit(PGD,1); else set_bit(PGD,0); set_bit(PGC,1); if(ms) wait_ms(ms); wait_us(1); cmdn >>=1; set_bit(PGC,0); } } }
/*------------------------------------------------------- * 复位1-wire总线,并探测是否有温度芯片DS18B20(TO-92 * 封装)挂在总线上,有返回SUCC,没有返回FAIL */ BOOL ds18b20_reset(void) { BOOL bus_flag; DQ_TO_0(); // 设置1-wire总线为低电平(占领总线)... /* 现在延迟480us~960us, 与硬件密切相关,但应尽可能选小值(480us), 把抖动留给系统(比如在延迟期间发生中断导致延迟变长)。 */ wait_us(490); // 490us cli(); // 下面这段时间要求比较严格,为保险起见,关中断 DQ_TO_1(); // 设置1-wire总线为高电平(释放总线) /* 这个浮点数是由编译器计算好的,而不是由你的MCU在运行时临时计算的, 所以不会占用用户MCU的时间,不必担心(看看前面的宏你就可以确定了) */ wait_us(67.5); // 最佳时间: 60us+7.5us!(忙延时,只是一种策略) // 探测总线上是否有器件 if(DQ_status()) bus_flag=FAIL; // 复位单总线但没有发现有器件在线 else bus_flag=SUCC; // 复位单总线并发现有器件在线 sei(); // 退出临界代码区(开中断) /* 保证Master释放总线的时间(不是说总线处于高电平的时间)不小于 480us即可,这一时间从读总线状态之前就开始了,所以这里把这个 时间计算在内。在Master释放总线的前半段,也是被动器件声明它们在线之时。 */ wait_us(490-67.5); // 490-67.5us return(bus_flag); }
bool DHT22::sample() { DigitalInOut DHT22(_data_pin); int dht22_dat [5]; DHT22.output(); DHT22.write(0); wait_ms(18); DHT22.write(1); DHT22.input(); wait_us(40); wait_us(80); int i,j,result=0; for (i=0; i<5; i++) { result=0; for (j=0; j<8; j++) { while (DHT22); while (!DHT22); wait_us(50); int p; p=DHT22; p=p <<(7-j); result=result|p; } dht22_dat[i] = result; } int dht22_check_sum; dht22_check_sum=dht22_dat[0]+dht22_dat[1]+dht22_dat[2]+dht22_dat[3]; dht22_check_sum= dht22_check_sum%256; if (dht22_check_sum==dht22_dat[4]) { _humidity=dht22_dat[0]*256+dht22_dat[1]; _temperature=dht22_dat[2]*256+dht22_dat[3]; return true; } return false; }
int main() { int i, j; setup(); while(1) { i = 0; while(i < D_SIZE) { j = 0; if(data[i]) { while(j < FREQ/(D_SIZE * BEACON) ) { wait_us(1000000/(2*FREQ) ); LPC_GPIO2->FIOCLR = IR_ARRAY; wait_us(1000000/(2*FREQ) ); LPC_GPIO2->FIOSET = IR_ARRAY; j++; } } else { while(j < FREQ/(D_SIZE * BEACON) ) { wait_us(1000000/FREQ); j++; } } i++; } } }
inline void SetCmdb1(uchar b) { if(b) set_bit(PGD,1); else set_bit(PGD,0); wait_us(1); set_bit(PGC,1);wait_us(1); set_bit(PGC,0);wait_us(1); }
void flash25spi::enableWrite() { _enable->write(0); wait_us(1); _spi->write(0x06); wait_us(1); _enable->write(1); }
//复位DS18B20 void DS18B20_Rst(void) { DS18B20_IO_OUT(); //SET PA0 OUTPUT DS18B20_DQ_OUT=0; //拉低DQ wait_us(750); //拉低750us DS18B20_DQ_OUT=1; //DQ=1 wait_us(15); //15US }
void LED_Hi_Bit() { mDIN_H=1; mDIN_L=1; wait_us(mWait_US); mDIN_H=0; mDIN_L=1; wait_us(mWait_US); }
void LED_Low_Bit() { mDIN_H=0; mDIN_L=0; wait_us(mWait_US); mDIN_H=0; mDIN_L=1; wait_us(mWait_US); }
int flash25spi::readStatus() { _enable->write(0); wait_us(1); _spi->write(0x5); int status=_spi->write(0x00); wait_us(1); _enable->write(1); return status; }
void flash25spi::clearMem() { enableWrite(); _enable->write(0); wait_us(1); _spi->write(0xc7); wait_us(1); _enable->write(1); waitForWrite(); }
//static void wait_ms(uchar ms) { do { wait_us(250); wait_us(250); wait_us(250); wait_us(250); }while(--ms); }
int nRF24L01P::write(int pipe, char *data, int count) { // Note: the pipe number is ignored in a Transmit / write // // Save the CE state // int originalCe = ce_; disable(); if ( count <= 0 ) return 0; if ( count > _NRF24L01P_TX_FIFO_SIZE ) count = _NRF24L01P_TX_FIFO_SIZE; // Clear the Status bit setRegister(_NRF24L01P_REG_STATUS, _NRF24L01P_STATUS_TX_DS); nCS_ = 0; int status = spi_.write(_NRF24L01P_SPI_CMD_WR_TX_PAYLOAD); for ( int i = 0; i < count; i++ ) { spi_.write(*data++); } nCS_ = 1; int originalMode = mode; setTransmitMode(); enable(); wait_us(_NRF24L01P_TIMING_Thce_us); disable(); while ( !( getStatusRegister() & _NRF24L01P_STATUS_TX_DS ) ) { // Wait for the transfer to complete } // Clear the Status bit setRegister(_NRF24L01P_REG_STATUS, _NRF24L01P_STATUS_TX_DS); if ( originalMode == _NRF24L01P_MODE_RX ) { setReceiveMode(); } ce_ = originalCe; wait_us( _NRF24L01P_TIMING_Tpece2csn_us ); return count; }
inline uchar GetCmdb1(void) { uchar b=0; set_bit(PGC,1); wait_us(1); // b = get_port(Pinb) & (1<<PGD); if(inPGD) b=1; set_bit(PGC,0); wait_us(1); return b; }
void Init_SPITemp420() { char cmdByte, cfgByte; // Setup the spi for 8 bit data, high steady state clock, // second edge capture, with a 1MHz clock rate spitemp420.format(8,0); spitemp420.frequency(5000); // deSelect the device by setting chip select high cs420A=1; cs420B=1; cstemp=1; // Initialize ADT7310 cstemp=0; cmdByte=0x08; // to write cfg register cfgByte=0x40; //01000000, 1 SPS mode spitemp420.write(cmdByte); wait_us(10); // wait 10us for sending cmdByte spitemp420.write(cfgByte); wait_us(10); // wait 10us for sending cmdByte // cmdByte=0x54;// 01010100, continuous read // spitemp420.write(cmdByte); cstemp=1; // Initialize AD420 /* 4-20mA test codes */ /* while(1) { // readTemp(); // wait_ms(10); // set420mAOutput(4.000488, 1); set420mAOutput(4.000488, 1); // set420mAOutput(8, 2); wait_ms(25); set420mAOutput(8.000488, 1); wait_ms(25); set420mAOutput(10.000, 1); wait_ms(25); set420mAOutput(14.000, 1); wait_ms(25); set420mAOutput(18.000, 1); wait_ms(25); set420mAOutput(19.000, 1); wait_ms(75); } */ printf("Init SPI temperature sensor & AD420...OK\n"); }
long HCSR04::echo_duration() { timer.reset(); //reset timer trig = 0; // trigger low wait_us(2); // wait trig = 1; // trigger high wait_us(10); trig = 0; // trigger low while(!echo); // start pulseIN timer.start(); while(echo); timer.stop(); return timer.read_us(); }
flash25spi::flash25spi(SPI *spi, PinName enable) { unsigned char chipid[3] = {0}; unsigned int i = 0; _spi=spi; _enable=new DigitalOut(enable); _enable->write(1); // wait_us(1000); _enable->write(0); wait_us(1); // send address _spi->write(0x9f); chipid[0] = _spi->write(0); // get vendor ID chipid[1] = _spi->write(0); // get device ID chipid[2] = _spi->write(0); // get capacity wait_us(1); _enable->write(1); _size = 0; #ifdef DEBUG printf ("got flash ids: %x, %x, %x\n", chipid[0], chipid[1], chipid[2]); #endif while (_size == 0) { #ifdef DEBUG printf ("checking: %x, %x, %x\n", devices[i].vendor, devices[i].device, devices[i].capacity); #endif if (devices[i].vendor == 0) { printf("flash device not found\n"); return; } if ((chipid[0] == devices[i].vendor) && (chipid[1] == devices[i].device) && (chipid[2] == devices[i].capacity)) { _size=devices[i].memsize; _blockSize=devices[i].blocksize; _sectorSize=devices[i].sectorsize; _pageSize=devices[i].pagesize; #ifdef DEBUG printf("device found: %x - %x, %x, %x, %x\n",i, _size, _blockSize, _sectorSize, _pageSize); #endif } else i++; } return; }
//从DS18B20读取一个位 //返回值:1/0 u8 DS18B20_Read_Bit(void) // read one bit { u8 data; DS18B20_IO_OUT();//SET PA0 OUTPUT DS18B20_DQ_OUT=0; wait_us(2); DS18B20_DQ_OUT=1; DS18B20_IO_IN();//SET PA0 INPUT wait_us(12); if(DS18B20_DQ_IN)data=1; else data=0; wait_us(50); return data; }