Lab X1 and 16F877 examples for beginners


Closed Thread
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585

    Lightbulb Lab X1 and 16F877 examples for beginners

    I figured this would be a good way to get back into the swing of things. I've had this X1 for years and still kept on using breadboards for all testing.

    Lab X1 jumpers:
    JP1=n/a (unused)
    JP2=2-3 (up)
    JP3=2-3 (up)
    A=1-2 (down)
    B=1-2 (down)
    C=2-3 (up)

    These examples are essentially for beginners and serve only as a starting point. I try not to assume anything and hope to include everything you need to have your program start on the 1st try.

    Code:
    '*****************************************************************
    '*  Name     : LAB X1 LCD.BAS              Version : 1.0         *
    '*  Author   : Demon                Date    : 2012-01-06  *
    '*  Task     : Display 2 lines of text                           *
    '*  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                    *
    '*           : MeLabs PM Assembler                               *
    '*****************************************************************
    'define  LOADER_USED 1
    @ DEVICE PIC16F877, HS_OSC, WDT_OFF, PWRT_ON, BOD_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, PROTECT_OFF
    
    define  OSC 20
    
    DEFINE  LCD_DREG      PORTD         ' Set LCD data port
    DEFINE  LCD_DBIT      0             ' 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      8             ' 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
    
    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
    
        PAUSE   1000                    ' Wait for LCD to initalize
        LCDOUT  $FE,1,"LAB X1 LCD"
        LCDOUT  $FE,$C0,"PIC 16F877"
        end
    Robert
    Last edited by Demon; - 4th October 2016 at 17:21.

  2. #2
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Lab X1 and 16F877 examples for beginners

    Code:
    '*****************************************************************
    '*  Name     : LAB X1 LEDs.BAS             Version : 1.0         *
    '*  Author   : Demon                Date    : 2012-01-06  *
    '*  Task     : Blink LEDs in sequence                            *
    '*  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                    *
    '*           : MeLabs PM Assembler                               *
    '*****************************************************************
    'define  LOADER_USED 1
    @ DEVICE PIC16F877, HS_OSC, WDT_OFF, PWRT_ON, BOD_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, PROTECT_OFF
    
    define  OSC 20
    
    TRISD = %00000000                   ' Set all port D pins to output
    ADCON1 = 7                          ' A/D off, all digital
    
    LED1 var PORTD.0                    ' LEDs, starting from the right
    LED2 var PORTD.1
    LED3 var PORTD.2
    LED4 var PORTD.3
    LED5 var PORTD.4
    LED6 var PORTD.5
    LED7 var PORTD.6
    LED8 var PORTD.7
    
        PORTD = %00000000               ' Turn off all LEDs
    
    CYCLE:
        TOGGLE LED1                     ' Turn 1st LED ON
        pause 100                       ' Short delay
        TOGGLE LED1                     ' Turn LED back off
        pause 100
    
        TOGGLE LED2
        pause 100
        TOGGLE LED2
        pause 100
    
        TOGGLE LED3
        pause 100
        TOGGLE LED3
        pause 100
    
        TOGGLE LED4
        pause 100
        TOGGLE LED4
        pause 100
    
        TOGGLE LED5
        pause 100
        TOGGLE LED5
        pause 100
    
        TOGGLE LED6
        pause 100
        TOGGLE LED6
        pause 100
    
        TOGGLE LED7
        pause 100
        TOGGLE LED7
        pause 100
    
        TOGGLE LED8
        pause 100
        TOGGLE LED8
        pause 100
    
        GOTO    CYCLE
        end
    Last edited by Demon; - 4th October 2016 at 17:21.

  3. #3
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Lab X1 and 16F877 examples for beginners

    Code:
    '*****************************************************************
    '*  Name     : LAB X1 LEDs 2.BAS           Version : 1.0         *
    '*  Author   : Demon                Date    : 2012-01-06  *
    '*  Task     : Blink LEDs back and forth                         *
    '*  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                    *
    '*           : MeLabs PM Assembler                               *
    '*****************************************************************
    'define  LOADER_USED 1
    @ DEVICE PIC16F877, HS_OSC, WDT_OFF, PWRT_ON, BOD_ON, LVP_OFF, CPD_OFF, WRT_OFF, DEBUG_OFF, PROTECT_OFF
    
    define  OSC 20
    
    TRISD = %00000000                   ' Set all port D pins to output
    ADCON1 = 7                          ' A/D off, all digital
    
    WSLOOP  var byte                    ' Loop counter
    
        PORTD = %00000001               ' Turn 1st LED on
        pause 100                       ' Short delay
    
    CYCLE:
        for wsloop = 1 to 7             ' Turn on next LED from right to left
            PORTD = PORTD << 1
            pause 100
        next
        for wsloop = 1 to 7             ' Turn on next LED from left to right
            PORTD = PORTD >> 1
            pause 100
        next
    
        GOTO    CYCLE
        end
    Last edited by Demon; - 4th October 2016 at 17:21.

  4. #4
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Lab X1 and 16F877 examples for beginners

    By now you should have a good understanding of configuring your software and hardware for a 16F877 on the Lab X1.

    Check in your PBP folder, there are many advanced examples under Lab-X \ LABX1 \ PBP (folder location may vary with newer releases of PBP).

    Have fun!

    Robert

  5. #5
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Lab X1 and 16F877 examples for beginners

    To use MPASM Assembler, use this config:

    Code:
    @ __config _HS_OSC & _WDT_OFF & _PWRTE_ON & _BODEN_ON & _LVP_OFF & _CPD_OFF & _WRT_ENABLE_OFF & _DEBUG_OFF & _PWRTE_OFF

    Open 16F877.INC in PBP folder and comment out CONFIG for MPASM like this:

    Code:
            NOLIST
        ifdef PM_USED
            LIST
            include 'M16F87x.INC'   ; PM header
            device  pic16F877, xt_osc, wdt_on, pwrt_on, lvp_off, protect_off
            XALL
            NOLIST
        else
            LIST
            LIST p = 16F877, r = dec, w = -302
            INCLUDE "P16F877.INC"   ; MPASM  Header
           ; __config _XT_OSC & _WDT_ON & _PWRTE_ON & _LVP_OFF & _CP_OFF
            NOLIST
        endif
            LIST

    Don't forget to include the config in all your 16F877 programs now that the default setting is commented out.
    Last edited by Demon; - 8th January 2012 at 03:52.

  6. #6
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    Did you find this post helpful? Yes | No

    Default Re: Lab X1 and 16F877 examples for beginners

    Here's an example of elapsed time using DT's Interrupts:
    http://darreltaylor.com/DT_INTS-14/intro.html

    Interrupt include files are available here:
    http://darreltaylor.com/DT_INTS-14/downloads.htm

    Elapsed include file available here:
    http://www.picbasic.co.uk/forum/show...7473#post17473


    NOTE: You must use MPASM Assembler to use DT Interrupt routine.


    Code:
    '*****************************************************************
    '*  Name     : LAB X1 Timer Interrupt.BAS  Version : 1.0         *
    '*  Author   : Demon                Date    : 2012-01-06  *
    '*  Task     : Display elapsed time on LCD 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
    
    DEFINE  LCD_DREG      PORTD         ' Set LCD data port
    DEFINE  LCD_DBIT      0             ' 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      8             ' 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
    
        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
    
    wsave   VAR BYTE    $70 SYSTEM      ' Required by Instant Interrupt routine
    
        PAUSE   1000                    ' Wait for LCD to initialize
        LCDOUT  $FE,1,"LabX1 16F877 Tim Int"
    
    '---[INT - interrupt includes]--------------------------------------------------
    INCLUDE "DT_INTS-14.bas"        ; Base Interrupt System
    INCLUDE "ReEnterPBP.bas"        ; Include if using PBP interrupts
    INCLUDE "Elapsed_INT.bas"       ; Elapsed Time interrupts
    
    '---[INT - interrupt definition]------------------------------------------------
    ASM
    INT_LIST  macro    ; IntSource,        Label,  Type, ResetFlag?
            INT_Handler   TMR1_INT,  _ClockCount,   PBP,  yes
        endm
        INT_CREATE                  ; Creates the interrupt processor
    ENDASM
    
    @   INT_ENABLE  TMR1_INT        ; Enable Timer 1 Interrupts  
    
        GOSUB ResetTime             ' Reset Time to  0d-00:00:00.00
        GOSUB StartTimer            ' Start the Elapsed Timer
    
    Main:
        IF SecondsChanged = 1 THEN  
            SecondsChanged = 0
            LCDOUT $FE,$C0, DEC Days,"d-",DEC2 Hours,":",DEC2 Minutes,":",DEC2 Seconds
        ENDIF
        GOTO Main
    
        end
    Last edited by Demon; - 4th October 2016 at 17:22. Reason: Added note

  7. #7
    Join Date
    Jan 2005
    Location
    Montreal, Quebec, Canada
    Posts
    2,585


    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: 2126
Size:  44.6 KB
    Last edited by Demon; - 4th October 2016 at 17: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