• 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.

about PIC18 assembly code

Status
Not open for further replies.

fengshao

New Member
Hi everyone ,recently i got some code for my demo board .bt i met some part dont understand

Code:
Initialize
	bsf		STATUS,RP0		; bank 1
    
	bcf 	TRISC,1			; Output: RC1
	bcf 	TRISC,2        ; Output: RC2
	movlw	b'00001111'		; Outputs: RD2,RD4-RD7
	movwf	TRISD		
	movlw	b'01110001'		; 8Mhz
	movwf	OSCCON
	movlw	b'00001110'		; enable AN0
	movwf	ADCON1
	movwf	b'00000101'		; Fosc/16
	movwf	ADCON2
	movlw	0x3F			; 31.2 kHz PWM
	movwf	PR2
	movlw	b'00000000'		; 1:2 prescaler for TMR0
	movwf	T0CON
	bcf		STATUS,RP0		; bank 0

	clrf	PORTD			; turn off windings
	clrf	PORTC
	movlw	b'00000001'		; left justified, AN0 selected, module on
	movwf	ADCON0			
	movlw	0x07
	movwf	CMCON			; turn off comparators
	clrf	CCPR1L
	clrf	CCPR2L
	movlw	b'00001100'		; pwm mode
	movwf	CCP1CON
	movwf	CCP2CON	        ; turn on PWM
	bsf 	T2CON,TMR2ON	; turn on Timer 1
	bsf		T1CON,TMR1ON	; turn on Timer 1

	clrf	State			; initialize motor state pointer and duty cycle
	clrf	Index			;  index pointer
	clrf	Mode
	clrf	ADRESH
	movlw	1
	movwf	StepModeScaler
	movwf	Counter
	clrf	Speed			
	clrf	ButtonState		; initialize button state

Main

 	[COLOR="Red"]btfss	INTCON,TMR0IF		; Wait 2.5ms
	goto	Main
	bcf		INTCON,TMR0IF[/COLOR]
   	call	ButtonPress		; Call the button press debounce routine
	btfsc	STATUS,C		; The carry flag is set if a button press just occurred
	incf	Mode,f			;  Change to the next state  when a button press occurs.
..............................................................
............................................................
...................................................

the 3 red color line ,when i add ,the whole programme will always loop there ,when i delete these code ,my motor can run ,someone can help
 

Attachments

Last edited:

gabeNC

Member
All you are doing is looping the btfss statement with the goto on the next line.
You probably want the goto statement at the end.
 

AtomSoft

Well-Known Member
you need to show way more code than that.

How is the timer set up?

TIP:
When you first start a interrupt you should clear the associated flag right after you enable it.
 

Pommie

Well-Known Member
Most Helpful Member
You need to turn on Timer 0,
Code:
	bsf 	T0CON,TMR0ON	            ; turn on Timer 0
Main
 	btfss	INTCON,TMR0IF		; Wait 2.5ms
	goto	Main
	bcf	INTCON,TMR0IF
Mike.
 

AtomSoft

Well-Known Member
lol dude where is you ISR? Timer Intterupt? You need that in order to clear it in the interrup itself.

When you have a interrupt it jumps to s special location:

Most Pics have is like:
High Priority = Address 0x08
Low Priority = Address 0x18

Need more code. Somewhere there you sure have something similar to :


Code:
	ORG	0x0000
	bra    Init    ;where ever you program starts.


	ORG	0x0008
	bra	HighInt        ; If using a high interrupt if not then 0x0018
	...................
	..................
HighInt:
        bcf    INTCON,TMR0IF
	retfie    FAST
	END
 

AtomSoft

Well-Known Member
not tested but something like:

Code:
	ORG	0x0000
	bra Initialize

	ORG	0x0008
	bra	HighInt        ; If using a high interrupt if not then 0x0018

Initialize
	bsf		STATUS,RP0		; bank 1
    
	bcf 	TRISC,1			; Output: RC1
	bcf 	TRISC,2        ; Output: RC2
	movlw	b'00001111'		; Outputs: RD2,RD4-RD7
	movwf	TRISD		
	movlw	b'01110001'		; 8Mhz
	movwf	OSCCON
	movlw	b'00001110'		; enable AN0
	movwf	ADCON1
	movwf	b'00000101'		; Fosc/16
	movwf	ADCON2
	movlw	0x3F			; 31.2 kHz PWM
	movwf	PR2
	movlw	b'00000000'		; 1:2 prescaler for TMR0
	movwf	T0CON
	bcf		STATUS,RP0		; bank 0

	clrf	PORTD			; turn off windings
	clrf	PORTC
	movlw	b'00000001'		; left justified, AN0 selected, module on
	movwf	ADCON0			
	movlw	0x07
	movwf	CMCON			; turn off comparators
	clrf	CCPR1L
	clrf	CCPR2L
	movlw	b'00001100'		; pwm mode
	movwf	CCP1CON
	movwf	CCP2CON	        ; turn on PWM
	bsf		T2CON,TMR2ON	; turn on Timer 1
	bsf		T1CON,TMR1ON	; turn on Timer 1
