Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Need help with macro (very basic)

Status
Not open for further replies.

c36041254

Member
Hi there,
I just made a code for LED blinking with brightness control but, it is too long, see for your self.......
Code:
List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL-302
;**********************************************************************
	cblock 0x20
		temp
		d1
		d2
		don
		doff
		dis
	endc
;***********************************************************************
	org 0x00
	goto main
;***********************************************************************
main:
portset:
	bsf STATUS,RP0
	clrf TRISA 
	clrf TRISB
	clrf TRISC
	bcf STATUS,RP0
start:					; THE BASIC FUNDA OF THIS PROGRAM IS THAT LOOP
	call bright			;"bright" SHOULD KEEP RUNNING DURING "delay" OR IN OTHER
	movlw b'00000000'	;WORDS EQUAL TO THAT TIME PERIOD WHICH IS USED IN HERE.
	movwf PORTC
	call delay
	goto start
delay:
	decfsz d1
	goto delay
	decfsz d2
	goto delay
	return
bright:
	incf dis,f
	movlw b'00000001'
	movwf PORTC
	call delayons
	movlw b'00000000'
	movwf PORTC
	call delayoffs
	btfss dis,0x00
	goto bright
	btfss dis,0x01
	goto bright
	btfss dis,0x02
	goto bright
	btfss dis,0x03
	goto bright
	btfss dis,0x04
	goto bright
	btfss dis,0x05
	goto bright	
	btfss dis,0x06
	goto bright
	btfss dis,0x07
	goto bright
	clrf dis
	return
delayons:
	movlw d'30'
	movwf don
delayon:
	decfsz don
	goto delayon
	return
delayoffs:
	movlw d'226'
	movwf doff
delayoff:
	decfsz doff
	goto delayoff
	return
	end
Now, I want to make a macro for this part.....
Code:
bright:
	incf dis,f
	movlw b'00000001'
	movwf PORTC
	call delayons
	movlw b'00000000'
	movwf PORTC
	call delayoffs
	btfss dis,0x00
	goto bright
	btfss dis,0x01
	goto bright
	btfss dis,0x02
	goto bright
	btfss dis,0x03
	goto bright
	btfss dis,0x04
	goto bright
	btfss dis,0x05
	goto bright	
	btfss dis,0x06
	goto bright
	btfss dis,0x07
	goto bright
	clrf dis
	return
I know that there is macro tool available in MPLAB IDE but, I don't know how to generate macro, I see the record and "keyboard follow " and "mouse follow" option but I am confused about it, do I need to move cursor with arrow keys along the code while recording ?, will just writing above program generate the macro for it ? I will be very thankful if someone actually show me form the scratch that how to generate macro, if possible please explain me with the above code. From the very start (what to include and not, you know from the first line called "list") I tried goggling but there are very difficult and huge examples and libraries, Please help me.
 
I don't understand your question about a macro but you could shorten your code to,
Code:
bright
	incf	dis,f
	movlw	b'00000001'
	movwf	PORTC
	call	delayons
	movlw	b'00000000'
	movwf	PORTC
	call	delayoffs
	incfsz	dis,w		;was dis 0xff
	goto	bright		;no, so loop
	return			;yes, so return

Mike.
 
Thanks Mike,
All I know is that a macro is a small program warped in a form that it becomes the part of the basic files such as 16f690.inc and then all you have to do is that just call the macro an the respective program will automatically run at the appropriate stage, but this is all I know,
Say, I want to built a macro for the reformed code you gave me then how can I do that, especially with MPLAB.
HTML:
I know that there is macro tool available in MPLAB IDE but, I don't know how to generate macro, [B]I see the record and "keyboard follow " and "mouse follow" option but I am confused about it, do I need to move cursor with arrow keys along the code while recording ?[/B], will just writing above program generate the macro for it ?
.............see that shows what I know about macro, I'm sorry if you don't get this even this time, but just let me make it clear that I WANT TO MAKE MACRO FOR THAT (now yours) CODE, AND DON'T KNOW WHERE TO START. (don't advice goggling it's very confusing, I tried that)
 
A macro is just a lump of text that is given a short name. It will not make your code any shorter, it will just save on the amount of typing.

Say you wanted a short cut to change banks, you could define a macro as,
Code:
Bank	macro	var
	if(var==0)
	  bcf STATUS,RP0
	  bcf STATUS,RP1
	endif
	if(var==1)
	  bsf STATUS,RP0
	  bcf STATUS,RP1
	endif
