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 Interrupt

Status
Not open for further replies.

swapan

Member
Hi friends,

In one battery charger circuit, I like to use an indication of being the battery charged by blinking a LED. For this purpose, I feel that TMR0 Interrupt could easily be employed. Thus I have assembled a code. But unfortunately, the LED does not blinks. On setting of first interrupt flag, the LED changes its state but it does not respond to the successive interrupt flag settings. I have checked the code in OSHONSOFT simulator which shows the same result. Please see my code and guide me in right way.

regards

swapan.

Code:
list      p=16F676           ; list directive to define processor
    #include <p16F676.inc>        ; processor specific variable definitions

    errorlevel  -302              ; suppress message 302 from list file

    __CONFIG   _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _HS_OSC 

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

;***** VARIABLE DEFINITIONS (examples)

; example of using Shared Uninitialized Data Section
INT_VAR     UDATA_SHR   0x20   
w_temp      RES     1       ; variable used for context saving 
status_temp RES     1       ; variable used for context saving

;**********************************************************************
RESET_VECTOR    CODE    0x0000       ; processor reset vector
        goto    start                ; go to beginning of program


INT_VECTOR      CODE    0x0004       ; interrupt vector location

INTERRUPT

        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

	movlw	b'00000001'
	xorwf	PORTC,1
	bcf		INTCON,T0IF

        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_PROG       CODE

; these first 4 instructions are not required if the internal oscillator is not used
		start
       
		bsf			STATUS,RP0
		movlw		b'111111'		;	All bits are input.
		movwf		TRISA
		movlw		b'111110'		;	All but bit 0 are input.
		movwf		TRISC
		movlw		b'00000000'		;	
		movwf		OPTION_REG
		bcf			STATUS,RP0
		movlw		b'10100000'		;	GIE enabled, Peripheral Interrupts disabled, 
		movwf		INTCON			;	TMR0 interrupt enable, External interrupt disabled,
								;	PORTA change interrupt disabled, TMR0 Overflow bit clear,
							
		btfss		PORTA,0			;	This an infinite loop to check the Interrupt service.
		goto		$-1
		goto		$-2

	; initialize eeprom locations

		EE      CODE    0x2100
        DE  	0x00, 0x01, 0x02, 0x03

        END                       ; directive 'end of program'
 
Shouldn't there be some code to set up the TMR0 period?
 
HTML:
Shouldn't there be some code to set up the TMR0 period?

TMR0 will increment on every instruction cycle since no prescaler is used. When TMR0 overflows from FF to 00, INTCON bit T0IF will set, thus initiating an interrupt.

swapan
 
You have the prescaler set to 2 and so the interrupt will happen every 512 cycles. That is far too fast for the eye to see. If you set the prescaler to 256 then it will flash at 15 times per second with a 4MHz crystal which is still too fast. You need to use a counter to allow many interrupts between port changes.

Edit, zero in option reg assigns the prescaler to timer0.

Mike.
 
Last edited:
Thanks mike. I have wrongly mentioned that there was no pre-scaler. While using in hardware I choose a pre-scaler of 1:256. And when I simulate the code in OSHONSOFT, a pre-scaler of 1:2 is used. In both the case, the LED lits once and continues in that state indefinitely.


swapan
 
hi,
Your program will not assemble in Oshonsoft.
Modified the code so that OSH can run it, the ports are not being initialised correctly , look at this image.

PORTC.1 is set as analog.???
 

Attachments

  • AAesp02.gif
    AAesp02.gif
    10.2 KB · Views: 200
  • swapan1m.asm
    2.8 KB · Views: 140
Last edited:
PORTC.1 is set as analog.???

I have used only RC0 bit as output. All other bits are input. Would it make any difference if PORTC,1 is set as analogue ? Actually the literal b'00000001' is XORed with PORTC and after XORing, the result is stored in PORTC. Hence the command:

xorwf PORTC,1

Looking for assistance from any member please.
 
Would it make any difference if PORTC,1 is set as analogue ?
Yes it would!
 
I have used only RC0 bit as output. All other bits are input. Would it make any difference if PORTC,1 is set as analogue ? Actually the literal b'00000001' is XORed with PORTC and after XORing, the result is stored in PORTC. Hence the command:

xorwf PORTC,1

Looking for assistance from any member please.

hi,
You must set PORTC as a digital output if you want to drive an LED.

Look at this code fragment on this image.
 

Attachments

  • AAesp04.gif
    AAesp04.gif
    41.2 KB · Views: 180
Status
Not open for further replies.

Latest threads

Back
Top