[COLOR="Red"] 	bsf 	T0CON,TMR0ON	; turn on Timer 0
	movlw	0xE0
	movwf	INTCON			;Turn on Timer0 Interrupt .
	bsf		INTCON2,TMR0IP	;Make timer 0 high priority. [/COLOR]
	clrf	State			; initialize motor state pointer and duty cycle
	clrf	Index			;  index pointer
	clrf	Mode
	clrf	ADRESH
	movlw	1
	movwf	StepModeScaler
	movwf	Counter
	clrf	Speed			
	clrf	ButtonState		; initialize button state

Main;[COLOR="Red"]
 	btfss	myFlag,0		; Wait 2.5ms
	goto	Main			
	bcf		myFlag,0		;Clear the flag for next time[/COLOR]
   	call	ButtonPress		; Call the button press debounce routine
	btfsc	STATUS,C		; The carry flag is set if a button press just occurred
	incf	Mode,f			;  Change to the next state  when a button press occurs.
..............................................................
............................................................
...................................................
[COLOR="Red"]HighInt:
    bcf		INTCON,TMR0IF	;
    bsf		myFlag,0 ;  Create your own flag
	retfie	FAST[/COLOR]
	END
 
Last edited:

fengshao

New Member
not tested but something like:

Code:
	ORG	0x0000
	bra Initialize

	ORG	0x0008
	bra	HighInt        ; If using a high interrupt if not then 0x0018

Initialize
	bsf		STATUS,RP0		; bank 1
    
	bcf 	TRISC,1			; Output: RC1
	bcf 	TRISC,2        ; Output: RC2
	movlw	b'00001111'		; Outputs: RD2,RD4-RD7
	movwf	TRISD		
	movlw	b'01110001'		; 8Mhz
	movwf	OSCCON
	movlw	b'00001110'		; enable AN0
	movwf	ADCON1
	movwf	b'00000101'		; Fosc/16
	movwf	ADCON2
	movlw	0x3F			; 31.2 kHz PWM
	movwf	PR2
	movlw	b'00000000'		; 1:2 prescaler for TMR0
	movwf	T0CON
	bcf		STATUS,RP0		; bank 0

	clrf	PORTD			; turn off windings
	clrf	PORTC
	movlw	b'00000001'		; left justified, AN0 selected, module on
	movwf	ADCON0			
	movlw	0x07
	movwf	CMCON			; turn off comparators
	clrf	CCPR1L
	clrf	CCPR2L
	movlw	b'00001100'		; pwm mode
	movwf	CCP1CON
	movwf	CCP2CON	        ; turn on PWM
	bsf		T2CON,TMR2ON	; turn on Timer 1
	bsf		T1CON,TMR1ON	; turn on Timer 1
[COLOR="Red"] 	bsf 	T0CON,TMR0ON	; turn on Timer 0
	movlw	0xE0
	movwf	INTCON			;Turn on Timer0 Interrupt .
	bsf		INTCON2,TMR0IP	;Make timer 0 high priority. [/COLOR]
	clrf	State			; initialize motor state pointer and duty cycle
	clrf	Index			;  index pointer
	clrf	Mode
	clrf	ADRESH
	movlw	1
	movwf	StepModeScaler
	movwf	Counter
	clrf	Speed			
	clrf	ButtonState		; initialize button state

Main;[COLOR="Red"]
 	btfss	myFlag,0		; Wait 2.5ms
	goto	Main			
	bcf		myFlag,0		;Clear the flag for next time[/COLOR]
   	call	ButtonPress		; Call the button press debounce routine
	btfsc	STATUS,C		; The carry flag is set if a button press just occurred
	incf	Mode,f			;  Change to the next state  when a button press occurs.
..............................................................
............................................................
...................................................
[COLOR="Red"]HighInt:
    bcf		INTCON,TMR0IF	;
    bsf		myFlag,0 ;  Create your own flag
	retfie	FAST[/COLOR]
	END
Hi AtomSoft, i just test your code got some errors like that:
Code:
Warning[207] D:\5-8\STEPPER\MAIN1.ASM 7 : Found label after column 1. (SW)
Warning[205] D:\5-8\STEPPER\MAIN1.ASM 12 : Found directive in column 1. (ORG)
Error[113]   D:\5-8\STEPPER\MAIN1.ASM 68 : Symbol not previously defined (myFlag)
Error[113]   D:\5-8\STEPPER\MAIN1.ASM 70 : Symbol not previously defined (myFlag)
Error[113]   D:\5-8\STEPPER\MAIN1.ASM 436 : Symbol not previously defined (myFlag)
 

AtomSoft

Well-Known Member
Try This:
Code:
		CBLOCK	0x000
		myFlag
		ENDC

		ORG	0x0000
	bra Initialize

		ORG	0x0008
	bra	HighInt        ; If using a high interrupt if not then 0x0018

