Continue to Site

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.

Hi-Tech compiler and MPLAB and function read_adc()

Status
Not open for further replies.

sanna

Member level 2
Joined
Jul 19, 2007
Messages
49
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,658
read_adc()

I am programming in MPLAB and my compiler is Hi-Tech. I am using pic12f675 and trying to read data from pin 3 (analog input from my circuit). but the compiler doesn't read this! is any other function I should use instead of read_adc () //read channel 0?

thanks for your help.
 

read_adc

Which version of HI-tech C are you using?

Regards

Nic

P.S. You will get more answers on the HI-TECH site itself in their forums.
 

mplab hi-tech c read pin

Hi there,

I have looked through my Hi-Tech PicL manual and could not find such a function as read_adc(). You are obviously using an proprierty library, do you?

Regards

Nic
 

pic analog read_adc pic c compiler

I think I shoul right flash_read() instead of read_adc in Hi-Tech. I have the last version of Hi-Tech. I found it in th e manual. Do you know if it is this function flash_read() I should use for reading the pin for analog signal?

Regard,
Sanna
 

mplab adcon0

Salam!
Sana there is no function in Hi-Tech c for directly reading the ADC. First of all you have to configure the ADC for proper operation and then write a fucntion to read the analog pin. Here is an example of a simple code.This code has been written for 16F877A running at 4MHz. The analog input is connected to RA0 and the ADC reference is the RA3 pin.
/********************************************************/
/* ADC MACROS */
/********************************************************/
#define START_CONVERSION ADGO = 1
#define ADC_BUSY ADGO
//*******************************************************************
// This function initilises the ADC Module
//************************************************************************
void Init_ADC(void)
{
ADCON0 = 0X41;
ADCON1 = 0X85; //RA0 analog,RA3 analog reference, rest digital IO's
}
//*******************************************************************
// This function converts the ADC Channel 0(RA0)
// to its equivalent digital output
//*******************************************************************
unsigned int Start_ADC_conversion(void)
{
unsigned int ADC_result;

START_CONVERSION;
while(ADC_BUSY);
ADC_result = ADRESH;
ADC_result = (ADC_result<<8)|ADRESL;
return(ADC_result);
}

If you still cannot understand then kindly post ur code.
Allah hafiz.
 

mplab adcon0 hi-tech

Hi Waseem,

Thanks for that confirmation. I thought I was going mad because I am also doing my ADC as you do but when I saw this read_adc() function I was intrigued. Thanks a lot

Regards

Nic
 

hi tech compilers

Salam Waseem,
Thanks for your sample, it helps a lot. I will review my code again with your sujestions.

Thanks
Sanna
 

hi-techcompiler

Salaam Hafiz,
I tried your code but my compiler still doesn't read ADCON1, here is my code:
I am using MPLAB 7.0 PICC_lite_9 and pic12f675


Thanks,


#include <htc.h>

#define XTAL 4000000
#define INPUT 1 //port directions
#define OUTPUT 0
#define HIGH 1
#define LOW 0



int counter = 0;
int pulsVec[1000];
int cleanPulsVec[1000];
int HEART = 0;
int LEVEL = 0;

//Functions
void AddFind(int val);
void CopyCleanVec(void);
void interrupt time_isr(void);
void Init_Hw(void);


//

int main()
{
Init_Hw();
interrupt time_isr();
LEVEL=flash_read(0x01); //read the Vref from AN0

//here I want to read from AN0 and save in LEVEL

while(1)
{


//Wrong code SET_ADC_CHANNEL(0);
HEART=read_adc(0x01); // read channel 0
AddFind(HEART);


}//End of while()

return 0;
} // End of Main()

void Init_Hw(void)
{

TRISIO=0x1b; //00011011 An0,An1,An3 as inputs and An2 output
ADCON0=0x48; //An3 Analog to digital
ADCON1=0x1b; //set AN0(Comparator),AN1(Vref),An3(signal from sensor) Analog input rest digital I/O

}

Added after 17 minutes:

I think I know, instead of ADCON1 I should may be use ANSEL, :idea:

Added after 44 seconds:
 

hitech function

