News:

PROTON pic BASIC Compilers for PIC, PIC24, dsPIC33

Main Menu

dsPIC33CK128MP202 Timer1 interrupt demo

Started by top204, Nov 14, 2021, 04:57 PM

Previous topic - Next topic

top204

Because I have just altered, and corrected, the interrupt vectors with the dsPIC33xxCK devices, I created a few sample programs to test them, so I thought I would share some of them. The program below is a simple Timer1 interrupt that will interrupt every 100ms, and output text to a serial terminal every 1 second:

'
'   /\\\\\\\\\
'  /\\\///////\\\
'  \/\\\     \/\\\                                                 /\\\          /\\\
'   \/\\\\\\\\\\\/        /\\\\\     /\\\\\\\\\\     /\\\\\\\\   /\\\\\\\\\\\  /\\\\\\\\\\\  /\\\\\\\\\
'    \/\\\//////\\\      /\\\///\\\  \/\\\//////    /\\\/////\\\ \////\\\////  \////\\\////  \////////\\\
'     \/\\\    \//\\\    /\\\  \//\\\ \/\\\\\\\\\\  /\\\\\\\\\\\     \/\\\         \/\\\        /\\\\\\\\\\
'      \/\\\     \//\\\  \//\\\  /\\\  \////////\\\ \//\\///////      \/\\\ /\\     \/\\\ /\\   /\\\/////\\\
'       \/\\\      \//\\\  \///\\\\\/    /\\\\\\\\\\  \//\\\\\\\\\\    \//\\\\\      \//\\\\\   \//\\\\\\\\/\\
'        \///        \///     \/////     \//////////    \//////////      \/////        \/////     \////////\//
'                                  Let's find out together what makes a PIC Tick!
'
' Timer1 interrupt demo for a dsPIC33CK128MP202 device
'
' Written for the Positron16 compiler by Les Johnson
'
    Device = 33CK128MP202
    Declare Xtal = 120
'
' USART1 setup
'
    Declare Hserial_Baud = 9600
    Declare HRSOut1_Pin = PORTB.0
'
' PPS output defines for a dsPIC33CK128MP202 device
'
$define PPS_OutPin_RB0  cOut_Pin_RP32
$define PPS_OutPin_RB1  cOut_Pin_RP33
$define PPS_OutPin_RB2  cOut_Pin_RP34
$define PPS_OutPin_RB3  cOut_Pin_RP35
$define PPS_OutPin_RB4  cOut_Pin_RP36
$define PPS_OutPin_RB5  cOut_Pin_RP37
$define PPS_OutPin_RB6  cOut_Pin_RP38
$define PPS_OutPin_RB7  cOut_Pin_RP39
$define PPS_OutPin_RB8  cOut_Pin_RP40
$define PPS_OutPin_RB9  cOut_Pin_RP41
$define PPS_OutPin_RB10 cOut_Pin_RP42
$define PPS_OutPin_RB11 cOut_Pin_RP43
$define PPS_OutPin_RB12 cOut_Pin_RP44
$define PPS_OutPin_RB13 cOut_Pin_RP45
$define PPS_OutPin_RB14 cOut_Pin_RP46
$define PPS_OutPin_RB15 cOut_Pin_RP47
'
' Create some variables for the demo
'
    Dim Global_wElapsed As Word
    Dim Global_Wordout As Word

'-------------------------------------------------------------------------
$define Timer1_Flag() IFS0bits_T1IF                 ' The Timer1 flag

'-------------------------------------------------------------------------
$define Timer1_IntEnable() IEC0bits_T1IE = 1        ' Enable the Timer1 interrupt
$define Timer1_IntDisable() IEC0bits_T1IE = 0       ' Disable the Timer1 interrupt

'-------------------------------------------------------------------------
$define Timer1_Start() T1CONbits_TON = 1            ' Start Timer1
$define Timer1_Stop() T1CONbits_TON = 0             ' Stop Timer1

