// Read adc value and output value as a frequency to clk L297
#include <p18f2420.h>
#include <adc.h> //the code for the adc routine is in here
#include <stdlib.h>
#include <delays.h> //the code for the delay routines is in here
#pragma config OSC = INTIO7 //Internal oscillator with Fosc/4 on RA6, I/O on RA7
#pragma config WDT = OFF
int result; //put this here to clr the 'out of scope' msg in watch window
#define clk PORTAbits.RA5 //pin 7
void clksig (void) //clk signal to L297 stepper controller inversely proportional to ADRESH
{
clk = 1;
Delay1KTCYx(result); //delay = 1000 x instruction cylce time x ADRESH
clk = 0;
Delay1KTCYx(result);
}
void main (void)
{
OSCCON = 0b01100010; //set Fosc = 4Mhz
TRISA = 0x1;
TRISAbits.TRISA0=1; // ADC input on pin 2
OpenADC(ADC_FOSC_RC &
ADC_LEFT_JUST &
ADC_0_TAD, ADC_CH0 &
ADC_INT_OFF &
ADC_VREFPLUS_VDD &
ADC_VREFMINUS_VSS, 0 );
while (1)
{
ConvertADC(); // Start conversion
while(BusyADC()); // Wait for completion
result = 256 - ADRESH; //result = ADRES high byte (i.e. left justified)
clksig();
}
}