• AVR Freaks

Hot!PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344

Page: 12 > Showing page 1 of 2
Author
The alchmiste
Starting Member
  • Total Posts : 70
  • Reward points : 0
  • Joined: 2018/03/14 02:55:48
  • Location: France
  • Status: offline
2020/10/21 23:59:20 (permalink)
0

PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344

Hi there,
 
I'm facing some dificulties to get the PWM output with my code below.
Is anyone able to provide me some tips on where should I look to solve it?
 
I've used the timer 1 to generate a bunch of ADC while waiting for PWM periode to end and I've used the timer 2
to generate the PWM periode but still can't understand why I do not have any output on Pin RC1.
 
I've tried the onboard debuging but the value on ADRESH register is frozeen to 0x02 while I'm turning the potentiometer I've set on Pin RC0 but don't seem to make any change on this register dedicated to analog pin scan.
 
                                                               

    list p=16f15344 , r=dec
 #include <p16f15344.inc>


; CONFIG1
; __config 0x3E8C
 __CONFIG _CONFIG1, _FEXTOSC_OFF & _RSTOSC_HFINT32 & _CLKOUTEN_OFF & _CSWEN_ON & _FCMEN_ON
; CONFIG2
; __config 0x279C
 __CONFIG _CONFIG2, _MCLRE_ON & _PWRTE_ON & _LPBOREN_OFF & _BOREN_NSLEEP & _BORV_LO & _ZCD_OFF & _PPS1WAY_OFF & _STVREN_OFF
; CONFIG3
; __config 0xFFFF
 __CONFIG _CONFIG3, _WDTCPS_WDTCPS_31 & _WDTE_OFF & _WDTCWS_WDTCWS_7 & _WDTCCS_SC
; CONFIG4
; __config 0x1FE0
 __CONFIG _CONFIG4, _BBSIZE_BB64K & _BBEN_OFF & _SAFEN_ON & _WRTAPP_OFF & _WRTB_OFF & _WRTC_OFF & _WRTSAF_OFF & _LVP_OFF
; CONFIG5
; __config 0x3FFF
 __CONFIG _CONFIG5, _CP_OFF

; _RSTOSC_HFINT32 -> permet l'activation de l'oscillateur interne a une frequence de 32Mhz soit 31,25 nanoS/osccillation-> 25 interuption / seconde avec: postdiviseur=10, prediviseur=128, seuil timer2=249,
; _LVP_OFF -> permet la programmation a faible tenssion
; _CP_OFF -> Ne cache pas le code 
; _WDT_OFF -> Desactive le Watch Dog


; Definition des differentes constantes du programme
echantionnage EQU 0x003F;

    cblock 0X20
compteurposition : 1
valeuranalogique : 0x10
valeuraddition : 2
    endc

;*******************************************************************************
; Reset Vector
;*******************************************************************************

    org 00H ; processor reset vector
    goto INIT ; go to beginning of program

; TODO INSERT ISR HERE

;*******************************************************************************
; Routine d'interuption
;*******************************************************************************
    org 0x004 ; interrupt vector location
    
 
    banksel PIR1
    btfss PIR1,ADIF
    goto intpwm
    call intad
    banksel PIR1
    bcf PIR1,ADIF
       
intpwm 
    banksel PIR4
    btfss PIR4,TMR2IF
    goto restorereg
    call inttmr2
    banksel PIR4
    bcf PIR4,TMR2IF
    bcf PIR6,CCP2IF
    
 restorereg

retfie
  
    
    

inttmr2 

banksel valeuranalogique
movlw valeuranalogique
BANKSEL FSR0
movwf FSR0
BANKSEL valeuraddition
clrf valeuraddition
BANKSEL valeuraddition+1
clrf valeuraddition+1

addition
BANKSEL INDF0
movfw INDF0
BANKSEL valeuraddition
addwf valeuraddition+1
btfsc STATUS,C
BANKSEL valeuraddition
incf valeuraddition
BANKSEL FSR0
incf FSR0
btfss FSR0,4
goto addition
    

