Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] ADC for PIC32MX250F128D

Status
Not open for further replies.

rajib.das

Member level 3
Joined
Oct 23, 2013
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Hi Guys

I am using mikroC pro for pic32. Using the code
"ADC1_Init();" code from Help ADC Library.
which is not working . Am I missing anything? or Any Suggestion.

Thanks in advance.
 

rajib.das

Member level 3
Joined
Oct 23, 2013
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Yap , It was loading

Target Detected
Device ID Revision = 10000000

Programming...
Programming/Verify complete
Code:
//--------------------------------TEST------------------------------------------

// LCD module connections
sbit LCD_RS at LATA10_bit;
sbit LCD_EN at LATA7_bit;

sbit LCD_D4 at LATC6_bit;
sbit LCD_D5 at LATC7_bit;
sbit LCD_D6 at LATC8_bit;
sbit LCD_D7 at LATC9_bit;

sbit LCD_BackPW at RA8_bit;       //LCD pin 15 vs JP7 pin 12  //5.0 v

sbit LCD_RS_Direction at TRISA10_bit;
sbit LCD_EN_Direction at TRISA7_bit;

sbit LCD_D4_Direction at TRISC6_bit;
sbit LCD_D5_Direction at TRISC7_bit;
sbit LCD_D6_Direction at TRISC8_bit;
sbit LCD_D7_Direction at TRISC9_bit;

sbit LCD_BackPW_Direction at TRISA8_bit;
// End LCD module connections


sbit Enable_Load_A0 at LATA0_bit;
// Pin direction
sbit Enable_Load_A0_Direction at TRISA0_bit;

sbit Enable_Load_A1 at LATA1_bit;
// Pin direction
sbit Enable_Load_A1_Direction at TRISA1_bit;

char txt1[] = "mikroElektronika";
char txt2[] = "LV32MX v6";
char txt3[] = "Lcd4bit";
char txt4[] = "example";

char i;                              // Loop variable

void Move_Delay() {                  // Function used for text moving
  Delay_ms(750);                     // You can change the moving speed here
}

void main(){
  //CHECON = 0x32;
  //AD1PCFG  = 0xFFFF;                 // Configure AN pins as digital I/O
//  ADC1_Init();
  //ANSELA = 0;                         // Configure AN pins as digital
//  ANSELB = 0;
//  ANSELC = 0;
  
//  ADC1BUFA = 1;
//  ADC1BUFB = 1;
//  ADC1BUFC = 1;
  
//      AD1CON1CLR = 0x8000;    // disable ADC before configuration
//    AD1CON1 = 0x00E0;       // internal counter ends sampling and starts conversion (auto-convert), manual sample
//    AD1CON2 = 0;            // AD1CON2<15:13> set voltage reference to pins AVSS/AVDD
//    AD1CON3 = 0x0f01;       // TAD = 4*TPB, acquisition time = 15*TAD
//    AD1CON1SET = 0x8000;    // Enable ADC
//    AD1CON1CLR = 0xFFFF;
//    AD1CON1SET = 0xFFFF;    // Enable ADC
  ADC1_Init();
  //ADC1_Init_Advanced(_ADC_INTERNAL_REF);

  
  //ANSELA = 0;
  Enable_Load_A0 = 0; //Clear PORTD by setting to 0
  Enable_Load_A0_Direction = 0;   // designate PORTB2 pins as output
  
  Enable_Load_A1 = 0; //Clear PORTD by setting to 0
  Enable_Load_A1_Direction = 0;   // designate PORTB2 pins as output

  Lcd_Init();                        // Initialize LCD
  RA8_bit = 1;
  
  
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,6,txt3);                 // Write text in first row

  Lcd_Out(2,6,txt4);                 // Write text in second row
  Delay_ms(2000);
  Lcd_Cmd(_LCD_CLEAR);               // Clear display

  Lcd_Out(1,1,txt1);                 // Write text in first row
  Lcd_Out(2,5,txt2);                 // Write text in second row

  Delay_ms(2000);

  // Moving text
  for(i=0; i<4; i++) {               // Move text to the right 4 times
    Lcd_Cmd(_LCD_SHIFT_RIGHT);
    Move_Delay();
  }

  while(1) {                         // Endless loop

    Enable_Load_A1 = 0;
    Delay_ms(2000);
    Enable_Load_A1 = 1;
    Delay_ms(2000);
  
    for(i=0; i<8; i++) {             // Move text to the left 7 times
      Lcd_Cmd(_LCD_SHIFT_LEFT);
      Move_Delay();
    }

    for(i=0; i<8; i++) {             // Move text to the right 7 times
      Lcd_Cmd(_LCD_SHIFT_RIGHT);
      Move_Delay();
    }
    

    
  }
}
//---------------------------------------------------------------------------------------------------

