Welcome! Log In Create A New Profile

Advanced

A/D conversion in PIC

Posted by khiraly 
A/D conversion in PIC
April 18, 2008 04:44PM
Hi!

Im wondering, if there is any A/D conversion in the original firmware code (for pic using sdcc).

I want to write a simple C program to measure volt between 0-5Volt. So I want to use the pic's builtin A/D converter and Im looking for simple codewinking smiley

I thought the extruder use the A/D converter for measuring the temperature, but I dont find anything in the sourcecode (extruder1.c, extruder2.c)

Can somebody help me out?

Thanks in advance,
Khiraly
Re: A/D conversion in PIC
April 18, 2008 04:59PM
The PIC 16F628A that Reprap used to use doesn't have an A/D converter. Many other PIC microprocessors do, however. It's 8 bit, mind.
Re: A/D conversion in PIC
April 18, 2008 05:07PM
Im playing with an 16f877 and it has many(8). Just cant find a simple C program as an example.
And the resolution would be 10 bit if im correct.
Im playing with an idea to measure a 4-20mA current loop. I have builded the hardware, I have stuck with the programming currently.
Re: A/D conversion in PIC
April 18, 2008 05:20PM
Pardon me, it is 10 bit. They do, however, make it rather difficult to get the least significant 2 bits, though.

