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.

can any one explain this code with example. PIC12F1822

Status
Not open for further replies.

poojapatel2210

New Member
WTemp equ 0x33
StatusTemp equ 0x32
IntStat equ 0x34

movwf WTemp ;save W register (what is the content of w to consider here or content of Wtemp which is tempory register)
swapf STATUS,w (Is this the status register as in data sheet then what is its initial value as soon as i power on controller )
movwf StatusTemp (again i dont know the content of statustemp which is temporary register )

movf INTCON,w ;save itn stattus (Here again i want to know the what must be the initial value in INTCON register to movf )
movwf IntStat (what is the content of w to consider here or content of IntStat which is tempory register)

swapf StatusTemp,w (Can u please explain all this with numbers, i know what that command do i want to check i am not going wrong to understand this code)
movwf STATUS
swapf WTemp,f
swapf WTemp,w ;restore W

clrf INTCON
retfie

end
 
The W register is often saved like this when entering a interrupt in order to recover it when the interrupt is over.
The Status of each and every register on power up is shown in a list in the 1822 manual.
Again the STATUStemp will be a temporary register assigned at the pgm start by the pgm originator.
The instruction set description listing explains fairly precisely what occurs when each of the instructions are performed.
In fact the list of instructions you show are just a way of saving critical registers while a interrupt routine is performed and the recovery of them afterwards.
Max.
 
Last edited:
The Status of each and every register on power up is shown in a list in the 1822 manual.
I searched datasheet but what i find is just table of instruction set befor instruction description at last pages
Can u give me link of the one u mentioning
 
There are very few differences in register status between either condition, but if you look at the STATUS register for e.g. it is only bits 3&4 affected and see register description to see which applies or to consider in your particular program.
For the most part these bits can be left untouched.
The same applies to other registers which mode you have to be aware of according to program conditions.
Max.
 
That code is the start and end of an interrupt service routine.

In itself it does nothing much, except to make sure that the code that has been interrupted won't be stopped from working. Lots of code uses the status register and the w register.

For example:
Code:
    movlw   0x02
    decf    reg1, f
    btsc    status, Z
    movwf  reg1

Now that simple bit of code decrements reg1 and then forces it to a value of 2 if it hit zero. The W reg is set in the first line, and possibly used in the 4th line. The zero flag of the status register is set in the second line and used in the third.

If an interrupt occurs while that line of code is being run, it is important that the W and status registers are not affected. Neither movwf nor swapf affect the status registers, so they can be used in the interrupt routine to store the W and status registers and then restore them.

Your code with my comments:-
Code:
; this is the start of the interrupt service routine. The values of W and status are not known, but they may be important.
movwf WTemp ; save W register, so whatever was in W is now in WTemp. Status is not affected by movwf
swapf STATUS,w  ;move the status register to W, using swap so that status isn't affected while doing this move.
movwf StatusTemp ;save what was the status register, now swapped round, in StatusTemp

movf INTCON,w ;
movwf IntStat  ; Save the INTCON register. I don't know why, but possibly to work out what to do during the interrupt.

; This is where the actual interrupt code can go. It can do anything it likes to the W or status
;It is possible that the interrupt only needed to save INTCON, but doing that would have affected the W and status registers, so they had to be protected first

swapf StatusTemp,w  ;Now what was in status register before the interrupt is swapped back to its original order
movwf STATUS           ; and put back into the status register.
swapf WTemp,f           ;The temporary store for the original W register is swapped
swapf WTemp,w         ; and then it is swapped back and at the same time put back in W
clrf INTCON                ;INTCON is cleared to disable more interrupts
retfie                             ;returns from interrupt
I hope this explains what the code is doing.
 
if there are many function call are inherited in a subroutine . then is that possibe to execute those i needed or all will be executed in a sequence.
 
for example
goto Init ;reset vector
Init:
clrwdt
bcf INTCON,GIE ;disable global int

call SetBank1 ;***
movlw b'11000111' ;no WPUB,rising edge INT, internal clk,TMR0 to presc/256
movwf OPTION_REG

movlw b'00111100'
movwf TRISA

movlw b'00000011' ;no LP wakeup, no BOR, reset POR,BOR
movwf PCON
movlw b'01001010' ;250KHz, internal osc
movwf OSCCON
clrf PIE1
call SetBank2
clrf FVRCON

call SetBank3 ;***
clrf ANSELA ;portA digital

call SetBank0 ;***
clrf INTCON ;disable all int

call Delay1Sec
movlw 0x01
DelayXSec:
movwf SecCntr
DelayXSec_1:
call SetDel1Sec
SetDel1Sec:
call SetBank1
movlw 0x07
iorwf OPTION_REG,f ;set prescaler to /256
call SetBank0

movlw 0x0A ; [1/(250000/4/256)]*245=1S
movwf TMR0
bcf INTCON,T0IF ;clear timer overflow flag
return


DelayXSec_2:
btfss INTCON,T0IF ;check for overflow
goto DelayXSec_2

movlw 0x0A ; [1/(250000/4/256)]*245=1S
movwf TMR0
bcf INTCON,T0IF

movlw 0x07
andwf SecCntr,f ;mask unused bits first
decfsz SecCntr,f
goto DelayXSec_2
return



call Delay1Sec
call ValveOff ;turn valve off after reset

call SetBank7
bsf IOCAP,5 ;both RA$ and 5 rising edge
bsf IOCAP,4
clrf IOCAF ;enable RA4 valve on int
call SetBank1
bsf WDTCON,SWDTEN
call SetBank0
clrf IntStat
clrf ValTmrStat
clrf PortAIntStat
 
can u see there is subroutine inherited in subroutine so is that i can skip that step and move on ther if that is not needed
The microcontroller does what you tell it. When you say that the subroutine is inherited, is it code you have got from somewhere else, and is it code that you can change?

If you can change the code, just leave out the bits you don't need.

That code doesn't seem to have any interrupts in it. The code is very short of comments so I really don't know what it is doing.
 
It sounds like you may want to go through some of the many online Pic tutoriols first before trying to decifer a program.
Max.
 
The code in the first post is explained in good detail in the PICmicro Mid-Range MCU Family Reference Manual, pages 8-11 through 8-13. See attached.
 

Attachments

  • 33023a_p811_813.pdf
    58.8 KB · Views: 265
Sorry to come late to the party. But, doesn't the 12F1822 have automatic context saving and restoration? So, why is that code even included?

John
 
Yes. You're right. It appears then that the OP is adapting some old software to a new processor.
 
An any one ex[plain why masking is done here what is the use of it here ?

movlw 0x07
andwf SecCntr,f ;mask unused bits first
decfsz SecCntr,f
goto DelayXSec_2
return
 
Suggestion: Please use the code tags ({code} {/code} ) , replace braces with brackets to make your code more readable.

Without masking, what would the instruction, decfsz work on? What is in bits <3:7> ?. Masking it, reduces its size to a maximum of 7 in this case.

John

Moderators: Is this thread in the correct forum?
 
What a long thread is this going to be...
 
An any one ex[plain why masking is done here what is the use of it here ?
Code:
movlw 0x07
andwf SecCntr,f ;mask unused bits first
decfsz SecCntr,f
goto DelayXSec_2
return

Masking is done to limit the value of SecCntr to the range of 0..7.
The counter decrements: 7, 6, 5, 4, 3, 2, 1, 0, and then on the next decrement it will go to 255. The andwf instruction forces it back to 7.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top