Absolutely Clueless.
Nothing is working.

Thanks
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
You are using LCD example and say that you are using ADC example. Which development board are you using ? Zip and post the complete mikroC PRO PIC32 project files.
 

bigdogguru

Administrator
Joined
Mar 12, 2010
Messages
9,831
Helped
2,348
Reputation
4,690
Reaction score
2,274
Trophy points
1,413
Location
Southwest, USA
Activity points
62,510
As you are using Mikro C, have you set the correct system clock frequency in the appropriate section of the IDE?

Many of the Mikro C libraries utilize software delays which will not produce the desired delay, if the system clock frequency is not set properly.

Which in turn will affect the functionality of their LCD, ADC drivers, etc.

What frequency have you configured the system clock?


BigDog
 

horace1

Advanced Member level 5
Joined
Nov 18, 2008
Messages
2,123
Helped
596
Reputation
1,188
Reaction score
573
Trophy points
1,393
Location
Norwich, UK
Activity points
13,071
I don't use MicroC but this is code I used on an Microchip Explorer 16 with a PIC32MX360F512L PIM using MPLABX with the XC32 compiler

it may help with configuration etc?
Code:
// program to test PIC32MX360F512L ADC
 
#include <stdio.h>
#include "hardware.h"
#include "uart2.h"
#include "timer.h"

// *****************************************************************************
// Section: Configuration bits
// SYSCLK = 80 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 40 MHz
#pragma config FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FPLLODIV = DIV_1
// WDT ON, prescaler bits set
#pragma config  FWDTEN = ON, WDTPS = PS16384
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
#pragma config POSCMOD = HS, FNOSC = PRIPLL, FPBDIV = DIV_1

// **********************************************************************************
// ADC code taken from Microchip's example PIC32_ADC_CodeExample on http://www.microchip.com/CodeExamplesByFunc.aspx

// Define setup parameters for OpenADC10 function
// Turn module on | Ouput in integer format | Trigger mode auto | Enable autosample
#define config1     ADC_FORMAT_INTG | ADC_CLK_AUTO | ADC_AUTO_SAMPLING_ON
// ADC ref external | Disable offset test | Disable scan mode | Perform 2 samples | Use dual buffers | Use alternate mode
#define config2     ADC_VREF_AVDD_AVSS | ADC_OFFSET_CAL_DISABLE | ADC_SCAN_OFF | ADC_SAMPLES_PER_INT_2 | ADC_ALT_BUF_ON | ADC_ALT_INPUT_ON
// Use ADC internal clock | Set sample time
#define config3     ADC_CONV_CLK_INTERNAL_RC | ADC_SAMPLE_TIME_15
// Do not assign channels to scan
#define configscan  SKIP_SCAN_ALL

// AN4 = Temperature Sensor, AN2 = POT
#define configport  ENABLE_AN4_ANA | ENABLE_AN2_ANA

unsigned int tempSensor;    // Connected to AN4
unsigned int pot;   // Connected to AN2 or AN5 (depending on device)
unsigned int offset;	// Buffer offset to point to the base of the idle buffer

void ADCinit(void)
{
        CloseADC10();   // Ensure the ADC is off before setting the configuration
    // Configure to sample AN4 & AN5
    // Use ground as neg ref for A | use AN4 for input A | use ground as neg ref for A | use AN5 for input B
    SetChanADC10( ADC_CH0_NEG_SAMPLEA_NVREF | ADC_CH0_POS_SAMPLEA_AN4 |  ADC_CH0_NEG_SAMPLEB_NVREF | ADC_CH0_POS_SAMPLEB_AN5);
    // Configure ADC using the parameters defined above
    OpenADC10( config1, config2, config3, configport, configscan );
    EnableADC10(); // Enable the ADC
}

// read Explorer 16 temperature and potentiometer ADC values
int ADCread(void)
{
           while ( !mAD1GetIntFlag() )
        {
            // Wait for the first conversion to complete so there will be vaild data in ADC result registers
        }
        // Determine which buffer is idle and create an offset
        offset = 8 * ((~ReadActiveBufferADC10() & 0x01));
        // Read the result of temperature sensor conversion from the idle buffer
        tempSensor = ReadADC10(offset);
        // Read the result of pot conversion from the idle buffer
        pot = ReadADC10(offset + 1);
        printf("temperature= %d  Potentiometer = %d\n", tempSensor, pot);
        mAD1ClearIntFlag();
}

int main(void)
{
    int i;
	// Explorer16 LEDs are on lower 8-bits of PORTA and to use all LEDs, JTAG port must be disabled.
    mJTAGPortEnable(DEBUG_JTAGPORT_OFF);

	// Configure the device for maximum performance but do not change the PBDIV
	// Given the options, this function will change the flash wait states, RAM
	// wait state and enable prefetch cache but will not change the PBDIV.
	// The PBDIV value is already set via the pragma FPBDIV option above..
	SYSTEMConfig(GetSystemClock(), SYS_CFG_WAIT_STATES | SYS_CFG_PCACHE);

	mPORTAClearBits(0xff); 			// clear LEDs
	mPORTASetPinsDigitalOut(0xff);	// make LEDs outputt.

	UART2Init(PC_BAUDRATE);			// initialise UART2 to PC host
	Timer1init();					// initialise timer 1
    for(i=0;i<10;i++)				// blink LEDs 0.1sec using timer
      { mPORTAToggleBits(0xff);  mSecDelay(100); }

   	UART2PrintString("*** UART and ADC  tests ***\r\n");
   	printf("\nExplorer 16  test \n");
	mPORTAClearBits(0xff); 			// clear LEDs
    ADCinit();
	// loop 
	while (1)
       {
	 	// if .5 second elapsed read ADC and toggle LED
		mSecDelay(500);
        mPORTAToggleBits(BIT_0); 
        ADCread();          // read temperature and potentiometer value
		ClearWDT(); 		// clear watchdog timer - comment out to test
       }
	return 0;
}
when run it gives (you can see the potentiometer reading changes as I turn it)
Code:
*** UART and ADC  tests ***

Explorer 16  test
temperature= 277  Potentiometer = 852
temperature= 276  Potentiometer = 850
temperature= 277  Potentiometer = 992
temperature= 277  Potentiometer = 1023
temperature= 277  Potentiometer = 884
temperature= 275  Potentiometer = 775
temperature= 275  Potentiometer = 666
temperature= 275  Potentiometer = 544
temperature= 275  Potentiometer = 399
temperature= 277  Potentiometer = 214
temperature= 276  Potentiometer = 43
temperature= 276  Potentiometer = 0
temperature= 276  Potentiometer = 0
temperature= 276  Potentiometer = 163
temperature= 276  Potentiometer = 270
temperature= 275  Potentiometer = 273
 

rajib.das

Member level 3
Joined
Oct 23, 2013
Messages
54
Helped
0
Reputation
0
Reaction score
0
Trophy points
6
Activity points
415
Thanks Guys, My ADC firmware is working . Solved like I had to use
Code:
  ANSELA = 0;
  ANSELB = 0;
  ANSELC = 0;
  ADC1_Init();

But turns out I have a problem with LCD connection pins.
Please try to help on this: https://www.edaboard.com/threads/339581/#post1448507
Thanks

- - - Updated - - -

Oscillator Frequency [MHz] is 10.000000
 

milan.rajik

Banned
Joined
Apr 1, 2013
Messages
2,528
Helped
540
Reputation
1,078
Reaction score
523
Trophy points
1,393
Activity points
0
If you are reading ADC channel 0 that is AN0 then ANSELA should be 0x01.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Top