Help with PIC / ASSEMBLY for a project from a book - Page 1
Forum section: electronics / general

author: Zybyyy
author_rank: Newbie
timestamp: February 19, 2019, 10:47:12 pm
content: Hello everyone,I am new to the world of PIC programming. I bought a book "PIC in Practice" by D W Smith 1st editionWhile trying to recreate the task 1 there are some problems I can not sole for the past 3 days. I really want to progress and carry on being better however at this stage I do not understand enough to solve it.Program says that the LED supposed to blink - I can not make this LED to blink, basically nothing happens. I do not know whySo, the language is ASSEMBLY, I use PICkit 2, and MPLAB X 5.10, I tried this on 16F84 and 16F628A, 2-3 each. I use MPLAB breadboard.I have attached schematics for this.Programm from the book tells me:; Header84.ASM for 16F84. This sets PORTA as an INPUT(NB 1; means input) and PORTB as an OUTPUT; (NB 0 means output). The OPTION; register is set to /256 to give timing pulses; of 1/32 of a second.; 1 second and 0.5 second delays are; included in the subroutine section.;*****************************************************************************; EQUATES SECTIONTMR0 EQU 1 ; means TMR0 is file 1STATUS EQU 3 ; means STATUS is file 3PORTA EQU 5 ; means PORTA is file 5PORTB EQU 6 ; means PORTB is file 6ZEROBIT EQU 2 ; means ZEROBIT is bit 2COUNT EQU 0CH ; means COUNT is file 0C; a register to count events;*****************************************************************************LIST P=16F84 ; we are using 16F84ORG 0 ; the start address in memory is 0GOTO START ; goto start;*****************************************************************************;*****************************************************************************; SUBROUTINE SECTION; 1 second delayDELAY1 CLRF TMR0 ; start TMR0LOOPA MOVF TMR0,W ; read TMR0 into WSUBLW .32 ; TIME -32BTFSS STATUS,ZEROBIT ; check TIME-W = 0GOTO LOOPA ; time is not = 32RETLW 0 ; time is 32, return; 0.5 second delayDELAYP5 CLRF TMR0 ; start TMR0LOOPB MOVF TMR0, W ; read TMR0 into WSUBLW .16 ; TIME -16BTFSS STATUS,ZEROBIT ; check TIME-W = 0GOTO LOOPB ; time is not = 16RETLW 0 ; time is 16, return; CONFIGURATION SECTIONSTART BSF STATUS,5 ; Turns to BANK1MOVLW B'00011111'TRIS PORTAMOVLW B'00000000'TRIS PORTB ; PORTB is OUTPUTMOVLW B'00000111' ; prescaler is /256OPTION ; TIMER is 1/32 secs.BCF STATUS,5 ; return to BANK0CLRF PORTA ; clears PORTACLRF PORTB ; c;ears PORTB;*****************************************************************************; PROGRAM STARTS NOWBEGIN BSF PORTB,0 ; turn ON B0CALL DELAY1 ; wait 1 secondBCF PORTB,0 ; turn OFF B0CALL DELAY1 ; wait 1 secondGOTO BEGIN ; repeatEND ; you must end !!!Please help !!! Thank you very much in advance.

author: SiliconWizard
author_rank: Super Contributor
timestamp: February 19, 2019, 11:51:49 pm
content: I haven't used the 16F family in ages, but I think you need to set the config bits somewhere. This will set, among other things, the oscillator source. I bet the clock is not running because the config bits are not properly set.

