ECCP with a 16f1829??

Status
Not open for further replies.

yfx4

Member
I am trying to set up my ECCP1 on the 16f1829 to output single waveform PWM to two pins--right now P1A and P1B. I get a good build (other than bank errors) and the Pickit3 says it programmed. I am working on my turn signal controller--just experimenting now.

https://www.electro-tech-online.com/threads/stuck-planning-my-turn-signal-module.123061/

Attached is the schematic for my basic test circuit and my rudimentary code. I started with 16f1827TEMP.asm since there are no 1829 templates available. I am trying to make the 2 leds light up at 30% duty cycle, then go bright to 90% for the button press, then dim again when the button is released. Right now I get NOTHING. What did I miss please???

Thank you!!!!!!

Code:
;******************************************************************************
;   This file is a basic code template for code generation on the             *
;   PIC16F1827. 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.                                                *
;                                                                             *
;   Refer to the respective data sheet for additional                         *
;   information on the instruction set.                                       *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Filename:        16f1829 flash.asm                                       *
;    Date:                                                                    *
;    File Version:                                                            *
;                                                                             *
;    Author:                                                                  *
;    Company:                                                                 *
;                                                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Files Required: P16F1829.INC                                             *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Notes:                                                                   *
;                                                                             *
;******************************************************************************
;                                                                             *
;    Revision History:                                                        *
;                                                                             *
;******************************************************************************


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

;------------------------------------------------------------------------------
;
; CONFIGURATION WORD SETUP
;
;
;------------------------------------------------------------------------------    

    __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_ON & _CP_OFF & _CPD_OFF & _BOREN_OFF & _CLKOUTEN_ON & _IESO_OFF & _FCMEN_OFF
    __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_OFF & _BORV_19 & _LVP_OFF

;------------------------------------------------------------------------------
; VARIABLE DEFINITIONS
;
; Available Data Memory divided into Bank 0-15.  Each Bank may contain 
; Special Function Registers, General Purpose Registers, and Access RAM 
;
;------------------------------------------------------------------------------

    CBLOCK 0x20 ; Define GPR variable register locations
	
    ENDC

SW1	Equ	4			;Button on RBx

;------------------------------------------------------------------------------
; EEPROM INITIALIZATION
;
; The 16F1827 has 256 bytes of non-volatile EEPROM, starting at address 0xF000
; 
;------------------------------------------------------------------------------

DATAEE    ORG  0xF000
    DE    "MCHP"  ; Place 'M' 'C' 'H' 'P' at address 0,1,2,3

;------------------------------------------------------------------------------
; RESET VECTOR
;------------------------------------------------------------------------------

    ORG     0x0000            ; processor reset vector
    PAGESEL START
    GOTO    START             ; When using debug header, first inst.
                              ; may be passed over by ICD2.  

;------------------------------------------------------------------------------
; INTERRUPT SERVICE ROUTINE
;------------------------------------------------------------------------------

    ORG      0x0004

;------------------------------------------------------------------------------
; USER INTERRUPT SERVICE ROUTINE GOES HERE
;------------------------------------------------------------------------------

; Note the 16F1827 family automatically handles context restoration for 
; W, STATUS, BSR, FSR, and PCLATH where previous templates for 16F families
; required manual restoration.  Shadow registers store these SFR values, and
; shadow registers may be modified since they are readable and writable for
; modification to the context restoration.    

    RETFIE                    ; return from interrupt
    
;------------------------------------------------------------------------------
; MAIN PROGRAM
;------------------------------------------------------------------------------

START
;set up intosc
	MOVLW	b'01111000'            	; SET INTERNAL OSCILLATOR FREQUENCY
			;'0-------'	PLL OFF
			;'-1111---'	16MHz CLOCK SPEED, SEE DATASHEET PAGE 71
			;'-0000---'	31KHz CLOCK SPEED
			;'------00'	SYSTEM CLOCK SET BITS TO INTOSC--set in config so actual value irrelevant
	MOVWF	OSCCON    		; INITIALIZE clock
 

;*********set up ports and pins

; may need to use APFCON0 OR APFCON1 registers to set function of ECCP pins


	CLRF	PORTA		; PortA all low
	CLRF	PORTB		; PortB all low
	CLRF	PORTC		; PortC all low
	
   	MOVLW 	b'00000000'	;
   	MOVWF 	TRISA		;PORTA outputs
	MOVWF 	TRISC		;PORTC outputs


   	MOVLW 	b'11111111'	;set PortB as inputs
   	MOVWF 	TRISB

