Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Need help in MPLAB IDE

Status
Not open for further replies.

emperror123

Member level 5
Joined
Dec 11, 2010
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
1,288
Activity points
1,880
as i understand the question is mean they have active low switch connected to RB3 and when it is pressed, active high led will on for 1 second, in between 1 sec, any switch press will not happen anything. 2nd is LED in RA1 will on / off 0.25second will be in ISR. both use RC oscillator and 800kHz

below are my coding
Code:
#include p16f84a.inc

D3 equ 21
D2 equ 22
D1 equ 23
	

		org 00
		goto start

		org 04
		goto int_routine

start 	bsf STATUS, 5
		movlw B'00000000'
		movwf TRISA
		movlw B'00001000'
		movwf TRISB
		movlw B'00000000'
		movwf OPTION_REG
		bcf STATUS, 5
		
switch	btfsc PORTB, 4
		bsf PORTB, 3
		call delay2
		bcf PORTB, 3
		goto switch

int_routine
			
LED		
			bsf PORTA, 1
			call delay2
			bcf PORTA, 1
			call delay2
			goto LED
		
delay1	movlw	D'20'
		movwf D3
		movlw D'36'
		movwf D2
		movlw D'45'
		movwf D1
		decfsz D1
		goto $ - 1
		decfsz D2
		goto $ - 5
		decfsz D3
		goto $ - 9
		return		

delay2	movlw	D'10'
		movwf D3
		movlw D'25'
		movwf D2
		movlw D'32'
		movwf D1
		decfsz D1
		goto $ - 1
		decfsz D2
		goto $ - 5
		decfsz D3
		goto $ - 9
		return		

end

i am just learn from it, but still not familiar with it, just would like to ask how should i modified it. thank and ur help is much appreciated
 
Last edited:

Hi,

You are going along on the right lines, though your code, particularly with the TM0 / Interrupt needs some work.

What you really want to do, while you are learning , is break the project down in to three easier separate tasks.

First do some code, using one of your software delays to flash the RA1 led every .25secs.
Get that working fully first.

Second, do the code for RB3 switch input sensing, when activated switch the RB4 led on for 1 second.

Third will be to learn how to use TM0 and the Interrupt routine which once understood you can then use to pull the whole project together.

You can come back and show your code and circuit for each step so we can check you are on the right lines.

Are you running you code in a simulator or on actual hardware .

Below is some modifications to the opening bit of your code which will make it run better

Your delays timed in MPlab Sim at 800khz are .5sec for delay1 and 0.125 for delay2

Code:
	list      p=16F84A            ; list directive to define processor
	#include p16f84a.inc
	
	__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

; '__CONFIG' directive is used to embed configuration data within .asm file.
;            it tells it the oscillator type etc.  


;    user variables  - ram area actually starts at 0x0C though 21 ok

	D3 equ 21
	D2 equ 22
	D1 equ 23
	
		
	org 00
	goto start
	
	org 04
;	goto int_routine		; no routine to go to yet , so just return out.
	retfie
	
	start bsf STATUS, 5
	movlw B'00000000'
	movwf TRISA
	movlw B'00001000'
	movwf TRISB
	movlw B'00000000'
	movwf OPTION_REG			;  ? which parameters are you selecting ?
	bcf STATUS, 5
 
  • Like
Reactions: sahu

    sahu

    Points: 2
    Helpful Answer Positive Rating
actually i work out with TMR0 / interrupt in my program as mention in my question

i am using virtual breadboard as my simulator.

since i use peripheral of ratio 1:2 in my option register of timer 0, but i still need to set range for 1 second and 0.25 second, is it i need to set it on 0.5second or 0.125 second as one my timer to calculate in timer 0

2nd, the int_routine does not help in doing multi tasking work, how should i modified it in order to do multitasking work of my circuit?

below program i saw that just set up the port a, port b and option register and also include of RC osc. hmm, how shud i ISR since it nid multitasking
 

Below is an extract from a programme that I wrote. I used TMR0 to do the timing.

It could be modified so the PIC can do other functions while the timer is running.

Note that I did not use interrupts as I detected the TMR0 overflow by polling the TMR0 overflow flag.

I have also used this technique for detecting button presses. I polled the RB change flag.

I hope this may be of interest.

Len BTW how do you insert code?

------------------------------------------------

#include <p16f877A.inc> ; processor specific variable definitions