author: Buriedcode
author_rank: Super Contributor
timestamp: February 19, 2019, 11:53:04 pm
content: Whilst it is admirable you're starting with assembly language (which is fine, but doesn't really give you any advantage over C, and makes life harder) and working from a book, the PIC16F84 is andancientdevice. Even the flash version, the PIC16F84Ais what, 25 years old? The PIC16F628A is newer, but not much. This means they are both pretty basic, but perhaps more importantly, can be rather expensive, and difficult to program.With that said.. a PICkit 2 is fine for this, and the micro's lack of peripherals I suppose can be an advantage, as they really only have things like a couple of timers, comparators and a UART for the 628A. So when using assembly, - there's less things to turn off in the init routines.Now, the code. Please use code tags - highlight the code you posted and click the '#' button above the textbox. Or enclose the code in [ code ] [ / code] (without spaces).This is specifically for the PIC16F84. The line "LIST P=16F84" tells the compiler what processor, and is usually with an "#include <p16F84A.inc>" which loads a file containing the register names and address. Thats essentially what your "equates" section is doing, but including the above, will do that for you. Search your MPLABX folder for "p16F84.inc" and "p16F84A.inc" - these will show your all the register names used in mpasm assembly and their registers for that processor. For the purposes of your code, the 84 and 84A are essentially the same, but may *not" work with the PIC16F628A without changes.The main program - between BEGIN and END - looks fine, very simple. The config code also looks OK - correctly setting PORTB as output, PORTA as input, configuring TMR0 prescaler etc.. So I'm thinking this is a hardware problem. Your schematic shows a 32kHz crystal oscillator with 68pF caps. In order to use that kind of crystal the "config" bits in MPLAB should be set for it. See page 23 of the datasheet (https://ww1.microchip.com/downloads/en/devicedoc/35007b.pdf) it should be configured for LP Oscillator. I believe the default is for an RC oscillator which requires a capacitor and resistor instead of a crystal. If you have set the config bits, please post the "Config word" you see in PICKit2 when you load in your hex file and flash it. It'll be somethingother than3FFF (which means its not configured).If you have access to a function generator, or something that will generate a 5V square wave of anything from 1kHz to 100kHz, you could put this on OSC1 pin (pin 16) to clock the device. If the LED blinks then, you know you've successfully programmed the device, and the code works. If you wish to use your 32Khz watch crystal, then you'll need an oscilloscope to check it is oscillating.edit: siliconwizard beat me to it

author: KL27x
author_rank: Super Contributor
timestamp: February 20, 2019, 12:53:40 am
content: Most of the problem has already been covered.Additional 2 cents:In addition to ensuring the oscillator settings are correct in your configuration settings, you also have to make sure the WDT is off. Since there is no handling of WDT in this code, if you turn/leave WDT on, the chip will just continually reset itself and start the code over from the beginning.<edited for brain fart.>From the datasheet:QuoteAn alternative to using MPLAB configuration menu, you can write this stuff in your header, manually. This way when you share your project, like in a book, this information is clear. It's weird he wrote out those defines/equs (which are sorta redundant), but he didn't write out the configuration stuff. There's only 4 things in the configuration word.Manually, you could just write at the top of your .asm page. It looks something like this. TBH, I'm not sure what osc setting you need, if it's ext/int, or how to spell it exactly. And I don't know how I learned this or where to look it up. I suppose it should be documented in the IDE manual somewhere. Maybe they're defined in the .inc file? I must have copied it from somewhere... I have dicked around and figured out many configuration instructions since then by trial and error.__CONFIG _WDT_OFF & _PWRTE_ON & _ExtLP_OSC & _CP_OFFAlternatively, you could figure out the bits and just write it like that, too.__CONFIG <insert the 5 bit configuration word, here, from 4,3,2,1,0, in dec/hex/binary, w/e you want>example, I think you could write it like__CONFIG b'10000'or__CONFIG 0x10*edit: since your .asm page doesn't include the 16f84.inc file (like BuriedCode has suggested), the first way I did that will probably not work! But the direct number should.

author: notsob
author_rank: Frequent Contributor
timestamp: February 20, 2019, 01:31:50 am
content: Although these are quite old, pcbheaven has some PIC assembly tutorialspcbheaven.com

author: KL27x
author_rank: Super Contributor
timestamp: February 20, 2019, 01:58:28 am
content: Found it. This stuff is defined in the .inc fileQuoteSo if you "#include p16f84a.inc" in your code, you can select your configuration like this:__CONFIG _PWRTE_ON & _CP_OFF & _WDT_OFF & <include the define for your oscillator, here!>(there are 2x underscores in front of "CONFIG" and only one everywhere else.)*Also, while perusing these files, I found my source of how I learned to use this __CONFIG directive... At least with the older versions of MPLAB, they include device specific template asm files to help get you started. They all have an example of how to use the __CONFIG directive.Try going to your drive where you installed the IDE > Program files x86 (or whatnot) >Microchip >MPASM SuiteThis folder contains the .inc files, and it has a folder named "templates" where you can get example asm templates for your device. After the first one you do, you will probably never use it again.