Salam Sana!
I have seen ur code, there are a few errors. I will try my best to post a fully working code for you today. Pray for me, because at the moment i am suffering from severe flu.
Regards.
 

    sanna

    Points: 2
    Helpful Answer Positive Rating
mplab hi-tech library download

Thank you Hafiz,
Wish you will be well soon, Take care
 

hi tech compiler code examples

Salam!
Here is the simple test project with the source code. First you test this simple circuit. If it works then for you then study the code. Although i don't have much time but i will try my best to post a good explanation of what is happening in the code.

This is the source code

#include<htc.h>;/*general header file for pic processors*/
__CONFIG(0X1184);
unsigned int value;
/*********************************************************/
/* AD MODULE FUNCTIONS AND MACROS */
/*********************************************************/
void setup_adc(void);
unsigned int start_ad_conversion(void);
/*********************************************************/
#define ad_on ADON = 1
#define start_conversion GODONE = 1
/*********************************************************/
/* FUNCTION PROTOTYPES */
/********************************************************/
void init (void);
void compare_result(unsigned int data);
/********************************************************/
void main (void)
{
unsigned char i;
init();/*initilise the various I/O and peripheral registers*/
setup_adc();/*configure the ad module*/
value = start_ad_conversion();
while(1)
{
value = start_ad_conversion(); //convert the signal to digital
//value
compare_result(value);
}
}
/*************************************************************/
void setup_adc(void)
{
ANSEL = 0X18;/*RA3 ANALOG PIN,ADC CONVERSION CLOCK Fosc/8*/
ADCON0 = 0X8C;/*SELECT ADC CHANNEL 3(AN3)*/
ad_on; //Turn On ADC
}
/*************************************************************/
unsigned int start_ad_conversion(void)
{
unsigned int adc_result;
start_conversion;
while(start_conversion);
adc_result = ADRESH;
adc_result = (adc_result<<8)|ADRESL;
return(adc_result);
}
/**************************************************************/
void init (void)
{
OPTION = 0X00;/*PULLUPS DISABLED,PRESCALER TO TMR0*/
INTCON = 0X00;/*ALL INTERRUPTS DISABLED*/
PIE1 = 0X00;/*DISABLE ALL PERIPHERAL INTERRUPTS*/
PIR1 = 0X00;/*CLEAR ALL INTERRUPT FLAGS*/
T1CON = 0X00;/*TIMER0 DISABLED,PRESCALER 1:1,INTERNAL CLOCK*/
CMCON = 0X07;/*PORT A COMPARATOR PINS CONFIGURED AS DIGITAL*/
ADCON0 = 0X00;/*AD IS TURNED OFF*/
ANSEL = 0X00;/*PORTA ALL DIGITAL I/O*/
WPU = 0X37;
TRISIO = 0X39;/*GPIO0,GPIO3,GPIO5,GPIO4 INPUTS,GPIO1,GPIO2 OUTPUTS*/
OSCCAL = _READ_OSCCAL_DATA();
}
/*************************************************************/
/*compares the ad_result with some fixed value and toggles */
/*output_1 if result is greater than that fixed value */
/************************************************************/
void compare_result(unsigned int data)
{
if(data>512)
GPIO1 = 1;
else
GPIO1 = 0;
}



This code simply monitors the voltage (any signal) on the RA3 input and compares the result to a fixed value of 512. If the result is greater than 512 then the LED turns ON otherwise it is turned OFF. Note the following things.

1. In this case the ADC reference is the 5V supply.
2. For configuring the pins as ADC inputs you have to disconnect them from the
comparator module i.e CMCON = 0X07; read this in data sheet carefully.
3. The controller is running with the internal 4MHz oscillator.
4. Another important thing is that the there is an oscillator calibration value sotred
at the last program memory location. It should be there for the 12F675 to
function properly.

The complete project including the diagram of the simple test circuit is attached.

I hope it helps u.

Regards.
Waseem
 
mplab hitech c reference

Thank you Waseem, it was very nice of you to help me. You are realy good and helpful. I will study your code.
May I ask you where you live?

Regards,
Sana
 

read_adc + mplab

Salam!
Thanks a lot for ur prayers.
Its a pleasure to help people. My motto is to "Gain knowledge, spread knowledge".
Sometimes i wish to help beginners on this form but i cann't because i am very busy doing my Masters in Mechatronics.

