Skip to content

Instantly share code, notes, and snippets.

@wildan3105
Created August 16, 2017 04:27
Show Gist options
  • Save wildan3105/d926a5b09dc21aaae6f4f11fe4d036af to your computer and use it in GitHub Desktop.
Save wildan3105/d926a5b09dc21aaae6f4f11fe4d036af to your computer and use it in GitHub Desktop.
#define FCY 8000000UL // FCY = FOSC / 2
#include <xc.h>
#include <stdio.h>
#include <libpic30.h>
#include <p30F2020.h>
#include "config.h"
int main(void)
{
int channel1Result;
int channel3Result;
/* Set up the ADC Module */
ADCONbits.ADSIDL = 0; /* Operate in Idle Mode */
ADCONbits.FORM = 0; /* Output in Integer Format */
ADCONbits.EIE = 1; /* Enable Early Interrupt */
ADCONbits.ORDER = 0; /* Even channel first */
ADCONbits.SEQSAMP = 1; /* Sequential Sampling Enabled */
ADCONbits.ADCS = 5; /* Clock Divider is set up for Fadc/14 */
ADPCFG = 0xFFFC; /* AN0 and AN1 are analog inputs */
ADSTAT = 0; /* Clear the ADSTAT register */
ADCPC0bits.TRGSRC0 = 1; /* Use SW trigger */
ADCPC0bits.IRQEN0 = 1; /* Enable the interrupt */
ADCONbits.ADON = 1; /* Start the ADC module */
/* Set up the Interrupts */
IFS0bits.ADIF = 0; /* Clear AD Interrupt Flag */
IPC2bits.ADIP = 4; /* Set ADC Interrupt Priority */
IEC0bits.ADIE = 1; /* Enable the ADC Interrupt */
ADCPC0bits.SWTRG0 = 1; /* Trigger the Conversion Pair 0 */
int a1;
int a2;
// enable UART communication
U1BRG = 12; // baudrate
U1MODEbits.UARTEN = 1; // enable UART
while (1)
{
while(ADCPC0bits.PEND0); /* Wait for the 2nd conversion to
complete */
channel1Result = ADCBUF1; /* Read the result of the second
conversion */
a1 = channel1Result;
a2 = channel1Result;
ADCPC0bits.SWTRG0 = 1; /* Trigger another conversion */
printf("Analog 1 : %d \n", a1);
printf("Analog 2 : %d \n", a2);
__delay_ms(1000);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment