Menu

"Bit" variable references.

2024-11-12
2024-11-12
  • Ccin E Crout

    Ccin E Crout - 2024-11-12

    I have some code that reads serial data into a byte, certain "bits" of the byte contain various status flags. I then refer to the status flags using SerialByte.0 SerialByte.1 and so on. It struck me that this might be more understandable if the "bits" could be assigned to a readable name.

    I thought that Alias might be the command I should use but that seems to be for bytes rather than bits.

    Is it possible to do this using something like dim?
    Or #Define?

    Dim SerialByte As Byte
    Dim StatusReady As SerialByte.0
    Dim StatusError As SerialByte.1
    Dim StatusMotor As SerialByte.2
    Dim StatusOkBut As SerialByte.3
    Dim StatusUpBut As SerialByte.4
    Dim StatusDnBut As SerialByte.5
    Dim StatusLeBut As SerialByte.6
    Dim StatusRiBut As SerialByte.7
    

    Is the sort of thing I was imagining would be a neater way to code, rather than just referring to SerialByte.x every time, or having to have "bit" variables and assigning the value each time SerialByte is updated.

    Dim SerialByte As Byte
    Dim StatusReady As Bit
    Dim StatusError As Bit
    Dim StatusMotor As Bit
    Dim StatusOkBut As Bit
    Dim StatusUpBut As Bit
    Dim StatusDnBut As Bit
    Dim StatusLeBut As Bit
    Dim StatusRiBut As Bit
    
    Let StatusReady = SerialByte.0
    Let StatusError = SerialByte.1
    Let StatusMotor = SerialByte.2
    Let StatusOkBut = SerialByte.3
    Let StatusUpBut = SerialByte.4
    Let StatusDnBut = SerialByte.5
    Let StatusLeBut = SerialByte.6
    Let StatusRiBut = SerialByte.7
    
     
  • Ccin E Crout

    Ccin E Crout - 2024-11-12

    I think I've answered my own question.

    This seems to work, and the status "bits" appear to reference "SerialByte.x" in the .asm file as I was hoping.

    I realise the logic of the test program probably won't work, but I wanted to refer to all the variables, quickly.

    #Option Explicit
    #Chip 16F1825, 32
    
    #Config CP=On
    'Read protected
    
    Dim SerialByte As Byte
    #Define StatusReady  SerialByte.0
    #Define StatusError  SerialByte.1
    #Define StatusMotor  SerialByte.2
    #Define StatusOkBut  SerialByte.3
    #Define StatusUpBut  SerialByte.4
    #Define StatusDnBut  SerialByte.5
    #Define StatusLeBut  SerialByte.6
    #Define StatusRiBut  SerialByte.7
    
    Let SerialByte = 0
    
    Do
        Let SerialByte = SerialByte + 1
        If StatusReady = 1 Then
            Let StatusError = 0
        End If
        If StatusError = 1 Then
            Let StatusMotor = 0
        End If
        If StatusMotor = 1 Then
            Let StatusOkBut = 0
        End If
        If StatusOkBut = 1 Then
            Let StatusUpBut = 0
        End If
        If StatusUpBut = 1 Then
            Let StatusDnBut = 0
        End If
        If StatusDnBut = 1 Then
            Let StatusLeBut = 0
        End If
        If StatusLeBut = 1 Then
            Let StatusReady = 0
        End If
    Loop
    
    ;Program compiled by GCBASIC
    ;Need help? 
    ;  See the GCBASIC forums at http://sourceforge.net/projects/gcbasic/forums,
    ;  Check the documentation and Help at http://gcbasic.sourceforge.net/help/,
    ;or, email us:
    ;   w_cholmondeley at users dot sourceforge dot net
    ;   evanvennn at users dot sourceforge dot net
    
    ;********************************************************************************
    
    ;Set up the assembler options (Chip type, clock source, other bits and pieces)
     LIST p=16F1825, r=DEC
    #include <P16F1825.inc>
     __CONFIG _CONFIG1, _FCMEN_ON & _CLKOUTEN_OFF & _CPD_OFF & _CP_ON & _MCLRE_OFF & _WDTE_OFF & _FOSC_INTOSC
     __CONFIG _CONFIG2, _LVP_OFF & _PLLEN_OFF & _WRT_OFF
    
    ;********************************************************************************
    
    ;Set aside memory locations for variables
    SERIALBYTE                       EQU      32          ; 0x20
    
    ;********************************************************************************
    
    ;Vectors
        ORG 0
        pagesel BASPROGRAMSTART
        goto    BASPROGRAMSTART
        ORG 4
        retfie
    
    ;********************************************************************************
    
    ;Start of program memory page 0
        ORG 5
    BASPROGRAMSTART
    ;Call initialisation routines
        call    INITSYS
    
    ;Start of the main program
    ;Read protected
    ;Dim SerialByte As Byte
    ;Let SerialByte = 0
        clrf    SERIALBYTE
    ;Do
    SysDoLoop_S1
    ;Let SerialByte = SerialByte + 1
        incf    SERIALBYTE,F
    ;If StatusReady = 1 Then
        btfsc   SERIALBYTE,0
    ;Let StatusError = 0
        bcf SERIALBYTE,1
    ;End If
    ;If StatusError = 1 Then
        btfsc   SERIALBYTE,1
    ;Let StatusMotor = 0
        bcf SERIALBYTE,2
    ;End If
    ;If StatusMotor = 1 Then
        btfsc   SERIALBYTE,2
    ;Let StatusOkBut = 0
        bcf SERIALBYTE,3
    ;End If
    ;If StatusOkBut = 1 Then
        btfsc   SERIALBYTE,3
    ;Let StatusUpBut = 0
        bcf SERIALBYTE,4
    ;End If
    ;If StatusUpBut = 1 Then
        btfsc   SERIALBYTE,4
    ;Let StatusDnBut = 0
        bcf SERIALBYTE,5
    ;End If
    ;If StatusDnBut = 1 Then
        btfsc   SERIALBYTE,5
    ;Let StatusLeBut = 0
        bcf SERIALBYTE,6
    ;End If
    ;If StatusLeBut = 1 Then
        btfsc   SERIALBYTE,6
    ;Let StatusReady = 0
        bcf SERIALBYTE,0
    ;End If
    ;Loop
        goto    SysDoLoop_S1
    SysDoLoop_E1
    BASPROGRAMEND
        sleep
        goto    BASPROGRAMEND
    
    ;********************************************************************************
    
    ;Source: system.h (174)
    INITSYS
    ;asm showdebug This code block sets the internal oscillator to ChipMHz
    ;asm showdebug OSCCON type is 105 'Bit(SPLLEN) Or Bit(IRCF3) And NoBit(INTSRC) and ifdef Bit(IRCF3)
    ;equates to OSCCON = OSCCON AND b'10000111' & OSCCON = OSCCON OR b'11110000'
    ;= 32Mhz
    ;Set IRCF3 On
        banksel OSCCON
        bsf OSCCON,IRCF3
    ;Set IRCF2 On
        bsf OSCCON,IRCF2
    ;Set IRCF1 On
        bsf OSCCON,IRCF1
    ;Set IRCF0 Off
        bcf OSCCON,IRCF0
    ;Set SPLLEN On
        bsf OSCCON,SPLLEN
    ;asm showdebug _Complete_the_chip_setup_of_BSR_ADCs_ANSEL_and_other_key_setup_registers_or_register_bits
    ;Ensure all ports are set for digital I/O and, turn off A/D
    ;SET ADFM OFF
        bcf ADCON1,ADFM
    ;Switch off A/D Var(ADCON0)
    ;SET ADCON0.ADON OFF
        bcf ADCON0,ADON
    ;ANSELA = 0
        banksel ANSELA
        clrf    ANSELA
    ;ANSELC = 0
        clrf    ANSELC
    ;Set comparator register bits for many MCUs with register CM2CON0
    ;C2ON = 0
        banksel CM2CON0
        bcf CM2CON0,C2ON
    ;C1ON = 0
        bcf CM1CON0,C1ON
    ;
    ;'Turn off all ports
    ;PORTA = 0
        banksel PORTA
        clrf    PORTA
    ;PORTC = 0
        clrf    PORTC
        return
    
    ;********************************************************************************
    
    ;Start of program memory page 1
        ORG 2048
    ;Start of program memory page 2
        ORG 4096
    ;Start of program memory page 3
        ORG 6144
    
     END
    
     
    • Anobium

      Anobium - 2024-11-12

      This is genius!

      I have often wanted use aliases with BITs but ALIASing BITs does not work. I guess is should.

      Your method works.

      I shall put into the Help NOW!!

       
  • Anobium

    Anobium - 2024-11-12
     
  • Ccin E Crout

    Ccin E Crout - 2024-11-12

    It is something that I've been thinking about for a while now. I finally got around to checking the help pages for 'Alias' this morning and realised that wasn't going to work for bits in the way I wanted.

    As I was posting my question, I wondered if #Define might work. I thought it should. Gave it a try and hey presto.

    Glad you like it and I'm pleased to have helped..

     
    👍
    1

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.