Help me with Assembly Language for PIC18F4523

Status
Not open for further replies.
Joined
Apr 8, 2012
Messages
201
Helped
3
Reputation
6
Reaction score
2
Trophy points
1,298
Activity points
0
Hello!

I need a asm program for pic18f4523 with blinks led's connected to port b with a delay of 500ms. I am using mpasm.
 

Hello!

I need a asm program for pic18f4523 with blinks led's connected to port b with a delay of 500ms. I am using mpasm.

Hi,

If you look in MPlab, MPASMSuite, you will see a TEMPLATES folder which contain a starter file for every Pic chip.

Below is the one for the 4523, I have modified it to run from the Internal Oscillator at 4mhz.
You just need to start coding from at the Main section.

Plenty of tutorials around to show you how to flash a led, most are in 16F code but its virtually the same in 18F code, just do not use any $+or - instructions from the 16F examples, use Goto's instead.

Code:
;******************************************************************************
;   This file is a basic template for assembly code for a PIC18F4523. Copy    *
;   this file into your project directory and modify or add to it as needed.  *
;                                                                             *
;   Refer to the MPASM User's Guide for additional information on the         *
;   features of the assembler.                                                *
;                                                                             *
;   Refer to the PIC18F2423/2523/4423/4523 Data Sheet for additional          *
;   information on the architecture and instruction set.                      *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:                                                                *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             * 
;******************************************************************************
;                                                                             *
;    Files Required: P18F4523.INC                                             *
;                                                                             *
;******************************************************************************

	LIST P=18F4523		;directive to define processor
	#include <P18F4523.INC>	;processor specific variable definitions

;******************************************************************************
;Configuration bits
;Microchip has changed the format for defining the configuration bits, please 
;see the .inc file for futher details on notation.  Below are a few examples.



;   Oscillator Selection:
 ;   CONFIG	OSC = LP             ;LP

 CONFIG OSC=INTIO67, PWRT=ON, BOREN=OFF, WDT=OFF, MCLRE=ON, LPT1OSC=OFF, PBADEN=OFF
 CONFIG LVP=OFF, XINST=OFF, DEBUG=OFF

;******************************************************************************
;Variable definitions
; These variables are only needed if low priority interrupts are used. 
; More variables may be needed to store other special function registers used
; in the interrupt routines.

		CBLOCK	0x080
		WREG_TEMP	;variable used for context saving 
		STATUS_TEMP	;variable used for context saving
		BSR_TEMP	;variable used for context saving
		ENDC

		CBLOCK	0x000
		EXAMPLE		;example of a variable in access RAM
		ENDC

;******************************************************************************
;EEPROM data
; Data to be programmed into the Data EEPROM is defined here

		ORG	0xf00000

		DE	"Test Data",0,1,2,3,4,5

;******************************************************************************
;Reset vector
; This code will start executing when a reset occurs.

		ORG	0x0000

		goto	Main		;go to start of main code

;******************************************************************************
;High priority interrupt vector
; This code will start executing when a high priority interrupt occurs or
; when any interrupt occurs if interrupt priorities are not enabled.

		ORG	0x0008

		bra	HighInt		;go to high priority interrupt routine

;******************************************************************************
;Low priority interrupt vector and routine
; This code will start executing when a low priority interrupt occurs.
; This code can be removed if low priority interrupts are not used.

		ORG	0x0018

		movff	STATUS,STATUS_TEMP	;save STATUS register
		movff	WREG,WREG_TEMP		;save working register
		movff	BSR,BSR_TEMP			;save BSR register

;	*** low priority interrupt code goes here ***


		movff	BSR_TEMP,BSR			;restore BSR register
		movff	WREG_TEMP,WREG		;restore working register
		movff	STATUS_TEMP,STATUS	;restore STATUS register
		retfie

;******************************************************************************
;High priority interrupt routine
; The high priority interrupt code is placed here to avoid conflicting with
; the low priority interrupt vector.

HighInt:

;	*** high priority interrupt code goes here ***


		retfie	FAST

;******************************************************************************
;Start of main program
; The main program code is placed here.

Main:	movlw	b'01100010'		; set internal osc to 4 mhz
		movwf	OSCCON


;	*** main code goes here ***


;******************************************************************************
;End of program

		END
 

Thanks wp100.

What is the config for using external osc @ 20 MHz?
 

What is the config for using external osc @ 20 MHz?

The Datasheet for the 4523 is microchips shortened version so you need to look at the 4520 datasheet for full details of the oscillator.

Change the OSC=HS for a 20 mhz crystal, remember to remove the OSCON instruction in the Main code

Are you aware the internal oscillator , up to 8 mhz, can be run up to 16 or 32 mhz by using the PLL feature ? - again details in the 4520 datasheet.
 
