New to mikroE C

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
New guy here, so please go easy on me :p

It's my second night on the easyPIC and I think madam is angry.
But I managed to write what I need on the default PIC & LCD. i.e Stop the scrolling and add delays. And switch the Display from three different sentences as such.
So far so good.

My aim was to Display and read ADC.

I thought why not go for the PIC that I would be using for my project not the big 40pin one.

After searching I found the obvious one. A 16F88. Enough ports for 16*2 LCD and analog ones for ADC, two total I needed.

Went through the collection and luckily I had two.

So now to code for the 16F88 in C. Took me while but managed to change the code and programmed and works like charm.

Thought I'll check the second F88. plugged in and hit program and mikroProg gave me Vcc voltage error :eek:. Checked the PIC and it was HOT. I managed to put in reverse. Crap ! :eek:

Took it out and I thought I will put it back again. So tried and to my surprise it's still working. These little bugger's are tough and so was the easyPIC. !!

Whew ! What a relief. :)

Now I am trying to read the ADC on AN0 and AN1.

I think I would need help but I will let you guys know after trying.

I thought a heads up would be worth it.
 

nerdegutta

Joined Dec 15, 2009
2,684
Hi R!f@@.

Sometimes the PICs can handle anything, and sometimes they don't...

Here's a program I used to test on a PIC 16F688, and in C on MPLAB. I know it's a different PIC, but...

Rich (BB code):
// ADC test program
 
#include <hth.h>
 
#define _XTAL_FREQ 4000000
#define led RA2
 
unsigned int adc_raw;
 
// Configuration
__CONFIG(FOSC_XT & // Crystal/resonator on RA4 & RA5
WDTE_OFF & // Watchdog timer off
PWRTE_OFF &  // POwer up timer enable off
MCLRE_OFF & // MCLR pin function is digital input, MCLR internally tied to VDD
CP_OFF & // Code protection off
CPD_OFF & // Data code protection off
BOREN_OFF &  // Brown out off
IESO_OFF &  // Internat switchover off
FCMEN_ON); // Fail-Safe clock monitor on
 
// Functions
void adc_init(void)
{
ADCON0bits.ADFM = 1; // Right justified
ADCON0bits.VCFG = 0; // VDD as reference
ADCON0bits.CHS = 0b111; // Analog channel set to AN7
ADCON0bits.ADON = 1; // ADC is enabled
ADCON1bits.ADCS = 0b001; // ADC Conversion Clock Select bit
} // End adc_init
 
void adc_read(void)
{
ADCON0bits.GO = 1;
__delay_us(10);
while(ADCON0bits.nDONE);
} // End adc_read
 
// Main program
void main (void)
{
CMCON0 = 0x07; // Turn off comparators
ANSEL = 0b00001000; // Analog input on RC3/AN7
 
TRISA = 0b00000000;
TRISC = 0b00001000;
 
led = 0;
 
adc_init();
 
while (1)
{
adc_read();
adc_raw = (unsigned int) ADRESH;
adc_raw = adc_raw*256;
adc_raw += (unsigned int) ADRESL;
 
if (adc_raw <= 512)
{
led = 1;
__delay_ms(500);
led = 0;
__delay_ms(500);
}
else
{
led = 1;
__delay_ms(100);
led = 0;
__delay_ms(100);
}
}
}
It's the classic "Turn LED on when ADC value is..."
 

nerdegutta

Joined Dec 15, 2009
2,684
No, I do not think so. I thought it might give you some pointers on how it is in Hi tech C on Mplab. I learn pretty good from reading other code and try to convert it to what i need. :)
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
I got to Admit this was easier than I thought, well upto now.

I managed to control RA pins ( some as ouput ) RA0 & 1 remains as ADC input.
Now everything is working as I want it.
The Backlit is properly timed and buzzer too.

Giving delays and producing sound was never this easy.

I wrote so much in a few hrs.

I am lovin it.

Now comes the ADC reading part.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
OK this is waaaay confusing.

I am not posting the code yet but I do need some advice.
PIC is 16F88

I like to Read the ADC value at 10 bit on RA0 and RA1.
Actually they are voltage level and current (A) level. But I am now using the pots on the easyPIC Eva.
I have now XX.XX Volts and XX.XX Amps displayed. At certain location at Row 1 & 2
I would need up date the XX.XX characters with the ADC value.
My top will be for now 5V and low is 0V.

I think I would need to scan RA0, store the value, display the value at the respective LCD location and scan RA1 and do the same.

I need some pointers as I would like to do it myself.

What way I should go about.

Thanks
 

hexreader

Joined Apr 16, 2011
581
Select ADC library.

Double-click on "ADC_Read" for help.

Select "conversions" library

Double-click "WordToStr" for help

... same again for LCD library if you have not already succeeded with LCD
 

