Author Topic: Having trouble writing blinky program with PIC assembly language  (Read 10284 times)

0 Members and 1 Guest are viewing this topic.

Offline DaveHardyTopic starter

  • Regular Contributor
  • *
  • Posts: 103
Having trouble writing blinky program with PIC assembly language
« on: February 17, 2012, 10:31:08 pm »
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 PORTA
BANKSEL LATA ;Data Latch
CLRF LATA ;
BANKSEL ANSELA ;
CLRF ANSELA ;digital I/O
BANKSEL TRISA ;
MOVLW B'00111000' ;Set RA<5:3> as inputs
MOVWF 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   05
      org 7FFFh
      goto Start
      org 0
;============
; Subroutines:
Init    clrf porta ; resets Port A
      movlw b’00000000’ ; RA0: LED, RA1-7: not connected
      tris porta
      retlw 0
;============
; Program Start:
Start    call Init ; sets up inputs and outputs
Main    bsf  porta, 0 ; turns on LED
      goto Main ; loops back to Main
END


----------------------------------------------------------------------
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.41
Preprocessor 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=1
Warning[205] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 11 : Found directive in column 1. (list)
Warning[205] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 14 : Found directive in column 1. (__CONFIG)
Warning[205] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 15 : Found directive in column 1. (__CONFIG)
Error[113]   C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 27 : Symbol not previously defined (b’00000000’)
Warning[224] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 28 : Use of this instruction is not recommended.
Warning[205] 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.41
Preprocessor symbol `__DEBUG' is defined.
Fri Feb 17 15:12:20 2012
----------------------------------------------------------------------
BUILD FAILED

-Dave


« Last Edit: February 17, 2012, 10:32:42 pm by DaveHardy »
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1084
  • Country: gb
    • Circuitology - Electronics Assembly
Re: Having trouble writing blinky program with PIC assembly language
« Reply #1 on: February 17, 2012, 10:37:45 pm »
Change the b'00000000' to 0x00 or simply 00

If 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.
« Last Edit: February 17, 2012, 10:43:40 pm by 8086 »
 

Offline cybergibbons

  • Frequent Contributor
  • **
  • Posts: 400
Re: Having trouble writing blinky program with PIC assembly language
« Reply #2 on: February 17, 2012, 10:46:26 pm »
It hurts my head that someone with the nick 8086 is instructing someone else in PIC ASM.

 

Offline DaveHardyTopic starter

  • Regular Contributor
  • *
  • Posts: 103
Re: Having trouble writing blinky program with PIC assembly language
« Reply #3 on: February 17, 2012, 11:46:07 pm »
Change the b'00000000' to 0x00 or simply 00

If 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.

Man, 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 have
labelled start, so we make sure the instruction at 1FFh is goto start. In the
template, org is used to place instruction goto start at 1FFh, making it the first
to be executed. However, subsequent instructions must start counting from 0, so
the following command is org 0. Writing the program memory address by the
instructions shows how it works:
         org 1FF
01FF goto start
         org 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 will
begin 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.41
Preprocessor 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=1
Warning[205] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 11 : Found directive in column 1. (list)
Warning[205] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 14 : Found directive in column 1. (__CONFIG)
Warning[205] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 15 : Found directive in column 1. (__CONFIG)
Warning[224] C:\USERS\ALEXANDER ROBOTNICK\DESKTOP\TEST1.ASM 28 : Use of this instruction is not recommended.
Warning[205] 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" /W
MPLINK 4.41, Linker
Device Database Version 1.5
Copyright (c) 1998-2011 Microchip Technology Inc.
Error - section '.org_0' can not fit the absolute section. Section '.org_0' start=0x00007fff, length=0x00000002
Errors    : 1

Link 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.41
Preprocessor symbol `__DEBUG' is defined.
Fri Feb 17 17:07:42 2012
----------------------------------------------------------------------
BUILD FAILED
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1084
  • Country: gb
    • Circuitology - Electronics Assembly
