18f452 interrupt problem


Closed Thread
Results 1 to 4 of 4

Hybrid View

  1. #1
    Join Date
    Sep 2004
    Location
    Les Angles (FRANCE)
    Posts
    18

    Talking 18f452 interrupt problem

    HI,

    i'm trying to generate two oposite 16HZ square wave with a 18f452 but i'm lost

    here is the code i am trying to use:

    Code:
     '                                --------------
    '                                |   18F452   |
    '               MCLR/Vpp ------> |1         40| <-----> RB7/PGD 
    '                RA0/AN0 <-----> |2         39| <-----> RB6/PGC 
    '                RA1/AN1 <-----> |3         38| <-----> RB5/PGM 
    '          RA2/AN2/Vref- <-----> |4         37| <-----> RB4 
    '          RA3/AN3/Vref+ <-----> |5         36| <-----> RB3/CCP2* 
    '              RA4/TOCK1 <-----> |6         35| <-----> RB2/INT2 
    '       RA5/AN5/SS/LVDIN <-----> |7         34| <-----> RB1/INT1 
    '             RE0/RD/AN5 <-----> |8         33| <-----> RB0/INT0
    '             RE1/WR/AN6 <-----> |9         32| <-----  Vdd 
    '             RE2/CS/AN7 <-----> |10        31| <-----  Vss 
    '                    Vdd  -----> |11        30| <-----> RD7/PSP7 
    '                    VSS  -----> |12        29| <-----> RD6/PSP6 
    '              OSC1/CLK1  -----> |13        28| <-----> RD5/PSP5 
    '          OSC2/CLKO/RA6 <------ |14        27| <-----> RD4/PSP4 
    '        RC0/T1OSO/TICK1 <-----> |15        26| <-----> RC7/RX/DT 
    '        RC1/T1OSI/CCP2* <-----> |16        25| <-----> RC6/TX/CK 
    '               RC2/CPP1 <-----> |17        24| <-----> RC5/SDO 
    '            RC3/SCK/SCL <-----> |18        23| <-----> RC4/SD1/SDA 
    '               RD0/PSP0 <-----> |19        22| <-----> RD3/PSP3  
    '               RD1/PSP1 <-----> |20        21| <-----> RD2/PSP2       
    '                                --------------
    
    
    '------------------------------------------------------------------------------'
    '-- Clear All The Variables And Each Port Before Setting The TRIS Registers ---'
    '------------------------------------------------------------------------------'
     
    Clear    'Set all ram registers to zero
    PORTA = 0
    PORTB = 0                           
    PORTC = 0   
    PORTD = 0
    PORTE = 0
     
    '--------------------------- VSS VDD MCLR Ect. Pins ---------------------------'
     
    'MCLR        (Pin 1)
    'VDD         (Pin 11)
    'VSS         (Pin 12)
    'OSC/CLKIN   (Pin 13)
    'OSC2/CLKOUT (Pin 14)
    'VSS         (Pin 31)
    'Vdd         (Pin 32)
     
    '((OPTION_REG=127 ' ENABLE WEAK PULL UP RESISTORS ON PORTB))
     
    'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
    'XXXXXXXXXXXXXXXXXXXXXXXXXX Set The TRIS In/Out Pins XXXXXXXXXXXXXXXXXXXXXXXXXX'
    'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX BELOW XXX'    
    
    '-------------------- PORTA PINS --------------------'
    TRISA.0 = 1  '(pin 2) IN_LTC1966  
    TRISA.1 = 1  '(pin 3) NOT USED  
    TRISA.2 = 1  '(pin 4) NOT USED  
    TRISA.3 = 1  '(pin 5) NOT USED  
    TRISA.4 = 1  '(pin 6) NOT USED  
    TRISA.5 = 1  '(pin 7) NOT USED 
      
    '-------------------- PORTB PINS --------------------'
    TRISB.0 = 0  '(pin 33) LED_10MN  
    TRISB.1 = 1  '(pin 34) BP_10MN
    TRISB.2 = 0  '(pin 35) LED_5MN
    TRISB.3 = 1  '(pin 36) BP_5MN
    TRISB.4 = 0  '(pin 37) LED_3MN
    TRISB.5 = 1  '(pin 38) BP_3MN
    TRISB.6 = 1  '(pin 39) BP_STOP
    TRISB.7 = 1  '(pin 40) BP_TEST
     
    '-------------------- PORTC PINS --------------------'
    TRISC.0 = 0  '(pin 15) LED8
    TRISC.1 = 0  '(pin 16) LED9
    TRISC.2 = 0  '(pin 17) LED10
    TRISC.3 = 0  '(pin 18) LED11
    TRISC.4 = 0  '(pin 23) LED12
    TRISC.5 = 0  '(pin 24) LED13
    TRISC.6 = 0  '(pin 25) LED14
    TRISC.7 = 0  '(pin 26) LED15
     
    '-------------------- PORTD PINS --------------------'
    TRISD.0 = 0  '(pin 19) LED0
    TRISD.1 = 0  '(pin 20) LED1
    TRISD.2 = 0  '(pin 21) LED2
    TRISD.3 = 0  '(pin 22) LED3
    TRISD.4 = 0  '(pin 27) LED4
    TRISD.5 = 0  '(pin 28) LED5
    TRISD.6 = 0  '(pin 29) LED6
    TRISD.7 = 0  '(pin 30) LED7
    
    '-------------------- PORTE PINS --------------------'
    TRISE.0 = 0  '(pin 8)  SORTIE1
    TRISE.1 = 0  '(pin 9)  SORTIE2
    TRISE.2 = 1  '(pin 10) NOT USED 
    
    
    ;==========================================================================
    ;@ __CONFIG _CONFIG1H, _OSCS_ON_1H & _XT_OSC_1H ' use this one for 8 mhz and 
                                                    ' also declare the osc as 4mhz
    @ __CONFIG _CONFIG1H, _OSCS_OFF_1H & _HSPLL_OSC_1H ; use this one for 32 mhz
    @ __CONFIG _CONFIG2L, _BOR_OFF_2L & _BORV_20_2L & _PWRT_ON_2L
    @ __CONFIG _CONFIG2H, _WDT_ON_2H & _WDTPS_128_2H
    @ __CONFIG _CONFIG3H, _CCP2MX_OFF_3H
    @ __CONFIG _CONFIG4L, _STVR_OFF_4L & _LVP_OFF_4L & _DEBUG_OFF_4L
    @ __CONFIG _CONFIG5L, _CP0_OFF_5L & _CP1_OFF_5L & _CP2_OFF_5L & _CP3_OFF_5L
    @ __CONFIG _CONFIG5H, _CPB_OFF_5H & _CPD_OFF_5H
    @ __CONFIG _CONFIG6L, _WRT0_OFF_6L & _WRT1_OFF_6L & _WRT2_OFF_6L & _WRT3_OFF_6L
    @ __CONFIG _CONFIG6H, _WRTC_OFF_6H & _WRTB_OFF_6H & _WRTD_OFF_6H
    @ __CONFIG _CONFIG7L, _EBTR0_OFF_7L & _EBTR1_OFF_7L & _EBTR2_OFF_7L & _EBTR3_OFF_7L
    @ __CONFIG _CONFIG7H, _EBTRB_OFF_7H 
    
    
     DEFINE OSC 32     ' using HSPLL X 4 gives us OSC 32
    @ errorlevel -306 ; turn off the Crossing page boundary warnings 
    
     ADCON1 = 7  ' Set to Digital
     INTCON2.7 = 0 ' enable pull-up resistor
     INTCON2.2 = 1  ' TMR0 Overflow Interrupt Priority bit = 1 = High priority
    
    '-------------------------------------------------------------------------------
    '--- configuration du timer 0 via T0CON ---
    '-------------------------------------------------------------------------------
    
    'bit 7 TMR0ON: Timer0 On/Off Control bit
    '   1 = Enables Timer0
    '   0 = Stops Timer0
    'bit 6 T08BIT: Timer0 8-bit/16-bit Control bit
    '   1 = Timer0 is configured as an 8-bit timer/counter
    '   0 = Timer0 is configured as a 16-bit timer/counter
    'bit 5 T0CS: Timer0 Clock Source Select bit
    '   1 = Transition on T0CKI pin
    '   0 = Internal instruction cycle clock (CLKO)
    'bit 4 T0SE: Timer0 Source Edge Select bit
    '   1 = Increment on high-to-low transition on T0CKI pin
    '   0 = Increment on low-to-high transition on T0CKI pin
    'bit 3 PSA: Timer0 Prescaler Assignment bit
    '   1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler.
    '   0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
    'bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits
    '   111 = 1:256 prescale value
    '   110 = 1:128 prescale value
    '   101 = 1:64 prescale value
    '   100 = 1:32 prescale value
    '   011 = 1:16 prescale value
    '   010 = 1:8 prescale value
    '   001 = 1:4 prescale value
    '   000 = 1:2 prescale value
    
        T0CON = %00000010
    
    '-------------------------------------------------------------------------------
    '--- Déclaration des E/S ---
    '-------------------------------------------------------------------------------
    
        IN_TLC1966  var porta.0
        
        BP_TEST     var portb.7
        BP_STOP     var portb.6
        BP_3MN      var portb.5
        LED3MN      var portb.4
        BP_5MN      var portb.3
        LED5MN      var portb.2
        BP_10MN     var portb.1
        LED10MN     var portb.0
        
        LED_LOW     var portd
        
        LED_HIGH    var portc
        
        SORTIE1     var porte.0
        SORTIE2     var porte.1
    
    '-------------------------------------------------------------------------------
    '--- Déclaration des Variables ---
    '-------------------------------------------------------------------------------
    
        TEMPO       var word
        X           var byte
        AFFICHE_TEMPO   var byte
        START_TEMPO var bit
        
        TMR0RunOn   var word
    
        ' quartz => 8MHz + HSPLL ==> 32MHz
        ' Fosc/4 = 8MHz
        ' sortie1 = not sortie2 = 16HZ => t= 1/16 = 62.5ms => t/2 = 31250µs
        ' prescaler = 8 ==> TMR0 accuracy = 1µs
        ' preset TMR0(16bits) = $FFFF - (31250µs) = $FFFF - $7A12 = $85ED 
    
        TMR0Preset var word
        
        tmr0preset = $85ED
    
    '-------------------------------------------------------------------------------
    '--- Valeurs par défaut ---
    '-------------------------------------------------------------------------------
    
        led_low = 255       ' LED0 to LED7 off
        led_high = 255      ' LED8 to LED15 off
        led3mn = 1          ' LED3MN off
        led5mn = 1          ' LED5MN off
        led10mn = 1         ' LED10MN off
        
        'pour test
        sortie1 = 0
        sortie2 = 1
    '    sortie1 = 0
    '    sortie2 = 0
    
        start_tempo = 0
        
    '-------------------------------------------------------------------------------
    '--- Test à la mise sous tension ---
    '-------------------------------------------------------------------------------
        
        if bp_stop = 0 and bp_test = 0 then
            led_low = 254
            pause 200
            for x = 1 to 8
                led_low = led_low << 1
                pause 200
            next x    
            led_low = 255
            led_high = 254
            pause 200
            for x = 1 to 8
                led_high = led_high << 1
                pause 200
            next x
            led_high = 255
            low led3mn
            pause 200    
            high led3mn
            low led5mn
            pause 200    
            high led5mn
            low led10mn
            pause 200    
            high led10mn
        endif
        
    '-------------------------------------------------------------------------------
    '--- Début du programme ---
    '-------------------------------------------------------------------------------
    
    ' Initialise TMR0 Interrupts
    ' --------------------------
        
        gosub tictac    'for test
        
        Gosub Set_Timer ' Set the Timer for next 31.25mS Interrupt
        
        gosub tictac    'for test
        gosub tictac    'for test
        
        On Interrupt goto Tick_Count
        
        gosub tictac    'for test
        gosub tictac    'for test
        gosub tictac    'for test
        
        INTCON.5=1      ' Enable TMR0 Interrupts
        INTCON.6=1      ' Enable all unmasked Interrupts
        INTCON.7=1      ' Enable Global Interrupts
    
    DEBUT:
    
        gosub tictac    'for test
    
        if bp_3mn = 0 then
            tempo = 180
            low led3mn
            high led5mn
            high led10mn
            goto affichage_tempo
        endif
    
        if bp_5mn = 0 then
            tempo = 300
            high led3mn
            low led5mn
            high led10mn
            goto affichage_tempo
        endif
    
        if bp_10mn = 0 then
            tempo = 600
            high led3mn
            high led5mn
            low led10mn
            goto affichage_tempo
        endif
    
        if bp_test = 1 then goto debut
    
    LECTURE_LTC1966:
        
        ' lire entrée analogique
        ' afficher valeur LTC1966
        '...
        '...
        '...
        '...
    '    if bp_stop = 1 then goto lecture_ltc1966
        
        goto debut
    
    AFFICHAGE_TEMPO:
    
        affiche_tempo = tempo / 16
        start_tempo = 1
        '...
        '...
        '...
        '...
        goto debut
    
    TICTAC:
        led_high = 0
        pause 200
        led_high = 255
        pause 200
        return
        
    '-------------------------------------------------------------------------------
    '--- Gestion des interruptions ---
    '-------------------------------------------------------------------------------
    
    '
    ' Subroutine Loads TMR0 values (based on Melanie's olympic timer)
    ' ============================
    SET_TIMER:
        
        T0CON.7=0                   ' Stop the Clock
        tmr0runon.Highbyte=TMR0H    ' Load the Run-On (Over-Run) value (if any)
        tmr0runon.Lowbyte=TMR0L
        tmr0runon=tmr0preset+tmr0runon ' Calculate the New (adjusted) value for TMR0
    
        TMR0H=tmr0runon.Highbyte    ' Save new values to TMR0
        TMR0H=tmr0runon.Lowbyte
        T0CON.7=1                   ' Restart the Clock
    
        INTCON.2 = 0                'Reset TMR0's Interupt Flag
        
        Return
    
    '
    ' Timer Interrupt Handler
    ' =======================
    TICK_COUNT:
        
        Gosub Set_Timer             ' Set the Timer for next 31.25mS Interrupt
        
        toggle sortie1
        toggle sortie2
    
        Resume
        
        End
    note: TICTAC is only a test witch work only when i delete the line ON INTERRUPT.
    i think i mist something on TMR0 configuration but i did not find it.
    can someone help me?

    MERCI

    Bisous de France

    sorry for my english !!!!!!!!!!!!!!!!
    Jean-François

    F4CDH

  2. #2
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    A quick look and I see a couple thing missing.
    Code:
    ' Timer Interrupt Handler
    ' =======================
    DISABLE
    TICK_COUNT:
        
        Gosub Set_Timer             ' Set the Timer for next 31.25mS Interrupt
        
        toggle sortie1
        toggle sortie2
    
        Resume
    ENABLE
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Jul 2003
    Posts
    2,405


    Did you find this post helpful? Yes | No

    Default

    SET_TIMER needs work too. With the timer set to 16-bit read/write operation you need to write to TMR0H first, then TMR0L. Reading is just to opposite. Read TMR0L, then read TMR0H.

    And, do you really want TMR0H to be loaded with the low & then high bytes of tmr0runon?

    TMR0H=tmr0runon.Highbyte ' Save new values to TMR0
    TMR0H=tmr0runon.Lowbyte

    This wouldn't work anyhow, since you never write to TMR0L, which loads TMR0H..;o)
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  4. #4
    Join Date
    Sep 2004
    Location
    Les Angles (FRANCE)
    Posts
    18


    Did you find this post helpful? Yes | No

    Smile

    HI all,

    1- Mackrackit
    it's not working where you place them but it's working when i do :
    DEBUT:
    ENABLE
    ...
    ...
    ...
    DISABLE
    goto DEBUT

    2- Bruce
    indeed it was a 'typo' mistake i will write : TMR0L=tmr0runon.Lowbyte

    well, for now all is ok so i can go on

    thanks for helping
    Jean-François

    F4CDH

Members who have read this thread : 0

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts