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.

16F88 PWM and A/D conversion

Status
Not open for further replies.
Oh and Nigel , initialize is spelled with a Z.
lol NOT AN S

can anyone link me to the errata sheet for the midrange reference manual
 
does anyone know if this code works??
its from the microchip midrange manual..
its not working for me..
the pwm output pin does nothing.. not a blip and certainly no pwm..??..
Code:
CLRF CCP1CON ; CCP Module is off 
CLRF TMR2 ; Clear Timer2 
MOVLW 0x7F ; 
MOVWF PR2 ; 
MOVLW 0x1F ; 
MOVWF CCPR1L ; Duty Cycle is 25% of PWM Period 
CLRF INTCON ; Disable interrupts and clear T0IF 
BSF STATUS, RP0 ; Bank1 
BCF TRISC, PWM1 ; Make pin output 
CLRF PIE1 ; Disable peripheral interrupts 
BCF STATUS, RP0 ; Bank0 
CLRF PIR1 ; Clear peripheral interrupts Flags 
MOVLW 0x2C ; PWM mode, 2 LSbs of Duty cycle = 10 
MOVWF CCP1CON ; 
BSF T2CON, TMR2ON ; Timer2 starts to increment 
; 
; The CCP1 interrupt is disabled, 
; do polling on the TMR2 Interrupt flag bit 
; 
PWM_Period_Match 
BTFSS PIR1, TMR2IF 
GOTO PWM_Period_Match 
; 
; Update this PWM period and the following PWM Duty cycle 
; 
BCF PIR1, TMR2IF
just thought i'd ask again
 
Hi Willi,

In that code you just posted, isn't PR2 in Bank 1 Sir? Did I miss the bank switch in your code?

Regards, Mike
 
Hi Willi,

Which PIC are you using? 16F88 or 16F684?

Also, shouldn't you be turning on TMR2 with a bsf T2CON,TMR2ON instruction somewhere? Oops, I see it... Excuse me...

Regards, Mike
 
Sorry i know its hard to follow but i am using the 16F628..
i just redid the .asm file ..
assembled it ..
reprogrammed the 628 .....
still nothing..damn
 
Hi Willi,

Yep, that banking stuff gets me all the time (grin)... It may seem silly, but I use something like this to keep track of banking on the 14-bit core PICs;

Code:
;
        CLRF    CCP1CON         ; CCP Module is off      |B0
        CLRF    TMR2            ; Clear Timer2           |B0
        MOVLW   0x7F            ;                        |B0
        MOVWF   PR2             ;                        |B1 <-- oops!
        MOVLW   0x1F            ; duty cycle is 25%      |B0
        MOVWF   CCPR1L          ; of PWM Period          |B0
        CLRF    INTCON          ; Disable interrupts     |B0
        BSF     STATUS, RP0     ; Bank1                  |B1
        BCF     TRISC, PWM1     ; Make pin output        |B1
        CLRF    PIE1            ; Disable peripheral inte|B1
        BCF     STATUS, RP0     ; Bank0                  |B0
;

Regards, Mike
 
cool thanks..!!
let me ask a dumb question ..
is the code as written able to run just once or should i loop through it..?
 
Hi Willi,

Sorry, just got back from an errand... I'll look for that code now... IIRC, I used Hyperterminal (which I do a lot) and I repeatedly pressed the left and right arrow keys to ramp up or ramp down the speed of a 12-vdc computer fan... A single PWM output was driving a TIP122 which was driving the fan... I was just experimenting... I haven't done anything tangible with PWM... Sorry...

Regards, Mike
 
Hi Willi,

I can't find that code... Bummer!!!

So, I dug out the Data Sheet and threw this code together... Let me burn it into a '628A and throw it into a board along with an LED... Oops! Just realized the Data Sheet is for a '628 and not a '628A so I better check for the differences... Hang in there... Or, feel free to disect this code and compare it to yours...

Regards, Mike

Code:
;******************************************************************

        org	0x0000

RESET	clrf    STATUS          ;                                 |B0
        clrf    PORTA           ; clear Port A data latches       |B0
        clrf    PORTB           ; clear Port B data latches       |B0
        movlw   h'07'           ;                                 |B0
        movwf   CMCON           ; turn comparator off             |B0
        bsf     STATUS,RP0      ; select bank 1                   |B1
        clrf    TRISA           ; port A all outputs              |B1
        clrf    TRISB           ; port B all outputs              |B1
        bcf     STATUS,RP0      ; select bank 0                   |B0
;
;  setup PWM (looks like CCPR1L uses Tosc while PR2 uses Tcyc)
;
        clrf    T2CON           ; TMR2 prescale:1                 |B0
        movlw   d'100'          ; same as (100*4)>>2              |B0
        movwf   CCPR1L          ; 100 usecs, 50% duty cycle?      |B0
        bsf     STATUS,RP0      ; select bank 1                   |B1
        movlw   d'200'-1        ; 200 1.0 usec 'ticks'            |B1
        movwf   PR2             ; Period=200 usecs, Freq=5.0 KHz  |B1
        bcf     STATUS,RP0      ; select bank 0                   |B0
        movlw   b'00001100'     ;                                 |B0
        movwf   CCP1CON         ; put CCP module in PWM mode      |B0
        bsf     T2CON,TMR2ON    ; turn on TMR2                    |B0
;
;  now test to see if the LED on RB3 is glowing at 50% brightness
;
LOOP    goto    LOOP            ; loop indefinately               |B0
;
;***************************************************************
 
Yep, no changes were required... And, it works... I tried 50%, 100%, 25%, and 10% duty cycles and I could see a difference, though I'm not sure I'm calculating duty cycle correctly...

Is this enough of an example to get yours going?

Regards, Mike

<added>

I should mention the '628A was running INTOSC at 4-MHz...
 
i changed movlw d'100'
to 50
and got 25% duty cycle
it was fifty ..
this is soooo cool...
thanks :wink:
 
Yeah, change the value in CCPR1L to any value between d'000' and d'200' and you'll effect changes in 0.5% increments...

If you change PR2 to d'100' then you'll have a 1 to 1 relationship with the CCPR1L register... CCPR1L values of d'001' through d'100' will be 1.0% increments (period=100-usecs, freq=10-KHz)...

If you change PR2 to d'255' then you'll have a CCPR1L range of d'000' to d'255', which would be a perfect match for the high 8-bits of the ADC module and a potentiometer...

Good luck... Have fun... Regards, Mike
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top