Quantcast
Channel: MEL PICBASIC Forum
Viewing all articles
Browse latest Browse all 4745

50mS counter on Timer0 (18F4550 on Lab X1)

$
0
0
Attachment 7795

Code:

'***************************************************************************
'*  Name    : RHTimer0.pbp                                                *
'*  Author  : Demon                                                      *
'*  Date    : Apr 15 2015                                                *
'*  Version  : 1.0                                                        *
'*  Notes    : 50mS counter on Timer0                                      *
'*  Hardware : PIC 18F4550, 20mhz crystal                                  *
'*            MeLabs U2 Programmer v4.32                                  *
'*            Lab X1 board                                                *
'*  Software : PIC Basic Pro v2.60C                                        *
'*            MicroCode Studio Plus v2.2.1.1                              *
'*            MPASM v5.46                                                *
'***************************************************************************
'--- if you use these, you must comment the ones in the .inc file ---
@  __CONFIG    _CONFIG1L, _PLLDIV_5_1L & _CPUDIV_OSC1_PLL2_1L & _USBDIV_2_1L
@  __CONFIG    _CONFIG1H, _FOSC_HSPLL_HS_1H
@  __CONFIG    _CONFIG2L, _PWRT_OFF_2L & _BOR_ON_2L & _BORV_1_2L & _VREGEN_ON_2L
@  __CONFIG    _CONFIG2H, _WDT_OFF_2H
@  __CONFIG    _CONFIG3H, _CCP2MX_ON_3H & _PBADEN_OFF_3H & _LPT1OSC_OFF_3H & _MCLRE_ON_3H
@  __CONFIG    _CONFIG4L, _STVREN_ON_4L & _LVP_OFF_4L & _ICPRT_OFF_4L & _XINST_OFF_4L

;--- Oscillator speed ----------------------------------------------------------

DEFINE OSC 48

;--- Setup Registers -----------------------------------------------------------

ADCON1 = %00001111                  ' A/D CONTROL REGISTER 1
'  Bit 7-6 Unimplemented: Read as ‘0’
'  Bit 5 VCFG1: Voltage Reference Configuration bit (VREF- source)
'              1 = VREF- (AN2)
'              0 = VSS
'  Bit 4 VCFG0: Voltage Reference Configuration bit (VREF+ source)
'              1 = VREF+ (AN3)
'              0 = VDD
'  Bit 3-0 PCFG3:PCFG0: A/D Port Configuration Control bits:
'              1111 = All digital on Port A
'                      See datasheet for table

INTCON2 = %00000000                ' INTERRUPT CONTROL REGISTER 2
'  Bit 7 RBPU: PORTB Pull-up Enable bit
'              1 = All PORTB pull-ups are disabled
'              0 = PORTB pull-ups are enabled by individual port latch values
'  Bit 6 INTEDG0: External Interrupt 0 Edge Select bit
'              1 = Interrupt on rising edge
'              0 = Interrupt on falling edge
'  Bit 5 INTEDG1: External Interrupt 1 Edge Select bit
'              1 = Interrupt on rising edge
'              0 = Interrupt on falling edge
'  Bit 4 INTEDG2: External Interrupt 2 Edge Select bit
'              1 = Interrupt on rising edge
'              0 = Interrupt on falling edge
'  Bit 3 Unimplemented: Read as ‘0’
'  Bit 2 TMR0IP: TMR0 Overflow Interrupt Priority bit
'              1 = High priority
'              0 = Low priority
'  Bit 1 Unimplemented: Read as ‘0’
'  Bit 0 RBIP: RB Port Change Interrupt Priority bit
'              1 = High priority
'              0 = Low priority

T0CON = %10000011                  ' TIMER0 CONTROL REGISTER
'  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

;--- Setup Interrupts ----------------------------------------------------------

INCLUDE "DT_INTS-18.bas"            ' Base Interrupt System
INCLUDE "ReEnterPBP-18.bas"        ' PBP interrupts

ASM
INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
        INT_Handler  TMR0_INT,  _EndTimer,    PBP,  yes
    endm
    INT_CREATE                      ; Creates the interrupt processor
ENDASM

;--- Setup Port directions -----------------------------------------------------

    TRISA = %00000000
    TRISB = %11110000              ' Set columns to input, rows to output
    TRISC = %00000000
    TRISD = %00000000
    TRISE = %00000000

;--- Pins ----------------------------------------------------------------------

Column1    VAR PORTB.4
TimerLed    VAR PORTD.1

;--- Variables (Constants) -----------------------------------------------------

Row1        CON %00001110          ' Set selected row to VSS, others to VDD

;--- Initialize variables ------------------------------------------------------

    LOW TimerLed
    PORTB = Row1                    ' Set selected row to VSS, others to VDD

;--- Subroutines ---------------------------------------------------------------

goto ProgramStart                  ' Jump over sub-routines

;---- Start Timer0 -------------------------------------------------------------
StartTimer:
    TMR0H = 109                    ' 50 mS Timer
    TMR0L = 144
    HIGH TimerLed           
@ INT_ENABLE  TMR0_INT              ; enable Timer 0 interrupts
RETURN

;---- Stop Timer0 --------------------------------------------------------------
EndTimer:
    LOW TimerLed           
@ INT_RETURN

;---- Scan only top left button on 4 x 4 keypad --------------------------------
KeypadInput:
    IF PORTB.4 = 0 THEN            ' Column 1
        GOSUB StartTimer
        WHILE PORTB.4 = 0: WEND
    ENDIF
RETURN

;--- Program Start -------------------------------------------------------------

ProgramStart:
    GOSUB KeypadInput              ' Scan button

    GOTO ProgramStart
END

Robert
Attached Files

Viewing all articles
Browse latest Browse all 4745

Trending Articles