;********setup ECCP1**********

	MOVLW	b'00011010'	;  
				;'00------'		CCP1 timer 2
				;'--01----'		CCP2 timer 4
				;'----10--'		CCP3 timer 6
				;'------10'		CCP4 timer 6
	MOVWF	CCPTMRS		; Set Pwm timer sources  page 237

	MOVLW	b'11111001'
	MOVWF	PR2		; 249


	MOVLW	b'00000111'	; 
	MOVWF	T2CON		; Prescaler 1:16 (bits 0-1) and postscaler 1:1 (bits 3-6)

	MOVLW	b'01001010'	; bits 2-10 of 10 bit pwm for 30% duty cycle
	MOVWF	CCPR1L		; 
	
	MOVLW	b'00111100'	;  
				;'00------'		Single Output ECCP
				;'--11----'		PWM Low side bits
				;'----1100'		Set ECCP pins P1A,C,B,D active high
				;
	MOVWF	CCP1CON		; Set Pwm Mode ECCP1  page 236
				;						  
	BSF	T2CON,TMR2ON	; Enable PWM
	GOTO	Loop		; Reset -> init



	

; 1/Fosc = Tosc  (1/ 16 Mhz = 6.25 x 10^-8)
; PWM Period = [(PRx)+1] * 4 * Tosc * TMR2 prescale value
; PWM Period = [249+1] * 4 * 6.25 x 10^-8 * 16
; PWM Period =  .001sec = 1000Hz


;The following steps should be taken when configuring the CCP module for PWM operation.
;1. Set the PWM period by writing to the PR2 register.
;2. Set the PWM duty cycle by writing to the CCPRxL register and CCP1<4:5> bits of CCPxCON register.
;3. Set the TMRx prescale value and enable Timerx by writing to TxCON register.
;4. Configure the CCPx module for PWM operation.

;------------------------------------------------------------------------------
; PLACE USER PROGRAM HERE
;------------------------------------------------------------------------------


Loop	btfsc	PORTB,	SW1
	call	bright
	btfss	PORTB,	SW1
	call	dim
	goto	Loop

bright
	MOVLW	b'11100000'	; set bits 9-2 for 90% duty cycle
	MOVWF	CCPR1L
	return

dim
	MOVLW	b'01001010'	; set bits 9-2 for 30% duty cycle
	MOVWF	CCPR1L
	return

	END

; That's all folks!
 

Attachments

  • schematic.jpg
    52.2 KB · Views: 663
I have tried running it through MPSIM and i am not getting any values loaded into OSCCON or CCPR1L when stepping through and watching the Special Function Registers window. The TRISx and CCPTMRS registers are not changing either. I tried using the hex value from the memory map (datasheet) as well as the 'common name' as referenced in the .inc file. This is confusinating.

Is anybody out there??????
 
Last edited:
And it looks like the comments on the CCPTMRS has the CCPx assignments upside down. TMR6 is assigned to CCP1, but only TMR2 is initialized. Try a clrf CCPTMRS to start with, and you know that it's TMR2 for sure, then build out from there.
 
Last edited:
THANK YOU!!!!!!~!

I see I was also selecting the banks incorrectly to load the values. Once I fixed that I got values loaded into the registers as they should be. I have not checked the timer setup yet. I will. THANK YOU!

How can I (or can I) look at the PWM output on the pins in a simulator to ensure the output is set up properly?? Is there a free 'oscilloscope' simulator add on (if that would be the correct tool to look at the output of P1A, etc)??

Scott
 
I fixed your code banking and tried it last night nic is right about the timer I been looking at the data sheet and it got late so i didn't post any thing

When i get home I'll post what I did if you want.

There a logic Analyzer in mplab that you can use it's under View at the bottom.
 
Last edited:
nickelflippr. Thank you for the heads up on the timers. I found it too. It is like proofreading my own essay, I miss obvious stuff. ADD what? :-} OOOOh look at the birdie.......

be80be. Thanks for looking at the banking. I'd love to see how you did it. I am sure it is much more elegant than what I did. Thank you again.

I'll test it on the hardware tonight after work.

Scott

Code:
START
;set up intosc
	BANKSEL OSCCON
	MOVLW	b'01111011'            	; SET INTERNAL OSCILLATOR FREQUENCY
			;'0-------'	PLL OFF
			;'-1111---'	16MHz CLOCK SPEED, SEE DATASHEET PAGE 71
			;'-0000---'	31KHz CLOCK SPEED
			;'------00'	SYSTEM CLOCK SET BITS TO INTOSC--set in config so actual value irrelevant
	MOVWF	OSCCON    	;	 INITIALIZE clock
 

