Jump to content


Photo

Adc With 16f877


8 replies to this topic

#1 leighcom

leighcom

    Newbrie

  • EstablishedMember
  • 8 posts
  • Gender:Male
  • Location:Sri Lanka

Posted 05 June 2008 - 11:59 AM

I have no experiance in this, Can anyone send me a sample program how to write a ADC program to 16F877 pic. please.
Sam

Edited by leighcom, 05 June 2008 - 12:09 PM.


#2 Reynard

Reynard

    Maniac

  • EstablishedMember
  • PipPipPipPip
  • 660 posts
  • Gender:Male
  • Location:Scotland
  • Interests:Archery - target and field

Posted 05 June 2008 - 01:30 PM

To all Newbries out there reading this forum I would like to recommend an excellent book on the PIC Micrcontroller.

The book is "The Quintessential PIC Microcontroller" by Sid Katzen published by Springer, ISBN 1-85233-942-X

This book covers practically all you need to know about the 16 series and its peripherals. Most examples are in assembler so you can see exactly what is going on with a few examples in CCS C.

It gives an excellect explanation of the adc for the 16F87X.

Cheers

Reynard

#3 trossin

trossin

    Super Enthusiast

  • Moderator
  • PipPipPip
  • 243 posts
  • Gender:Male
  • Location:Colorado

Posted 05 June 2008 - 05:52 PM

Is this what you are looking for?

void InitADC(void)
{
	adcon1 = 0x0e;	// (left justified) 1 A/D channel
	adcon0 = 0x81;  // FOSC/32, channel=0, ADON=1
}

unsigned char SampleADC(void)
{
	set_bit(adcon0,GO);  // Start conversion
	while(test_bit(adcon0,GO));
	return(adresh);		// Fetch high 8 bits of result
}


#4 RSABear

RSABear

    Regular

  • EstablishedMember
  • Pip
  • 75 posts
  • Gender:Male
  • Location:Johannesburg

Posted 12 June 2008 - 05:40 AM

I have no experiance in this, Can anyone send me a sample program how to write a ADC program to 16F877 pic. please.
Sam


Firstly - I hate all the PIC16F877s in the world - consider the PIC16F887 device. Sorry it is a personal thing.

I tried to copy and paste all the relevant lines of code from my project. I stay away from floating point maths, and in this example I convert the ADC value to an ASCII string for output to a data logger. It will not compile, you have to adapt it for your hardware and I do not guarantee accuracy - the example should get you going in good time. I hope it helps you as it took me a couple of weeks to figure the plan on this one.

#include <system.h>
#include <adc.h>

// _PIC16F877A
// Set the 16F877A device configuration bits
#pragma DATA _CONFIG, _CPD_OFF & _DEBUG_ON & _PWRTE_ON & _LVP_OFF & _BODEN_ON & _PWRTE_ON & _WDT_OFF & _XT_OSC

#pragma CLOCK_FREQ 4000000				// 4Mhz Clock

// Calculate the conversion factor in this type of programme
// Input range from voltage divider 0 - 5.0V &#58; ADC Input range 0 - 5V Max

// Voltage - 5.0V max on the ADC might be differ in your circuit - meassure it with a DVM
// Max level at &#40; 5.0/5.0 * 100 &#41; = 100%
// Digital value = 1024 * 100% = 1024
// e.g. for 33V PSU Output &#40;33/1024&#41; = 0.032226562V = 32.2mV Resolution
// Conversion factor = 0.03 * 1000000 = 32226

// Volt Value - For calibration
#define volt_conversion_factor 32226
 
#define buff_len 10

// Create a buffer to store the ASCII value of the ADC Value
char volts_buffer&#91;buff_len&#93;;			// 10-Digits + ZERO at the end of the string

unsigned int int_volt_value;
unsigned long lng_voltage;

// Create a pointer to reference the ASCII values by
char *str_voltage;

