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.

PIC programming question

Status
Not open for further replies.

ljcox

Well-Known Member
I downloaded a PIC programme from the internet for a PIC16F88.

PORTA 1, 2 & 3 are used as analogue inputs.

But there is an instruction in the programme that I don’t understand.

movlw (1<<ana1)|(1<<ana2)|(1<<ana3)

I’m familiar with the normal movlw instruction, eg. movlw 0x1A, but not with the one above.

In the definitions, the parameters are defined as:-

ana3 equ 3 ; analog input AN3

ana2 equ 2 ; analog input AN2

ana1 equ 1 ; analog input AN1

Any assistance will be appreciated.
 
The "<<" directive shifts the number left by the number to the right.

So,
1<<3 = 8
1<<6 = 64

and so the movlw instruction above if the same as movlw b'00001110'.

Mike.
 
So it is moving a 1 right so it would put
Code:
 b'00001110'
but why the pipe | is it for stacking
Code:
movlw (1<<ana1)|(1<<ana2)|(1<<ana3)
which is the same as this
Code:
movlw b'00001110
And this set a 0
Code:
movlw b'11111111'-(1<<ana1)-(1<<ana2)-(1<<ana3)
which is
Code:
movlw b'11110001
 
The | is inclusive or and so,

b'00000010' or b'00000100' or b'00001000' = b'00001110'

Mike.
 
Thanks for the prompt responses. Much appreciated.

I understand. eg. 1<<n means shift left n places.

| means inclusive OR.

It seems a complicated way to do something simple, but I guess he had his reasons.
 
Arithmetic operators may be used with directives and their variables.
These operators cannot be used with program variables. They are for use with directives only.

From Mplab Assembler
 
The reason it is done this way is so that if sometime in the future the design was changed and instead of A2, A4 was used then it is a simple matter to change,

ana2 equ 4 ; analog input AN2

rather than trying to find all occurrences in the code.

I use to use this method to setup SFRs. EG,
Code:
	movlw	(1<<GIE|1<<PEIE|0<<TMR0IE|0<<INTE|0<<RBIE|0<<TMR0IF|0<<INTF|0<<RBIF)
	movwf	INTCON;		enable Peripheral interrupts
but it confused people and so I stopped using it.:rolleyes:

Mike.
 
The reason it is done this way is so that if sometime in the future the design was changed and instead of A2, A4 was used then it is a simple matter to change,

ana2 equ 4 ; analog input AN2

rather than trying to find all occurrences in the code.

I use to use this method to setup SFRs. EG,
Code:
	movlw	(1<<GIE|1<<PEIE|0<<TMR0IE|0<<INTE|0<<RBIE|0<<TMR0IF|0<<INTF|0<<RBIF)
	movwf	INTCON;		enable Peripheral interrupts
but it confused people and so I stopped using it.:rolleyes:

Mike.

Thanks Mike.
 
I like It. It works for me and makes a lot of sense to me.
_______________________________________
But I have never been confused:confused::D
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top