PDA

View Full Version : The basics



malc-c
- 21st June 2008, 19:02
I've had a year or so from programming, and even then I was still very much a newbie, and had a lot of help from you guys and gals here.

For me one of the area's I find hard to get my head round is the config of the basics such as making ports digital, turning on/off analogue pins, etc. I look at the data sheets for most of the PICs I use and I might as well be looking at somthing writen in Vulcan ! I also think that I'm mnot alone, and as such would love to have a point of refference to make things a little clearer. A sticky, or a link to some page on the site would be great

I'm sure it wouldn't be too hard, as it seems that most pics have things in common, for example most pics with A/D convertors, use the same pins on PORTA, so, for example it would be simple to give examples for setting PORT A up for whatever combinations you could get (with in reason)

Eg to use the first A/D on pin 1 and set all others to digital use "command" value X, to use two A/D on pin 1 and 2, with the others set to digital use "command" value Y

Having this sticky / link to web page would also help seasoned members by not having to repeat answers and hopefully it would help newbies learn quicker

Just a suggestion, but would anyone be willing to create such a post.

mackrackit
- 21st June 2008, 19:18
Cool idea!! But it has been done.

http://www.picbasic.co.uk/forum/showthread.php?t=561

malc-c
- 21st June 2008, 19:51
Thanks for the link, and it does go some way to answering the questions, but there are occasions, where the user may want to use A/D to read a pot, but also digital I/O on other pins to turn on LEDs for example. Mels excelent post gives the values to turn things on/ off completely.

mackrackit
- 21st June 2008, 20:19
Well the problem is that so many of the chips are just enough different, I do not think short of duplicating major sections of the data sheet there is a way of doing what you ask.

Maybe someone could go through all of the different PICs, sort out what ones have the same settings and build a database?

Or have a wiki type database. When a user come across a chip not on the list and figures it out, add it. Over time it might be useful.

Myself, I use a dozen or so different chips. I would bet that many here use less. I just do not see how a project like this would be much of a benefit when there are so many other things to learn about the chip one plans to use.

The data sheet can be daunting, but after it is read a few times it comes together. If one is having problems... Post it.

But I do like your idea. SQLite would work for the database. I have a server that is not doing much... What do you think? Would that work?

Acetronics2
- 21st June 2008, 20:54
Hi, Malc

Your suggestion sounds to me Freescale " Processor Expert", Cypress " Psoc Express" Microchip "Visual initializer" , Matrix "Flowcode" or similar "high End" IDEs.

you just choose what you want to do with a Pin and a pre-processor writes the code for you ...

I think I had such a talk on this forum with Mel when Microchip released its Visual ini. ...

May be that's not what PBP team has wanted to do ... or could do (Hey, PBP Team : we are still waiting for you to release PBPL Dll ...).

I seriously think you should turn to another compiler, if you want such features ...
PBP is not written to do that ...

But libraries are open ... and you can write the corresponding asm conditionnal assembly routines ...

Not so good an idea ... in the end !

cheers

Alain

malc-c
- 21st June 2008, 21:38
Hi Alain,

I think that would be nice, but I think its a long way off if PBP went that route.

No I think the previous poster understood me a little better. If this helps, let me give you an example.

I have a 28 pin device, want to use port C as output to leds, port B to an LCD and port A to be a mix of digital I/O for switches, and a pot or two. This is where I (and probably most newbies) stumble when it comes to configuring the PIC. Whilst following Mels post would indeed set up port A for digital, if you want to use one of the AN pins, but have switches on the others, then its confusing. Maybe a database is too complicated, I would simply like somehing along the lines of:

"to set PORTA.0 to analogue, PORTA.1-7 Digital use ADCON = X"
"to set PORTA.1 to analogue, PORTA.0 and A.2-7 digital use ADCON = Y

etc

On a current project I've resurrected I want PortA.0, 2 & 3 digital for switches, and Porta.1 analoge to read the value from a 10K pot. But I'm not sure what value I need to set ADCON to get this mix.

EDIT: - I see "bit 3" needs to have a value of 1110 (if I follow the data sheet), but how do I convert that into a statement "ADCON = ???" (Data sheet http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf) - I'm using a 16F873A

