Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

A/D Conversion on PIC12CE674 with C

Status
Not open for further replies.

GammaRay86

New Member
Hi guys,

I'm very new to programming PICs. I'm trying to do an A/D conversion of a voltage given obtained from a sensor. I'm using the PIC12CE674 for now and programming in C.

Here is my code:

Code:
#pragma chip PIC12CE674
#include "delay.h"

void main(void){

int adval;		 // adval is the ADC value
TRIS.0 = 1; 		 // Set GP0/AN0 as input
TRIS.4 = 0;		 // Set GP4/AN3 as output
ADCON1 = 0b.0000.0010;   // Set GP4 as Digital and GP0 as Analog, Vref = Vdd

while(1){
//Do the sensor conversion
delay_us(50);			// Set delay of 50 us for sampling (TAD)
ADCON0 = 0b.1100.0101;		// Set oscillator to FRC, Analog Channel 0 (GP0/AN0) selected, Go/Done=1, ADON=1
while(ADCON0.2 == 1){}		// A/D conversion started, continue to check Go/Done, when 0 it is completed
				// Bit 1 of ADCON0 is cleared when finished
adval = ADRES;			// Read ADRES into adval

if(adval >100){			// LED on at  GP4/AN3 (pin 3) when AN0 > 1.8V, off < 1.8V
	GPIO = 0b.0001.0000;
}
else {
GPIO = 0b.0000.0000;
}
}
}

Here's a link to the datasheet: https://www.electro-tech-online.com/custompdfs/2009/12/PIC12LCE674-04-P.pdf

It compiles fine in MPLAB but when I go to program it into the PIC with PICSTART PLUS, it gives an error of "device NOT blank: Program Memory". What does this mean? Why would the program memory not be blank considering the PIC is brand new?

I was reading on some things and I came across MCLR. What exactly does this do and do I need to use this somehow? I would appreciate the help.Thanks a lot.
 
Last edited:
That is a very old chip that can only be programmed once and I suspect it has already been programmed. Did it work the first time you tried to program it?

You should try and find a chip that has an F in the part number as those are reprogrammable. Microchip suggest using the 12F683.

Mike.
 
Thanks a lot. I didn't know the PIC was one-time programmable. I'm just using it now until I get my 16F684.

For the code I have, do I require that delay command if the oscillator was set to internal RC? I'm really confused about that part.

On another note, here's my code for the PIC16F684. I have the delay here as well and I'm not sure if its needed. If anyone can look at it and let me know if it's fine. Thanks!

Code:
#pragma chip PIC16F684
#include "delay.h"

void main(void){

int adval, a, b;	// adval is the ADC value
TRISA.0 = 1; 		// Set RA0/AN0 (pin 13) as I/P
TRISC.2 = 0; 		// Set RC2/AN6 (pin 8) as a dig O/P
ANSEL.0 = 1; 		// Set RA0/AN0 (pin 13) as analog I/P
ADCON1 = 0b.0011.0000;  // Clock set to internal Frc

while(1){
//Do the sensor conversion
delay_us(50);			// Set delay of 50 us for sampling (TAD)
ADCON0 = 0b.1000.0011;		// Right justified, Vref = Vdd, 
				// Read from AN0 (pin 13), Go/Done = 1, ADC Enabled
while(ADCON0.1 == 1){}		// A/D conversion started, continue to check Go/Done, when 0 it is completed
				// Bit 1 of ADCON0 is cleared when finished
a = ADRESH.1;			// Read bit 1 of ADRESH into variable a
a = a * 512;			// Multiply "a" by the position of that bit (2^9)
b = ADRESH.0;			// Read bit 0 of ADRESH into variable b
b = b * 256;			// Multiply "b" by the position of that bit (2^8)
adval = ADRESL + a + b;		// ADC value is the sum of ADRESL, a, and b

if(adval <369){			// LED on at  RC2 (pin 8) when AN0 < 1.8V, off > 1.8V
	PORTC = 0b.0000.0100;
}
else {
PORTC = 0b.0000.0000;
}
}
}
 
The way to use the ADC is to switch it on, select the channel, wait for the cap to charge and then set the go/done bit. You are turning on the ADC at the same time as setting the go/done bit which will cause problems.

Also, I don't know which compiler you are using but I have not come across a compiler that lets you put periods in the middle of binary numbers. I would check what code is produced for ADCON1 = 0b.0011.0000;. Does it compile OK?

Edit, the normal way to read a 16 bit value is to do adval = ADRESH*256 + ADRESL;

Mike.
 
Last edited:
How do I go about setting a delay between switching the ADC on and setting the Go/Done bit? I'm not sure how to do this.

I'm using CC5X compiler and this is how it represent binary and it compiles fine :).

I don't understand your adval equation. When you do ADRESH*256, what about the bit 9? Do both bits in ADRESH get multiplied by 256 by doing that?

Thanks for the help.
 
How do I go about setting a delay between switching the ADC on and setting the Go/Done bit? I'm not sure how to do this.

I'm using CC5X compiler and this is how it represent binary and it compiles fine :).

I don't understand your adval equation. When you do ADRESH*256, what about the bit 9? Do both bits in ADRESH get multiplied by 256 by doing that?

You're making it FAR more complicated than it is - ADRESH is the higher byte of a 16 bit number, so you just multiply the entire byte by 256 and add the lower byte (ADRESL) to it. The fact that the value is only ten bits makes no difference, it's just a 16 bit number with the upper six bits set to zero.
 
Okay I think I understand now. So I'll just use adval = ADRESH*256 + ADRESL.

Maybe my calculation for the 'trip point' is wrong. In my if-else statement at the bottom, I use an adval value of 369. This I calculated was from an analog input of about 1.8 V.

The equation I used was ADC value = (2^10 - 1)*(Vin/Vref) where Vin = 1.8V and Vref = 5 V. Is this equation right?

Edit: If I wanted the 7 highest significant digits only, and disregard the lowest 3, how could I do it? Would adval = ADRESH*256 - ADRESH.0 work assuming left justification? Also would my ADC value equation still apply for the trip point?
 
Last edited:
Get the 16 bit value by doing adval = ADRESH*256 + ADRESL. Then, to keep just the 7 most significant bits you would do, adval>>=3; This moves each bit right 3 places and so the rightmost 3 bits get lost. After shifting the valid values will be 0-127.

Mike.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top