PIC's by assembler 2

For stand-alone microprocessors

PIC's by assembler 2

Postby Starfire » Mon Jun 20, 2011 12:26 pm

Onward to PIC's;-

PIC's are stupid and need to be told the base of the numbers and this is refer to as the 'Radix' and is told to the computer by a Radix statement defining the base you are working with. e.g;- Radix = hexadecimal. or by adding a base indicator to the number so a hex number is recorded as 20H or a binary as 1100B or 08D for decimal etc.

Binary is sometime written as b'00001100' and hex as 0x20 but basically the thing is the assembler will tell you if a mistake is made.

A source program tells the PIC what to do and is a sequential series of steps and is man readable – it has rules for how information is presented and is a series of text words or mnemonics that an assembler can recognise and use to convert to binary – the text is man readable and the hex file used by the computer is computer readable – it will appear as a set of two hex numbers representing the 8 bit binary code used by the computer. The PIC we will use is 8 bit.

An Assembler is a special program which converts the text from a source program to hex which is converted by the computer to binary – OK - you write a program as a series of instructions in a human readable form of text and then convert it with an assembler to hex for the computer to read.
Then load it into the PIC with a programmer.

A source code is simple a human readable set of instructions.

A hex file is a computer readable set of instructions.

An assembler is a program which converts a source file to a hex file which can be read be the computer.

Download the datasheet for the 10f200 from :-

http://www.microchip.com/wwwproducts/De ... e=en019863

A basic PIC
The PIC word for the device we will use is 8 bits wide (called a byte)
OK think of pigeon holes or 8 boxes tied together
so the memory is a group of 8 bit bytes (meaning that the 8 bits relate to each other to form a byte)
The memory can consist of 1024 bytes of 8 bits wide (total 8182 bits )
The memory is either RAM ( = random access memory) or ROM (= read only memory )
Ram is as special registers (pigeon hole store!) and Rom as the main stored memory
The architecture will have a working register called the ' W ' Register and a set of PORT's which connect to the pins and a ALU or arithmetic logic unit and a timer – the PORT's can be configured as input or as output.
Some pins have special dedication and may be only input or output.
Special pins may have special function such as power pins
Some pins have a configurable dedicated purpose
all processors have a timer or several timers


Key words;-

RAM (can be written to or read from )
ROM (the main memory and is only written to when the device is programmed it is normally read from)
register ( can be written to or read from and is used as temp store )
PORT ( connected to the pins – can be configured as input or output and can be changed on the fly )

A basic PIC has 35 instructions

These can be divide into three groups;-
General instructions
Bit instructions
Control instructions

It is suggested that you obtain a pickit 2 debug express development module. The pack contains all you will need including a Mplab IDE, assembler and starter software. The tutorial is based upon this set.

Forget the dev board supplied with the kit – instead we will use a humble 10f200 which incorporates all of the basic features without any complication.

The 10f200 has only eight pins but two are not used leaving only 6 working pins and two of them are supply leaving only four active pins.

Of these four, one pin is dedicated as an input so you have only three to worry about for the start of the project.

The pins are called a General Purpose Interface or GPIO instead of a PORT.
The chip has a built-in RC osc so you don't have to worry about an oscillator either.

It only has 256 bytes of memory but more than enough for our purpose.

First we need to build the computer so you will need a 10f200 chip in a DIP package, a eight pin holder, an LED and a resistor, a header and a piece of vero to build it on. – a total of only six components – Wow - a complete computer with only 6 components or five if you are very sure you won't blow the chip and can do without the holder. :)

The computer is programmed directly from the PicKit 2 and it can even be run using the PicKit as the power supply. If you make a mistake or just want to try a new programme – no problem – it will also erase and re-programme the part again and again.

The chip also uses five of the pins during programming via the six pin header ( one pin not used )

You need to decide the layout but you have to connect the various chip pins to the header -


Header
1 Programming voltage – to chip pin 8
2 VDD supply – to pin 2
3 VSS ground – to pin 3
4 data to - to pin 5
5 clock to- to pin 4
6 not used

Also connect the resistor to pin 3 ( GP2/T0CK1) and then to the LED anode ( remember an LED is a diode ) and then the other side of the LED to ground – so apart from the programming pins you only have a diode connected to the computer via a resistor,

Now open the wizard in the projects drop-down from the running Mplab – select 10f200 as the processor from the list offered – click on next and then next again (selecting the choices offered) – it now asks for a name so dream up a name but remember to include the full string of the working directory where it is kept – click next and it invites you to enter the file you require – select the p10f200.asm first and then the 10f200.inc – finish.

You will have two files appear in the Mplab and if you click open the .asm file it will open to reveal a template already prepared for you.
Starfire
 
Posts: 143
Joined: Thu Aug 05, 2010 4:26 pm
Location: North Ireland

Re: PIC's by assembler 2

Postby Joe Jarski » Thu Jun 23, 2011 1:19 am

Thanks for the putting up the info on assembler. It's kind of off topic, but I never realized that they made uPs with just a few I/O pins like this. I've always associated them with "big and complicated". After getting my toes wet a little bit though, I can see where they might come in handy instead of using logic chips.
User avatar
Joe Jarski
 
Posts: 231
Joined: Thu Sep 16, 2010 8:37 pm
Location: SouthEast Michigan

Re: PIC's by assembler 2

Postby Doug Coulter » Thu Jun 23, 2011 10:45 am

You can use assembler in the CCS C compiler inline, we do that when needed, so it's nice to know something about it, yes. It can also help you write better C code. If you look at the listing file, you can see which code you write that makes ugly assembler (not much with this compiler but some - nothing is perfect) and rewrite the C a little to get a better result as well. When we did that kid tactile "hearing aid" that needed autocorrelations at audio speeds, we did the hard parts in ASM completely, and one of the ISR's too. The deal is - use the best tool for the job (or part of the job).