THE_RB

Joined Feb 11, 2008
5,438
A good practice when working with the ADC inputs is to read the ADC pin (use the MikroC help examples for code) and display the ADC value directly on the LCD.

That lets you see the ADC read itself is working. Remember on the EasyPIC7 there are lots of things that can be selected to attach to the PIC pins, like the LEDs and pullup resistors, and these can affect the ADC reading.

So I generally display the raw ADC value on the display. If you set the ADC setup to "left justify" the ADC reading you can just display the HI byte of the ADC (0-255) on the LCD using the ByteToStr() function;

Rich (BB code):
unsigned char txt[12]; // used to hold general display texts

ByteToStr(ADRESH,txt); // makes byte 0-255 to txt "  0" to "255"
LCD_Write(0,0,txt);  // display it
(sorry I can't remember the exact function name for LCD_write).

Once you have the ADC values being read and displayed ok, you can move on to the math code to convert the ADC value to volts. And display that.

Another good trick is to pick up a 20x4 text LCD, it will plug in and work just as good as the 16x2 LCD, but gives you a couple of extra lines to display extra data during testing.
:)
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
OK. To begin I need some bit of explanation on the A/D Conversion Clock select bits.
The bit 7-6 on ADCON0 of 16F88


ADCS<1:0>
if ADCS2=0
00=Fosc/2
01=Fosc/8
10=Fosc/32
11=FRC.

if ADCS2=1
00=Fosc/4
01=Fosc/16
10=Fosc/64
11=FRC.

I am using 8MHz Xtal.

What is bit value for bit 7-6 I should use?

My goal is to use a voltage divider from 0-20V input.
Should use a reference.?
I read about internal reference?
What about external reference ?
Any advice on this?
 
Last edited:

hexreader

Joined Apr 16, 2011
581
You are Waaaay over-thinking this...

You are not writing in assembler, you are using mikroC pro. All the hard work is done for you.

Use the ADC library function:
tempres = ADC_Read(n);

For advanced applications you may need to think about the issues you mention, but to start with, just accept the defaults and ignore these registers.

... and yes, keep the analogue input between Vss and Vcc to avoid possible damage to the PIC. Use a voltage divider in the final product, but use the EP7 potentiometers to test with.

Would you like a very simple starter example to get you going?
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Hoooray..! I did it, I did it :D.

By looking at ur code I wrote it, the while loop below my code with in the main

Now I can display RA0 (ASCII)value at the Voltage value characters and RA1 value at Current (A) value character location. And it changes according when I vary the two pots.

The value at RA0 is from 2(3) to 1007(8)
The value at RA1 is from 0(1) to 1006(7)
* the bracket value is that the display changes as I think the pot value is not stable.

Below is the while(1)
temp_result = ADC_Read(0);
WordToStr(temp_result ,txt);
Lcd_Out(1,9," ");
Lcd_Out(1,9,txt);
delay_ms(250);

temp_result = ADC_Read(1);
WordToStr(temp_result ,txt);
Lcd_Out(2,9," ");
Lcd_Out(2,9,txt);
delay_ms(250);
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
Turn off the LEDs for PORTA if you want the full 1023

SW3-1 to the left - off
Damn ! I was not thinking about that.
Got it. Is there a way to avoid that. I mean to use a separate supply instead of using USB power.

OK.. Now I need to convert it to Voltage first.
 

hexreader

Joined Apr 16, 2011
581
The problem is not with the power supply, the problem is that the current path through the LED alters the ADC voltages and makes the readings non-linear.

The solution is not to connect anything to the ADC input pin other than the signal that you want to measure.

converting 0-1023 into a voltage value for display will be a good test of your C programmings skills.

... or if you want to cheat, I would imagine that LIBSTOCK will have an example to follow. Maybe here http://www.libstock.com/projects/view/837/adc-t0-volts ... but I have not tried that program for myself, so cannot be sure it will help.

Try not to cheat if you can resist it. You will learn more by working it out for yourself.
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
I am working my ass off.

As I wrote inttostrg(voltage,vstrg) from the above example compiler says I reach demo limit. Only that line is giving me that error. I commented out the ammeter part to see if the code is bigger or not but to no vail.
 

THE_RB

Joined Feb 11, 2008
5,438
The compiler has a chart style window that shows you the functions and their sizes.

Since you are working with demo limits it is good to familiarise yourself with that window and see how large your functions are, and what functions the compiler has linked in by itself.

Why do you need IntToStr() (signed) when you already use WordToStr() (unsigned)?

Is there some reason you need to be using both unsigned 16bit vars and signed ones?
 

Thread Starter

R!f@@

Joined Apr 2, 2009
9,918
I dunno if there is a reason.
WordtoStr seems to work for now

I don't think I need signed ones for this project.
 
Top