for the ADC and TMR2 and CCP setups and routines:
should they be done in functions outside the main function and called from within the main loop?
for the to be tested ADC and TMR2 and CCP functionalities I think I will test both TMR2 and CCP last cus of their sheer complexity, but I am finding it hard to basically test it without the other two...
anyways this is what I came up with:
.
.
.
/*INCLUDES AND DEFINITIONS*/
#include <xc.h>
#include <stdint.h>
#define _XTAL_FREQ 4000000
/*VARIABLE DEFFENITION SECTION*/
uint16_t RESULT; // ADC RESULT VARIABLE
#define BIG_SW PORTAbits.RA4 //READ RA4 AND ASSIGN IT TO BIG_SW
#define SMALL_SW PORTAbits.RA5 //READ RA5 AND ASSIGN IT TO SMALL_SW
/*FUNCTION INTITILIZATION SECTION*/
void ADC_INIT()
{
ADCON0bits.CHS=0b000010; // ANALOG CHANNEL SELECTION BITS (RA2)
ADCON1bits.ADCS=0b111; // ADC COVERSION CLOCK SELECTION BITS (ADCRC)
ADCON0bits.ADON=1; // ANALOG CHANNEL SELECTION BITS (ADCON HIGH)
ADCON1bits.ADPREF=0b00; //ADC POSITIVE VOLTAGE REFRENCE CONFIGRATION bits (VREF+ IS VDD)
ADCON1bits.ADFM=1; // ADC RESULT FORMAT SELECTION BITS (RIGHT JUSTIFIED)
}
void ADC_RESULT()
{
ADCON0bits.GOnDONE=1; // ADC CONVERSION STATUS BITS
while(ADCON0bits.GOnDONE==1); // WAIT WHILE ADC STATUS IS ON
RESULT = ADRESH << 8; //Load ADC high value
RESULT |= ADRESL; //Load ADC low value
}
void ADC_TEST()
{
if ((SMALL_SW||BIG_SW)&&(RESULT>512))
{
LATAbits.LATA1 = 1; //LED ON
LATAbits.LATA0 = 1; //LED ON
__delay_ms(1000);
LATAbits.LATA1 = 0; //LED OFF
LATAbits.LATA0 = 0; //LED OFF
__delay_ms(1000);
}
else if(SMALL_SW&&(RESULT<512))
{
LATAbits.LATA1 = 1; //LED ON
}
else if (BIG_SW&&(RESULT<512))
{
LATAbits.LATA0 = 1; //LED ON
}
}
/*MAIN FUNCTION*/
void main(void)
{
OSCFRQ =0b101; // OSCFRQ(HFFRQ0-2) SET OSCILLATOR TO 16MHz
//(BIG_SW&&SMALL_SW)||(!BIG_SW&&!SMALL_SW)
/*TRISx AND ANSELx ASSIGNMENT */
TRISAbits.TRISA0=0; // TRISA0(RA0) BIG LIGHT DRIVE, OUTPUT
TRISAbits.TRISA1=0; // TRISA1(RA1) SMALL LIGHT DRIVE, OUTPUT
TRISAbits.TRISA2=1; // TRISA2(RA2) ANALOG POT, INPUT
TRISAbits.TRISA4=1; // TRISA4(RA4) BIG LIGHT SWITCH, INPUT
TRISAbits.TRISA5=1; // TRISA5(RA5) SMALL LIGHT SWITCH, INPUT
ANSELAbits.ANSA0=0; // ANSELA0(ANSA0) DISABLED, INPUT DIGITAL
ANSELAbits.ANSA1=0; // ANSELA0(ANSA1) DISABLED, INPUT DIGITAL
ANSELAbits.ANSA2=1; // ANSELA2(ANSA2) ANALOG POT, INPUT ANALOG
ANSELAbits.ANSA4=0; // ANSELA4(ANSA4) DISABLED, INPUT DIGITAL
ANSELAbits.ANSA5=0; // ANSELA5(ANSA5) DISABLED, INPUT DIGITAL
/*LATx STEADY-STATE*/
LATAbits.LATA1 = 0;
LATAbits.LATA0 = 0;
/*FUNCTION INTITILIZATION CALL*/
ADC_INIT();
/*MAIN LOOP*/
while(1)
{
if(BIG_SW==SMALL_SW)
{
LATAbits.LATA0 = 0; //LED OFF
LATAbits.LATA1 = 0; //LED OFF
}
else if(SMALL_SW)
{
ADC_RESULT();
ADC_TEST();
}
else if (BIG_SW)
{
ADC_RESULT();
ADC_TEST();
}
}
}
It seems to work pretty nicely however it glitches around the 512 marks, or when if I switch between the big and small switch, but as a proof of operation, I am stuck trying to do the same but for TMR2 and CCP as the individual test function is kinda confusing to write independently from ADC.