My country is Pakistan and i live in Wah Cantt which is about 37 Km from the Capital City of Islamabad.
By the way where do u live?

Regards.
 

adc header file of mplab

Salam Waseem,
Thank you, you are really nice. I wish you good luck. It was really big help because I am a beginner in programming microprocessor. To be honest this is my first PIC. The code was really useful the only different was that I am using Vref (AN1) as an analog input for a threshold as my referents. I attached the circuit and code. Thanks again for your help.
I have problem to "talk" to pin but I am learning. Do you have any suggestions, any good book?
I knew you are from Pakistan but I didn’t know where you are living. I am Iranian but living in Canada.

Take care,
Sana
 

hitech c porta disable ad

Salam Sana!
Thanks for the complements. Its a pleasure to help people.
Lots of books and other information is available about the programming of PIC microcontrollers on the internet. The best material is available at Microchip's own site (www.microchip.com). Simply download the data sheet of the microcontroller that you are porgramming. Also download the midrange manual.
If you are interested in books about PIC's then visit www.ebookee.com. Search for PIC and you will get a lot of books to download.
I have studied the code you have posted and tomorrow i will try to post all the problems in your program.
You said you are a beginner in PIC's so that's ok, i was also a beginner some three years ago.
I will be in a good position to help if you can briefly tell me about the whole project.
For direct personal contact you can email me at ws34pk@gmail.com.
By the way i am going to conduct a short course on the programming of microcontrollers for few of my friends in february. See if you can fly to Pakistan to attend it.

Take care.
Waseem
 

hi-tech c pic setting pin to ansel analog

Salam Sana!
I have studied you code in detail and have found quite a number of errors. e.g lets see this portion of the code

/* Add samples in an array and compares and finds the pulses
void PutInArray(int val)
{
pulsVec[counter++] = val;
if(counter == 999)
{
CopyCleanVec();+
counter = 0;
}
}

You are storing your results in an array and the length of the array is going to be 1000 bytes. This array will require 2000 bytes of RAM for storage because each integer will require two bytes of RAM. Now the interesting thing is that the RAM of 12F675 is only 64 bytes, so this code is never going to execute the way you want it to be.
So far what i have understood is that you want to convert the level on the AN3 pin with reference to the level on AN1 pin as indicated by this portion of the code

unsigned int read_threshold(void)
{
unsigned int level;

ANSEL=0x12; //An1 analog pin for the reference
ADCON0=0xC4; //Select AD chaneel for AN1,Vref
ADON; //turn on ad
level = Start_ADC_Conversion();
return(level);
}

You donot have to reconfigure the ADC for doing that. The only changes that have to be made is to set the ADC reference to AN1 (in the orignal ADC init funciton) and the ADC will automatically convert the level on the AN3 pin with reference to the voltage level on AN1.
I will make these changes to your code but if you can explain to me about the whole project then i will be able to help you with the code in a more easy manner.

Take Care
Waseem.
 

    sanna

    Points: 2
    Helpful Answer Positive Rating
flash_read() hi tech c

Hi Waseem,
You are right I took away these code which save the data in buffer. Now I compare it one by one so it takes less of the RAM.
Could you please explain for me how I can change the pin and read from that pin I want.
For example i want to read from my Vref (AN1) one time and save it and then I want to change the pin to AN3 and read from pin AN3 which is my sensor.
should I do all these in Init funktion?
Thanks,
Sana

Added after 3 hours 1 minutes:

I had missed a conection from the output of my sensor to comparator, here is the new circuit.

regards,
 

Dear I am working in one project where I need to get pulses on RA4 of 16F877, I am unable to do so, see if you can help me. I just need to get the pulses and display on LCD. I have uploaded all of my code at

www.evsoft.pk/azhar/Pulses.rar

Thanks in Advance.

-Azhar
 

Have you tried the C-compiler from Microelektronik?
It is free and works fine. You can find link to it on **broken link removed**

John
 

Hi All,
I am quite nwe to pic and I try ADC conversion code (stated above) but I found one error can't solved. Please help me .Thanks a lot..

undefined identifier "ADGO".I GOOGLE also could not find solution.
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top