Re: Having trouble writing blinky program with PIC assembly language
« Reply #4 on: February 18, 2012, 12:15:30 am »
Man, 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. 

The 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: [Select]
      org 7FFFh
      goto Start
      org 0

You are asking for it to put the first instruction in a non existent location.

The code should read:

Code: [Select]
      org 0x00
      goto Start

And 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.
 

Online Mechatrommer

  • Super Contributor
  • ***
  • Posts: 11634
  • Country: my
  • reassessing directives...
Re: Having trouble writing blinky program with PIC assembly language
« Reply #5 on: February 18, 2012, 02:55:59 am »
Quote
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
thats 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!
Nature: Evolution and the Illusion of Randomness (Stephen L. Talbott): Its now indisputable that... organisms “expertise” contextualizes its genome, and its nonsense to say that these powers are under the control of the genome being contextualized - Barbara McClintock
 

Offline DaveHardyTopic starter

  • Regular Contributor
  • *
  • Posts: 103
Re: Having trouble writing blinky program with PIC assembly language
« Reply #6 on: February 18, 2012, 03:21:26 am »
Quote
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
thats 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!

I 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
 

Offline 8086

  • Super Contributor
  • ***
  • Posts: 1084
  • Country: gb
    • Circuitology - Electronics Assembly
Re: Having trouble writing blinky program with PIC assembly language
« Reply #7 on: February 18, 2012, 03:32:58 am »
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.  :D
 

Offline Noize

  • Regular Contributor
  • *
  • Posts: 183
  • Country: gb
Re: Having trouble writing blinky program with PIC assembly language
« Reply #8 on: February 18, 2012, 04:14:26 am »
You could make a RC oscillator to clock the pic  ;)
Just 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 well  :)
http://www.users.globalnet.co.uk/~jchap/tvpropi2.htm

Look 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
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
thats 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!

I 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
« Last Edit: February 18, 2012, 05:00:45 am by Noize »
 

Offline DaveHardyTopic starter

  • Regular Contributor
  • *
  • Posts: 103
Re: Having trouble writing blinky program with PIC assembly language
« Reply #9 on: February 18, 2012, 04:47:23 am »
You could make a RC oscillator to clock the pic  ;)
Just 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)!


Quote
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
thats 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!

I 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

It 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. 
 

Offline Noize

  • Regular Contributor
  • *
  • Posts: 183
  • Country: gb
Re: Having trouble writing blinky program with PIC assembly language
« Reply #10 on: February 18, 2012, 05:07:36 am »
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.
« Last Edit: February 18, 2012, 05:17:35 am by Noize »
 

Offline DaveHardyTopic starter

  • Regular Contributor
  • *
  • Posts: 103
Re: Having trouble writing blinky program with PIC assembly language
« Reply #11 on: April 13, 2012, 12:46:24 am »
Thanks for the help everybody.  Noize, I did get pretty far with your RC suggestion.  It works pretty good for simple applications.
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1289
  • Country: fi
RE: Having trouble writing blinky program with PIC assembly language
« Reply #12 on: April 22, 2012, 10:51:12 am »
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
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline vk6zgo

  • Super Contributor
  • ***
  • Posts: 7588
  • Country: au
Re: Having trouble writing blinky program with PIC assembly language
« Reply #13 on: April 23, 2012, 04:09:51 am »
Throw the PIC away & use a 555!! ;D ;D ;D ;D
 

Offline steve_w

  • Regular Contributor
  • *
  • Posts: 190
  • Country: au
Re: Having trouble writing blinky program with PIC assembly language
« Reply #14 on: April 23, 2012, 12:25:22 pm »
Is this the easy alternative to an AVR?

So long and thanks for all the fish
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4199
  • Country: us
Re: Having trouble writing blinky program with PIC assembly language
« Reply #15 on: April 26, 2012, 11:20:47 pm »
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 recommended
The 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 TRISA
   MOVLW B'0' ;Set porta as all outputs
   MOVWF TRISA
is equivalent to your
   movlw B'00000000'
   tris porta

The "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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf