Author Topic: Pic microcontoller programming in assembly...  (Read 12915 times)

0 Members and 1 Guest are viewing this topic.

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Pic microcontroller programming in assembly...
« Reply #25 on: May 22, 2017, 02:15:16 am »
Not a bad PIC to learn on - its still a classic midrange core but is a lot more modern than a lot of the PICs that have been mentioned and has built-in silicon support for in-circuit debugging with any Microchip debugger.  Microchip used to bundle the high-end part from the same datasheet (PIC16F887) on a demo board with the PICkit 2 as the PICkit 2 Debug Express kit, which came with a reasonably good set of introductory assembler tutorials.  Apart from stuff being on different pins, it would largely be applicable to your PIC16F883.

PIC16F887 44-Pin Demo Board User's Guide (includes tutorials)

PICkit 2 Debug Express Lessons (PIC16F887)
« Last Edit: May 22, 2017, 02:17:25 am by Ian.M »
 

Offline cvrivTopic starter

  • Frequent Contributor
  • **
  • Posts: 275
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #26 on: May 22, 2017, 06:39:36 am »
Ok... I got through all the replies. Thanks to everyone for all the information.

I am definitely using a PIC16F883 for the class and we are definitely programming it with Assembly. I find it extremely frustrating that there are more online tutorials/ projects than actual books available. I actually prefer to read and learn from real books, but i guess i dont have a choice.

I will check out the Googlium thing and see what thats about. I actual have a friend that took the class already and he texted me some ebooks that he used. I trust this guy because he's very smart. I just thought that maybe there was a book or books I could buy.

I actually wanted to take C++ before taking this class, but the professor was like no! He saod in this class we're doing Assembly. He said to take this class now because its only available once a year. So i was like..... Ok.

I saved the projects folder for this class before the end of this semester, so I know what all the projects are going to be. I can actually get started over the summer if i want. The proffessor is awesome because if any of us have any trouble doing it over the summer, he'll arrange to meet to help walk us through any problems we may have. He said we dont really need a book and that all we really need are the data sheets project whatever. Im assuming its all in this folder I have on my PC.

I dont know anything about assembly... I just find it to be weird that i im going to learn this with datasheets and tutorials etc. As said, im used to learning via a book.
 

Offline CJay

  • Super Contributor
  • ***
  • Posts: 4136
  • Country: gb
Re: Pic microcontoller programming in assembly...
« Reply #27 on: May 22, 2017, 10:02:10 am »
There are plenty of PIC books out there, but there's such a *lot* of information out on the web that all you need is the datasheets and some good tutorials, the Gooligum Electronics ones are good.


Most of the learning I did from datasheets and experimenting though if you're determined to have a book then I'm sure I'll get shot down but I got on well with the Myke Predko books, there may well be better out there now.

https://www.amazon.co.uk/Programming-Customizing-PIC-Microcontroller-Electronics-x/dp/0071472878

They seem to have been abandoned now but the need for the web resources were minimal anyway and the books should come with CD
 

Offline NivagSwerdna

  • Super Contributor
  • ***
  • Posts: 2495
  • Country: gb
Re: Pic microcontoller programming in assembly...
« Reply #28 on: May 22, 2017, 10:18:45 am »
This is the book I bought...

https://www.amazon.com/Embedded-Design-PIC18F452-John-Peatman/dp/0130462136/ref=asap_bc?ie=UTF8

but that was >15 years ago... I imagine there is a lot that hasn't changed if you are using 16F

(I would not recommend buying a new copy at >$130 !!)
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3478
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #29 on: May 22, 2017, 10:32:54 am »
@cvriv

I suspect not many Assembly programmers actively use flow charts on a day to day basis.  Until the instruction set becomes second nature, a flow chart will allow you to break down the assignment into smaller manageable tasks.  It is also useful for getting the whole picture and reducing go to's (part of creating spaghetti).