author: Doctorandus_P
author_rank: Super Contributor
timestamp: February 20, 2019, 03:02:32 am
content: Do you have any particular reason for using assembly or a pic?Assembly is nowadays only usefull in a few niche applications and for coarses of learning computer architecture, but has hardly any use for writing uC applications.It can be done, but just because it can does not mean you should do so.You can scratch with nails into a clay tablet, but you can also use a typewriter.Euhm.Finding a nail to write into some clay is probably a lot easier than finding an actual typewriter nowaday's. but the're both obsolete.The PIC microntollers are also not the best uC architecture for writing software, but when you use a "higher language" most of the annoying stuff is abstracted away. "arduino" is C / C++ and fairly easy to get started with, but that platform has unfortunately crippled itself into a stat to only being useable for simple beginner projects.For a beginner that wants more than "only blinking leds" probably anything that has a good quality C compiler available is a good option.Some of the C / C++ compilers for uC's are still very expensive, or are crippled into only being able to make small programs. There is however the omni present GCC which is Free & Open Source and has been ported to several different uC architectures.

author: KL27x
author_rank: Super Contributor
timestamp: February 20, 2019, 03:28:49 am
content: QuoteThe typewriter is obsolete. It was made by some dudes who had some ideas about how best to print stuff. Then it was superceded by something better.Clay and nails will be useful forever.

author: picandmix
author_rank: Frequent Contributor
timestamp: February 20, 2019, 09:36:51 am
content: Hi,As you say you have those chips , then you might find this assembly tutorial more practical than your book, its one I got many years ago and equally found not to be that helpful.The WinPicProg author is still active so plenty of help around.http://www.winpicprog.co.uk/pic_tutorial.htmThough you are using MPlabX , the older but simpler IDE, MPlab V8.92 is still available from Microchip as a download.Edit, also using a crystal on a breadboard can often result in problems due to its fine leads etc.Much better to use the 628A chip which has its own inbuilt 4mhz oscillator.Think I still have one of those chips so can run a led flasher code up on it if you are stuck.

