Setting Up PWM For PIC-16f690

Find out how to setup your programmer's software and how to solve many common problems.

Moderator: phalanx

Post Reply
Ozwurld
Posts: 13
Joined: Sat Apr 26, 2014 9:06 am

Setting Up PWM For PIC-16f690

Post by Ozwurld » Sat Apr 26, 2014 9:12 am

Hello all,

I am new to PIC micros, actually started coding today. I am trying to make a PWM, i followed the instruction on the datasheet to the best of my abilities and came up with the code attatched, however upon using the Logic Analyser on MPlabs my PWM is not pulisng :(.

Someone please have a looksy at my code and tell me where i am wrong... Thank You.

Code: Select all

;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F690. This file contains the basic code               *
;   building blocks to build upon.                                    *  
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    PWM.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:        Mulaudzi Phindulo Owen                                                  *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F690.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************


	list		p=16f690		; list directive to define processor
	#include	<P16F690.inc>		; processor specific variable definitions
	
	__CONFIG    _CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF
	errorlevel -203
	errorlevel -205
	errorlevel -302

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels 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	0x7D			; variable used for context saving
status_temp	EQU	0x7E			; variable used for context saving
pclath_temp	EQU	0x7F			; 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
	movf		PCLATH,w		; move pclath register into W register
	movwf		pclath_temp		; save off contents of PCLATH register


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

	movf		pclath_temp,w		; retrieve copy of PCLATH register
	movwf		PCLATH			; restore pre-isr PCLATH register contents	
	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

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;1. Disable the PWM pin (CCP1) output driver by
;setting the associated TRIS bit.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
				;init port
banksel TRISC;
clrf	TRISC;

banksel	TRISC;
bsf		TRISC,5;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;2. Set the PWM period by loading the PR2 register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

banksel	PR2;
movlw 	d'10'; ??10Khz??
movwf 	PR2;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;3. Configure the CCP module for the PWM mode
;by loading the CCP1CON register with the
;appropriate values.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			;PWM output configuration
banksel CCP1CON;
			;ECCP mode select bits
movlw	b'00111100';
movwf	CCP1CON;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;4. Set the PWM duty cycle by loading the CCPR1L
;register and DC1B<1:0> bits of the CCP1CON
;register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
			;50% Duty cycle @ 10KHz
banksel	CCPR1L;
movlw	d'50'; ??50%??
movwf	CCPR1L;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;5. Configure and start Timer2:
;• Clear the TMR2IF interrupt flag bit of the PIR1 register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel PIE1;
bsf		PIE1,TMR2IE;

banksel PIR1;
bcf		PIR1,TMR2IF;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;• Set the Timer2 prescale value by loading the 
;T2CKPS bits of the T2CON register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
						;1:4 prescalar
banksel	T2CON;
bcf		T2CON,T2CKPS0;
bcf		T2CON,T2CKPS1;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;• Enable Timer2 by setting the TMR2ON bit of 
;the T2CON register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel	T2CON;
bsf		T2CON, TMR2ON;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;6. Enable PWM output after a new PWM cycle has
;started:
;• Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

banksel	PIR1;
btfss	PIR1,TMR2IF;
goto $-1;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;• Enable the CCP1 pin output driver by clearing 
;the associated TRIS bit.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel	TRISC;
bsf		TRISC,5;
banksel	PSTRCON;
bsf		PSTRCON,STRC;

goto main;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

END                       ; directive 'end of program'



skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: Setting Up PWM For PIC-16f690

Post by skimask » Sat Apr 26, 2014 5:31 pm

Follow your code...
What happens right after main?
What happens right before the "END"?
I ignore "one post wonders".

Ozwurld
Posts: 13
Joined: Sat Apr 26, 2014 9:06 am

Re: Setting Up PWM For PIC-16f690

Post by Ozwurld » Sun Apr 27, 2014 12:25 am

cant believe i ddnt notice that, Thank You. Atleast now the Logic analyser shows 1 peak.. its a start.


//Present state of code!

Code: Select all


;**********************************************************************
;   This file is a basic code template for assembly code generation   *
;   on the PIC16F690. This file contains the basic code               *
;   building blocks to build upon.                                    *  
;                                                                     *
;   Refer to the MPASM User's Guide for additional information on     *
;   features of the assembler (Document DS33014).                     *
;                                                                     *
;   Refer to the respective PIC data sheet for additional             *
;   information on the instruction set.                               *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:       PWM.asm                                           *
;    Date:                                                            *
;    File Version:                                                    *
;                                                                     *
;    Author:        Mulaudzi Phindulo Owen                                                  *
;    Company:                                                         *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files Required: P16F690.INC                                      *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;**********************************************************************


   list      p=16f690      ; list directive to define processor
   #include   <P16F690.inc>      ; processor specific variable definitions
   
   __CONFIG    _CP_OFF & _CPD_OFF & _BOR_OFF & _PWRTE_ON & _WDT_OFF & _INTRC_OSC_NOCLKOUT & _MCLRE_ON & _FCMEN_OFF & _IESO_OFF
   errorlevel -203
   errorlevel -205
   errorlevel -302

; '__CONFIG' directive is used to embed configuration data within .asm file.
; The labels 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   0x7D         ; variable used for context saving
status_temp   EQU   0x7E         ; variable used for context saving
pclath_temp   EQU   0x7F         ; 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
   movf      PCLATH,w      ; move pclath register into W register
   movwf      pclath_temp      ; save off contents of PCLATH register


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

   movf      pclath_temp,w      ; retrieve copy of PCLATH register
   movwf      PCLATH         ; restore pre-isr PCLATH register contents   
   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

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;1. Disable the PWM pin (CCP1) output driver by
;setting the associated TRIS bit.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel	TRISC;
clrf	TRISC;	;init


banksel TRISC;
bsf     TRISC,5;
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;2. Set the PWM period by loading the PR2 register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

banksel PR2;
movlw   d'10'; ??10Khz??
movwf	PR2;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;3. Configure the CCP module for the PWM mode
;by loading the CCP1CON register with the
;appropriate values.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         ;PWM output configuration
banksel CCP1CON;
         ;ECCP mode select bits
movlw   b'00101100';
movwf   CCP1CON;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;4. Set the PWM duty cycle by loading the CCPR1L
;register and DC1B<1:0> bits of the CCP1CON
;register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
         ;50% Duty cycle @ 10KHz
banksel   CCPR1L;
movlw   	d'50'; ??50%??
movwf   CCPR1L;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;5. Configure and start Timer2:
;• Clear the TMR2IF interrupt flag bit of the PIR1 register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel PIE1;
bsf      PIE1,TMR2IE;

banksel PIR1;
bcf      PIR1,TMR2IF;
banksel	TMR2IF;
clrf	TMR2IF;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;• Set the Timer2 prescale value by loading the 
;T2CKPS bits of the T2CON register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
                  ;1:4 prescalar
banksel   T2CON;
bcf      T2CON,T2CKPS0;
bcf      T2CON,T2CKPS1;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;• Enable Timer2 by setting the TMR2ON bit of 
;the T2CON register.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel   T2CON;
bsf      T2CON, TMR2ON;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;6. Enable PWM output after a new PWM cycle has
;started:
;• Wait until Timer2 overflows (TMR2IF bit of the PIR1 register is set).
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

banksel   PIR1;
btfss   PIR1,TMR2IF;
goto $-1;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
;• Enable the CCP1 pin output driver by clearing 
;the associated TRIS bit.
;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
banksel   TRISC;
bcf      TRISC,5;

goto 0x00;

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

END                       ; directive 'end of program'


skimask
Support Volunteer
Posts: 1244
Joined: Sun Nov 09, 2008 2:11 am
Location: Minot, N.D.

Re: Setting Up PWM For PIC-16f690

Post by skimask » Sun Apr 27, 2014 1:49 am

No...

Why are you effectively RESETting the PIC at the end of the code after each loop?

PWM is it's own module. It's an independent module. Once it's set up, it runs...and runs...and runs...and runs...until it gets changed, reset, modified, turned off, etc.etc.etc.
I ignore "one post wonders".

Ozwurld
Posts: 13
Joined: Sat Apr 26, 2014 9:06 am

Re: Setting Up PWM For PIC-16f690

Post by Ozwurld » Sun Apr 27, 2014 5:48 am

Well i kept getting this error from the simulator
CORE-E0002: Stack under flow error occurred from instruction at 0x000800
after the program reached

Code: Select all

END
.. i believe thats an overflow or something...... i read online somewhere that i should put a

Code: Select all

goto $
right before end to keep the program in an infinite loop...??? so im gonna do that..

Post Reply