I don't do C with PIC's, though, so I can't help you with that.
Re: A/D conversion in PIC
April 20, 2008 03:52PM
Ok, I have readed some more about interrupts, still I have many questions.
First, in sdcc, every interrupt has a number.
void high_isr(void) interrupt 1 {

But I can find nowhere a list of all interrupt numbers. So I dont know what is the number of AD converter in 16f877.

There is really little information about interrupts and sdcc on the net.sad smiley
What I could find are just these:
[sdccokr.dl9sec.de]
[datrus.com]
[njhurst.com]
[sdcc.sourceforge.net]

What is really interests me is why the interrupts have meaningless numbers instead of some meaningful names?

Every bit of information is really appreciated!

Khiraly
Re: A/D conversion in PIC
April 20, 2008 05:25PM
Interrupts on PIC's are usually triggered by which port they occur in. Now figuring out which bit in a port is actually the one interrupted can be a bit of a bugger.

The only problem with the 16F family of PIC's is that you have a very small interrupt stack and no way whatsoever of knowing whether you have a interrupt stack overflow. The 18F family of PIC's solves that problem. I'd suggest shifting from the 16F877A to either the 18F4610 or the 18F4550 depending on whether you want to communicate with it via either serial or USB.

I recommend those two chips mostly because they are pretty much pin compatible with the 16F877A. I did that shift last year and it saved me a lot of time.

Edited 1 time(s). Last edit at 04/20/2008 05:26PM by Forrest Higgs.
Re: A/D conversion in PIC
April 21, 2008 04:42AM
Forrest: If I have a 18F4550 to problem is still the same.
I dont know which interrupt number correspond to a particular interrupt. (in sdcc)

>Interrupts on PIC's are usually triggered by which port they occur in. Now figuring
> out which bit in a port is actually the one interrupted can be a bit of a bugger.

In sdcc for all the interrupts there are assigned a number. For timer0 there is the number 1. Please see this example code:
// high-priority interrupt service routine
void high_isr(void) interrupt 1 {
if (INTCONbits.T0IF) { // timer interrupt was raised

Or I have completely misinterpreted something. If I need to figure out in the interrupt routine from where the interrupt comes (timer0, ad, etc), why there are numbers in the interrupt function definition?:
void high_isr(void) interrupt 1 {
And what it determines? The priorities?

----
I will switch to 18F4550, I even have one. But its way more expensive and I already fried three 16f877 pics.

So the current task is figure out which magic interrupt number corresponds to the analog digital conversion. (and find a list of all the interrupt numbers)

I dont know where to start because I cant find available solution on the websad smiley

(I will read carefully through what I have (the posted links), maybe I just overlooked something)

Best regards,
Khiraly

ps:
(Oh and Forrest please switch from basic over sdccwinking smiley) And on the computer from visual basic over python (with pygtk). I can even help you (with python and pygtk))

Edited 1 time(s). Last edit at 04/21/2008 04:49AM by khiraly.
Re: A/D conversion in PIC
April 21, 2008 05:29AM
Ok, there are some progress.

There are new terminology:
IVT - Interrupt Vector Table
ISR - Interrupt Service Routine

In the function definition:
void high_isr(void) interrupt 1 {

The number "1" is the position in the interrupt vector table.
I have found also this link:
[www.beyondlogic.org]

So Im back to researchwinking smiley
If somebody has anything to add, just do it.

Forrest: did you use interrupts already? Can you give me some hints what are these definitions?(IVT, ISR)

Khiraly
Re: A/D conversion in PIC
April 21, 2008 06:15AM
The PICs I have used don't have a separate vector for each peripheral. They just have a high priority vector and a low priority vector. You can program each peripheral to use one or the other. The ISR has to handle all the devices assigned to that priority.

Your best source of information is the datasheet for the chip.

Edited 1 time(s). Last edit at 04/21/2008 07:15AM by nophead.


[www.hydraraptor.blogspot.com]
Re: A/D conversion in PIC
April 21, 2008 10:50AM
"I dont know which interrupt number correspond to a particular interrupt. (in sdcc)"

Yeah, I was trying to politely ignore the fact that you are trying to use SDCC. eye rolling smiley

"(Oh and Forrest please switch from basic over sdccwinking smiley) And on the computer from visual basic over python (with pygtk). I can even help you (with python and pygtk))"

I'm quite happy with the development platforms that I am using, thanks all the same. spinning smiley sticking its tongue out

Edited 1 time(s). Last edit at 04/21/2008 10:52AM by Forrest Higgs.
Re: A/D conversion in PIC
April 21, 2008 05:43PM
Ok.I think I have made my homework.

The interrupt Vector Table (IVT) is a table, where you can define interrupt priorities. So for every interrupt you can define a subroutine which executes when this particular interrupt appears. So you can define interrupt priorities.

The dsPIC30F pic has 54 sources of interrupt(!), so the table can be 54 items long, and even you can have an alternate interrupt table (AIVT).
The IVT of the 18Fxxx pic has only two element, so there is two priorities.

And for the 16F877 there is no interrupt priorities. If an interrupt occurs it simply executes an soubroutine. The syntax for this subroutine is this:
static void isr(void) interrupt 0 {

The number "0" its just required by sdcc,and it does not define an interrupt vector table, simply because all interrupts has the same priority.

I have found this explicitely in the sdcc manual:
[sdcc.sourceforge.net]

But the evil is in the details. I have made a simple program which display on an eight segment display the numbers from 0-9 continously.
And if an interrupt occurs on the AN0 pin, simply turn on the dot on the seven segment display.

However it looks like the interrupt routine are never executed. And I dont know why. The code looks to me good.
Ihave posted the code here:
[pastebin.ca]

I have also two .h file.
pic14.h:
[pastebin.ca]

elso.h:
[pastebin.ca]

What I achieved:
- I think I understand the interrupt concept
- read through example codes (mainly in reprap firmware)
- wrote a simple demonstration program

What i didnt:
- the example code isnt working


Hmm, and now I dont know anymore where to continue. So if somebody have some clue, just write it here.

Khiraly
Re: A/D conversion in PIC
April 21, 2008 06:36PM
I have found it( [pastebin.ca] ):
line 249: ADON = 1; //start AD

change to:
GO = 1

The ADON bit is simply turn on the AD module. The GO bit starts the actual conversion.

So it looks like the AD conversion works nowwinking smiley)

Huh. Time to sleep. Thanks Nophead and Forrest for your comments!
Sorry, only registered users may post in this forum.

Click here to login