void sweep(int *stuff) // takes a pointer to structure containg 3 dimmensional array, sweeps back and forth { // moving servo first up, then down, and reading for each value // reads a "LENGHT" number of sweeps before returning int j = 0; move_servo(10); for(j = 0; j < STEPS; j++) { move_servo(10 + j); _delay_ms(50); stuff[j] = readadc(0); //sprintf(uartBuffer, "%d %d \n" , set(stuff[j]), j); //sendstring(uartBuffer); } for (; j >= 0; j--) { move_servo(10 + j); _delay_ms(50); stuff[j] = (stuff[j] + readadc(0))/2; stuff[j] = set(stuff[j]); //sprintf(uartBuffer, "%d %d \n" , set(stuff[j]), j); //sendstring(uartBuffer); } }
int main() { setup(); int last_x = 0; int last_y = 0; while (true) { int x = readadc(0); int y = readadc(1); if (check(x, last_x) || check(y,last_y)) { printf("x: %d y: %d\n", x, y); } last_x = x; last_y = y; delay(1000); } return 0; }
float adcAverage(int samples, int adcPortNum){ int sampleSet[SAMPLES], i; float average; for (i = 0; i < samples; i++){ if(digitalRead(BUTTON) == LOW){ delay(1); break; } sampleSet[i] = readadc(adcPortNum, SPICLK, SPIMOSI, SPIMISO, SPICS); delay(10); } average = 0; for (i = 0; i < samples; i++){ if(digitalRead(BUTTON) == LOW){ delay(1); break; } average += sampleSet[i]; } average /= samples; return average; }
// // read the wind direction sensor, return heading in degrees // float get_wind_direction() { unsigned int adc; adc = readadc(WIND_DIR_PIN); // get the current reading from the sensor // The following table is ADC readings for the wind direction sensor output, sorted from low to high. // Each threshold is the midpoint between adjacent headings. The output is degrees for that ADC reading. // Note that these are not in compass degree order! See Weather Meters datasheet for more information. if (adc < 380) return (112.5); if (adc < 393) return (67.5); if (adc < 414) return (90); if (adc < 456) return (157.5); if (adc < 508) return (135); if (adc < 551) return (202.5); if (adc < 615) return (180); if (adc < 680) return (22.5); if (adc < 746) return (45); if (adc < 801) return (247.5); if (adc < 833) return (225); if (adc < 878) return (337.5); if (adc < 913) return (0); if (adc < 940) return (292.5); if (adc < 967) return (315); if (adc < 990) return (270); return (-1); // error, disconnected? }
int main(int argc, char *argv[]) { int i, delay_ms; uint32_t x1; int print_output; printf ("SPI test program\n") ; // initialize the WiringPi API if (wiringPiSPISetup (0, 1000000) < 0) return -1 ; if (argc>2) print_output = 0; else print_output = 1; // get the channel to read, default to 0 if (argc>1) delay_ms = atoi(argv[1]); else delay_ms = 1; //we don't use this here... int counter_wrap = 100; if (delay_ms != 0) counter_wrap = 500/delay_ms; int counter = 0; // run until killed with Ctrl-C while (1) { counter++; // read data and add to total int chan; for (chan = 0; chan<8; chan++) { x1 = readadc(chan); if (print_output) { if (counter > counter_wrap) { printf("CH %i val= %d ", chan, x1); } } } if (counter > counter_wrap) { if (print_output) printf("\n"); counter = 0; } delay(delay_ms); } return 0 ; }
int main(void) { TRISD = 0xF100; unsigned short ADCReadings[10000]; int i = 0; // initialize timers and SPI initTimers(); initspi(); TMR3 = 0; // Reset timer int duration = 6250; unsigned short sample; unsigned short received; initadc(2); // use channel 2 (AN2 is RB2) while (1) { while(TMR3 < duration){ // wait } sample = readadc(); PORTD = sample; TMR3 = 0; // reset timer if (i < 10000) { ADCReadings[i] = sample; i++; } // send data over SPI received = spi_send_receive(sample-300);//(sample-150); if (PORTDbits.RD8 == 1) { // we received a pulse! playNote(527, 5); } } }
void Get_Power(void) //电量显示 { Adc_AIN10(); //电量检测 adc_charge = readadc(); //检测电量 }
int main(void){ ADMUX = 0b01000000; ADCSRA = 0x80; char buffer0; char buffer1; char xbeebuffer[20]; char history0[10]; char history1[10]; char data; int i = 0; int j; char slope0 = 0; char slope1 = 0; for(j = 0; j < 10; j ++){ history0[j] = 0; history1[j] = 0; } UCSR0A = 0x02; UCSR0B = 0x18; UCSR0C = 0x06; UBRR0L = 12; DDRD = 0b10110010; PORTD = 0b00110000; while(1){ if(UCSR0A & 0x80){ data = UDR0; //sendchar(data); if(data == 'r'){ //PORTD ^= 0x20; buffer0 = readadc(0)/10; buffer1 = readadc(1)/10; history0[i] = buffer0; history1[i] = buffer1; i ++; if(i == 10){ i = 0; } for(j = 0; j < 9; j ++){ slope0 += (history0[i] - history0[i + 1])/2; slope1 += (history1[i] - history1[i + 1])/2; } //sprintf(xbeebuffer, "%4d %4d %4d %4d\n\r", slope0, buffer0, slope1, buffer1); //sprintf(xbeebuffer, " %d \n\r", buffer); xbeebuffer[0] = 'r'; xbeebuffer[1] = ' '; xbeebuffer[2] = buffer0; xbeebuffer[3] = buffer1; xbeebuffer[4] = 0; sendstring(xbeebuffer); slope0 = 0; slope1 = 0; } } } return 0; }