rrf valeuraddition
rrf valeuraddition+1
  
rrf valeuraddition
rrf valeuraddition+1
    
rrf valeuraddition
rrf valeuraddition+1
    
rrf valeuraddition
rrf valeuraddition+1
    
; // On ajoute l'offset pour obtenir un minimum de 1ms
    
movlw B'11110111' ; 61,75=> 61,75X4 = 247
ADDwf valeuraddition+1
btfsc STATUS,C
incf valeuraddition
    
; // On met les bit fractionnaire à jour
    
BANKSEL CCPR2L
bcf CCPR2L,6
bcf CCPR2L,7
BANKSEL valeuraddition
btfsc valeuraddition+1,0
BANKSEL CCPR2L
bsf CCPR2L,6
BANKSEL valeuraddition
btfsc valeuraddition+1,1
BANKSEL CCPR2L
bsf CCPR2L,7
    
; // recuperer la valeur entier sur 8 bits
    
    
rrf valeuraddition+1
rrf valeuraddition
rrf valeuraddition+1
rrf valeuraddition

BANKSEL valeuraddition
movfw valeuraddition+1
BANKSEL CCPR2H
movwf CCPR2H
return




intad
    
BANKSEL compteurposition
movfw compteurposition
andlw 0x0F
BANKSEL valeuranalogique
addlw valeuranalogique
BANKSEL FSR0
movwf FSR0
BANKSEL ADRESH
movfw ADRESH
BANKSEL INDF0
movwf INDF0
BANKSEL compteurposition
incf compteurposition
    
return

    
INIT
    
BANKSEL INTCON
movlw b'01000000' ; GIE et PEIE registre d'autorisation interuption general et periferique
movwf INTCON
   
BANKSEL PIE1
movlw b'00000001'
movwf PIE1

BANKSEL PIE4
movlw b'00000010'
movwf PIE4
 
BANKSEL PIE6
movlw b'00000010'
movwf PIE6
    
                  ; // Configuration convertisseur A/D
    
    
movlw B'00000000' ;// on initalise la pin 0 du port C en mode output
banksel TRISC
movwf TRISC
    
;banksel analogc0 ;// on paramettre le pin 0 du port C en mode analogique
bsf analogc0

    
movlw b'00100000' 
    ;// justification a gauche bit 7 à 0 pour prendre seulement les 
    ;bits de poids fort
    ;selection de l'horloge, ici 010=F/32 si 110=F/64 ici ca donne 500khz si 000 = FOSC/2
    ; minimum 010 à 32Mhz -> Tad=1µs
    ;les 2 derniers bit pour les references de tensions: 11 = VREF+ is connected 
    ;to internal Fixed Voltage Reference (FVR) module(1)
banksel ADCON1
movwf ADCON1

movlw b'01000000' 
    ;// les 6 plus hauts bit utilisés pour selectionner les pin ou seront réalisé 
    ;les aquisition, ici 010000=RC0 port C pin 0
    ;le bit 1 sert a demarrer une converssion lorsque il est mis a 1 is se remet 
    ;à 0 des que la converssion es terminé
    ;le bità 0 sert a demarrer le module ADC donc debut l'aquisition
    
banksel ADCON0
movwf ADCON0
    
    
           ;// Configuration CCP1 (capture avec trigger sur convertisseur A/N timer 1)

 
