Jump to content
leighcom

Adc With 16f877

Recommended Posts

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

Share this post


Link to post
Share on other sites

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

Share this post


Link to post
Share on other sites

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
}

Share this post


Link to post
Share on other sites
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 : 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 ( 5.0/5.0 * 100 ) = 100%
// Digital value = 1024 * 100% = 1024
// e.g. for 33V PSU Output (33/1024) = 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[buff_len];			// 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(void){

// _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(adcon1, ADCS2);		  // 0 - Select Tad = 32 * Tosc @ 4.0Mhz
set_bit(adcon0, ADCS1);			  // 1 - Select Tad = 32 * Tosc @ 4.0Mhz
clear_bit(adcon0, ADCS0);		  // 0 - Select Tad = 32 * Tosc @ 4.0Mhz

clear_bit(adcon0, CHS2);		// 0 - Channel 1 AN3 PIN 3
clear_bit(adcon0, CHS1); 		// 0 - Channel 1 AN3 PIN 3
set_bit(adcon0, CHS0); 			// 1 - Channel 1 AN3 PIN 3

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

adc_on = 1;							// Activate AD module
}

// The C Entry Code
void main()
{

adc_init();							// 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[0];			// Get the address of the string buffers into the pointers

while( TRUE )
{

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

		ltoa( lng_voltage, str_voltage, 10 ); 			// 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(adcon0, CHS2);		// 0 - Channel 1 AN1 PIN 3
		clear_bit(adcon0, CHS1); 		// 0 - Channel 1 AN1 PIN 3
		set_bit(adcon0, CHS0); 			// 1 - Channel 1 AN1 PIN 3
		delay_ms(1);			// You need a short delay, in my application it can be 1ms
		adc_go = 1;																// Start the conversion
		while (adc_go);														// Wait until it is done
		MAKESHORT( int_volt_value, adresl, adresh );					// Combine 10bit ADC value into result
		lng_voltage = (int_volt_value * volt_conversion_factor); // Voltage conversion from ADC
		ltoa( lng_voltage, str_voltage, 10 ); 							// Convert the buffer to ascii and your result is a string with no decimal point. Thank you.


} // End While Loop

}	// End main()

 

B):):D

Edited by RSABear

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites

Your content will need to be approved by a moderator

Guest
You are commenting as a guest. If you have an account, please sign in.
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoticons maximum are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...

×