Last edited:

Can you give me the code for turning on port c for 500ms and then turning off for 500ms in an endless loop.
 

Hi,

Delays depend of the oscillator you are using, what frequency are you going to use, that 20 meg external ?
Think for 99% of typical hobby use, 4 or 8 meg interal oscillator is all thats needed, saves buying extra parts.
I use it on my working projects all the time.

Let me know the frequency then I will do some 18F working code to get you flashing.

As well as those sites mentioned by TpeTar in your other post have a look at this one, its in 16F code but you will soon learn how to convert it to 18F code which is so much better to use once you get into heavier coding.
http://www.winpicprog.co.uk/pic_tutorial.htm
 

Hi,

Here are two flash examples at 4meg internal and 20meg external.

Have cut that template file down to a bare minimum to hopefully make things clearer.
 

Attachments

  • FLASH20.rar
    1.5 KB · Views: 119

I will be using 20 MHz external Osc for my projects. I wanted to know how to configure both internal and external osc. And I'll be using both 16f and 18f mcus.
I just need some asm example codes for lcd hd44780, adc, pwm, eeprom, ans usart.

---------- Post added at 16:45 ---------- Previous post was at 16:39 ----------

I'll be using 20 MHz external osc for my projects. Im just beggining to code in asm. I need some example asm codes for lcd hd44780, adc, keypad, pwm, eeprom, usart, etc... I'll be using both 16f and 18f mcus.

---------- Post added at 17:08 ---------- Previous post was at 16:45 ----------

I'm getting the following messages when I compile the file.

Release build of project `C:\Documents and Settings\System Administrator\Desktop\pic18f4523_led_blink\led blink ext osc.disposable_mcp' started.
Language tool versions: MPASMWIN.exe v5.45, mplink.exe v4.43, mplib.exe v4.43
Fri May 18 17:08:19 2012
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "G:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F452 "led blink ext osc.asm" /l"led blink ext osc.lst" /e"led blink ext osc.err"
Warning[205] C:\DOCUMENTS AND SETTINGS\SYSTEM ADMINISTRATOR\DESKTOP\PIC18F4523_LED_BLINK\LED BLINK EXT OSC.ASM 1 : Found directive in column 1. (LIST)
Warning[215] C:\DOCUMENTS AND SETTINGS\SYSTEM ADMINISTRATOR\DESKTOP\PIC18F4523_LED_BLINK\LED BLINK EXT OSC.ASM 1 : Processor superseded by command line. Verify processor symbol.
Message[301] G:\PROGRAM FILES\MICROCHIP\MPASM SUITE\P18F4523.INC 33 : MESSAGE: (Processor-header file mismatch. Verify selected processor.)
Error[176] C:\DOCUMENTS AND SETTINGS\SYSTEM ADMINISTRATOR\DESKTOP\PIC18F4523_LED_BLINK\LED BLINK EXT OSC.ASM 5 : CONFIG Directive Error: (setting "MCLREError[176] C:\DOCUMENTS AND SETTINGS\SYSTEM ADMINISTRATOR\DESKTOP\PIC18F4523_LED_BLINK\LED BLINK EXT OSC.ASM 6 : CONFIG Directive Error: (setting "XINSTHalting build on first failure as requested.
----------------------------------------------------------------------
Release build of project `C:\Documents and Settings\System Administrator\Desktop\pic18f4523_led_blink\led blink ext osc.disposable_mcp' failed.
Language tool versions: MPASMWIN.exe v5.45, mplink.exe v4.43, mplib.exe v4.43
Fri May 18 17:08:21 2012
----------------------------------------------------------------------
BUILD FAILED
 

Hi,

In MPlab, goto the CONFIGURE menu and SELECT DEVICE pic18f4523.
 

I did as you said, but I still get the below messages.

----------------------------------------------------------------------
Release build of project `C:\Documents and Settings\System Administrator\Desktop\pic18f4523_led_blink\led blink ext osc.disposable_mcp' started.
Language tool versions: MPASMWIN.exe v5.45, mplink.exe v4.43, mplib.exe v4.43
Fri May 18 20:13:14 2012
----------------------------------------------------------------------
Clean: Deleting intermediary and output files.
Clean: Done.
Executing: "G:\Program Files\Microchip\MPASM Suite\MPASMWIN.exe" /q /p18F4523 "led blink ext osc.asm" /l"led blink ext osc.lst" /e"led blink ext osc.err"
Warning[205] C:\DOCUMENTS AND SETTINGS\SYSTEM ADMINISTRATOR\DESKTOP\PIC18F4523_LED_BLINK\LED BLINK EXT OSC.ASM 1 : Found directive in column 1. (LIST)
Error[176] C:\DOCUMENTS AND SETTINGS\SYSTEM ADMINISTRATOR\DESKTOP\PIC18F4523_LED_BLINK\LED BLINK EXT OSC.ASM 6 : CONFIG Directive Error: (missing setting=value pair in parameter list)
Halting build on first failure as requested.
----------------------------------------------------------------------
Release build of project `C:\Documents and Settings\System Administrator\Desktop\pic18f4523_led_blink\led blink ext osc.disposable_mcp' failed.
Language tool versions: MPASMWIN.exe v5.45, mplink.exe v4.43, mplib.exe v4.43
Fri May 18 20:13:15 2012
----------------------------------------------------------------------
BUILD FAILED

