Whats the quickest way to set bits?


Closed Thread
Results 1 to 34 of 34

Hybrid View

  1. #1
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Try the last formula I gave in post #6

    Testing here, making animation to prove it.
    It works.
    DT

  2. #2
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    <object id='stUkhdQ01IR1FYSF9cWVxcU1Nc' width='425' height='344' type='application/x-shockwave-flash' data='http://www.screentoaster.com/swf/STPlayer.swf' codebase='http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0'><param name='movie' value='http://www.screentoaster.com/swf/STPlayer.swf'/><param name='allowFullScreen' value='true'/><param name='allowScriptAccess' value='always'/><param name='flashvars' value='video=stUkhdQ01IR1FYSF9cWVxcU1Nc'/></object>

    Code:
    DEFINE OSC 4
    
    ;----[USART Settings]-------------------------------------------------------
    DEFINE HSER_RCSTA 90h    ' Enable serial port & continuous receive
    DEFINE HSER_TXSTA 24h    ' Enable transmit, BRGH = 1
    DEFINE HSER_SPBRG 25     ' 9600 Baud @ 4MHz, 0.16%
    DEFINE HSER_CLROERR 1    ' Clear overflow automatically
    
    ;----[LCD Settings]---------------------------------------------------------
    DEFINE LCD_DREG PORTA     ' LCD data port 
    DEFINE LCD_DBIT 4         ' LCD data starting bit 0 or 4 
    DEFINE LCD_RSREG PORTA    ' LCD register select port 
    DEFINE LCD_RSBIT 2        ' LCD register select bit 
    DEFINE LCD_EREG PORTA     ' LCD enable port 
    DEFINE LCD_EBIT 3         ' LCD enable bit 
    DEFINE LCD_BITS 4         ' LCD bus size 4 or 8 
    DEFINE LCD_LINES 2        ' Number lines on LCD 
    DEFINE LCD_COMMANDUS 2000 ' Command delay time in us 
    DEFINE LCD_DATAUS 50      ' Data delay time in us 
    
    ;----[A/D Settings]---------------------------------------------------------
    DEFINE ADC_BITS 10        ' ADCIN resolution  (Bits)
    DEFINE ADC_CLOCK 1        ' ADC clock source  (Fosc/8)
    DEFINE ADC_SAMPLEUS 11    ' ADC sampling time (uSec)
    
    ;----[Variables]------------------------------------------------------------
    GraphMAX   VAR WORD
    GraphMIN   VAR WORD
    GraphVal   VAR WORD
    X          VAR WORD
    ADval      VAR WORD
    Xmax       VAR WORD
    Xmin       VAR WORD
    char       VAR BYTE
    OverRange  VAR BIT
    UnderRange VAR BIT
    
    ;----[Aliases]--------------------------------------------------------------
    RCIF       VAR PIR1.5
    OverPin    VAR PORTC.5
    UnderPin   VAR PORTC.4
    
    ;----[Default Values]-------------------------------------------------------
    GraphMAX   = 100
    GraphMIN   = 0
    Xmax    = 100
    Xmin      = 0
    
    ;----[Initialize]-----------------------------------------------------------
    PORTB = 0
    TRISB = 0
    PORTD = 0
    TRISD = 0
    ANSEL = %00000001
    ANSELH = 0
    ADCON1.7 = 1
    PAUSE 250 : LCDOUT $FE,1 : PAUSE 250 : LCDOUT $FE,1
    GOSUB ShowMenu
    GOSUB ShowLimits
    OverRange = 0
    UnderRange = 0
    
    ;----[Main Program Loop]----------------------------------------------------
    Main:
        IF RCIF THEN GOSUB Terminal
        GOSUB GetAD
        GOSUB ShowGraph
        GOSUB CheckOverUnder
    GOTO Main
    
    ;----[Get and Scale Analog Reading]-----------------------------------------
    GetAD:
        ADCIN 0, ADval
        X = ADval * (Xmax - Xmin)
        X = DIV32 1023 + Xmin
        LCDOUT $FE,$80, "AD=",DEC ADval, "    "
        LCDOUT $FE,$8B, "X=",DEC X, "    "
    RETURN
    
    ;----[Show the BarGraph]----------------------------------------------------
    ShowGraph:
        IF X > GraphMAX THEN
            OverRange = 1
        ELSE
            OverRange = 0
        ENDIF
        IF X < GraphMIN THEN
            UnderRange = 1
        ELSE
            UnderRange = 0
        ENDIF
        X = (X MIN GraphMAX) MAX GraphMIN
        
        ;-- This is the formula being tested ---------------------
        GraphVal = $FFFF >> (16-(((X - GraphMIN) * 16) / (GraphMAX - GraphMIN)))
        
        PORTD = GraphVal & $FF
        PORTB = GraphVal >> 8
    RETURN
    
    ;----[Show Menu for Terminal]-----------------------------------------------
    ShowMenu:
        HSEROUT ["MENU: 1=GraphMAX, 2=GraphMIN, 3=Xmax, 4=Xmin :"]
    RETURN
    
    ;----[Handle the RS232 Terminal]--------------------------------------------
    Terminal:
        HSERIN [char]
        SELECT CASE char
          CASE "1" : HSEROUT [13,10,"Enter the GraphMAX: "]
                     HSERIN  [DEC GraphMAX]
          CASE "2" : HSEROUT [13,10,"Enter the GraphMIN: "]
                     HSERIN  [DEC GraphMIN]
          CASE "3" : HSEROUT [13,10,"Enter the Xmax: "]
                     HSERIN  [DEC Xmax] 
          CASE "4" : HSEROUT [13,10,"Enter the Xmin: "]
                     HSERIN  [DEC Xmin]
          CASE ELSE : HSEROUT [7]
        END SELECT
    
    ;----[Show Limits]----------------------------------------------------------
    ShowLimits:
        LCDOUT $FE,$C0,"    GRAPH    X      "
        LCDOUT $FE,$94,"MAX:                "
        LCDOUT $FE,$D4,"MIN:                "
        LCDOUT $FE,$99,DEC GraphMAX
        LCDOUT $FE,$D9,DEC GraphMIN
        LCDOUT $FE,$A1,DEC Xmax
        LCDOUT $FE,$E1,DEC Xmin
    
    RETURN
    
    ;----[Look for Over and Under Range]----------------------------------------
    BlinkSpeed  CON 50
    BlinkCount  VAR WORD
    
    CheckOverUnder:
        BlinkCount = BlinkCount + 1
        IF BlinkCount = BlinkSpeed THEN
            BlinkCount = 0
            IF OverRange THEN
                TOGGLE OverPin
            ELSE
                LOW OverPin
            ENDIF
            IF UnderRange THEN
                TOGGLE UnderPin
            ELSE
                LOW UnderPin
            ENDIF
        ENDIF
    RETURN
    
    This post has been promoted to a bar graph wiki page, located here: http://www.picbasic.co.uk/forum/cont...ge-min-and-max
    Last edited by ScaleRobotics; - 23rd October 2010 at 18:33. Reason: Added promotion tag
    DT

  3. #3
    Join Date
    Nov 2003
    Location
    Wellton, U.S.A.
    Posts
    5,924


    Did you find this post helpful? Yes | No

    Default

    Wow!!!!!
    That is cooool!!!!!!!
    Dave
    Always wear safety glasses while programming.

  4. #4
    Join Date
    May 2008
    Location
    Italy
    Posts
    825


    Did you find this post helpful? Yes | No

    Default

    Assuming you have to measure max 18 volts, then you will need a resistor devider by 4 which will yield 4,5 volts to the ADC input. Assuming you will use a 10 bits convertion then:

    45000/1024 = 44 (rounded)

    Your 16 leds graphs = $FFFF >>( 16- ((ADC * 44 * 16)/45000))

    Cheers

    Al.
    All progress began with an idea

  5. #5
    Join Date
    May 2004
    Posts
    81


    Did you find this post helpful? Yes | No

    Default

    thanks daryl, it does in fact work. I got tired last night and gave up on it and I think we were cross posting some how. Now all I have to do is tackle communications and Im done with this part of the project.

    Btw: Whats that cool little program your using in your video?

  6. #6
    Join Date
    Jul 2003
    Location
    Colorado Springs
    Posts
    4,959


    Did you find this post helpful? Yes | No

    Default

    Yeah, it was kind of late.
    But like I said, I needed the practice.

    The simulator is Proteus VSM from Labcenter Electronics.
    Definately the coolest program I've ever had.
    DT

  7. #7
    Join Date
    Sep 2005
    Location
    Campbell, CA
    Posts
    1,107


    Did you find this post helpful? Yes | No

    Default

    Darrel,

    I'm thinking of buying Proteus VSM. I know I should ask Labcenter, but since you use it with PBP, could I ask you a few questions?

    All my schematic capture is in Altium (formerly Protel). Do you know if Proteus will import other formats? Altium will export in Orcad 8 format and a few others.

    If I write a program in PBP, I assume that I have to feed Proteus the .ASM file in for the simulation. Is that true? I can't imagine that it would show me the PBP line it was executing during an animation. Just how easy is it to use in conjunction with PBP?
    Charles Linquist

Members who have read this thread : 0

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