;add code for bank 2 and 3
	endm

And you could then type "Bank 0" or "Bank 1".

Mike.
 
O.K. fine that macro will select BANK 0 if BANK 1 is already selected and vice verse , now the question is that HOW YOU SAVE THAT IN A FORM OF MACRO because you probably don't want to write the whole thing again in every program you will just CALL the <name> of the macro right ?, but for that the code you gave must be saved in such a form normal .ASM won't work right ? HOW DO YOU DO THAT?
 
You save it as a .asm file and include it in your project. So, save it as bank.asm and then at the top of your file you would put include "bank.asm".

You dont "call" the name of the macro, you just type the name.

Mike.
 
macro not working

Hi Mike,
I tried to make the macro for bank as you said
Code:
List p=16f690
	#include <p16f690.inc>
		__CONFIG   _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL - 302

Bankx	macro	var
	if(var==0)
	  bcf STATUS,RP0
	  bcf STATUS,RP1
	endif
	if(var==1)
	  bsf STATUS,RP0
	  bcf STATUS,RP1
	endif
	endm
	end
has saved it as normal .ASM and then trying to use it in this blink code.............
Code:
List p=16f690
	#include <p16f690.inc>
	[COLOR="Red"]#include <CM-BANK.inc>[/COLOR]
	__CONFIG   _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL - 302
;**********************CREATE REGISTER STORAGE BLOCKS******************
	cblock 0x20
		temp
		d1
		d2
		d3
		multiply
		multiply2
	endc
;************************SETUP START POINT********************************
	org 0x00
	goto Main
;************************SETUP THE CONSTANTS*****************************
	Main:
	Bankx 1	;switch to BANK 1
	movlw b'00000000'	
	movwf TRISC			;set port c all output
	movwf TRISA			;set port a all output, for precaution
	movlw TRISB			;set port a all output, for precaution
	Bankx 0	;switch back to BANK 0
;**************************MAIN LOOP*********************************
	Loop:
	movlw b'00000001'	
	movwf PORTC			
	call Multi		;2 * start from Multi  subroutine	
	movlw b'00000000'		
	movwf PORTC			
	call Multi
	goto Loop	
	Multi:			; 5 x Delay (1 sec)
	movlw d'5'
	movwf multiply
	Loop1:
	call Delay		;2	* Delay ( 2 * 124,000)
	decfsz multiply	;1
	goto Loop1		;2	*
	retlw 00h		;1	* the 2 of call Multi =  gives TOTAL DELAY OF 992000uS = 1 SECOND
	Delay:
	movlw d'62'	; 62uS	
	movwf d1		
	movlw d'50'	; 50uS	
	movwf d2
	movlw d'5'	; 5uS
	movwf d3
	Loop2:
	decfsz d1	;62	*
	goto Loop2	;2	*
	decfsz d2	;50	*
	goto Loop2	;2	*
	decfsz d3	;5	*
	goto Loop2	;2			=	124,000	and *
	retlw 00h	;1			
	end
the error comes that #include<CM-BANK.inc> not found, where I made mistake ?

EDIT :HAVE SAVED THE MACRO PROJECT FILE AND.ASM FILE AS "CM-BANK"
 
Last edited:
Try changing #include <CM-BANK.inc> to include "CM-BANK.inc" so it looks in the project directory.

Mike.
 
I just noticed your macro has include etc. in it. Delete the first 4 lines of the macro file so that the first non blank line is the macro code. Make sure you save the file in the same folder as the project file and it should work.

Mike.
 
Hve you saved the macro file as "CM-BANK.ASM" or as "CM-BANK.INC" ? As your code is looking for a file with the .inc ending and you mention just saving it as a normal asm which I assume uses just the normal .asm file ending attribute. If so it wont of course find it.
 
Hi there,
I tired removing those include etc. from macro code but then the hex file couldn't be generated (shows that no configuration set, though the build was successful ), so I again add those include and etc.(first 4 line of macro code) then the massage that config. is not set had gone but the hex format shows all 3fff (blank):confused::confused:
 
The macro file should not be in your project as a source file but as a other type file.

Here is one of my projects showing where the additional files should be,

Mike.
 

Attachments

  • Project.png
    Project.png
    15.4 KB · Views: 161
Hi Mike,
It still not working, check the code of macro file saved as .ASM........
Code:
Bankx macro	var
	if(var==0)
	  bcf STATUS,RP0
	  bcf STATUS,RP1
	endif
	if(var==1)
	  bsf STATUS,RP0
	  bcf STATUS,RP1
	endif
	endm
	end