Initialize:
	bsf		STATUS,RP0		; bank 1
	bcf 	TRISC,1			; Output: RC1
	bcf 	TRISC,2        ; Output: RC2
	movlw	b'00001111'		; Outputs: RD2,RD4-RD7
	movwf	TRISD		
	movlw	b'01110001'		; 8Mhz
	movwf	OSCCON
	movlw	b'00001110'		; enable AN0
	movwf	ADCON1
	movwf	b'00000101'		; Fosc/16
	movwf	ADCON2
	movlw	0x3F			; 31.2 kHz PWM
	movwf	PR2
	movlw	b'00000000'		; 1:2 prescaler for TMR0
	movwf	T0CON
	bcf		STATUS,RP0		; bank 0

	clrf	PORTD			; turn off windings
	clrf	PORTC
	movlw	b'00000001'		; left justified, AN0 selected, module on
	movwf	ADCON0			
	movlw	0x07
	movwf	CMCON			; turn off comparators
	clrf	CCPR1L
	clrf	CCPR2L
	movlw	b'00001100'		; pwm mode
	movwf	CCP1CON
	movwf	CCP2CON	        ; turn on PWM
	bsf		T2CON,TMR2ON	; turn on Timer 1
	bsf		T1CON,TMR1ON	; turn on Timer 1
 	bsf 	T0CON,TMR0ON	; turn on Timer 0
	movlw	0xE0
	movwf	INTCON			;Turn on Timer0 Interrupt .
	bsf		INTCON2,TMR0IP	;Make timer 0 high priority. 
	clrf	State			; initialize motor state pointer and duty cycle
	clrf	Index			;  index pointer
	clrf	Mode
	clrf	ADRESH
	movlw	1
	movwf	StepModeScaler
	movwf	Counter
	clrf	Speed			
	clrf	ButtonState		; initialize button state

Main:
 	btfss	myFlag,0		; Wait 2.5ms
	goto	Main			
	bcf		myFlag,0		;Clear the flag for next time
   	call	ButtonPress		; Call the button press debounce routine
	btfsc	STATUS,C		; The carry flag is set if a button press just occurred
	incf	Mode,f			;  Change to the next state  when a button press occurs.

HighInt:
    bcf		INTCON,TMR0IF	;
    bsf		myFlag,0 ;  Create your own flag
	retfie	FAST
	END
 

fengshao

New Member
Try This:
Code:
		CBLOCK	0x000
		myFlag
		ENDC

		ORG	0x0000
	bra Initialize

		ORG	0x0008
	bra	HighInt        ; If using a high interrupt if not then 0x0018

Initialize:
	bsf		STATUS,RP0		; bank 1
	bcf 	TRISC,1			; Output: RC1
	bcf 	TRISC,2        ; Output: RC2
	movlw	b'00001111'		; Outputs: RD2,RD4-RD7
	movwf	TRISD		
	movlw	b'01110001'		; 8Mhz
	movwf	OSCCON
	movlw	b'00001110'		; enable AN0
	movwf	ADCON1
	movwf	b'00000101'		; Fosc/16
	movwf	ADCON2
	movlw	0x3F			; 31.2 kHz PWM
	movwf	PR2
	movlw	b'00000000'		; 1:2 prescaler for TMR0
	movwf	T0CON
	bcf		STATUS,RP0		; bank 0

	clrf	PORTD			; turn off windings
	clrf	PORTC
	movlw	b'00000001'		; left justified, AN0 selected, module on
	movwf	ADCON0			
	movlw	0x07
	movwf	CMCON			; turn off comparators
	clrf	CCPR1L
	clrf	CCPR2L
	movlw	b'00001100'		; pwm mode
	movwf	CCP1CON
	movwf	CCP2CON	        ; turn on PWM
	bsf		T2CON,TMR2ON	; turn on Timer 1
	bsf		T1CON,TMR1ON	; turn on Timer 1
 	bsf 	T0CON,TMR0ON	; turn on Timer 0
	movlw	0xE0
	movwf	INTCON			;Turn on Timer0 Interrupt .
	bsf		INTCON2,TMR0IP	;Make timer 0 high priority. 
	clrf	State			; initialize motor state pointer and duty cycle
	clrf	Index			;  index pointer
	clrf	Mode
	clrf	ADRESH
	movlw	1
	movwf	StepModeScaler
	movwf	Counter
	clrf	Speed			
	clrf	ButtonState		; initialize button state

Main:
 	btfss	myFlag,0		; Wait 2.5ms
	goto	Main			
	bcf		myFlag,0		;Clear the flag for next time
   	call	ButtonPress		; Call the button press debounce routine
	btfsc	STATUS,C		; The carry flag is set if a button press just occurred
	incf	Mode,f			;  Change to the next state  when a button press occurs.

HighInt:
    bcf		INTCON,TMR0IF	;
    bsf		myFlag,0 ;  Create your own flag
	retfie	FAST
	END

thanks bro

bt new problem

Error - section '.org_1' can not fit the absolute section. Section '.org_1' start=0x00000008, length=0x000002d4
Errors : 1
 

AtomSoft

Well-Known Member
did you place anything between:

Code:
		ORG	0x0000
	bra Initialize
        ;DO NOT PLACE ANYTHING HERE
		ORG	0x0008
	bra	HighInt        ; If using a high interrupt if not then 0x0018
 
Status
Not open for further replies.

EE World Online Articles

Loading
Top