---------- Post added at 20:17 ---------- Previous post was at 20:14 ----------



---------- Post added at 20:24 ---------- Previous post was at 20:17 ----------

OK. I got it working. How to make PORTD all pins high and then all pins low?

---------- Post added at 20:28 ---------- Previous post was at 20:24 ----------

Can we move b'11111111' to working register and then move the value in WREG to PORTD? i.e.,

movlw b'11111111'
movwf PORTD
call delay_500ms
movlw 'b00000000'
movwf PORTD

.......................................

Please explain the below code of yours.

bsf PORTD,0 ; TURN ON PORTD,0
bcf PORTD,0 ; TURN OFF PORTD,0



I've attached my project to view.
 

Attachments

  • pic18f4523_led_blink_ext_osc.rar
    24.5 KB · Views: 115
Last edited:

Hi,

The error /message given in the outut report means what it says.

Point to the message on the report and click it, that then opens your code and it goes to the line in error.

In this case its the first line of code, for some reason when you imported the code its put things in column 1.

Normally the only code that starts in column 1 are the Labels *

All you have to do is insert a space before the list and it should build ok.

The screenshot shows how the code looks when I 'open' the unpacked code in Mplab.


*Labels - from Mplab Help
A label is used to represent a line or group of code, or a constant value. It is needed for branching instructions (Example 1-1.)

Labels should start in column 1. They may be followed by a colon ), space, tab or the end of line. Labels must begin with an alpha character or an under bar (_) and may contain alphanumeric characters, the under bar and the question mark.



Also note that though the 20meg code still shows the OSCON setting for 4meg , it is ignored because we have already told it to use the OSC=HS 20meg
 

Attachments

  • ScreenShot001.jpg
    53.7 KB · Views: 113

OK. It is working. Can you please explain the working of the following code clearly

Code:
[syntax = assembly]

;SUBROUTINE

delay_500ms:   					; Delay = 0.5 seconds Clock frequency = 20 MHz
	
	
		movlw	0x16
		movwf	d1
		movlw	0x74
		movwf	d2
		movlw	0x06
		movwf	d3
Delay_0
		decfsz	d1, f
		goto	dly0
		decfsz	d2, f
dly0	goto	dly1
		decfsz	d3, f
dly1	goto	Delay_0

		return

[/syntax]


---------- Post added at 21:03 ---------- Previous post was at 20:59 ----------

Please explain every instruction.
 

Hi,

Glad you have got it working, play around with the code using different pins of portD and sequences so you build up your knowledge of the instructions.
You can then try setting up the other ports and using them as well.

Assume you have downloaded the 4520datasheet, towards the end of that you will find the full Pic18 Assembler Instruction Set - worth printing it out for reference.

The Delay suboutine, it confuses most beginners, me included,
First be aware its a Subroutine that you Call from your Main code, when the Subroutine finishes it Returns to the instruction following the Call.

Second the timings - you are using a 20 mhz crystal, but the frequency at which the Pic18 chips operate is F/4 , so thats a 5 Mhz Clock or Cycle
As you can see from the Instruction Set pages, many Instructions take just 1 Cycle to complete, a few take 2 or more.

With your Software Delay of 500ms it needs to execute 2,500,000 single Cycle instructions to complete its task.
This is achieved by loading three User registers with pre calculated values, they are then, in a Nested loop, reduced by 1 on each pass of the loop until 2.5m cycles have been completed.

How do we arrive at those initial values, well you can work them out for yourself, but most use a online calculator like this one.
Notice that its in 16F format and need a little modifying for the 18F.
**broken link removed**

Again notice I am talking of a Software delay here, as you can see it needs the full use of the processor.
There are Hardware Delays were you can use the Pics inbuilt hardware timers to perform delays, while allowing the processor to continue with other work.
Something to learn about another day.

---------- Post added at 18:36 ---------- Previous post was at 17:39 ----------



Hi,

Only just spotted you post addition.

