ADC dspic30f

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

ADC dspic30f

Postby arelone » Wed Nov 25, 2015 12:08 am

Hi all,

I'm new in micro-controller and now i'm working in dspic30f6014a for a imaging project. I found out this forum very useful and hope I could learn and share in terms of technical and programming as much as possible among experienced members. I have little experience in assembly language however not in c language. So forgive me if some of terms I used sometimes are not appropriate. Currently I have a project which require me to understand the C language programming. In the project I need to read surface potential from 16 sensors each of them is connected to ADC (AN0 to AN15) at portB. I searched various examples in ADC particularly for dspic30f. For the beginning, I tried to understand the programs and modified here and there to suits for my applications. To reduce the complexity of the program, I modify the program to read only from 2 analog inputs (RB0/AN0 and RB1/AN1). For my first C program the objective is to read the analog signals then digitized it and store in respective buffer (ADCBUF0 & ADCBUF1) before send it via UART1. I'm using pickit2, MPLAB ver8.92 , C30 compiler and 20 MHz oscillator. However when I build the program, unfortunately it failed. Please advice.

Thank you in advance.


//modified form original program: http://www.mikroe.com/chapters/view/55/ ... converter/

#include<p30f6014a.h>
#define FCY29480000UL
#include<libpic30.h>
#include<adc12.h>
#include<stdio.h>
#include<delay.h>


int ADCvalue0;
int ADCvalue1;
_FWDT(WDT_OFF); // off watch-dog timer -bit configuration
_FOSC(CSW_FSCM_OFF & HS); //clock switching, fail safe monitor disable, high-speed osc
_FBORPOR(PBOR_ON & BORV_27 & MCLR_EN);
_FGS(CODE_PROT_OFF); //code protection-off




int main()
{
ADCON1bits.ADON = 0; //turn ADC OFF
ADCON1bits.ADON = 0; // turn ADC OFF before initialization
ADCON2bits.BUFM = 0; // Buffer configured as one 16-word
ADCON2bits.ALTS = 0; // Always use MUX A input multiplexer
ADCON2bits.SMPI = 0; // Int. at the completion of conversion for each sample/convert sequence.
ADCON1bits.ADON = 1; // turn ADC ON

TRISB = 0xFFFF; //Port B is input
ADPCFG = 0xFFFA; //channel AN0 and AN1 (analog) is sampled and converted
//ADCON1 = 0x0040; //ADC off, output_format=INTEGER
//Timer 3 starts convesion
ADCHS = 0x0003; //Connect RB0 on AN0 and RB1 on AN1 as inputs
ADCSSL = 0; //No scan
ADCON3 = 0x0F00; //TAD = internalTCY
//Sample time ends with timer 3 clock
/*TIMER3 init*/
TMR3 = 0; //Reset TIMER3 counter
PR3 = 0x3FFF; //PR3, number of TIMER3 clocks between two conversions start
T3CON = 0x8010; //TIMER3 ON, prescale 1:1

while(1)
{
// Channel AN0

ADCHSbits.CH0SA = 0b0000;
ADCON1bits.SAMP = 1; // start sampling ...
__delay_us(10);
while
(!ADCON1bits.DONE); // conversion done?
__delay_us(10);
ADCvalue0 = ADCBUF0;
//mCsend16adc(); //load buffer and transmit


// Channel AN1

ADCHSbits.CH0SA = 0b0001;
ADCON1bits.SAMP = 1; // start sampling ...
__delay_us(10);
while
(!ADCON1bits.DONE); // conversion done?
__delay_us(10);
ADCvalue1 = ADCBUF1;
//mCsend16adc();


}

}
arelone
Greenhorn
 
Posts: 3
Joined: Tue Nov 24, 2015 10:27 pm

Re: ADC dspic30f

Postby ober » Wed Nov 25, 2015 9:58 am

What is the error after build fail? Print screen and show us here.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: ADC dspic30f

Postby yonghui » Wed Nov 25, 2015 9:58 am

what are the error shown by the compiler?

if you never use PIC for project, advise u to start do a LED blinking project first before proceed with adc things.
add a thing step by step is easier to debug where goes wrong.

if u add everything into the code, then only found out it cant compile then u wont know where goes wrong.
thanks&regards,
yh
yonghui
Moderator
 
Posts: 732
Joined: Mon Sep 28, 2009 3:27 pm

Re: ADC dspic30f

Postby arelone » Wed Nov 25, 2015 3:29 pm

Hi,

Thank you for your response. I attached the error message.

Regards
Attachments
Error Message.jpg
arelone
Greenhorn
 
Posts: 3
Joined: Tue Nov 24, 2015 10:27 pm

Re: ADC dspic30f

Postby ober » Fri Nov 27, 2015 9:13 am

From the error message, the __delay_us() is not defined.

Where did that function come from? It is in C30 compiler or other compiler? Not all compiler have this function. As far as I use, the HITECH uses that for delay with condition you need to define the value of XTAL_FREQ, maybe the new MPLAB XC16 have that too.

Well, I would advise to use the correct compiler of where the original function is, else remove those function in error message.

You might get more error messages, solve 1 by 1.

BTW, no error message does not mean the code is working :)
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: ADC dspic30f

Postby Brian Griffin » Mon Nov 30, 2015 1:48 pm

Test the code part by part. This is how we build more complex system out of it.

For a start, please write a "Hello World" code first with an LED blinking per 500ms. If this is working, then you proceed to activating other peripherals like ADC and such.
PIC - UIC00B from Cytron (replacement for my broken PICKit 2), Pickit 3, MikroC for PIC
dsPIC - MikroC for dsPIC, mikromedia board (dsPIC33)
AVR - AVR Dragon
Parallax - Prop tool
User avatar
Brian Griffin
Enthusiast
 
Posts: 403
Joined: Mon Jan 17, 2011 9:36 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 13 guests

cron