__CONFIG _CP_OFF & _WDT_OFF & _BODEN_OFF & _PWRTE_ON & _XT_OSC & _WRT_OFF & _LVP_ON & _CPD_OFF

cblock 0x20
count_O ;used to count TMR1 overflows
count_a ;used in delay routine
count_b ;used in delay routine

count_k ;constant used in delay routine
TMR0_k ;constant used in delay routine
endc

#define tmr0_of INTCON, TMR0IF ;TMR0 overflow (bit 2)

#define rb0_if INTCON, INTF ;RB0 change flag (bit 1)
#define rbc_if INTCON, RBIF ;RB change flag (bit 0)

#define c_bit STATUS, C
#define z_bit STATUS, Z

Main programme not included

;Delay Sub Routines
delay_5s
movlw d'200' ;delay 200 mS
movwf count_k

movlw d'25'
movwf count_b
goto delay_n

delay_1s
movlw d'100' ;delay 100 mS
movwf count_k

movlw 0x0A
movwf count_b
goto delay_n

delay_500m
movlw 0x02 ;delay 2 mS
movwf count_k

movlw 0xF8 ;times 248
movwf count_b
goto delay_n

delay_300m
movlw 0x02 ;delay 2 mS
movwf count_k

movlw 0x95 ;times 149
movwf count_b
goto delay_n

delay_250m
movlw 0x01 ;delay 1 mS
movwf count_k

movlw 0xF8 ;times 248
movwf count_b
goto delay_n

delay_100m
movlw d'100' ;delay 100 mS
movwf count_k

movlw 0x01
movwf count_b
goto delay_n

delay_30m
movlw d'30' ;delay 30 mS
movwf count_k

movlw 0x01
movwf count_b
goto delay_n

delay_20m
movlw d'20' ;delay 20 mS
movwf count_k

movlw 0x01
movwf count_b
goto delay_n

delay_5m
movlw 0x05 ;delay 5 ms
movwf count_k

movlw 0x01 ;times 1
movwf count_b
goto delay_n

delay_2m
movlw 0x02 ;delay 2 ms
movwf count_k

movlw 0x01 ;times 1
movwf count_b
goto delay_n

delay_1m ;1 ms
movlw 0x01
movwf count_k

movlw 0x01
movwf count_b
goto delay_n

delay_250u ;250 us
movlw 0x01
movwf count_k

movlw 0x01
movwf count_b

movlw 0xC6 ;complement of d'57'
movwf TMR0_k ;TMR0 constant
goto dda

delay_n
movlw 0x07 ;complement of d'248'
movwf TMR0_k ;TMR0 constant

dda movfw count_k ;constant
movwf count_a

ddb
movfw TMR0_k ;constant
movwf TMR0

bcf tmr0_of
btfss tmr0_of
goto $-1

decfsz count_a
goto ddb

decfsz count_b
goto dda
return

END
 

in my first program before combine is

Code:
list p=16f84a
#include p16f84a.inc
__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
D3 equ 21
D2 equ 22
D1 equ 23
	

		org 00
		goto start

	
start 	bsf STATUS, 5
		movlw B'00000000'
		movwf TRISA
		movlw B'00001000'
		movwf TRISB
		movlw B'00000000'
		movwf OPTION_REG
		bcf STATUS, 5
		
switch	btfsc PORTB, 4
		bsf PORTB, 3
		call delay2
		bcf PORTB, 3
		goto switch

delay1	movlw	D'20'
		movwf D3
		movlw D'36'
		movwf D2
		movlw D'45'
		movwf D1
		decfsz D1
		goto $ - 1
		decfsz D2
		goto $ - 5
		decfsz D3
		goto $ - 9
		return
in which the program will run in 1s while the button is press as what i been modified it

another is
Code:
list p=16f84a
#include p16f84a.inc
__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC
D3 equ 21
D2 equ 22
D1 equ 23
	

		org 00
		goto start
LED		
			bsf PORTA, 1
			call delay2
			bcf PORTA, 1
			call delay2
			goto LED
delay2	movlw	D'10'
		movwf D3
		movlw D'25'
		movwf D2
		movlw D'32'
		movwf D1
		decfsz D1
		goto $ - 1
		decfsz D2
		goto $ - 5
		decfsz D3
		goto $ - 9
		return		

end
above are the on / off LED for 0.25second, in order to combine both and function together, how shud i set up the ISR to combine both of them?
 

Hi,

Your code looks good, for the Timer routine you need to have a look at these pages.

Golligum shows you good detail of how to use and set up Timer0. **broken link removed**