'-------------------------------------------------------------------------
Main:
    IntOsc_120MHz()                                 ' Setup the device to operate at 120MHz using its internal oscillator

    Global_Wordout = 0
    Global_wElapsed = 0

    PPS_Unlock()                                    ' Unlock the PPS peripheral
    PPS_Output(PPS_OutPin_RB0, cOut_Fn_U1TX)        ' Set the appropriate PPS pin for the USART1 peripheral TX

    HRSOutLn "Start "                               ' Transmit text so we know the device is operating at the correct speed for testing

    Timer1_Enable()                                 ' Configure and enable Timer1 and its interrupt

    Do                                              ' Create a loop that does nothing
       DelayCS 1
    Loop

'-------------------------------------------------------------------------
' Timer1 interrupt handler and enabler
' Input     : None
' Output    : None
' Notes     : Wraps the ISR handler within a procedure
'             This means that if the ISR is in a library,
'             it will only ever be included into the program if it is enabled
'
' The interrupt handler increments and transmits a decimal value serially every second
'
Proc Timer1_Enable()
    TMR1 = 0                                    ' Clear Timer1
    PR1 = %1000001000110010                     ' Period = 100ms with a 120MHz oscillator
    T1CON = %1000001000110010                   ' TCKPS 1:256
    Timer1_Flag() = 0                           ' Reset the Timer1 flag
    Timer1_IntEnable()                          ' Enable the interrupt
'
' The interrupt handler is inside the procedure:
'
Isr- T1Interrupt
    Inc Global_wElapsed                         ' Increment the count per interrupt
    If Global_wElapsed >= 10 Then               ' Has the 100ms interrupt fired 10 times?
        Global_wElapsed = 0                     ' Yes. So reset the count per interrupt variable
        HRSOutLn "Timer1 = ", Dec Global_Wordout ' Transmit the time count to a serial terminal
        Inc Global_Wordout                      ' Increment the time count
    EndIf
    Timer1_Flag() = 0                           ' Reset the Timer1 interrupt flag
EndIsr-
EndProc

'----------------------------------------------------------------------------------
' Set the internal oscillator to 120MHz
' Input     : None
' Output    : None
' Notes     : None
'
Proc IntOsc_120MHz()
    CLKDIV = %0011000000000001
    PLLFBD = 120
    OSCTUN = $00
    PLLDIV = %0000000001000001
    ACLKCON1 = $0101
    APLLFBD1 = 150
    APLLDIV1 = $41
    Write_OSCCON($0101)
'
' Wait for the Clock switch to finish
'
    While OSCCONbits_OSWEN <> 0 : Wend
    While OSCCONbits_LOCK <> 1 : Wend
EndProc

'----------------------------------------------------------------------------------
' Set the fuses for internal oscillator on a dsPIC33CK128MP202 device
'
    Config FSEC     = BWRP_OFF, BSS_DISABLED, BSEN_OFF, GWRP_OFF, GSS_DISABLED, CWRP_OFF, CSS_DISABLED, AIVTDIS_OFF
    Config FOSCSEL  = FNOSC_FRC, IESO_OFF
    Config FOSC     = POSCMD_NONE, OSCIOFNC_ON, FCKSM_CSECMD, PLLKEN_OFF, XTCFG_G0, XTBST_ENABLE
    Config FWDT     = SWDTPS_PS2147483648, RCLKSEL_LPRC ', FWDTEN_ON_SW
    Config FPOR     = BISTDIS_DISABLED
    Config FICD     = ICS_PGD1, JTAGEN_OFF, NOBTSWP_DISABLED
    Config FDMT     = DMTDIS_OFF
    Config FDEVOPT  = ALTI2C1_ON, ALTI2C2_ON, ALTI2C3_ON, SMBEN_STANDARD, SPI2PIN_PPS
    Config FALTREG  = CTXT1_OFF, CTXT2_OFF, CTXT3_OFF, CTXT4_OFF

charliecoutas

Thanks Les,  can use this as well!

Charlie