Basically, I'm looking to program a PIC16F917, so that a particular int will increment by one every time I press a button.
Now I got the ADC up and running, I got loops to count, but I seem to be lacking the knowledge and experience to put it altogether, and latch the input back to zero before the int increases by a number greater than one.
The following code is what I have written so far. The problem with it is that it increments multiple times per push (as many delay cycles that the button was held for), and I'd love to make it only count by one. I was thinking of creating a new function and int but I just can't seem to put it together - much help needed.
Thanks for helping, Ezra
#include <xc.h>
#include "config-bits.h"
#define _XTAL_FREQ 4000000
int result;
int output;
void delay()
{
int i;
for(i=0; i<100; i++)
{
/*Timer Stuff*/
}
}
int main()
{
TRISA=1; //Set all pins to input
TRISB=1;
TRISC=1;
TRISD=1;
TRISE=1;
TRISDbits.TRISD0 = 0; //LED output
ANSELbits.ANS0 = 1; //Select ADC input
ADCON0bits.ADFM = 1; //ADC result is right justified
ADCON0bits.VCFG = 0; //Vdd is the +ve reference
ADCON1bits.ADCS = 0b001; //Fosc/8 is the conversion clock
//This is selected because the conversion
//clock period (Tad) must be greater than 1.5us.
//With a Fosc of 4MHz, Fosc/8 results in a Tad
//of 2us.
ADCON0bits.CHS = 0; //select analog input, AN2
ADCON0bits.ADON = 1; //Turn on the ADC
while(1)
{
delay(); //Wait the acquisition time (about 5us).
ADCON0bits.GO = 1; //start the conversion
while(ADCON0bits.GO==1){}; //wait for the conversion to end
result = (ADRESH<<8)+ADRESL; //combine the 10 bits of the conversion
if(result > 512)
{
PORTD=output++;
delay();
}
else
{
NOP();
}
}
}