dsPIC30f4013 ADC not working

Status
Not open for further replies.

P.Copper

Member level 5
Joined
Mar 18, 2013
Messages
82
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Visit site
Activity points
1,807
Hi all

I want to test the ADC of dsPIC30f4013, what i really want to see at the output is to control an LED brightness by just using a variable resistor. I connected an LED to pin 3 of PORTD and my analog input is all PORTB pins. for analog input I'm supplying a 47K potentiometer with 2 volts(DC). the problem i'm having is i run the code but nothing happens...... here's the code

Code:
#include "p30f4013.h"
#include <libpic30.h>


//*******SYSTEM CONFIGURATION BITS*******
_FOSC(CSW_FSCM_OFF & FRC);     //Internal fast RC oecillator
_FWDT(WDT_OFF);                //Disable the watcdog timer
_FBORPOR(MCLR_EN & PWRT_OFF);  //Enable the master clear pin
_FGS(CODE_PROT_OFF); // Disable code protection



void configure_pins();
unsigned int read_analog_channel(int n);

int main()
{
	long int n;

	// Set up which pins are which
	configure_pins();

	while(1)
	{
		// Read the analog channel. The result is an
		// integer between 0 and 1023 inclusive.
		n = read_analog_channel(0);

		// LED on
		_LATD0 = 1;
		__delay32(30*n); // m us

		// LED off
		_LATD0 = 0;
		__delay32(30*(1023 - n)); // (1023-m) us
	}

	return 0;
}

void configure_pins()
{
	// Configure digital I/O
	LATD = 0;
	TRISD = 0b11111110;

	// Configure analog inputs
	TRISB = 0x01FF;      // Port B all inputs
	ADPCFG = 0xFF00;     // Lowest 8 PORTB pins are analog inputs
	ADCON1 = 0;          // Manually clear SAMP to end sampling, start conversion
	ADCON2 = 0;          // Voltage reference from AVDD and AVSS
	ADCON3 = 0x0005;     // Manual Sample, ADCS=5 -> Tad = 3*Tcy = 0.1us
	ADCON1bits.ADON = 1; // Turn ADC ON
}

// This function reads a single sample from the specified
// analog input. It should take less than 2.5us if the chip
// is running at about 30 MIPS.
unsigned int read_analog_channel(int channel)
{
	ADCHS = channel;          // Select the requested channel
	ADCON1bits.SAMP = 1;      // start sampling
	__delay32(30);            // 1us delay @ 30 MIPS
	ADCON1bits.SAMP = 0;      // start Converting
	while (!ADCON1bits.DONE); // Should take 12 * Tad = 1.2us
	return ADCBUF0;
}
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…