For the Interrupts first look at the Template file, for every pic chip Microchip have created a basic Template to help get you started, they are in the MPASM/Templates folder.
Here is the code for the ISR.

You have to think of it like this, your program is running along, it uses various system registers like W and STATUS, when an interrupt occurs it branches to your ISR which also uses these registers, so it must first make backup coplies of them, run the ISR code, then restore them before returning to the main code. This routine is called ISR Context Saving.

It is generally good practice to keep your ISR code short so you do not miss other interrupts and avoid Calling or Going to code outside your ISR routine - more of a problem for very large programs.



I could point to to some completed examples but try setting up Timer0 and the ISR yourself first.
If you get stuck we can help.


I have not used VB simulator but MPlab Sim still measures Delay1 as .5sec at 800khz.




Code:
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F84A.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************


	list      p=16F84A            ; list directive to define processor
	#include <p16F84A.inc>        ; processor specific variable definitions

	__CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The lables following the directive are located in the respective .inc file.
; See respective data sheet for additional information on configuration word.




;***** VARIABLE DEFINITIONS
w_temp        EQU     0x0C        ; variable used for context saving 
status_temp   EQU     0x0D        ; variable used for context saving








;**********************************************************************
		ORG     0x000             ; processor reset vector
  		goto    main              ; go to beginning of program


		ORG     0x004             ; interrupt vector location
		movwf   w_temp            ; save off current W register contents
		movf	STATUS,w          ; move status register into W register
		movwf	status_temp       ; save off contents of STATUS register


; isr code can go here or be located as a call subroutine elsewhere


		movf    status_temp,w     ; retrieve copy of STATUS register
		movwf	STATUS            ; restore pre-isr STATUS register contents
		swapf   w_temp,f
		swapf   w_temp,w          ; restore pre-isr W register contents
		retfie                    ; return from interrupt



main

; remaining code goes here
 

i have tried to modified it until as below

Code:
list p=16F84A
#include p16f84a.inc

__CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC

	w_temp equ 12
	status_temp equ 13

		org 00
		goto start

		org 04
		goto int_routine

start	bsf STATUS, RP0
		movlw b'00001000'
		movwf TRISB
		clrf TRISA
		movlw b'00000111'
		movwf OPTION_REG
		bcf STATUS, RP0

loop	btfsc PORTB, 4
		call delay
		bcf PORTB, 4
		bcf PORTB, 5

delay	bsf PORTB, 5
		; i do not know how to short cut way to calculate the 1s
dell	btfss INTCON, T0IF
		goto dell
		bcf INTCON, T0IF
		return

int_routine
		movwf w_temp
		movf STATUS, W
		movwf status_temp
		movf    status_temp,W     
		movwf	STATUS           
		swapf   w_temp,F
		swapf   w_temp,W         
		retfie      
                end

but i still unable to modified until 1second delay for the RB4 and also multitasking RA1 to on / off for every 0.25s, i am still weak on coding of MPLAB IDE, nid someone to guide me for the question above. and before that, when RB3 is pressed, no any other button will function when the RA4 is set as what question stated in

thank a lot, hopefully u all can help me, and ur help is much much much appreciated
 

Hi,

You can use this link to generate the delay code for you.
**broken link removed**

Will send some ISR code shortly.

---------- Post added at 14:32 ---------- Previous post was at 14:07 ----------

Hi again,

You seem rather stuck on a lot of the coding and me giving you the all code will teach you nothing.
So why don't you pause and have a good look at this beginners 16F84A assembler tutorial.
**broken link removed**

It gradually takes you though all the basics and works up to a ISR in step 12 and would work well with your VB.
It may take you a few days or weeks to go though it all properly, but at least you will understand things better.

If you follow all 12 steps then you should be able to add in TM0 to your ISR .

If you get stuck on any part of the tutorial you can always ask for help here.
 
hi, wp100

thank for the link, i will try to have a look in it,
just for your information, i am just PIC/ MPLAB learner during last month, for basic thing i am still ok, but goes to interrupt, i still got little problem, anyway, will try to solve it out, and hopefully will help me solve from both 2 links u give me
 

Hi,

The examples of Timer0 and the Interrupt Service Routine together can seem a lot to take onboard.

Here is an example of just Timer0 in its simplest form flashing a led.

Have a look at the code and try an see how it uses Timer0 IF flag without needing to involke the ISR routine.

