/* --- 1-wire functions --- */ unsigned char OneWire_Reset() { /* - held low for more than 480µs - release for 480 us -- slave waits 15-60 us -- presence pulse 60-240 us */ //PE0 - VCC - PushPull out Slow PE_DDR_bit.DDR0 = 1; PE_CR1_bit.C10 = 1; PE_CR2_bit.C20 = 0; PE_ODR_bit.ODR0 = 1; //PE1 as OpenDrain output fast mode PE_DDR_bit.DDR1 = 1; PE_CR1_bit.C11 = 0; PE_CR2_bit.C21 = 1; //PE2 - GND - PushPull out Slow PE_DDR_bit.DDR2 = 1; PE_CR1_bit.C12 = 1; PE_CR2_bit.C22 = 0; PE_ODR_bit.ODR2 = 0; //reset impulse PE_ODR_bit.ODR1 = 0; Delayus(500); PE_ODR_bit.ODR1 = 1; Delayus(100); unsigned char presence = PE_IDR_bit.IDR1; Delayus(400); return presence; }
void ClockWise(uint16_t ID,uint16_t speed,uint16_t angle) /* 舵机模式 0<= speed<= 100 0<= angle<=1023 */ { uint16_t buf2,buf1; buf1=speed; speed_H=(uint8_t)(buf1>>8); speed_L=(uint8_t)buf1; buf2=angle; angle_H=(u8)(buf2>>8); angle_L=(u8)buf2; USART1_Config(); Delayus(10000); Send(0xFF); Send(0xFF); Send(ID); Send(0x07); Send(0x03); Send(0x1E); Send(angle_L); Send(angle_H); Send(speed_L); Send(speed_H); Sum=~(ID+0x07+0x03+0x1E + angle_H+angle_L+speed_L+speed_H); Send(Sum); RECEIVE_Config(); Delayus(10000); }
u8 CTMU_isPressed(u8 channel) { u16 Vread; //storage for reading CTMU_init(); CTMUCONHbits.CTMUEN = 1; // Enable the CTMU CTMUCONLbits.EDG1STAT = 0; // Set Edge status bits to zero CTMUCONLbits.EDG2STAT = 0; CTMUCONHbits.IDISSEN = 1; // Drain charge on the circuit Delayus(ETIME); // Wait 125us CTMUCONHbits.IDISSEN = 0; // End drain of circuit CTMUCONLbits.EDG1STAT = 1; // Begin charging the circuit using CTMU current source Delayus(ETIME); // Wait 125us CTMUCONLbits.EDG1STAT = 0; // Stop charging circuit Vread = analogread(channel); /* PIR1bits.ADIF = 0; // make sure A/D Int not set ADCON0bits.GO=1; // and begin A/D conv. while(!PIR1bits.ADIF); // Wait for A/D convert complete Vread = ADRES; // Get the value from the A/D PIR1bits.ADIF = 0; // Clear A/D Interrupt Flag */ if (Vread < OPENSW - TRIP) { return PRESSED; } else if (Vread > OPENSW - TRIP + HYST) { return UNPRESSED; } }
void Baud_Config(uint16_t ID,uint32_t baud) /* 前六个波特率断电保存 */ { uint32_t rate; rate=baud; switch(rate) { case 1000000 : baud=1;break; case 500000 : baud=3;break; case 250000 : baud=7;break; case 115200 : baud=16;break; case 57600 : baud=34;break; case 19200 : baud=103;break; default:baud=2000000/rate-1;break; } USART1_Config(); Delayus(10000); Send(0xFF); Send(0xFF); Send(ID); Send(0x03); Send(0x03); Send(0x04); Send(baud); Sum=~(0x03+0x07+ID+baud); Send(Sum); RECEIVE_Config(); Delayus(10000); }
float CTMU_getVoltage(u8 channel) { u8 j; u16 VTot = 0, Vread = 0; float Vavg, Vcal; CTMU_init(); for(j=0;j<10;j++) { CTMUCONHbits.IDISSEN = 1; // drain charge on the circuit Delayus(ETIME); // Wait 125us CTMUCONHbits.IDISSEN = 0; // end drain of circuit CTMUCONLbits.EDG1STAT = 1; // Begin charging the circuit Delayus(ETIME); // Wait 125us CTMUCONLbits.EDG1STAT = 0; // Stop charging circuit using CTMU current source Vread = analogread(channel); /* PIR1bits.ADIF = 0; // make sure A/D Int not set ADCON0bits.GO=1; // and begin A/D conv. while(!PIR1bits.ADIF); // Wait for A/D convert complete Vread = ADRES; // Get the value from the A/D PIR1bits.ADIF = 0; // Clear A/D Interrupt Flag */ VTot += Vread; // Add the reading to the total } Vavg = (float)VTot / 10.000; // Average of 10 readings Vcal = (float)(Vavg / ADSCALE * ADREF); return Vcal; }
int main(void) { Init(); Init_port(); Init_buzzer(); while(1) { PR3=10; while(PR3<0x2000) { OC4RS = PR3/2; PR3++; Delayus(1000); } while(PR3>10) { OC4RS = PR3/2; PR3--; Delayus(1000); } } Delayms(1000); }
void Delayms(unsigned int msec){ if (msec <= 65) { Delayus(msec*1000); } else { unsigned int cycles = msec /64; unsigned int finale = msec %64; for (unsigned int i=0; i < cycles ; i++) Delayus(64000); Delayus(finale*1000); } }
unsigned char OneWire_ReadBit() { /* - low for 10 us - release and read - wait 50 us */ PE_ODR_bit.ODR1 = 0; Delayus(10); PE_ODR_bit.ODR1 = 1; Delayus(1); unsigned char ow_bit = PE_IDR_bit.IDR1; Delayus(60); return ow_bit; }
//#============================================================================= //# Delayms //#----------------------------------------------------------------------------- void Delayms(unsigned int t) { unsigned int T = 0; while ( (T++) < t ) { Delayus(999); } } //Delayms
void Shift(unsigned char* pixel) { unsigned char i = 0; for (i = 0; i < 192; i++) { if (pixel[i] & 0x01) SHIFT_DATA = 1; else SHIFT_DATA = 0; SHIFT_CLOCK = 1; Delayus(1); SHIFT_CLOCK = 0; } /* Latch after 192 clock pulses */ SHIFT_STROBE = 1; Delayus(1); SHIFT_STROBE = 0; }
int Fpc1020_init(void) { unsigned char Ret=0xff; unsigned char Hw_id[2] = {0}; Fpc1020_reset(); //复位FPC1020 Ret = 0x55; Fpc1020Select(); //Chip select signal to sensor Active (low) Fpc1020Transfer(20); Ret = Fpc1020Transfer(0); Fpc1020UnSelect(); //Chip select back up. Delayus(100); Fpc1020Select(); //Chip select signal to sensor Active (low) Fpc1020Transfer(252); //Send command for reading the interrupt ID Hw_id[0]= Fpc1020Transfer(0); Hw_id[1]= Fpc1020Transfer(0); Fpc1020UnSelect(); //Chip select back up. if(Hw_id[0]==0x02&&Hw_id[1]==0x0a) Ret = ERR_OK; Fpc1020_write_sensor_setup(); return Ret; }
void Eraseleds(void) { unsigned char g = 0; for (g = 0; g < 192; g++) { SHIFT_DATA = 0; SHIFT_CLOCK = 1; Delayus(1); SHIFT_CLOCK = 0; Delayus(1); } SHIFT_DATA = 0; SHIFT_STROBE = 1; Delayus(1); SHIFT_STROBE = 0; Delayms(1); }
void MainLoop() { width = 1; char data[width]; //register to hold letter sent and received int i = 0; while (1) { PORTD; while (!(nrf24l01_irq_pin_active())); while (!nrf24l01_irq_rx_dr_active()); LED2 = ~LED2; nrf24l01_read_rx_payload(data, width); //get the payload into data nrf24l01_irq_clear_all(); //clear interrupts again for (i = 0; i < width; i++) { sprintf(RS232_Out_Buffer, "%c", data[i]); putsUART1(RS232_Out_Buffer); } putsUART1("\r\n"); Delayus(130); //wait for receiver to come from standby to RX LED3 = ~LED3; } }
void OneWire_WriteBit(unsigned char ow_bit) { /* 60+ us slot + 1+ us recovery 1: release in 15 us (prefer 10) 0: hold low whole time */ PE_ODR_bit.ODR1 = 0; Delayus(10); if (ow_bit == 0) { Delayus(50); PE_ODR_bit.ODR1 = 1; } else { PE_ODR_bit.ODR1 = 1; Delayus(50); } }
/****************************************************************************** * Function: void SetupADCToReadVddAndVpp(void) * * Overview: Setup Analog to Digital converter to read Vdd and Vpp. * The original PK2 code used an timer interrupt (every * 125usecs) to read Vdd (the ADC was set up to read Vdd * before the interrupt and the ADC complete interrupt was * diabled). So, when timer1 happened, the ADRESH contained * the (left justified) 8 MSBs of the Vdd. Then at the end of * the timer 1 interrupt we set the ADC to read Vpp and enabled * the interrupt on completion. When the ADC interrupted, the * Vpp value was available in ADRESH. So effectively, every * 125 usecs we got both a sample of Vdd and Vpp. * * In PK3 to avoid using an extra timer for this we use the * alternating input selection. We select Vdd as one channel * and then the ADC will switch to measuring the second * channel (Vpp). Also, to mimic the 125 usec interval in PK2, * we setup the ADC to give us an interrupt at the end of the * sampling of Vdd and Vpp. Since the ADC is fast, we ask the * ADC to also sample more than once each voltage. * * PreCondition: None * * Input: None * * Output: None * * Side Effects: None * * Note: None *****************************************************************************/ void SetupADCToReadVddAndVpp(void) { AD1CON1 = 0x0000; // SAMP bit = 0 ends sampling, select integer format // and starts converting AD1CHS0 = 0x0302; // AN2 (Vdd)->channel A, AN3 (Vpp)->channel B, use AVss as Vr- AD1CON2 = 0x0005; // Select AVdd and AVss for Vr+ and Vr-, no channel scan on mux A, // use a single 16 word buffer, interrupt after 2 conversions (one for // Vdd and one for Vpp), use alternate sampling. // We want to convert 2 samples every 125 usecs // So, for each sample: // 62.5usecs = 12TAD + SAMP*TAD. SAMP is the AD1CON3<SAMC4:SAMC0> bits. // And we have that TAD = ADCS*TCY where ADCS is the AD1CON3<ADCS7:ADCS0> bits. // 62.5usecs = 12*ADCS*TCY + SAMP*ADCS*TCY // 62.5usecs = ADCS*TCY*(12 + SAMP) // If we fix the SAMP to say 16 (we let ADCS be the 'variable' since it has a bigger // range). And Tcy = 62.5nsecs (1/16MIPS) // 62.5usecs = ADCS*62.5nsecs*28 // Then ADCS = 62.5e-6/(62.5e-9*28) = 36 = 0x24 AD1CON3 = 0x1024; // Autosample every 16 TADs and a TAD is 36*TCy AD1CON1 = 0x00E0; // Integer format, use internal counter to finish sampling, _AD1IF = 0; _AD1IE = 0; // No interrupts until SetVdd or SetVpp are called _AD1IP = 6; // High priority to avoid damaging HW // INTERRUPT PRIORITY LEVEL AD1CON1bits.ASAM = 1; //automatically start sampling after the last conversion) AD1CON1bits.ADON = 1;// turn ADC ON // Give the A2D time to boot up Delayus(200); }
void Delayms(UINT32 ms) { do { Delayus(1000); // 1 ms } while(--ms); }
void Ds18b20_CopyScratchpad() { OneWire_Reset(); Ds18b20_SendByte(SKIP_ROM); Delayus(100); Ds18b20_SendByte(COPY_SCRATCHPAD); //TODO: wait for 1 Delayms(1000); }
//==================================================================================== int Read_Y(void) { int i; WR_CMD(CHY); //while(TP_BUSY); Delayus(10); i=RD_AD(); return i; }
//==================================================================================== static void WR_CMD (unsigned char cmd) { unsigned char buf; unsigned char i; TP_CS(1); TP_DIN(0); TP_DCLK(0); TP_CS(0); for(i=0;i<8;i++) { buf=(cmd>>(7-i))&0x1; TP_DIN(buf); Delayus(5); TP_DCLK(1); Delayus(5); TP_DCLK(0); } }
void Reset_Config(uint8_t ID) /* 复位一个舵机 从电机模式后要复位 */ { USART1_Config(); Delayus(10000); Send(0xFF); Send(0xFF); Send(ID); Send(0x02); Send(0x06); Sum=~(ID+0x02+0x06); Send(Sum); RECEIVE_Config(); Delayus(10000); }
void timer_test(){ Delayus(1000); while(1){ if(get_key(27)){ timer2_disable(); return; } } }
int ADC(int c) { AD1CHS0bits.CH0NA = 0; // Vref- = negative input AD1CHS0bits.CH0SA = c; // AN<c> = positive input AD1CON1bits.SAMP = 1; // start sampling Delayus(10); // sampling time = 10us AD1CON1bits.SAMP = 0; // start conversion while (!AD1CON1bits.DONE); return (ADC1BUF0); }
//---- 毫秒级延时程序----------------------- void Delayms(unsigned int time) { unsigned int i; while(time--) for(i=900;i>0;i--) Delayus(); }
void ChangeID(uint8_t ID) /* 改变舵机的ID 断电保存 0<= ID <=255 */ { USART1_Config(); Delayus(10000); Send(0xFF); Send(0xFF); Send(0xFE); Send(0x04); Send(0x03); Send(0x03); Send(ID); Sum=~(0xFE+0x04+ID+0x03+0x03); Send(Sum); RECEIVE_Config(); Delayus(10000); }
//==================================================================================== static unsigned short RD_AD(void) { unsigned short buf=0,temp; unsigned char i; TP_DIN(0); TP_DCLK(1); for(i=0;i<12;i++) { Delayus(5); TP_DCLK(0); Delayus(5); temp= (TP_DOUT) ? 1:0; buf|=(temp<<(11-i)); Delayus(5); TP_DCLK(1); } TP_CS(1); buf&=0x0fff; return(buf); }
void Ds18b20_ReadScratchpad(char* buffer, unsigned char len){ //byte 0 LSBit first //Init - Read rom - read scratch (up to 9 bytes) //TODO: len must be <=9 OneWire_Reset(); Ds18b20_SendByte(SKIP_ROM); Delayus(100); Ds18b20_SendByte(READ_SCRATCHPAD); for (unsigned int i = 0; i < len; i++){ buffer[i] = Ds18b20_ReadByte(); } }
void Ds18b20_WriteScratchpad(char* buffer, unsigned char len){ // Bytes 2(TH), 3(TL), 4(CONF) only, byte 2 LSBit first // Conf: 0x1F - 9 bit (0.5 C), 0x7F - 12 bit (0.0625 C) // TODO: len must be <=3 OneWire_Reset(); Ds18b20_SendByte(SKIP_ROM); Delayus(100); Ds18b20_SendByte(WRITE_SCRATCHPAD); for (unsigned int i = 0; i < len; i++){ Ds18b20_SendByte(buffer[i]); } }
void Ds18b20_ConvertT(){ /* master can issue “read time slots” after the Convert T command DS18B20 will respond by transmitting 0 while the temperature conversion is in progress and 1 when the conversion is done Does not work with parasite power */ OneWire_Reset(); Ds18b20_SendByte(SKIP_ROM); Delayus(100); Ds18b20_SendByte(CONVERT_T); //TODO: wait for 1 Delayms(1000); }
int Fpc1020_read_Image(unsigned char *cpImage) { unsigned char Ret; int i; unsigned char Regit_value[6]; Fpc1020Select(); //Chip select signal to sensor Active (low) Fpc1020Transfer(28); Regit_value[1] = Fpc1020Transfer(0); // Fpc1020UnSelect(); //Chip select back up. Delayus(100); Fpc1020Select(); //Chip select signal to sensor Active (low) Fpc1020Transfer(FPC1020_CMD_CAPTURE_IMAGE); // Ret = Fpc1020Transfer(0); // Fpc1020UnSelect(); //Chip select back up. Delayus(100); Fpc1020Select(); //Chip select signal to sensor Active (low) Fpc1020Transfer(FPC1020_CMD_READ_IMAGE); // Delayus(100); Fpc1020Transfer(0); for(i=0;i<192*192;i++) { cpImage[i]= Fpc1020Transfer(0); // } Fpc1020UnSelect(); //Chip select back up. memcpy(cpImage,Regit_value,2); Fpc1020Select(); //Chip select signal to sensor Active (low) Fpc1020Transfer(52); // cpImage[1] = Fpc1020Transfer(0); // Fpc1020UnSelect(); //Chip select back up. return ERR_OK; }
//==================================================================================== static u16 RD_AD(void) { u16 buf=0; u8 i; TP_DIN_L(); //TP_DIN(0); TP_DCLK_H(); //TP_DCLK(1); for(i=0; i<12; i++) { buf <<= 1; TP_DCLK_H(); //TP_DCLK(0); Delayus(5); TP_DCLK_L(); //TP_DCLK(1); if(TP_DOUT) buf |= 1; Delayus(5); } TP_CS_H(); //TP_CS(1); //buf>>=4;//只有12位有效 //buf&=0x0fff; return(buf); }