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
 
So it is moving a 1 right so it would put
Code:
 b'00001110'
but why the pipe | is it for stacking
The pipe means OR. I've seen people do this in C too. What it usually means is that they don't know how to convert to binary. This gets the bits in there in a roundabout way.
 
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.
 
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.

Isn't it just standard C? - and I don't even do C.
 
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.

Mike.
 

Thanks Mike.
 
I like It. It works for me and makes a lot of sense to me.
_______________________________________
But I have never been confused
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…