banksel RC0PPS 
movlw 0x15 ;On associe CCP1 input a RC0 ( pour l'aquisition Analogique)
movwf RC0PPS

banksel CCP1CON
movlw 0x9B ;// bit 7 pour activer bit 3 à 0: 1011 = Compare mode: output will 
 ;pulse 0-1-0; Clears TMR1
movwf CCP1CON

 ;// on charge le timer avec la periode d'un echantionage
BANKSEL CCPR1H
movlw high echantionnage
movwf CCPR1H

BANKSEL CCPR1L
movlw low echantionnage
movwf CCPR1L 

banksel T1CLK ;// On associe la reference d'horloge au timer 1 avec F= Fosc/4 car mode compare avec AD
movlw B'00000001'
movwf T1CLK
 
banksel T1CON ;// on demarre le timer 1
bsf T1CON,0
 
 
 ;// Configuration CCP2 (PWM avec interruption timer 2 apres PR2 debordement)


banksel RC1PPS 
movlw 0x0A ;On associe CCP2 input a RC1 ( pour la sortie du signal PWM)
movwf RC1PPS

banksel CCP2CON
movlw 0x9F ;// bit 7 pour activer CCP , bit 4 pour une justification à gauche
            ;bit 3 à 0: 1111 = PWM mode (Timer2 as the timer source)
movwf CCP2CON
 
banksel T2PR ;// on assigne la periode du signal PWM
movlw 0xFF
movf T2PR
  
 
banksel T2CON ;// on assigne au timer 2 le prescaler a 128
movlw B'01110000'
movwf T2CON
 
banksel T2CON ;// on demarre le timer 2
bsf T2CON,7

BANKSEL PIR1
clrf PIR1
     
BANKSEL PIR4
clrf PIR4
     
BANKSEL PIR6
clrf PIR6

BANKSEL INTCON
bsf INTCON,7
     
    
end

 
Thanks
Thealchimiste
#1

22 Replies Related Threads

    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/22 00:39:08 (permalink)
    0
    Your INIT code simply falls off the end of program memory.
    If you want it to do nothing (and just service interrupts), you will need to put a
    GOTO $
    instruction there to stop it running through the rest of program memory.
     
    Note, "END" is not a processor instruction, and has no effect on execution.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #2
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/25 03:26:59 (permalink)
    0
    Hi Ric,
     
    Thanks, but, when I put as you suggested the 2 lines as below:
     
    Bonjour
    GOTO $
     
    The PC get stuck to " GOTO $ " and don't want to move on the onboard debuging do you know why?
     
    Thanks
    Thealchimiste
    #3
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/25 03:32:56 (permalink)
    0
    Of course normal execution stops at the GOTO $
    What do you expect to happen next?
    Have you set a breakpoint in the interrupt service code?
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #4
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/25 03:43:23 (permalink)
    0
    Thanks for your quick feedback,
     
    1) I was execting an infinit loop from " Bonjour" to " GOTO$ " while there is no interupt, maybe I've missunderstood the concept?
     
    2) For the breakpoint in the interupt service, If no one of the 2 interupt (ADIF or TMR2IF) are identified, there is a simple return from interupt :
        org 0x004 ; interrupt vector location
        
     
        banksel PIR1
        btfss PIR1,ADIF
        goto intpwm
        call intad
        banksel PIR1
        bcf PIR1,ADIF
           
    intpwm 
        banksel PIR4
        btfss PIR4,TMR2IF
        goto restorereg
        call inttmr2
        banksel PIR4
        bcf PIR4,TMR2IF
        bcf PIR6,CCP2IF
        
     restorereg

    retfie

    #5
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/25 03:48:51 (permalink)
    0
    The alchmiste
    1) I was execting an infinit loop from " Bonjour" to " GOTO$ " while there is no interupt, maybe I've missunderstood the concept?

    "Bonjour" is a label, not an instruction, so you won't see it go there.
    GOTO $ just continually jumps to itself.
     

    2) For the breakpoint in the interupt service, If no one of the 2 interupt (ADIF or TMR2IF) are identified, there is a simple return from interupt :

    So?
    If they are the only two you have enabled, that will never happen.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #6
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/25 04:09:26 (permalink)
    0
    Ok Ric I got the point for the goto, to see it work I should have put "GOTO $-1"
    For the breakpoints:
    - The ADIF should hapen since I've enabeled the CCP1 with ADC compare triggering the ADC interupt flag
    - The TMR2 as well since there is a the PR2 value specified as the limit
    shouldn't it?
     
    Thanks
    Thealchimiste
    #7
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/25 04:36:09 (permalink)
    +3 (3)
    You're trying to get too many things working all at once.
    Do it one step at a time. Try setting the Go/DONE bit once after your initialisation, and see if that triggers an ADC interrupt.
    Try doing something else to verify your CCP is working... e.g. toggle an LED.
    That's the secret to effective debugging, work with the minimum number of unknowns each step, so if it doesn't work you have a reasonable chance of guessing WHICH bit is not working.

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #8
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 13:43:07 (permalink)
    0
    Thanks Ric,
     
    It's on it's way to get solved, I'll post final code once finalized.
    Do you know what is going wrong with lines below to secure the memory area to 16 bytes for one variable 2 bytes for an other, it seems like no incrementation is hapening on the debugging :
    compteurposition : 1
    valeuranalogique : 0x10
    valeuraddition : 2
    #9
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 14:20:16 (permalink)
    0

    it seems like no incrementation is hapening on the debugging

    What exactly are you seeing that makes you say this?
    i.e. it's better to report the observed facts, not your intepretation of something you are seeing.
     
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #10
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 14:44:15 (permalink)
    0
    Here is the picture of the watches and the "valeuranalogique" is always at 0, that should not hapen...

    Attached Image(s)

    #11
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 14:46:41 (permalink)
    0
    I thought you meant the incrementing of addresses performed by CBLOCK.
    Are you talking about the value in the variable?
     
    post edited by ric - 2020/10/26 14:50:17

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #12
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 14:50:07 (permalink)
    0
    you're right, it's the position 0x21, I don't know how I could get the entire 16 byte shown on the watches?
    #13
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 14:51:33 (permalink)
    0
    As I remember, right click on the variable in the watch window, and select "Properties"
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #14
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 15:02:35 (permalink)
    0
    I've tried and searched somwhere else but the right click do not show properties

    Attached Image(s)

    #15
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/26 15:26:09 (permalink)
    0
    Did you try the last option "Display value column as" ?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #16
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/27 02:49:14 (permalink)
    0
    I found a way through file registers as on picture below:

     
     
    I still have one problem, when trying to copy value of "valeuraddition"  on CCPR2H register, it do not get updated.
    I've tryed to see if it is due to the size of the variable by creating a new variable VALEUR on only 1 byte, but do not make any change, otherwise, when I copy the ADRESH value on CCPR2H register it work. Do you know what is the issue? 
     
    thanks
    Thealchimiste

    Attached Image(s)

    #17
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/27 23:46:49 (permalink)
    0
    Hi Ric any idea on why CCPR2H register do not get updated with the value of "valeuraddition" ?
     
    Thanks
    Thealchimiste
    #18
    The alchmiste
    Starting Member
    • Total Posts : 70
    • Reward points : 0
    • Joined: 2018/03/14 02:55:48
    • Location: France
    • Status: offline
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/29 15:48:51 (permalink)
    0
    Anyone have an idea?

    Thanks
    The alchimiste
    #19
    ric
    Super Member
    • Total Posts : 28943
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: online
    Re: PWM signal in ASM with timer 1 for ADC and timer 2 for PWM PIC 16F15344 2020/10/29 15:59:37 (permalink)
    +2 (2)
    NO, I don't know why that isn't working.
    Are you able to manually change the value in the Watch window?
     
    n.b. I just spotted a typo in your init code in post#1
    banksel T2PR ;// on assigne la periode du signal PWM
    movlw 0xFF
    movf T2PR
     
    should be
    banksel T2PR ;// on assigne la periode du signal PWM
    movlw 0xFF
    movwf T2PR
     
    You're probalby getting away with it as that is the power on default value.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5