Having trouble writing blinky program with PIC assembly language - Page 1
Forum section: electronics / general

author: DaveHardy
author_rank: Regular Contributor
timestamp: February 17, 2012, 10:31:08 pm
content: Does anybody know what I'm doing wrong? I'm having trouble with some of the instructions. The 16F1827 has a few extra commands in the data sheet for initializing the port A register via banksel that I don't quite get. From the datasheet:http://ww1.microchip.com/downloads/en/DeviceDoc/41391D.pdf; This code example illustrates; initializing the PORTA register. The; other ports are initialized in the same; manner.BANKSEL PORTA ;CLRF PORTA ;Init PORTABANKSEL LATA ;Data LatchCLRF LATA ;BANKSEL ANSELA ;CLRF ANSELA ;digital I/OBANKSEL TRISA ;MOVLW B'00111000' ;Set RA<5:3> as inputsMOVWF TRISA ;and set RA<2:I'm sure that it's something stupid. I know almost nothing about these things. This is my first program and I copied some of the instructions from a textbook.Here's my program and what I get when I try to Build it:;************************************; written by: Me; date: 2/17/2012; version: 1.0; file saved as: Test1; for PIC16F1827 E/P; clock frequency: Internal;************************************list p=16f1827 ; list directive to define processor#include <P16F1827.INC> ; processor specific variable definitions__CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_OFF & _IESO_OFF & _FCMEN_OFF__CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF;============; Declarations:porta equ 05org 7FFFhgoto Startorg 0;============; Subroutines:Init clrf porta ; resets Port Amovlw b’00000000’ ; RA0: LED, RA1-7: not connectedtris portaretlw 0;============; Program Start:Start call Init ; sets up inputs and outputsMain bsf porta, 0 ; turns on LEDgoto Main ; loops back to MainEND----------------------------------------------------------------------Debug build of project `C:\Users\Alexander Robotnick\Desktop\Test1.mcp' started.Language tool versions: MPASMWIN.exe v5.43, mplink.exe v4.41, mplib.exe v4.41Preprocessor symbol `__DEBUG' is defined.Fri Feb 17 15:12:19 2012----------------------------------------------------------------------Clean: Deleting intermediary and output files.Clean: Done.Executing: "C:\Program Files (x86)\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F1827 "Test1.asm" /l"Test1.lst" /e"Test1.err" /o"Test1.o" /d__DEBUG=1Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 11 : Found directive in column 1. (list)Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 14 : Found directive in column 1. (__CONFIG)Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 15 : Found directive in column 1. (__CONFIG)Error C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 27 : Symbol not previously defined (b’00000000’)Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 28 : Use of this instruction is not recommended.Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 35 : Found directive in column 1. (END)Halting build on first failure as requested.----------------------------------------------------------------------Debug build of project `C:\Users\Alexander Robotnick\Desktop\Test1.mcp' failed.Language tool versions: MPASMWIN.exe v5.43, mplink.exe v4.41, mplib.exe v4.41Preprocessor symbol `__DEBUG' is defined.Fri Feb 17 15:12:20 2012----------------------------------------------------------------------BUILD FAILED-Dave

author: 8086
author_rank: Super Contributor
timestamp: February 17, 2012, 10:37:45 pm
content: Change the b'00000000' to 0x00 or simply 00If it works, then I suspect there is a problem with your apostrophe character. It certainly looks different to mine.EDIT: Also, your program as it is won't blink the LED, it will just turn it on.EDIT 2: You also seem to have a weird execution order and some needless repeated instructions for the org/init parts of the program.

author: cybergibbons
author_rank: Frequent Contributor
timestamp: February 17, 2012, 10:46:26 pm
content: It hurts my head that someone with the nick 8086 is instructing someone else in PIC ASM.