;*********set up ports and pins

; may need to use APFCON0 OR APFCON1 registers to set function of ECCP pins
	BANKSEL PORTA 		;
	CLRF 	PORTA 		;Init PORTA
	BANKSEL LATA 		;Data Latch
	CLRF 	LATA 		;
	BANKSEL ANSELA 		;
	CLRF 	ANSELA 		;digital I/O
	BANKSEL TRISA 		;
	MOVLW 	B'00111000' ;Set RA<5:3> as inputs
	MOVWF 	TRISA 		;and set RA<2:0> as outputs
				; This is just to see bits get loaded. I dont use them.

	BANKSEL PORTB 		;
	CLRF 	PORTB 		;Init PORTB
	BANKSEL LATB 		;Data Latch
	CLRF 	LATB 		;
	BANKSEL ANSELB 		;
	BSF 	ANSELB,4 		;Analog Input PortB
	BANKSEL TRISB 		;
	MOVLW 	B'11111111' ;Set PORTB as inputs
	MOVWF 	TRISB 		;
						;
	
	BANKSEL PORTC
   	CLRF 	PORTC 		;Init PORTC
	BANKSEL LATC 		;Data Latch
	CLRF 	LATC 		;
	BANKSEL ANSELC 		;
	CLRF 	ANSELC 		;digital I/O
	BANKSEL TRISC 		;
	MOVLW 	B'00000000' ;Set PORTC as outputs
	MOVWF 	TRISC 		;
						;
	


;********setup ECCP1**********
	BANKSEL	CCPTMRS
	CLRF 	CCPTMRS 
	MOVLW	b'10100100'	;  
			;'10------'		CCP4 timer 6
			;'--10----'		CCP3 timer 6
			;'----01--'		CCP2 timer 4
			;'------00'		CCP1 timer 2
	MOVWF	CCPTMRS		 ;Set Pwm timer sources  page 237

	BANKSEL	PR2
	MOVLW	b'11111001'
	MOVWF	PR2		; 249

	BANKSEL	T2CON
	MOVLW	b'00000111'	; 
	MOVWF	T2CON		; Prescaler 1:16 (bits 0-1) and postscaler 1:1 (bits 3-6)

	BANKSEL CCPR1L
	MOVLW	b'01001010'	; bits 2-10 of 10 bit pwm for 30% duty cycle
	MOVWF	CCPR1L		; 

	BANKSEL	CCP1CON
	MOVLW	b'00111100'	;  
				;'00------'		Single Output ECCP
				;'--11----'		PWM Low side bits
				;'----1100'		Set ECCP pins P1A,C,B,D active high
				;
	MOVWF	CCP1CON		; Set Pwm Mode ECCP1  page 236

	BANKSEL	T2CON			;						  
	BSF		T2CON,TMR2ON	; Enable PWM
	
	GOTO	Loop		; Reset -> init



	

; 1/Fosc = Tosc  (1/ 16 Mhz = 6.25 x 10^-8)
; PWM Period = [(PRx)+1] * 4 * Tosc * TMR2 prescale value
; PWM Period = [249+1] * 4 * 6.25 x 10^-8 * 16
; PWM Period =  .001sec = 1000Hz


;The following steps should be taken when configuring the CCP module for PWM operation.
;1. Set the PWM period by writing to the PR2 register.
;2. Set the PWM duty cycle by writing to the CCPRxL register and CCP1<4:5> bits of CCPxCON register.
;3. Set the TMRx prescale value and enable Timerx by writing to TxCON register.
;4. Configure the CCPx module for PWM operation.

;------------------------------------------------------------------------------
; PLACE USER PROGRAM HERE
;------------------------------------------------------------------------------


Loop
	BANKSEL	PORTB
	btfsc	PORTB,	SW1
	call	bright
	BANKSEL	PORTB
	btfss	PORTB,	SW1
	call	dim
	goto	Loop

bright
	BANKSEL	CCPR1L
	MOVLW	b'11100000'	; set bits 9-2 for 90% duty cycle
	MOVWF	CCPR1L
	return

dim
	BANKSEL	CCPR1L
	MOVLW	b'01001010'	; set bits 9-2 for 30% duty cycle
	MOVWF	CCPR1L
	return

	END

; That's all folks!
 
I get a nice PWM output on CCP1 (the same as P1A and RC5) which corresponds to the ECCPA signal. I get nothing on the other P1x pins as it should. I will play with the PSTR1CON register to steer the outputs to different pins.
Now to test it with the hardware.
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…