The program I used  was Lucid Chart (https://www.lucidchart.com/users/login).  It was free, but it has been awhile since I opened it, so that may have changed.

John

Edit:  I am not recommending "flowcode" and other programs that claim to convert a flowchart to actual code.
« Last Edit: May 22, 2017, 10:35:15 am by jpanhalt »
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Pic microcontoller programming in assembly...
« Reply #30 on: May 22, 2017, 10:43:11 am »
I think all the paper books out there for PIC assembler are badly out of date.  Certainly the principles haven't changed, but the tools and chips have.   The only reasons to get a paper book is if your first language isn't English and a good introduction in your native language is available, or if you can only learn effectively by marking up a paper book with lots of annotations and hilighter.   Even so, for the latter, you'd do better to print out the Gooligum tutorials single sided to allow lots of room for notes and have them spiral bound.  You'll be hard-put to find anything else that covers Midrange PIC assembler and is as up-to-date as the Gooligum tutorials.

Personally, if I were you and I was trying to make every dollar count,  I'd invest in the Gooligum tutorials, a breadboard, a PICkit 3 clone, 3x every PIC used in the Gooligum tutorials + 3x PIC16F883, and a selection of LEDs, switches, resistors etc.  If budget isn't an issue , I'd swap the clone PICkit 3 for a genuine one and swap the Gooligum tutorials for their Baseline and Mid-Range PIC Training Board, which includes the tutorials.   
 
The following users thanked this post: NivagSwerdna

Online KL27x

  • Super Contributor
  • ***
  • Posts: 4103
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #31 on: May 22, 2017, 11:18:36 am »
Quote
I dont know anything about assembly... I just find it to be weird that i im going to learn this with datasheets and tutorials etc.

That's the thing. If you read the instruction set in the datasheet, it describes what each instruction does. Once you wrap your head around the memory structure (also in the datasheet), binary and hex, and you understand the format of this data, this IS PIC assembly. You could almost start writing assembly by stringing together instructions. Your actual code will mainly consist of direct use of the native instruction set of the device.  But this is the easy part. (Other parts will be pseudo instructions, configuration, defining memory, and assembly directives, initializing EEPROM).

You might want to take a peek in the folder you got. Maybe he has provided some basic info to get you started. If you just want to get an A and move on, this (and the datasheet) might indeed be all you need. But if you want to learn to use PIC assembly in practice, Gooligum is untouchable. Head and shoulder and ankles above the rest, IMO. And as Ian suggested, to print it out, the tutorials ARE books. Very concise and organized books that should be easy to print out. As well, you should print out the entire datasheet of the 16F883 as well as the devices for the Gooligum tutorials, if you choose to try them. But I suggest you print out the datasheets in 2:1 book format and bind them. That is some labor involved, but it will be much handier. The datasheets will be a near constant reference, and a PDF doesn't cut it. You will have two or three fingers in it at times, flipping back and forth to put all the puzzle pieces together.
« Last Edit: May 22, 2017, 11:49:42 am by KL27x »
 

Offline StillTrying

  • Super Contributor
  • ***
  • Posts: 2850
  • Country: se
  • Country: Broken Britain
Re: Pic microcontoller programming in assembly...
« Reply #32 on: May 22, 2017, 11:58:01 am »
We're going to be using a PIC16F883 controller.

You can program and run it long before you get the uP, in the MPLAB SIM.
.  That took much longer than I thought it would.
 

Online KL27x

  • Super Contributor
  • ***
  • Posts: 4103
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #33 on: May 22, 2017, 05:03:07 pm »
Btw there is also an active forum over at the microchip website. Ian and ric are seemingly able to answer everything. I suspect one or the other wrote these tutorials. :)
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Pic microcontoller programming in assembly...
« Reply #34 on: May 22, 2017, 05:18:30 pm »
No. David (Mr. 'Gooligum'), user meikled on the Microchip forum, isn't in any way connected with my self, other than by a number of public discussions on the Microchip forum, and to the best of my knowledge isn't connected to Ric (on Microchip forum, and also his own forum http://picforum.ric323.com/), either.
 

Offline lowtech

  • Newbie
  • Posts: 3
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #35 on: May 26, 2017, 04:24:37 am »
PICmicro x14 Basic Training Modules. I know it's x14 but a lot of the basics are there.

http://techtrain.microchip.com/x14/

The following modules are available to help you familiarize yourself with the PICmicro MCU. Each module is an animated movie with audio.
PICmicro x14 Architecture
PICmicro x14 Instruction Set
Development Tools Overview
PICmicro x14 Device Configuration
PICmicro x14 Resets
PICmicro x14 Oscillator

Just received my pickit 3, still waiting on my Gooligum training board, which includes all tutorials. I hope to start next week.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Pic microcontoller programming in assembly...
« Reply #36 on: May 26, 2017, 05:55:13 am »
x14, more commonly called the 14 bit core, is just what you need for 'classic' Midrange parts like the PIC16F883.  The core hasn't changed any however those videos are way out of date for the toolchain.  They refer to the original ICD debugger and mention the obsolete C17 compiler for the discontinued PIC17 range, which means they are probably circa late '90s and use the old 16 bit Windows MPLAB 5! 
 

Offline lowtech

  • Newbie
  • Posts: 3
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #37 on: May 26, 2017, 06:22:05 am »
Good to know Ian. Since I'm new to this as well, which of these are worth watching? Evidently not the Development Tools Overview.
 

Offline Ian.M

  • Super Contributor
  • ***
  • Posts: 12860
Re: Pic microcontoller programming in assembly...
« Reply #38 on: May 26, 2017, 07:40:55 am »
Personally, I don't like video tutorials*  - I'd much rather have text, diagrams and images (and optionally short <2min video clips to show particular techniques and manual skills), so I'm less than keen on any of them.  However if you learn well from videos the first two + resets should be worth a look.  The remaining two, Device Configuration, and Oscillator are still valid but of course don't cover t newer device features like multiple config words or internal oscillators.

* When I learned the Midrange (14 bit core) on the PIC16C84, video wasn't really an option.  IIRC we'd just got ISDN at work, I believe I'd acquired a MPLAB CD at an exhibition, and I hammered the ISDN connection grabbing datasheets, FAQs and schematics and sourcecode for a Tait programmer.  I've still got the binder full of printed off datasheets . . .
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: Pic microcontoller programming in assembly...
« Reply #39 on: May 26, 2017, 08:36:00 am »
I dont know anything about assembly... I just find it to be weird that i im going to learn this with datasheets and tutorials etc. As said, im used to learning via a book.

I learn best with examples and writing programs myself. I think the PIC assembly language is not the best to start learning assembly, because the small instruction set makes it more difficult. I can program in PIC assembler, but it is still always twisting a little bit my brain when I have to program "skip next instruction when some bit is set", and then the next instruction is the actual branch, or the strange things I have to do to implement a simple lookup table (and be careful with page boundaries if you try this), which is one instruction with other assembler instruction sets, like 6502. Bank-switching was not a problem for me, because I just select the bank all the time before I access something when in doubt.

That said, you should install the MPLAB-X IDE. It is free and has an integrated simulator, which is very helpful, because you can single-step through your assembler program and see for each instruction how it changes registers, and you can even create a virtual "oscilloscope", where you can see the waveform of a GPIO pin which you control from your program, all without hardware. Then you can start with a tutorial program and see how this works. Then enhance the tutorial program and soon you know PIC assembly.

But of course, would be best if you try it on real hardware. Buy a PICKit3 programmer (there are cheap clones on eBay) and a few PIC16F883, and a solderless breadboard if you don't have already one, and things you want to do with it, like LEDs (with resistors in series), buttons and sensors. Then think of a simple project, for example a button press should be delayed and light an LED (as I did here), and try to program it yourself. This is the best way to get good at programming: not by reading books, but by programming. But to read an introduction book first, what assembly is at all and how a CPU works, might be helpful, if you don't know this already.
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #40 on: May 26, 2017, 04:00:45 pm »
I actually wanted to take C++ before taking this class, but the professor was like no! He saod in this class we're doing Assembly. He said to take this class now because its only available once a year. So i was like..... Ok.

If assembly language is as close as you can get to the hardware, C++ is about as far away.  You will be coming at computers from both ends.  Not a bad plan!

I started with Fortran IV and migrated down to 8080 assembly language.  I was a bit slow on the uptake but one thing I think I have learned:  It isn't about the language as almost any program can be expressed in almost any language, it's about the data and how it is manipulated.

I'm of the opinion that coding is just a matter of applying patterns.  There are specific ways that various operations are coded and I like to think of them as patterns.  A loop, indexing an array, basic math, subroutine calls and returns; each of these operations will eventually degenerate into a pattern.  There may be alternatives and you will probably select one based on your personal taste.  It is still a pattern.

When you get to 'banks', you will see where it is useful to have a written map of where you want to put various data items.  It is best if related items are in the same bank as this reduces bank switching.  I sometimes copy the memory map out of the datasheet and draw on it.

See page 6-10 here:
http://ww1.microchip.com/downloads/en/DeviceDoc/31006a.pdf

While I am on the subject, read that document and follow along.  Put it under your pillow at night if you think it will help.  Everything you need to know about paging and banking is in that document.  Notice the special area at addresses 70h..7fh.  Regardless of the bank selection, you always get the same data.  There is only one copy and it allows access to those 16 bytes without worrying about banking.

The map in the above document is generic and it doesn't apply to the 16F883 directly.  Instead see the actual map here (Page 24):

http://ww1.microchip.com/downloads/en/DeviceDoc/41291D.pdf

In addition to datasheets for various families of chips, Microchip has a small document describing all of the mid-range CPUs.  It is a mere 688 pages and there will be a test later:

http://ww1.microchip.com/downloads/en/devicedoc/33023a.pdf

Read question 4 on page 29-45 and when you think you understand it, I assure you, you do not!  This Read-Modify-Write thing catches a lot of programmers.  And then you will come up with the idea of shadow registers where you keep a copy of what has been sent to the port.  You modify the shadow register (just a byte of memory somewhere) and then send it to the port rather than reading the port and modifying individual bits.

You said you wanted a book!  Well, there it is...
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #41 on: May 26, 2017, 04:17:57 pm »
@cvriv

I suspect not many Assembly programmers actively use flow charts on a day to day basis.  Until the instruction set becomes second nature, a flow chart will allow you to break down the assignment into smaller manageable tasks.  It is also useful for getting the whole picture and reducing go to's (part of creating spaghetti).

The program I used  was Lucid Chart (https://www.lucidchart.com/users/login).  It was free, but it has been awhile since I opened it, so that may have changed.

John

Edit:  I am not recommending "flowcode" and other programs that claim to convert a flowchart to actual code.

I wonder if they even teach flowcharting to CS majors these days...

One thing I like to do is write a kind of high level pseudo code of what I plan to do.  I can have a lot of detail or a little depending on the complexity.  Regardless of the target language, the pseudo code is a map toward the end result.  Which kind of leads back to my comments above on 'patterns'.

In the old days of Fortran and batch computing, there were three blocks of code:  Input, Crunch, Output.  Every program did the same 3 things, the code was just the details.  Sometimes there was a loop around the 3 blocks.

I always started with the output I wanted and worked back toward the input I needed.  I know it is totally obvious these days but, at the time ('70-), it took a while to understand these things.  I wasn't the brightest bulb...
 

Online jpanhalt

  • Super Contributor
  • ***
  • Posts: 3478
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #42 on: May 26, 2017, 08:27:22 pm »

I wonder if they even teach flowcharting to CS majors these days...

One thing I like to do is write a kind of high level pseudo code of what I plan to do.  I can have a lot of detail or a little depending on the complexity.  Regardless of the target language, the pseudo code is a map toward the end result.  Which kind of leads back to my comments above on 'patterns'.


What you describe as "pseudo code" sounds like a flow chart to me.   You have processes (rectangles) and decisions/branches (diamonds) and outputs (if you chose to use them).   Anyway, pseudo code, flow chart, recipe, or whatever you call it, will help avoid spaghetti code and is an aid to the beginner.  It was to me. 

I can't imagine a college-level course on "flow charting."   What did they do for the remaining 74 days after the first hour on day one? :D

John
« Last Edit: May 26, 2017, 08:29:15 pm by jpanhalt »
 

Offline rstofer

  • Super Contributor
  • ***
  • Posts: 9890
  • Country: us
Re: Pic microcontoller programming in assembly...
« Reply #43 on: May 26, 2017, 10:48:43 pm »

I wonder if they even teach flowcharting to CS majors these days...

One thing I like to do is write a kind of high level pseudo code of what I plan to do.  I can have a lot of detail or a little depending on the complexity.  Regardless of the target language, the pseudo code is a map toward the end result.  Which kind of leads back to my comments above on 'patterns'.


What you describe as "pseudo code" sounds like a flow chart to me.   You have processes (rectangles) and decisions/branches (diamonds) and outputs (if you chose to use them).   Anyway, pseudo code, flow chart, recipe, or whatever you call it, will help avoid spaghetti code and is an aid to the beginner.  It was to me. 

I can't imagine a college-level course on "flow charting."   What did they do for the remaining 74 days after the first hour on day one? :D

John


I don't know what they do today but I have IBM documentation from back in the '60s and it often contains flowcharts that were dozens of pages.  In fact, you can follow the assembly code directly from the flowchart, it was that detailed.  The stuff was all printed on a line printer so they must have had a very slick program for drawing it.  For that matter, their logic diagrams were also drawn on a line printer.

Again, back in the old days, I visited some business programmers in another department.  They wrote all their code in COBOL and the flowcharts they created were extensive.  Old school...

Flowcharting itself is pretty easy.  Program structure is the hard part.  In some ways business programming is much more complex than simple embedded programming as the decision trees can be quite complicated.  Think about sales taxes that vary within a state, county or city.  I'm not sure what a company's obligation is in terms of how far down they need to dig but I'm betting the problem is non-trivial.

My pseudo code turns out to look a lot like C without the proper syntax.
 

Offline ggchab

  • Frequent Contributor
  • **
  • Posts: 276
  • Country: be
Re: Pic microcontoller programming in assembly...
« Reply #44 on: May 27, 2017, 08:51:23 am »
I started with 16F84 et 16F877. Instructions set is not that difficult to learn but segmentation of memory (program and RAM) is a bit annoying. Something else that caused me many problems was the limited levels of the hardware stack. If you do too many nested calls, you might never come back to the instruction following the first call. Now, I am using PIC18F (18F25K22). There are easy solutions to most of the previous problems (far less need to switch between banks, 31 levels stack,...). I like programming them in assembly. This is not very time efficient but as a hobby, this is a lot of fun.
I also find very important to add a lot of comments. I document each blocks of code and, frequently, instructions themselves.
When I started programming PICs, there was a lot of Web sites to teach programming 16F84. For the others, I simply used the documentation of Microchip (printed copies). I find it well structured and easy to read.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf