void setupADC() { AD1PCFG = 0xFFFE; // bit 1 is zero, so AN1 is input AD1CHS = 1; // connect bit 0 as input AD1CON3bits.ADCS = 0b10; // ADC clock period is Tad = 2 * (ADCS+1) * Tpb AD1CON1bits.ADON = 1; // turn on A/D converter AD1CON1bits.SSRC = 0b111; } int readADC() { int i; int avg = 0; for (i = 0; i < 5; i++) { AD1CON1bits.SAMP = 1; while (!AD1CON1bits.DONE); avg += ADC1BUF0; } return avg / 5; } void makeSinWave() { // to use this function, the waveform array should be a global variable, and should have a length of 1000 int i; float freq = 2; //frequency in Hz of the sin wave created for (i = 0; i < 1000; i++) { waveform[i] = 1023 * (.25 * sinf(2.0 * M_PI * i * freq / 1000) + 0.5); } }