author: DaveHardy
author_rank: Regular Contributor
timestamp: February 17, 2012, 11:46:07 pm
content: Quote from: 8086 on February 17, 2012, 10:37:45 pmMan, that stupid apostrophe!!! Great Eyes!! I know it won't blink yet, that is the next step:)I don't understand what is weird about my execution order. What instructions are needless? I'm trying to do the best I can and follow the program the way that it is explained in the book.From The Pic Microcontroller - Your Personal Introductory Course, 3rd Ed by John Morton:"We want the PIC microcontroller to begin at the place in the program which we havelabelled start, so we make sure the instruction at 1FFh is goto start. In thetemplate, org is used to place instruction goto start at 1FFh, making it the firstto be executed. However, subsequent instructions must start counting from 0, sothe following command is org 0. Writing the program memory address by theinstructions shows how it works:org 1FF01FF goto startorg 0;===========; Subroutines:0000 Init clrf porta ;0001 clrf portb ;etc.The first instruction to be executed (goto start) makes the chip goto ( jump)to the part of the program labelled start, and thus the PIC microcontroller willbegin running the program from where you have written start."The whole org routine does seem stupid to me too but I don't really know what to do about it.Here's my latest Build Failed:Debug build of project `C:\Users\Alexander Robotnick\Desktop\Test1.mcp' started.Language tool versions: MPASMWIN.exe v5.43, mplink.exe v4.41, mplib.exe v4.41Preprocessor symbol `__DEBUG' is defined.Fri Feb 17 17:07:41 2012----------------------------------------------------------------------Clean: Deleting intermediary and output files.Clean: Done.Executing: "C:\Program Files (x86)\Microchip\MPASM Suite\MPASMWIN.exe" /q /p16F1827 "Test1.asm" /l"Test1.lst" /e"Test1.err" /o"Test1.o" /d__DEBUG=1Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 11 : Found directive in column 1. (list)Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 14 : Found directive in column 1. (__CONFIG)Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 15 : Found directive in column 1. (__CONFIG)Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 28 : Use of this instruction is not recommended.Warning C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 35 : Found directive in column 1. (END)Executing: "C:\Program Files (x86)\Microchip\MPASM Suite\mplink.exe" /p16F1827 "Test1.o" /u_DEBUG /z__MPLAB_BUILD=1 /z__MPLAB_DEBUG=1 /o"Test1.cof" /M"Test1.map" /WMPLINK 4.41, LinkerDevice Database Version 1.5Copyright (c) 1998-2011 Microchip Technology Inc.Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00007fff, length=0x00000002Errors : 1Link step failed.----------------------------------------------------------------------Debug build of project `C:\Users\Alexander Robotnick\Desktop\Test1.mcp' failed.Language tool versions: MPASMWIN.exe v5.43, mplink.exe v4.41, mplib.exe v4.41Preprocessor symbol `__DEBUG' is defined.Fri Feb 17 17:07:42 2012----------------------------------------------------------------------BUILD FAILED

author: 8086
author_rank: Super Contributor
timestamp: February 18, 2012, 12:15:30 am
content: Quote from: DaveHardy on February 17, 2012, 11:46:07 pmThe problem is the whole business with the reset vector. For the PIC16F1827, the reset vector is 0000h, and the program memory space ends at 0FFFh, so when you try and assemble this:Code:org 7FFFhgoto Startorg 0You are asking for it to put the first instruction in a non existent location.The code should read:Code:org 0x00goto StartAnd the rest of the code will follow in sequential memory locations.What I was saying about the unnecessary instructions is actually not an issue, I didn't see your retlw instruction at the end of init the first time around. Going to start to then call init and then go to main is quite a roundabout way of doing it, though.

author: Mechatrommer
author_rank: Super Contributor
timestamp: February 18, 2012, 02:55:59 am
content: Quotethats why, esp if you want to do with asm, move with smaller pic first, such as pic10f200. did you notice the latest psu making video from dave? he had a big rant on AVR chip (same with pic), and you know who he is? he is a "professional PCB" designer!

author: DaveHardy
author_rank: Regular Contributor
timestamp: February 18, 2012, 03:21:26 am
content: Quote from: Mechatrommer on February 18, 2012, 02:55:59 amI know...I'm doing my best to get it together. I bought a PICkit 3 a few days ago, 2 16F1827, and a 16F84A. Somebody told me to learn the 16F1827 after the 16F84A but I don't have a crystal for the 16F84A. I didn't know that the 16F84A didn't have an internal oscillator or I would have bought a crystal for it with my last order. Basically I'm stuck with these and some AVR stuff that I bought until my next parts order.So you suggest that I buy a PIC10F200 and start over? My LED still won't light, but I think you already know that. What is the best way to learn this stuff? There are absolutely tons of PIC ASM tutorials floating around on the internet and I thought that it would be the best place to start. I have an Arduino Uno that doesn't work and makes a strange de de de whenever I power it with USB since I updated to 1.0. I also have an AVR dragon that I need to get male headers for and ATMEGA328P. I have money and am willing to spend it on a nice kit that isn't overpriced.Ultimately, I want to learn AVR GCC C programming, but I thought that learning about assembly and architecture with well documented PIC ASM would be a better place to start.Thanks for the replies everybody!!-Dave