////////////////////////////////////////////////////////////
// PIC Microchip 16F77A  ADC hardware init code
////////////////////////////////////////////////////////////
void adc_init&#40;void&#41;{

	// _PIC16F877A
	volatile bit adc_on @ ADCON0 . ADON; //AC activate flag
	// The PCFG bit can setup AN0/1/3 for Analogue Input and keep the the other PINs Digital
	// Set-up for channel 1 - Voltage
	// A/D Conversion Clock Select bits
	clear_bit&#40;adcon1, ADCS2&#41;;		  // 0 - Select Tad = 32 * Tosc @ 4.0Mhz
	set_bit&#40;adcon0, ADCS1&#41;;			  // 1 - Select Tad = 32 * Tosc @ 4.0Mhz
	clear_bit&#40;adcon0, ADCS0&#41;;		  // 0 - Select Tad = 32 * Tosc @ 4.0Mhz

	clear_bit&#40;adcon0, CHS2&#41;;		// 0 - Channel 1 AN3 PIN 3
	clear_bit&#40;adcon0, CHS1&#41;; 		// 0 - Channel 1 AN3 PIN 3
	set_bit&#40;adcon0, CHS0&#41;; 			// 1 - Channel 1 AN3 PIN 3

	set_bit&#40;adcon1, ADFM&#41;;			// AD result needs to be right justified
	// 0100 AN0, AN1 & AN3 Input Vss & Vdd as reference voltages
	clear_bit&#40;adcon1, PCFG3&#41;;
	set_bit&#40;adcon1, PCFG2&#41;;
	clear_bit&#40;adcon1, PCFG1&#41;;
	clear_bit&#40;adcon1, PCFG0&#41;;

	adc_on = 1;							// Activate AD module
}

// The C Entry Code
void main&#40;&#41;
{

	adc_init&#40;&#41;;							// Setup and configure the PIC ADC

	// PIC Setup

	// Set the 16F877A device up
	cvrcon 	= CLEAR;
	cmcon 	= CLEAR;
	ccp1con 	= CLEAR;
	ccp2con 	= CLEAR;
	option_reg = 0B00000010;			// Set RB0/INT on falling edge, set TMR0 pre-scaler
#endif

	trisd = CLEAR;
	portd = 0x00;

	str_voltage = &volts_buffer&#91;0&#93;;			// Get the address of the string buffers into the pointers

while&#40; TRUE &#41;
	{

			int_volt_value = 0x0000;					// Clear the ADC result values
			lng_voltage = 0x0000;						// Clear the calculated result values

			ltoa&#40; lng_voltage, str_voltage, 10 &#41;; 			// Initialise the buffer for the display value

			// Read the Voltage
			// Set-up for Channel 1 - Voltage
	
			// _PIC16F877A
				// Set the 16F877A ADC Channel Selection
			clear_bit&#40;adcon0, CHS2&#41;;		// 0 - Channel 1 AN1 PIN 3
			clear_bit&#40;adcon0, CHS1&#41;; 		// 0 - Channel 1 AN1 PIN 3
			set_bit&#40;adcon0, CHS0&#41;; 			// 1 - Channel 1 AN1 PIN 3
			delay_ms&#40;1&#41;;			// You need a short delay, in my application it can be 1ms
			adc_go = 1;																// Start the conversion
			while &#40;adc_go&#41;;														// Wait until it is done
			MAKESHORT&#40; int_volt_value, adresl, adresh &#41;;					// Combine 10bit ADC value into result
			lng_voltage = &#40;int_volt_value * volt_conversion_factor&#41;; // Voltage conversion from ADC
			ltoa&#40; lng_voltage, str_voltage, 10 &#41;; 							// Convert the buffer to ascii and your result is a string with no decimal point. Thank you.


	} // End While Loop

}	// End main&#40;&#41;

B) :) :D

Edited by RSABear, 12 June 2008 - 07:38 AM.


#5 Picxie

Picxie

    Super Enthusiast

  • Moderator
  • PipPipPip
  • 284 posts
  • Location:Townsville

Posted 12 June 2008 - 08:12 PM

Firstly - I hate all the PIC16F877s in the world - consider the PIC16F887 device. Sorry it is a personal thing.

A rather harsh judgement, have you met all the PIC16F877s in the world?

#6 leighcom

leighcom

    Newbrie

  • EstablishedMember
  • 8 posts
  • Gender:Male
  • Location:Sri Lanka

Posted 13 June 2008 - 08:53 PM

Thank you very much, This will help me a lot to understand how to use ADC.
Sam

#7 kimmie

kimmie

    Newbrie

  • Members
  • 1 posts

Posted 23 November 2009 - 01:56 PM

Thanks for the recommendation Reynard. I will take not of what you just said.

#8 nadablootly

nadablootly

    Newbrie

  • Members
  • 2 posts
  • Location:United States
  • Interests:Playing sports

Posted 27 November 2009 - 12:15 AM

Hi,

Does anybody have use ADC convertion on the RCM4510W? If yes, do you have an example?

Thanks

#9 IanM

IanM

    Enthusiast

  • EstablishedMember
  • PipPip
  • 161 posts
  • Gender:Male
  • Location:UK

Posted 27 November 2009 - 11:56 AM

Does anybody have use ADC convertion on the RCM4510W? If yes, do you have an example?


Bloody Rabbits, Time for some Myxomatosis! :)



Reply to this topic



  


1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users