author: westfw
author_rank: Super Contributor
timestamp: February 20, 2019, 10:59:59 am
content: It looks OK (but it's been a long time), and it simulates approximately way you'd expect, but even in simulation, the PORTB bit isn't changing :-(Actually, the TRIS registers aren't changing either... That really implies that something is wrong with your bank selection... Hmm...Ah hah!Code:START BSF STATUS,5 ; Turns to BANK1MOVLW B'00011111'TRIS PORTAMOVLW B'00000000'TRIS PORTB ; PORTB is OUTPUTMOVLW B'00000111' ; prescaler is /256OPTION ; TIMER is 1/32 secs.BCF STATUS,5 ; return to BANK0The TRIS and OPTION "instructions" don't need you to switch banks; they directly modify the TRIS and OPTION registers regardless of the current bank. If I remove the BSF/BCF of the status register, the simulation will blink!So I guess ... TRIS and OPTIONinstructionsdon't work with the banks.Note that the datasheet (for 628) says:QuoteSo correct code would look like:Code:START BSF STATUS,5 ; Turns to BANK1MOVLW B'00011111'MOVWF TRISAMOVLW B'00000000'MOVWF TRISB ; PORTB is OUTPUTMOVLW B'00000111' ; prescaler is /256MOVWF OPTION ; TIMER is 1/32 secs.BCF STATUS,5 ; return to BANK0

author: picandmix
author_rank: Frequent Contributor
timestamp: February 20, 2019, 03:21:47 pm
content: Hi,Only had a 628 chip in the box, not the 628A so could only simulate the code, though it works ok.

author: Zybyyy
author_rank: Newbie
timestamp: February 20, 2019, 09:31:07 pm
content: Thank you all for your comments and help.I will try this solutions and see where I am.I am really interested in the microcontroller - robotics world and would like to learn what is the best.Can you please advise on best language nowadays to learn?I understand PIC is a bit "dated" but someone told me that this is a must foundation for programming?Thank you very much again!

author: KL27x
author_rank: Super Contributor
timestamp: February 20, 2019, 10:44:16 pm
content: That's easy. Assembly programmers are dickhead dinosaurs. You should be using Ar-Dwee-No! No, wait, Raspberry Pi. I want Raspberry Pi running linux controlling the ABS brakes in all the cars around me.Learn w/e interests you. Don't be a dickhead assembly programmer. Don't be a dickhead higher level program user that thinks he can make autonomous drones fly at 100 mph and avoid objects by connecting to a computer through the internet. There's computing power and memory and there's latency. You can't overcome latency with more memory and computing power.We got different tools for different jobs. Ideally, you will be able to use the best tool for the job. Practically, you will often force a square peg into a round hole and it will work just fine, so long as you know what you're doing.Even if you have a CNC machine, it it helpful to know how to use a chisel. I wouldn't spend 10 years to try to become Michael Angelo or Paul Sellers. But some basic knowhow can be useful. Sometimes a compiler doesn't do exactly what you want it to. Sometimes your C code will work fine with one compiler but it is bugged with another. Some things are more easily done or fixed in asm. Esp if you are interested in robotics and hardware. The main brain and computer interface for your robot might run linux, but there may be hundreds of sensors and motors with their own controllers and subsystems that need to react very quickly to very simple algorithms.

author: westfw
author_rank: Super Contributor
timestamp: February 20, 2019, 11:04:28 pm
content: QuoteAs a foundation for embedded programming, you should indeed learn some "fundamental" about registers, memory, timers, asynchronicity and busy loops, and the pain of what goes on in the actual chip.an old PIC is probably as good as anything for that - newer chips have significantly more complexity that masks some of those basics. I'm a little bit in awe of how little code it took to configure that PIC timer - a modern ARM chip is likely to have at least 4 types of timer to chose from, some with up to 128bytes of configuration.The problem with a really ancient chip like the PIC16F628 or (even older) 16F84 is that most of what you learn there will be ONLY "foundational." It won't give you the knowledge needed to build a modern project or product, and it won't be a key phrase on your resume. You need to learn some of those fundamentals and quickly move on to seeing how they apply in a more modern environment.

author: mariush
author_rank: Super Contributor
timestamp: February 20, 2019, 11:21:35 pm
content: Nothing wrong with PIC microcontrollers, it's just your approach.C compilers are now good enough to generate decent code, so you don't need to program in assembly anymore.The programming IDE (MPLAB and MPLAB-X) is free, and you have lots of tutorials online that can teach you how to program with C.The Pickit 3 programmer bundled with a circuit board and a microcontroller even had a CD with guides and tutorials that explained how to use MPLAB IDE (the one before MPLAB-X) and had example programs.Actually, let me see if I can find that CD... and here's an ISO image of the CD that came with the programmer:http://savedonthe.net/a/microchip/IDE_8_60.ISO(I'll leave it there for a few days)Here's just the pdf that contains some simple C examples ( lessons that worked on the micro with the dev board but most work with any pic16f or pic18f micro) :http://savedonthe.net/download/2362/PICkit_3_Lessons_UG_41370c.htmlYou can replace MPLAB with MPLAB-X and the download should come with documentation and c compilers in the package and everything you need:https://www.microchip.com/mplab/mplab-x-ideYou can easily adapt the lessons to other microcontrollers, you don't have to buy the exact chip they use in the lessons.

author: Zybyyy
author_rank: Newbie
timestamp: February 21, 2019, 01:03:07 pm
content: Thank you very much for that.Now I am thinking going between C and Python. Probably will go with Python.Thank you again. Great forum, great job everyone !

author: NorthGuy
author_rank: Super Contributor
timestamp: February 21, 2019, 02:28:04 pm
content: Quote from: Zybyyy on February 21, 2019, 01:03:07 pmUsing Python to learn programming is like becoming electrician to learn Ohm's law.