author: 8086
author_rank: Super Contributor
timestamp: February 18, 2012, 03:32:58 am
content: I see no real reason to give up on this and use a different PIC. You're getting there, just need a little bit more guidance as you go along, and you will be flashing that LED in no time.

author: Noize
author_rank: Regular Contributor
timestamp: February 18, 2012, 04:14:26 am
content: You could make a RC oscillator to clock the picJust google "RC oscillator pic"Just a resistor and a capacitor and you have a clock source(just make sure you configure the pic for rc oscillator)!Use resistor between 3k and 100k and capacitor above 20pF. Different values will give you different clock speeds.Bookmark this page and refer to it. I did when I got started. Funnily enough with a 16F84 as wellhttp://www.users.globalnet.co.uk/~jchap/tvpropi2.htmLook for a free PDF copy of "Pic in practice. A project based approach" on the net. The book starts off with blinking light and goes on from there. The projects use the 16F84A chip.Quote from: DaveHardy on February 18, 2012, 03:21:26 am

author: DaveHardy
author_rank: Regular Contributor
timestamp: February 18, 2012, 04:47:23 am
content: Quote from: Noize on February 18, 2012, 04:14:26 amIt probably won't work and isn't as easy as you might think. Furthermore, they are usually very noisy, inaccurate, and hard to stabilize in the MHz. I seriously doubt PICkit 3 would play with it.

author: Noize
author_rank: Regular Contributor
timestamp: February 18, 2012, 05:07:36 am
content: Its stable enough for blinking some leds. And no problems with Picket 3.Ps it isn't easy at the start. But keep trying, and try and get that PDF i recommended in my post above.

author: DaveHardy
author_rank: Regular Contributor
timestamp: April 13, 2012, 12:46:24 am
content: Thanks for the help everybody. Noize, I did get pretty far with your RC suggestion. It works pretty good for simple applications.

author: Kremmen
author_rank: Super Contributor
timestamp: April 22, 2012, 10:51:12 am
content: Without wanting to criticize your learning plan it looks to me that you will gain but little by learning PIC assembly in preparation for AVR-GCC. Surely both are useful but application design and implementation in C (++) is a different world. Skill in assy is useful but not really necessary to get a job done.Sent from my Lumia 800 using Board Express

author: vk6zgo
author_rank: Super Contributor
timestamp: April 23, 2012, 04:09:51 am
content: Throw the PIC away & use a 555!!

author: steve_w
author_rank: Regular Contributor
timestamp: April 23, 2012, 12:25:22 pm
content: Is this the easy alternative to an AVR?

author: westfw
author_rank: Super Contributor
timestamp: April 26, 2012, 11:20:47 pm
content: It is useful to go through all of the error and warning messages to make sure you understand them. When all the messages are gone and your program still doesn't work, the real debugging starts.>> Found directive in column 1.Assemblers date back to the dark ages of text parsing. To make things easier, "labels" were defined to start in column 0. Anything that didn't start in column 0 wasn't a label, so it would be directive or opcode. Assemblers are smarter now, so this is just a warning, but they'll go away if you put a space or so in front of the directives.>> Symbol not previously defined (b’00000000’)This has already been discussed; it doesn't like the format you're using to indicate a binary number.I think it wants a capital B instead of a lowercase b. Note that symbols are also case sensitive by default.>> Use of this instruction is not recommendedThe newer PICs do not use the "TRIS" instruction for setting port pin directions. Instead, they have separate IO registers "TRISx", whose contents control the pins. This goes back to your statement about mysterious initialization:BANKSEL TRISA ; Select the register back containing TRISAMOVLW B'0' ;Set porta as all outputsMOVWF TRISAis equivalent to yourmovlw B'00000000'tris portaThe "banksel" macro sets the register bank to the appropriate value. In the old days, entry-level PICs only had one bank of memory, so old coding examples may not do this. Now that PICs have more memory, you have to pay more attention to the (really annoying!) register banking. SOME of the newer PICs are enhanced in ways that make this easier, but it's still something that you'll need to learn about if you're going to be coding PICs in assembler.