PIC16f870 PWM module not working(Assembly Code)

Status
Not open for further replies.

Cantafford2

Newbie level 2
Joined
Oct 3, 2016
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
21
Hey,

I'm trying to set up the CCP1CON module of the PIC16f870 microcontroller in PWM mode. I wrote this simple code that's supposed to send a 100% duty cycle on the CCP1CON(RC2) pin. I have attached a dc motor interfaced with a L293D driver to test the duty cycle's value. I have calculated the values for the registers using this PWM calculator:

https://www.micro-examples.com/public/microex-navig/doc/097-pwm-calculator.html

This is the code:
Code:
#include "p16F870.inc"

; CONFIG
; __config 0xFF3A
__CONFIG _FOSC_HS & _WDTE_OFF & _PWRTE_OFF & _CP_OFF & _BOREN_OFF & _LVP_OFF & _CPD_OFF & _WRT_ALL

cblock 20h
  d1
  d2
  d3
endc


RES_VECT  CODE    0x0000            ; processor reset vector
    GOTO    START                   ; go to beginning of program

; TODO ADD INTERRUPTS HERE IF USED

MAIN_PROG CODE                      ; let linker place main program

START
  
    bsf 03h, 5 ; select bank1
    bcf 03h, 6
    movlw b'00000000' ; PORTB made as output
    movwf 86h
    call _pwm_init
    movlw h'01' ;direction assigned for the motor
    movwf 06h
  
loop ; loop forever
  
  
  
  
goto loop

  
_delay_1s
        movlw    0x2D
    movwf    d1
    movlw    0xE7
    movwf    d2
    movlw    0x0B
    movwf    d3
_delay_1s_0
    decfsz    d1, f
    goto    $+2
    decfsz    d2, f
    goto    $+2
    decfsz    d3, f
    goto    _delay_1s_0
        return
  
_pwm_init
    bcf 87h, 2 ;make RC2 bit an output
    movlw b'11001111'
    movwf 92h   ;PR2
    bcf 03h, 5 ; select bank0
         bcf 03h, 6
    movlw b'00000111'
    movwf 12h   ;T2CON
    movlw b'01100111'
    movwf 15h   ;CCPR1L
    movlw b'00111100'
    movwf 17h   ;CCP1CON
    return
    END

My CCP1CON(RC2) pin won't initialise. It stays in 0 logic no matter what duty cycle I send. What am I missing? I calculated a pwm frequency of 1.5khz and a duty cycle of 100%

This is a screenshot of the schematic in Proteus Simulator.
https://i64.tinypic.com/1tmwqb.png
 

You make the code difficult to follow!

Tip 1:
as you seem to be using Microchip's assembler, use the banksel macro and defined names to select the bank/register/bit you want to use. For example, change:
Code:
        bsf 03h, 5 ; select bank1
    bcf 03h, 6
    movlw b'00000000' ; PORTB made as output
    movwf 86h
to
Code:
 banksel TRISB
movlw 0
movwf TRISB
note that 'clrf TRISB' is easier!

Tip 2:
Avoid using '$' as a destination, it isn't portable to other PIC families.
Instead, add a label and 'goto' it instead.

Incidentally, 100% PWM means constantly ON! I suspect the simulator doesn't show that properly.

Brian.
 

Hey. Thanks for answering. The simulator shows 100% duty cycle fine. Worked for several other projects. Also I have tried different duty cycles to no avail.
About how the code looks well I will re-write it in the form that you said and post it here after .
 

What about MCLR ? Is it enabled or disabled ? If enabled then you need to pull it up to 5V using 10k resistor.
 

What about MCLR ? Is it enabled or disabled ? If enabled then you need to pull it up to 5V using 10k resistor.

The schematic shows it has a 10K pull-up to +5V and the 16F870 does not have configurable reset.

Brian.
 

The simulator shows 100% duty cycle fine.

If you are just simulating in Proteus then it will not work. It works fine for 100% duty cycle but not for others. I think you need a Core i7 system to simulate it in real time else it will just appear as flickering LED. Proteus can't simulate the LED model so fast. Test it in hardware.
 

Status
Not open for further replies.

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…