They make PIC's so small it's crazy. Again, their original philosophy was to go for smallest possible die size (eg cheap) and for them to be used as logic replacements in simple stuff -- traffic lights, microwave ovens, even lamp dimmers. In a high volume app, the savings make the NRE costs of programming look like nothing, and these are a great fit for that. For just fooling around at home the cost of the chip doesn't matter so much, and I myself tend to go with bigger ones for most things -- it's just easier when you have a lot of I/O resources to cobble things together quick and easy (and often the built in I/O hardware in the bigger pics is all you need). We once did a "PA System" demo that used the a/d for the mic input, and the pwm for the speaker output, and math in the PIC to be the "amplifier". Worked nice. If you want real power, there are some special fet buffers you can put on the PWM output -- one chip adds 5 watts or so of output power.

I'm going to move these threads over under embedded programming, so if you miss them, that's where they will be. I'm just trying to keep this nice "book" organized so stuff is easy to find later.
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA

Re: PIC's by assembler 2

Postby Starfire » Thu Jun 23, 2011 3:52 pm

Joe - assemblers are the best way if you are limited in memory or have a speed requirement. I intend to include a 10f200 computer as a basic platform in the tutorial - it has only a 10f200 chip, one resistor and an LED, a total of three active components and will allow all 33 instructions to be tested in a variety of program modules including using the timer. It provides a basic model to allow migration to more sophistic processors later.

The LED can be used as an indicator to test all the other instructions but it is important that one accepts it is a learning process and have no pre-concepts of ability requirement.

All sorts of Nuclear instruments can be produced and be purpose built and many parameters read and interpret using PIC's

To err is human and the greatest ability of the human mind is that to make excuse for itself. :cry:
Starfire
 
Posts: 143
Joined: Thu Aug 05, 2010 4:26 pm
Location: North Ireland

Re: PIC's by assembler 2

Postby Joe Jarski » Thu Jun 23, 2011 9:48 pm

John - Thanks for doing the tutorial. I'll be following along as I'm learning C so that I can make sense of both languages. I think one will compliment the other as I learn about PICs and come up with new projects.
User avatar
Joe Jarski
 
Posts: 231
Joined: Thu Sep 16, 2010 8:37 pm
Location: SouthEast Michigan

Re: PIC's by assembler 2

Postby Doug Coulter » Thu Jun 23, 2011 11:10 pm

John and Joe -- it's good to know asm, heck, it's good to know as much as you can, always. Ignorance is only temporary bliss at best.

John, if you saw the output this compiler usually makes, you'd have to back down a little that ASM is always tighter -- it's just not true anymore like it used to be, and even I, a many-decade veteran of ASM (starting with a PDP-8 in the '60s) have found it doing some stuff that was more clever than what I'd thought of. It's ability to ram-share temp variables can't be beat for one thing, and it comes up with some mighty clever code constructs and optimizations (both code and memory access) that would drive you crazy trying to do them in ASM. And keep you crazy whenever you changed your code -- the compiler does it all automatically.

On the other hand, it also lets you do wasteful things that you'd resist doing in asm -- printf comes to mind, as does the use of floating point numbers at all (and we won't even go to transcendental numbers and trig!). As always, it's still on the guy using the tool. It's becoming quite rare that we get any advantage in memory or cycles by using ASM anymore, assuming you're doing the same things in either one -- no fair comparing using ints in asm to floats in C, right?

The real tradeoff now seems to be hey, the C compiler isn't free, and asm is. On the other hand, my life and time aren't free either-- I eat and drink beer at non-zero cost -- and I can get more done, and certainly more complex things done, without errors, in C (or ugh, perl, another story there). If you include that, the C comes out looking pretty good and no question when things get complex, easier to program in. It's probably not worth the price of the compiler for a really tiny chip, as you can fill that up with little trouble with ASM, but these bigger ones, not so true with.
It's just too much work to manually make sure things are all on the best possible code or ram pages, handling parameter passing (doing it right instead of making it all global) and so on.

You'd never write USB or ethernet code in ASM -- your life would expire before finishing, and you know, a lot of PC's lack a serial port now. Yet you can get code for either in C.

For things where you won't make a zillion, for me anyway, the C is worth it even if it does take more chip to do the same job -- it's just not worth the extra time asm takes and the next larger chip is ridiculously cheap at the margin for a few of them compared to my time. But I've been real pleasantly surprised that it doesn't seem to anymore; this varies with compiler - a lot. There was a time when the even-more-expensive HiTech compiler was the thing to have for all PICs, for example (and the linker let you mix C and ASM nicely). But for the 18 series, they came out with one that truly stank (and Microchip bought them and even added some extended instructions to the pic to compensate the crappy compiler!). CCS had been second string, but really stepped up the game (and went from $35 to its current price along the way - but less than half HiTech). It's now the clear leader at any price, it's optimized for each family, unlike the Hitech, which tried to jam it all into a rewrite of a Z80/6800 compiler...not a very good fit at all, ruined their product in my eyes.

Please don't post on the old thread anymore, it's about to be deleted. This is where this belongs and it'll all be safe here...

Edit:

I see what's going on now -- the link at the old location just points here now, so you can't actually even post on the old thread - it's already gone. So you can (for now) get here by navigating either way...so it's now only a link I have to delete under "digital".
Posting as just me, not as the forum owner. Everything I say is "in my opinion" and YMMV -- which should go for everyone without saying.
User avatar
Doug Coulter
 
Posts: 3515
Joined: Wed Jul 14, 2010 7:05 pm
Location: Floyd county, VA, USA


Return to Embedded software

Who is online

Users browsing this forum: No registered users and 1 guest