And this is the program where macro is used ........
Code:
List p=16f690
	#include <p16f690.inc>
	#include CM-BANK
	__CONFIG   _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL - 302
;**********************CREATE REGISTER STORAGE BLOCKS******************
	cblock 0x20
		temp
		d1
		d2
		d3
		multiply
		multiply2
	endc
;************************SETUP START POINT********************************
	org 0x00
	goto Main
;************************SETUP THE CONSTANTS*****************************
	Main:
	Bankx 1	;switch to BANK 1
	movlw b'00000000'	
	movwf TRISC			;set port c all output
	movwf TRISA			;set port a all output, for precaution
	movlw TRISB			;set port a all output, for precaution
	Bankx 0	;switch back to BANK 0
;**************************MAIN LOOP*********************************
	Loop:
	movlw b'00000001'	
	movwf PORTC			
	call Multi		;2 * start from Multi  subroutine	
	movlw b'00000000'		
	movwf PORTC			
	call Multi
	goto Loop	
	Multi:			; 5 x Delay (1 sec)
	movlw d'5'
	movwf multiply
	Loop1:
	call Delay		;2	* Delay ( 2 * 124,000)
	decfsz multiply	;1
	goto Loop1		;2	*
	retlw 00h		;1	* the 2 of call Multi =  gives TOTAL DELAY OF 992000uS = 1 SECOND
	Delay:
	movlw d'62'	; 62uS	
	movwf d1		
	movlw d'50'	; 50uS	
	movwf d2
	movlw d'5'	; 5uS
	movwf d3
	Loop2:
	decfsz d1	;62	*
	goto Loop2	;2	*
	decfsz d2	;50	*
	goto Loop2	;2	*
	decfsz d3	;5	*
	goto Loop2	;2			=	124,000	and *
	retlw 00h	;1			
	end
I ADDED CM-BANK to other files both macro and the program gets build successfully but when the hex file loaded in the PICKit 2 programmer it show int it's window.............

Warning: No configuration in the hex file
and reads all 3fff:confused:
 
Last edited:
You somehow are not assembling your main file. Get a project that works and produces a hex file and then include the macro file.

Mike.
 
Hmm....., though I have had it tried already with a working project still I'll try it with another working project, b.t.w. I just notice that I'm making the macro in 16f690 template, is that a problem? I'm trying writing in a New blank file and not in any template, will let you know, Thanks for your time.
 
Hi Mike my next post will take some long time, I think first I better understand everything about macro than every time bothering you or some other member, so far I have not found good material on Macro (**broken link removed** )but now I have found this so my this study will take time, hope you will be here to help me (please do, as you have always because one can not understand everything on it's own), Thanks for your time and effort.
 
Last edited:
Hi Mike,
Though I have not succeeded yet in writing macro in a different file and then adding it to the "other" folder of the main program but, I have managed to write a macro in the same file and it's working can you just verify that this is actually working as a macro and not as a normal subroutine, though I know subroutine needs a "goto" or "call" functions but still I want to make it sure that this is a MACRO.........
Code:
List p=16f690
	#include<p16f690.inc>
	__CONFIG _MCLRE_ON & _CP_OFF & _WDT_OFF & _INTRC_OSC_NOCLKOUT
	ERRORLEVEL-302
;***********************************************************************
	cblock 0x20
		temp
		d1
		d2
		dis
	endc
;***********************************macroset**************************
	[COLOR="Red"]chris 	macro 
			bsf STATUS,RP0		;all output
			clrf TRISA
			clrf TRISB
			clrf TRISC
			bcf STATUS,RP0
			endm[/COLOR]
;************************************************************************
	org 0x0
	goto main
;************************************************************************
main:
portset:
	[COLOR="Red"]chris[/COLOR]
PWMset:
	movlw b'00000001'
	movwf PORTC
	call bright1		;call delay for that much time LED to be bright
	movlw b'00000000'
	movwf PORTC
	call bright2		; call delay for an LED to remain off
	goto PWMset
bright1:
	movlw d'200'		; select on time delay
	movwf d1
delayon:
	decfsz d1
	goto delayon
	return
bright2:
	movlw d'56'			;select off time delay
	movwf d2
delayoff:
	decfsz d2
	goto delayoff
	return
	end
 
Chris,

Open the .LST file for the program and you should see the instructions from the macro in the listing.

Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top