How to setup a PIC for A/D and PWM?

Hi

After some tries, i realy got some problems with making my PIC 16F876A working right.

I need it to be able to A/D konvert 3 analog inputs, 1 where it need to be able measure lenght (10-30cm), and the 2 others it only need to be able to see if there is someting or not. For detection objects.

I also need to use the 2 PWM outputs, where it shall be able to switch between speeds (slow and fast) Think i got the PWM to work on a 16F628.

Couting on using an ekstern 20Mhz crystal.

Right now, 1 of my problems is also that i think the __config isent set correct, but i cant figure out how to set it right.

I am writing the code in Asseamble. Using MPLAB with a picstart plus burner.

Not so good at making software, and it is begining to get on my nerves, that i cant get it to work right.

But the good news, is that my motor controller is finaly working :0)

This code, i will have a trim meter (10K ohm) at AN0, and then by turning the trim meter, i will have a binar reading on port B0-B7. But it wont work.

Why don’t you post your

Why don’t you post your code, and a list of what settings you’re trying to implement?

An easier way to set the

An easier way to set the _config word is to use a command like this:
__CONFIG _BOREN_OFF & _CP_OFF & _DATA_CP_OFF & _PWRTE_ON & _WDT_OFF & _LVP_OFF & _MCLRE_OFF & _INTOSC_OSC_NOCLKOUT
Inside the PIC16F876A.INC file is a list of bit masks for each of the config options, so you can just AND them together to make the _config word. I recommend opening PIC16F876A.INC and having a look so you know what’s in there.

You say you are “Couting on using an ekstern 20Mhz crystal.”, does this mean you don’t have one yet? If not, what oscillator are you using?

There is (usually) no need for a NOP after a label, you can just straight into the first subroutine instruction.

Between when the A/D Converter is enabled, and when you tell it to start converting, you need a short delay to proper initialise the A/D module. Two consecutive calls to ‘delay’ should do it.

Nice info

Hi, Thanks for that info about the .INC file, dident know that there where 1.

Isent it confusing if i write all those OFF and ON instead of a binar value?

About the crystal, a friend told me it will work better if i where using a ekstern crystal. Until i get a crystal, i will use 20MHz for oscillator. But isent it by default set to oscillat at 20MHz?

This code i have postet is just a experiment to get the A/D converter to work. In my projekt it will have 3 Analog inputs:

1 from a sharp sensor(forward), and 2 from some IR detectors(L and R side) i have made by my self.

Where the sharp sensor is to tjeck if there is any objects in range (30cm), and if the range to an object is under 30cm then the PIC will turn the PWM down so the robot will drive forward in slow speed.

And then if any object is under 10cm then the PIC will make the robot stop, then turn and drive in another direction. 

The advantage of using

The advantage of using BOREN_OFF, etc in the config word is that you can look at the command and immediately know that, for example, the Brown-Out Reset Enable (BOREN) is Off. You don’t have to keep referring back to the datasheet this way.

The processor will run faster with a good external crystal, but you need to tell the micro to use the internal oscillator until you have an external oscillator connected. Since you’ve set the oscillator options to HS (High Speed external) in the config, the PIC will not use the default.

might work, but

What does the _DATA_CP_OFF do?

Cant get it to work, and i cant find it in the datasheet. My program says Symbol not previously defined.

not working

think it is BODEN_OFF, as it wont take BOREN_OFF

But still it is comming with error when i am trying to “build” the projekt, error[113] and error[128]

That config word I gave you

That config word I gave you was just an example, that’s why I said to take a look at the .INC file.

_BODEN_OFF is the version used by PIC16F876A.INC, and instead of _DATA_CP_OFF you have _WRT_OFF.

ahh ok, INC file?

Have now tried to search my computer for the .INC file, cant find it any where.

Is it under the MPLAB program, found it :0)

Those lines that are undefined, what do it then call them?

think i got it working

Thanks for the help, i might got it working now, just need to try and put it into my test circuit.

If i set the oscillator to _HS_OSC, then it will run on the internal osc 20MHZ right? or is that for the external?

Just to be sure i have read the datasheet correct.

got time

Hi, i am  counting on trying over the next few days to put my whole program together. But i am not sure i got all the info about the PWM, and how to get it to work with the IR sensor. So might post some more questions. Hope you dont mind me asking alot.

HS, XT and LP oscillator

HS, XT and LP oscillator modes all require an external oscillator, such as a crystal resonator. RC mode uses a resistor and a capacitor connected externally. The 16F876A does not actually have an internal oscillator like other PICs, but you can easily make a super-cheap RC oscillator while you’re waiting for the real crystal, details are in section 14.2.3 of the datasheet.

Got it working :0)

Got it working, now i just need to get it working with my Sharp IR sensor. :0)

Thanks!

Hi again

Hi, do you know any methods to software control the speed on each wheel.

As it wont drive in a stright line, unles i regulate the PWM manual in my code.

Are you talking about

Are you talking about changing the speed manually (blind, trial and error), or using some sort of motor feedback? If you’ve just tweaking the value manually you can simply adjust the PWM duty cycle.

thinking of

I where thinking of some sort of motor feedback, just not sure how.

As the motors i use is just 2 normal DC motors