skimask
- 21st June 2008, 22:36
EDIT: - I see "bit 3" needs to have a value of 1110 (if I follow the data sheet), but how do I convert that into a statement "ADCON = ???" (Data sheet http://ww1.microchip.com/downloads/en/DeviceDoc/39582b.pdf) - I'm using a 16F873A

And that's what's called "RTFM" :D
How do you convert that into a statement?
Well, that all comes back to the bare bones basics of dealing with microprocessors and microcontrollers and binary and being able to count in binary and being able to convert a binary number to either a format compatible with the compiler/assembler you're using and/or another Base.

As you said - "bit 3 needs to have a value of 1110"
A bit is a "Binary digIT". It can't have a value of 1110. Just like you can't count over 5 on one hand. Binary has 2 distinct states, 0 and 1, off and on. Therefore, you can only count 0 and 1...that's it. Nothing more, nothing less.
If you take another good look at that datasheet, you'll see that the ADCON register (and I think you really want the ADCON1 register) has 8 bits, like most other registers. While I'm not looking at the datasheet at the moment, I'll assume that those 4 data bits are the lowest significant data bits of that register. What about the other 4 data bits of the ADCON1 register BYTE?
Again, taking a look at the PBP manual, you'll see that you can assign individual bits to something by using the "%" character to signify a binary quantity.
Those bits described above are most likely the lowest significant 4 bits of the ADCON1 register. I'll assume that the upper 4 bits of the ADCON1 register should be set to 0000 for example purposes only.
Therefore...it follows, according to the PBP manual and it's explanation of the usage of the "%" character, you can set bits in any register or variable from most significant bit to least significant bit, left to right.
Hence
ADCON1 = %00001110
The leftmost 0 is the most significant bit, the rightmost 0 is the least significant bit. And following this example, this would tell me that I'm setting the ADCON register as follows:
Bit 7 = 0 = Left Justified
Bit 6 = 0 = Depending on the state of the ADCON0 register, sets the clock used by the A/D converter
Bit 5 = 0 = Unused bit - read as 0
Bit 4 = 0 = Unused bit - read as 0
Bit 3 thru Bit 0 = 1110 - Looking thru the table in the datasheet, this tells me that this combination of bits will set all dual purpose pins to digital with the exception of PortA.0 and the VRef+ and VRef- will be referenced to the PIC's positive rail and ground respectively.

If that answers your question, great.
If it's entirely too much 'duh' information, sorry, didn't mean to insult your intelligence.

Thinking about it though, what you might really be wanting is a combination of digital and analog ports that isn't available in this PIC (I remember this subject coming up some time ago)

For instance:

You can set all pins to digital, lowest 4 bits set to "011x"
You can set all pins to digital and PortA.0 to analog, lowest 4 bits set to "1110"

You CAN'T set PortA.0 and PortA.5 to analog and the rest to digital...because that combination of bits isn't in the table. I think you can actually pick and chose in some of the newer PICs, not sure, haven't tried.

Is that the ticket?

(EDIT: Re-reading your post above, I see that is in fact what you really wanted to do...Hopefully this clears it up for you)

malc-c
- 21st June 2008, 23:05
And that's what's called "RTFM"

LOL - well I'm try. I know that to get help here it helps if one can be seen to be doing the basics, such as RTFM... even though I may not fully comprehend it :)



As you said - "bit 3 needs to have a value of 1110"
A bit is a "Binary digIT". It can't have a value of 1110. Just like you can't count over 5 on one hand....If you take another good look at that datasheet, you'll see that the ADCON register (and I think you really want the ADCON1 register) has 8 bits, like most other registers.

So its the lower half of a byte - having 8 bits



Therefore...it follows, according to the PBP manual and it's explanation of the usage of the "%" character, you can set bits in any register or variable from most significant bit to least significant bit, left to right.
Hence
ADCON1 = %00001110

So it follows that we can set values for ADCON, CMCON etc much the same way as we can set which pins are inputs and outputs on each port eg PORTB=%00000000



If that answers your question, great.
If it's entirely too much 'duh' information, sorry, didn't mean to insult your intelligence.

yes it does help and I appreciate the effort to post in such detail



Is that the ticket?

(EDIT: Re-reading your post above, I see that is in fact what you really wanted to do...Hopefully this clears it up for you)

Yup - that helps a lot. Like I said its been a while since I programmed these things and as I get older I seem not to be able to retain things as easily as I used to :)
Thanks

skimask
- 21st June 2008, 23:23
So its the lower half of a byte - having 8 bits
......
So it follows that we can set values for ADCON, CMCON etc much the same way as we can set which pins are inputs and outputs on each port eg PORTB=%00000000
......
yes it does help and I appreciate the effort to post in such detail


1) Yes - lower half, the lower 'nybble', bottom 4 bits...

2) Yes, that's exactly the case! Almost everything in the PIC10/12/14/16/17/18 world (in general, the small microcontroller world) is based on the BYTE...8 bits...2 nybbles...left to right = most significant to least significant (gotta be careful when you start playing with WORDs, QUADs, LONGs, etc. Some MCU's don't order them the same in memory, little endian vs. big endian).

Detail - I got on a roll, a rambling roll. Hopefully that little lecture will catch on and other 'less experienced' folks will read and comprehend it.

malc-c
- 21st June 2008, 23:28
Ok I now have something workable, but its still not behaving as I would hope.

I'm using the EasyPIC5 board, with 10K pot set to RA1. The following "abridged" code seems to give a decaying value for the varible D used to store the ADCIN.



main:
ADCIN 0, D
LCDOUT $FE,$C0, "Chase Speed " ,#D

'DO MAIN PART OF FLASH LEDS IN SEQUENCE HERE'

PAUSE D
NEXT
goto main:


With the post set to +5 it gives a reading (decimal) for D of 40ish, which then drops to 12 and then flucuates between 12 and 16, meaning that the speed at which the LEDS flash can't stay constant.

Any Ideas ?

(I can post full code if require)

skimask
- 21st June 2008, 23:34
with 10K pot set to RA1.

ADCIN 0, D

Check the datasheet. You might notice something wrong here! :)

This next tip might make it a bit easier for you in the future. Using DEC vs. using # keeps the length of the decimal output to the LCD (the number of characters sent) constant, regardless if it's zero (one character) or 9999 (4 characters).

That way you don't see 1000, when you actually want to see 0001.

LCDOUT $FE,$C0, "Chase Speed " ,DEC4 D

malc-c
- 21st June 2008, 23:49
with 10K pot set to RA1.

ADCIN 0, D

Check the datasheet. You might notice something wrong here! :)

Whoops !


ADCIN 1, D


:)

skimask
- 21st June 2008, 23:54
Whoops !


ADCIN 1, D

:)
Working now?

malc-c
- 22nd June 2008, 00:03
Yup !

Thanks,

And on that note as its gone midnight I'll give my brain a rest and get some sleep !

skimask
- 22nd June 2008, 00:09
Yup !
Thanks,
And on that note as its gone midnight I'll give my brain a rest and get some sleep !

Ya right! Now that you got it working....do you actually think you're going to get some sleep at all? :D