Lab X1 and 16F877 examples for beginners


Results 1 to 7 of 7

Threaded View

  1. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    3,170


    Did you find this post helpful? Yes | No

    Default Re: Lab X1 and 16F877 examples for beginners

    Here's an external interrupt example using DT's Interrupts:

    Code:
    '********************************************************************
    '*  Name     : LAB X1 External Interrupt.BAS  Version : 1.0         *
    '*  Author   : Demon                   Date    : 2012-01-06  *
    '*  Task     : External interrupt from keypad using DT Interrupts   *
    '*  Hardware : PIC 16F877, 20mhz crystal                            *
    '*           : Lab X1 Experimental Board                            *
    '*           : MeLabs U2 Programmer v4.32                           *
    '*  Software : PIC Basic Pro v2.60C                                 *
    '*           : MicroCode Studio Plus v2.2.1.1                       *
    '*           : MPASM Assembler v3.50                                *
    '********************************************************************
    'DEFINE  LOADER_USED 1
    
    ' Comment out CONFIG in MPASM include (File: 16F877.INC in PBP folder).
    @ __config _HS_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_ENABLE_OFF & _DEBUG_OFF
    DEFINE  OSC 20
    
    ' Using only 4 bit bus to control LCD
    
    DEFINE  LCD_DREG      PORTD         ' Set LCD data port
    DEFINE  LCD_DBIT      4             ' Set starting data bit
    DEFINE  LCD_RSREG     PORTE         ' Set LCD register select port
    DEFINE  LCD_RSBIT     0             ' Set LCD register select bit
    DEFINE  LCD_EREG      PORTE         ' Set LCD enable port
    DEFINE  LCD_EBIT      1             ' Set LCD enable bit
    DEFINE  LCD_BITS      4             ' Set LCD bus size
    DEFINE  LCD_LINES     2             ' Set number of lines on LCD
    DEFINE  LCD_COMMANDUS 2000          ' Set command delay time in microseconds
    DEFINE  LCD_DATAUS    50            ' Set data delay time in microseconds
    
        TRISB = %00001111               ' Set port B pins to 4 output, 4 input
                                        ' B0 = External Interrupt
        TRISD = %00000000               ' Set all port D pins to output
        TRISE = %00000000               ' Set all port E pins to output
    
        ADCON1 = 7                      ' A/D off, all digital
        OPTION_REG.6 = 0                ' Sets interrupt trigger on falling edge
        OPTION_REG.7 = 0                ' Enable weak pull-up resistors
    
    wsave   VAR BYTE    $70 SYSTEM      ' Required by Instant Interrupt routine
    LED1    VAR     PORTD.0
    
        PAUSE   1000                    ' Wait for LCD to initalize
        LCDOUT  $FE,1,"LabX1 16F877 Ext Int"
        LCDOUT  $FE,$C0,"Any button on row 1"
    
        PORTD = %00000000               ' Clear LEDs
    
    '---[INT - interrupt includes]--------------------------------------------------
    INCLUDE "DT_INTS-14.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas"        ; Include if using PBP interrupts
    
    '---[INT - interrupt definition]------------------------------------------------
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler    INT_INT,  _ToggleLED1,   PBP,  yes
        endm
        INT_CREATE                  ; Creates the interrupt processor
    ENDASM
    
    @   INT_ENABLE   INT_INT        ; enable external (INT) interrupts
    
    Main:
    
        PORTB = %00000000               ' Set output on keypad
                                        '   to prevent floating pins
        GOTO Main
    
    '---[INT - interrupt handler]---------------------------------------------------
    ToggleLED1:
        TOGGLE LED1
    @ INT_RETURN
    
        end

    This is what it looks like on the Saleae Logic Analyser after 4 button presses:

    Name:  4 button presses.JPG
Views: 2319
Size:  44.6 KB
    Last edited by Demon; - 4th October 2016 at 16:22.

Members who have read this thread : 1

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