author: picandmix
author_rank: Frequent Contributor
timestamp: February 21, 2019, 02:39:55 pm
content: Quote from: westfw on February 20, 2019, 11:04:28 pmAgree with @Westfw and the advice you say had initially received.You already have the Pic equipment and access to good tutorials so why not spend some time learning about them from the ground up; it is a good foundation that the higher level languages do not always give you, relying mostly on existing libraries written by others, so you learn very little by comparison.Nothing to stop you later moving onto C, C++, Python etc.Look in the local and national job adverts and see what languages the market is seeking or check your local colleges and its computing courses which should be geared to local needs.

author: jackthomson41
author_rank: Regular Contributor
timestamp: February 22, 2019, 03:25:21 pm
content: why people are still working on assembly language forPIC16F877a, I'll never know.

author: NivagSwerdna
author_rank: Super Contributor
timestamp: February 22, 2019, 05:13:22 pm
content: Try using RB1 and report back. (RB0 is also INT)

author: member_xyz
author_rank: Contributor
timestamp: February 22, 2019, 10:35:44 pm
content: QuoteNow you going from ASM to Python?It's pointless commenting or offering you any advice.You need to learn the hard way.

author: Zybyyy
author_rank: Newbie
timestamp: February 23, 2019, 11:49:49 pm
content: Quote from: member_xyz on February 22, 2019, 10:35:44 pmmember_xyz - Thank you very much for your "advice", as I looked up you did not contribute to the positive discussion so please spare yourself those comments.And on the other note I found the problemI jumped the guns a bit. I missed a part in the book where it says how to setup Arizona programmer - I thought not for me I have PICkit 2, well... I did wrong.There is a part where it talks about FUSES setup and LP oscillator setup. I set it as per book and it was beautifull. !!! Guys you were right !!!Massive thank you very much to all helpfull members.Special thanks to: westfw, mariush, picandmixYes !!! I will continue now with ASSEMBLY as I have the book and this error on my side just tought me a lot about PIC setup differences and how do they work.I will continue to have a good, old days understanding.Quote from: jackthomson41 on February 22, 2019, 03:25:21 pmJack - I am learning using ASSEMBLY to know how to operate chisel before CNCWhat I discovered:- The program is fine and all good but only for PIC16f84. Instead of using PICkit 2 I used K150 programmer and it was all clear - I need to setup FUSES correctly- PIC16f628A does not like that program beacuse of the instructions like it was said in comments.I have read most of the book and the tasks are much clearer now.Thank you very much again for your time!Best regards !

author: picandmix
author_rank: Frequent Contributor
timestamp: February 24, 2019, 12:05:28 am
content: Glad you have found your way, enjoyMicrochip have their own MPASM forum, though quiet a few of their experienced and helpful members are also here in EEVblog so can help if needed.

author: @rt
author_rank: Super Contributor
timestamp: March 01, 2019, 03:46:32 am
content: I find it very assuming to dissuade someone from learning assembly without even questioning their motivation for it.Some might say you can have no understanding of how a computer works until you’ve written at least some kind of cyclic program.I’d agree, and go further to say you can’t have as intimate knowledge of what’s actually going on inside a controller until you’vecopied a value from a register to an accumulator, performed an operation on it, and copied back to a register...or used a program counter to jump to an address, or a bunch of other things that are abstracted away by higher level languages.Or you might simply enjoy the idea of achieving a result with the least resource, etc.I think assembly is the best start, because most people will never go back if they begin with a higher level language.

author: KL27x
author_rank: Super Contributor
timestamp: March 02, 2019, 08:55:16 am
content: Different strokes, yeah.I've heard the old "C compilers are very reliable, these days. There's no need for assembly."The devices and compilers and IDE tools we use today are going to be obsolete, tomorrow. That might not matter to you. You'll just switch to the new one. But some people are making the new compilers. Some humans are adding support for new devices. Some people are writing and modifying C libraries. Some people are writing inline assembly.Cars are very reliable, today. This doesn't mean every person on the planet needs only to know how to pump the gas and turn the ignition key. There are people who work under the hood. There are people designing new cars. Every part of the car, there are people redesigning and improving each aspect. That might not be for you; maybe you just want to drive. And maybe you don't care if progress stopped, today. And you could drive the car you're comfortable with, forever. I know that would be fine to me. But we have an economic model that only works on constantchangeimprovement, and so the gears will keep turning. And there will be people maintaining and changing and sometimes even improving the tools that you use.