Try changing Timer0 parameters to get different times, and then try and create a method for making the program use Timer0 .33sec maximum delay to create your delay of 1 second and then 0.25seconds.
 

Attachments

  • TMR0_led.rar
    13.7 KB · Views: 50

i were doing something as what i have expected the out put to come out but unfortunate, cant be make my coding function

Code:
list p=16F84A
#include p16f84a.inc

__CONFIG   _CP_OFF & _WDT_ON & _PWRTE_ON & _RC_OSC

	w_temp equ 12
	status_temp equ 13
	d1 equ 21
	d2 equ 22
		org 00
		goto start

		org 04
		goto int_routine
timer	bcf PORTA, 1
		call delay
		bsf PORTA, 1
		call delay
		goto timer

start	bsf STATUS, RP0
		movlw b'00001000'
		movwf TRISB
		clrf TRISA
		bcf STATUS, RP0

loop	btfsc PORTB, 3
		call delay1
		bcf PORTB, 4
		goto loop


int_routine
		movwf w_temp
		incf status_temp, 1
		movlw 0x0A
		subwf w_temp, 0
		btfss STATUS, 0
		goto carry_on
		goto clear
carry_on	bcf INTCON, 0x01
			movfw w_temp
			retfie
clear		clrf status_temp
			bcf INTCON, 1      
		retfie

delay		movlw	0x0F
	movwf	d1
	movlw	0x28
	movwf	d2
Delay0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay0

			;2 cycles
	goto	$+1
	return 

delay1	movlw	0x3F
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay0

			;2 cycles
	goto	$+1
	return     
		end
the program will stuck at delay there and unable return back to my RA1 to proceed to another program, any problem occur on that or m i nid any modification?
 

Hi,

Your code modified so it works, you can refer to the previous posts for details of how it does it.

Code:
	list p=16F84A
	#include p16f84a.inc

	__CONFIG   _CP_OFF & _WDT_OFF & _PWRTE_ON & _RC_OSC

W_ISR_TEMP equ 12
STATUS_ISR_TEMP equ 13
d1 equ 21
d2 equ 22

		org 00
		goto start

		org 04
	movwf	W_ISR_TEMP	
	swapf	STATUS,W			
	clrf	STATUS				
	movwf	STATUS_ISR_TEMP	

	bcf			INTCON,T0IF
	
	btfss	PORTA,1
	goto 	ledon
	bcf		PORTA,1
	goto	isrend
	
ledon	bsf	PORTA,1


isrend	swapf	STATUS_ISR_TEMP,W
	movwf	STATUS			
	swapf	W_ISR_TEMP,F	
	swapf	W_ISR_TEMP,W
	retfie	
				


timer	bcf PORTA, 1
		call delay
		bsf PORTA, 1
		call delay
		goto timer

start	bsf STATUS, RP0
		movlw b'00001000'
		movwf TRISB
		clrf TRISA
		movlw	B'00000110'  	; set up timer0 , bits 0-2 prescaler 1:128,  bit3 prescaler to Timer 0, bit5  using main internal clock 800khz/4 = 200khz !
		movwf	OPTION_REG
		bcf STATUS, RP0

		bsf	INTCON,T0IE		;	enable interrupts
		BSF INTCON,GIE

loop	bsf	 PORTB,4
		call delay1
		bcf PORTB, 4
		CALL delay1
		goto loop




delay		movlw	0x0F
	movwf	d1
	movlw	0x28
	movwf	d2
Delay0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay0

			;2 cycles
	goto	$+1
	return 

delay1	movlw	0x3F
	movwf	d1
	movlw	0x9D
	movwf	d2
Delay_0
	decfsz	d1, f
	goto	$+2
	decfsz	d2, f
	goto	Delay0

			;2 cycles
	goto	$+1
	return     
		end
 
hi wp100

thank again for guiding me

ur help is much appreciated

---------- Post added at 21:03 ---------- Previous post was at 19:43 ----------

before i end this post, i have 1 more last question,

i would like to modified the code which is add another switch on RB0 to toggle connect RB7 immediately when press

in it they ask me to use INTCON, INTE

but when i modified
i do like below
Code:
;program omitted just below i try to add in
bsf INTCON, INTE

loop btfsc PORTB, 0
       call delay
       bcf PORTB, 7

delay bsf PORTB, 7
        ; the delay code omitted
       return
       end
in the end, there is no function but the RA1 will keep blinking non stop, not every 0.25 second on and off, am i nid to modified or add something like TMR0 or anything else?
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top