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.

18F4553 PWM Assembly help

Status
Not open for further replies.

superbrew

Member
I am trying to learn assembly and all was going well, until I tried to create a PWM signal. All I get is PORTD.1 blinking at about 10Hz.
Here is the code that I am using:
Code:
	list	p=18F4553
	include	<p18F4553.inc>
	CONFIG	FOSC=HS,WDT=OFF,MCLRE=ON,LVP=OFF
	
PWM1	RES		1
	
	ORG		0x00
	GOTO	INIT
	
	ORG 	0x08
	GOTO 	PWM_ISR
	
	ORG		0x18
	GOTO	PWM_ISR
		
INIT:
	MOVLW	b'00000001'
	MOVWF	TRISA
	CLRF	PORTA
	CLRF	TRISD
	CLRF	PORTD
	;AN0,AD OFF
	MOVLW	b'00000000'
	MOVWF	ADCON0
	;Vss,Vdd,AN3-AN0
	MOVLW 	b'00001011'  
	MOVWF 	ADCON1
	;Left,16Tad,FOSC/16
	MOVLW	b'00110101'
	MOVWF	ADCON2
	;Timer 1 on with Pre=2
	MOVLW	b'10010001'		
	MOVWF	T1CON
	MOVLW	b'00001011'		;Special event trigger
	MOVWF	CCP1CON
	MOVLW	0xFF
	MOVWF	CCPR1L
	
	BSF	ADCON0,ADON		;turn ad on
	BSF	PIE1,CCP1IE		;enable CCP1 interrupt
	BSF	INTCON,PEIE		;enable peripheral interrupts
	BSF	INTCON,GIE		;enable global interrupts
	CLRF	PWM1
	
LOOP:
	GOTO	LOOP

;*******************************
;PWM ISR                       *
;*******************************	
PWM_ISR:
	BTFSC   LATD,1
	GOTO	TURN_OFF
	BSF	ADCON0,1
AD:
	BTFSS   ADCON0,1
	GOTO	AD
	MOVF	ADRESH,W
	MOVWF	CCPR1L
	BSF	LATD,1
	GOTO	ISR_END
	
TURN_OFF:
	CLRF	PWM1
	MOVF	ADRESH,W
	SUBWF	PWM1
	MOVF	PWM1,W
	MOVWF	CCPR1L
	BCF	LATD,1
			
ISR_END
	BCF	PIR1,CCP1IF  ;clear int flag
	RETFIE

	END

I know the A/D is good because I used it on another project that read out on 7seg displays. This program also works with the Oshonsoft sim using the 18F4550, although it does act funny at the extremes of the A/D range.

Thanks
 
Hi,

You might find the following PWM tutorial of help.



Also the 4553 datasheet seems to be one of these cut down affairs - so look at the 4520 datasheet for some better info on various features.

Have not run your code, but a few things look a bit different to me -

Wondered why you need to run these routines from within the ISR which always complicates things - surely better to just do them in a simple main loop , then when proven add the ISR if needed.

Noticed you are using Osc =HS, you are aware that you have a good internal osc upto 8mhz avaialable ?
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top