1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

Code Snippets

Discussion in 'Oshonsoft' started by bryan1, Oct 24, 2010.

  1. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,680
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    This thread will deal with code snippets for the Oshonsoft Basic Compiler and is a normal thread for members to add code snippets and other members are free to ask questions. When a member puts code snippets in this thread I can move them into the sticky which is locked to prevent unneeded posts not directly dealing with snippets.

    To convert a binary value from 0x001 to 0x270f to 0001 to 9999 decimal

    Code (text):

    Dim ascbfr3 As Byte  'used in convert routing
    Dim ascbfr2 As Byte  'used in convert routing
    Dim ascbfr1 As Byte  'used in convert routine
    Dim ascbfr0 As Byte  'used in convert routine

    binval = volts
    Gosub bin2asc

    bin2asc:
    ascbfr3 = binval / 1000
    temp3 = binval Mod 1000
    ascbfr2 = temp3 / 100
    temp3 = temp3 Mod 100
    ascbfr1 = temp3 / 10
    ascbfr0 = temp3 Mod 10
    'results are BCD so
    'convert to ASCII for LCD
    ascbfr3 = ascbfr3 Or 0x30
    ascbfr2 = ascbfr2 Or 0x30
    ascbfr1 = ascbfr1 Or 0x30
    ascbfr0 = ascbfr0 Or 0

    ' and to show on a lcd after it is set up

    Lcdout "Volts:", ascbfr3, ascbfr2, ".", ascbfr1, ascbfr0

    'this will put a decimal point in and the example shows a voltage reading to 2 decimal places

     
    Regards Bryan
     
    Last edited: Oct 26, 2010
  2. Transistorman

    Transistorman Member

    Joined:
    Jun 7, 2010
    Messages:
    390
    Likes:
    2
    Location:
    Indonesian
    what is
    Code (text):
    binval = volts
    for ?
     
  3. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    hi TT,
    binval is short for 'binary value', its the variable name thats use in the binary to ASCII conversion.

    So let binval = volts and then call the bin2asc subroutine.

    OK.?
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Transistorman

    Transistorman Member

    Joined:
    Jun 7, 2010
    Messages:
    390
    Likes:
    2
    Location:
    Indonesian

    ok , i understand
     
  6. bryan1

    bryan1 Well-Known Member

    Joined:
    Aug 4, 2004
    Messages:
    1,680
    Likes:
    46
    Location:
    Adelaide Hills Sth Oz
    Ok guys lets make a new rule for this sticky for code snippets, only post in here if you are putting in code snippets or the sticky will just become a normal thread and that isn't the point of it.

    Regards Bryan
     
  7. Transistorman

    Transistorman Member

    Joined:
    Jun 7, 2010
    Messages:
    390
    Likes:
    2
    Location:
    Indonesian
    a simple count operation code
    Code (text):

    Define ADC_CLOCK = 3  'default value is 3
    Define ADC_SAMPLEUS = 10  'default value is 20
    Define LCD_BITS = 4  'allowed values are 4 and 8 - the number of data
    Define LCD_DREG = PORTB
    Define LCD_DBIT = 0  '0 or 4 for 4-bit interface, ignored for 8-bit
    Define LCD_RSREG = PORTC
    Define LCD_RSBIT = 1
    Define LCD_EREG = PORTC
    Define LCD_EBIT = 3
    Define LCD_RWREG = PORTC  'set to 0 if not used, 0 is default
    Define LCD_RWBIT = 2  'set to 0 if not used, 0 is default
    Lcdinit

    Dim adc1 As Word
    Dim adc2 As Word
    Dim total As Word

    TRISB = %11110000
    TRISA = %11111111

    ADCON0 = 0xc0  'set A/D conversion clock to internal source
    ADCON1 = 1  'set PORTA pins as analog inputs
    High ADCON0.ADON  'turn on A/D converter module

    loop:
        Adcin 0, adc1
        Adcin 1, adc2
        total = adc1 + adc2
        Lcdcmdout LcdClear
        Lcdout #total
        WaitMs 1
        Goto loop
     
    the code will totalize the output of AN0 and AN1

    take a try :)
     
  8. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Conversion Routines

    Simple conversion subroutines.
    Useful for manipulating DS1307 RTC i/o values.

    Code (text):

    dim temp1 as byte
    dim ascbfr0 as byte
    dim ascbfr1 as byte
    dim units as byte
    dim tens as byte

    '*******

    'convert RTC packed bcd to a bin number for inc/dec usage
    'enter with temp1 holding the packed BCD byte
    'exit with temp1 holding the binary byte
    bcd2bin:
    units = temp1 And 0x0f
    tens = temp1 And 0xf0
    tens = ShiftRight(tens, 4)
    tens = tens * 10
    temp1 = tens + units
    Return                                            

    '*******

    'convert the bin number to packed bcd for RTC write
    'enter with temp1 holding the binary byte
    'exit with temp1 holding the packed BCD byte
    'also ascbfr1,ascbfr0 hold the ASCII value
    bin2bcd:
    tens = temp1 / 10
    units = temp1 Mod 10
    ascbfr1 = tens Or 0x30
    ascbfr0 = units Or 0x30
    tens = ShiftLeft(tens, 4)
    temp1 = tens Or units
    Return

    '*******

    'convert packed bcd i/o to ascii for lcd
    'enter with the packed BCD byte in temp1
    'on exit ascbfr1 and ascbfr0 hold the ASCII numbers
    bcd2asc:
    ASM:        movf temp1,w
    ASM:        andlw 0x0f
    ASM:        iorlw 0x30
    ASM:        movwf ascbfr0
    ASM:        movf temp1,w
    ASM:        swapf temp1,w
    ASM:        andlw 0x0f
    ASM:        iorlw 0x30
    ASM:        movwf ascbfr1
    Return                                            

     
     

    Attached Files:

    Last edited: Oct 28, 2010
  9. ericgibbs

    ericgibbs Well-Known Member Most Helpful Member

    Joined:
    Jan 4, 2007
    Messages:
    21,240
    Likes:
    645
    Location:
    Ex Yorks' Hants UK
    Basic Bit String Output

    'Demo using Oshonsoft Basic for sending bit serial data from PIC to PIC
    'the receiving PIC could use, On PORTB.0 change interrupt to sense the clock
    'and another PIC pin to to test the incoming data pin.
    ''
    'Also useful for serial loading of external shift registers

    AllDigital

    'select the PIC pins
    Config PORTA.0 = Output 'set up port a.1 as an clock
    Config PORTA.1 = Output 'set up port a.1 as an output

    Symbol clk = PORTA.0
    Symbol dta = PORTA.1

    Dim bt As Byte
    Dim mydata(4) As Word

    mydata(0) = 0 'Dummy test data
    mydata(1) = %0000000101010101 'Dummy test data
    mydata(2) = %1010101011101111 'Dummy test data
    mydata(3) = %0000001100110011 'Dummy test data
    mydata(4) = %0000000011001100 'Dummy test data

    'change the (x) to show different WORD patterns
    'use the Sim Scope set for Display for Normal, use Zoom
    'select PORT pins on scope to suit
    mydata = mydata(2)

    'or use a WORD value
    ''mydata = 0x80ff

    Gosub xmitdata

    End

    xmitdata:
    dta = mydata.15 '1st msb bit
    WaitUs 1
    clk = 1
    clk = 0

    For bt = 0 To 14 '15 bits to follow
    mydata = ShiftLeft(mydata, 1)
    dta = mydata.15
    WaitUs 1
    clk = 1
    clk = 0
    Next bt
    dta = 0
    Return
     

    Attached Files:

Share This Page