You can use movlw b'11111111' or 0xFF or .256 to control the while port, but sometime you only want to select one single pin so you use the BSF - refer to the your copy of Instruction Set for details.
The same applies to when reading a Switch Input, you can just test one pin not the whole 8 pins of the Port

If you look at those online tutorials mentioned earlier it shows the many ways you can manipulate the flashing leds, you can also use PWM to dim them.

In ISIS Simulations you do not need to show the crystal in the circuit.
 

Where can I find the address of PORTS, i.e.m address of PORTA, PORTB, etc....
 

Why is the following code not working?

Code:
[syntax = assembly]

;LIST P=18F4523	
        #include <P18F4523.INC>	

   	    ;Configuration bits    
		;Oscillator selected = EXT 20 MHZ
        CONFIG OSC=HS, PWRT=ON, WDT=OFF, MCLRE=ON, LPT1OSC=OFF, PBADEN=OFF
        CONFIG LVP=OFF, XINST=OFF, DEBUG=OFF, BOREN=OFF

LEDPORT	Equ	PORTA			;set constant LEDPORT = 'PORTA'
SWPORT	Equ	PORTB			;set constant SWPORT = 'PORTA'
LEDTRIS	Equ	TRISA			;set constant for TRIS register
SWTRIS  EQU TRISB
SW1	Equ	0					;set constants for the switches
SW2	Equ	1
SW3	Equ	2
SW4	Equ	3
LED1	Equ	0				;and for the LED's
LED2	Equ	1
LED3	Equ	2
LED4	Equ	3
;end of defines
        ;Variable definitions
       
        CBLOCK	0x000
        			; variable for the Delay routine.
        ENDC

       	ORG	0x0000			;Reset vector This code will start executing when a reset occurs.

       	;goto	Main		;go to start of main code
						



Main	
		
		movlw	0x07
		movwf	CMCON			;turn comparators off (make it like a 16F84)
   	
   		movlw 	b'00000000'		;set PortA 4 inputs, 4 outputs
   		movwf 	LEDTRIS
		movlw 	b'00001111'		;set PortA 4 inputs, 4 outputs
   		movwf 	SWTRIS
		clrf	LEDPORT			;set all outputs low
		clrf	SWPORT
Scan
		btfss	SWPORT,	SW1
		call	Switch1
		btfss	SWPORT,	SW2
		call	Switch2
		btfss	SWPORT,	SW3
		call	Switch3
		btfss	SWPORT,	SW4
		call	Switch4
		goto 	Scan
		goto	Main

Switch1	clrf	LEDPORT			;turn all LED's off
		bsf		LEDPORT, LED1		;turn LED1 on
		retlw	0x00

Switch2	clrf	LEDPORT			;turn all LED's off
		bsf		LEDPORT, LED2		;turn LED2 on
		retlw	0x00

Switch3	clrf	LEDPORT			;turn all LED's off
		bsf		LEDPORT, LED3		;turn LED3 on
		retlw	0x00

Switch4	clrf	LEDPORT			;turn all LED's off
		bsf		LEDPORT, LED4		;turn LED4 on
		retlw	0x00

    
        END		         ;End of program

[/syntax]
 
Last edited:

simulate with the attached file.
 

Attachments

  • PIC18F4523_EXT_OSC_LED_SW1.rar
    22.6 KB · Views: 112

simulate with the attached file.

Hi,

You seem to have worked out the Ports ok, TRIS to Set the Direction, Inputs or Output; PORT to read or write to.

The code looks ok apart from one point, at Power On all the Input Analogue ports are enabled, so for Digital I/O use you must use this along with turning off the comparators CMCON
movlw 0x07 ; turn off all analogue pins
movwf ADCON1
 
Check the attachment. The led is flickering.
 

Attachments

  • pic18f4523_led_sw.rar
    23.6 KB · Views: 108

I tried both

movlw 0x07 ; turn off all analogue pins
movwf ADCON1

and

movlw 0x0F ;all pins digital
movwf ADCON1

its still flickering
 

Hi,

Yes you are right 0F to turn off all analogue function, 07 is just for PortA only.

Two things with you code now I can see the schematic.

Your switch input is normally LOW , you are testing btfSS, which means if the Switch is HIGH then SKIP the next instruction, which is the reverse of what you want to do, so use the btfSC.

When you turn on a LED , what happens, within a few milli seconds you are back testing the keys again.

You need to do a delays of at least 250ms after turning the led on or off so you can see it happening.

How are you using ISIS ? - are you creating your circuit in ISIS and importing the .hex file or are you using MPLAB, Debugger, Proteus VSM ?
Using VSM you can step though your code and Watch what every instruction does
For delays you can use Breakpoints in your code so you Run over them.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…