Nice neat code posts


Closed Thread
Results 1 to 40 of 40

Hybrid View

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


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    Below is an unedited cut-and-paste from MCS. Even when I go back and try to clean it up, the final results look terrible. I'd post more code if it didn't look so bad.



    Code:
    GetNum:
            Result = 
    0
            MaxNum = 
    5     ; set for max number of digits you will 
    accept
            YY = 
    1
     
    POSN=0
            IsNeg = 
    0
            TimedOut = 
    0
            ArrayWrite InputString,[Rep 
    $FF\8]  ; Fill with 
    $FF
     
    GetMoreN:              
     
            HSERIN 
    9000,InputTimeout,[Keyin]
     
    IF Keyin = ESC THEN 
    ZeroNum
                If 
    Keyin = CR THEN 
    DunInputn
     
    If allowNeg 
    then
                IF 
    Keyin = "-" and Posn = 0 THEN 
     
     
    IsNeg = 
    1
     
    Hserout 
    ["-"]
     
    goto 
    GetMoreN
     
    ENDIF 
     
    endif   
     
                IF Keyin 
    = BS 
    THEN
     
    HSEROUT 
    [BS,SP,BS]
     
    IF POSN > 0 
    THEN
     
    POSN = POSN - 
    1
     
    InputString [POSN] = 
    $FF
     
    GOTO 
    GetMoreN
     
    ENDIF   
     
     
    ENDIF
            IF Keyin < "0" or Keyin 
    > "9" THEN GetMoreN 
            HSEROUT 
    [Keyin]
            InputString [POSN] = Keyin 
    - "0"
            IF POSN >= (MaxNum -1) 
    THEN DunInputN
            POSN = POSN 
    +1
            GOTO 
    GetMoreN
    DunInputN:
            For xN = 7 
    to 0 STEP - 1 
     
    IF InputString[XN]= 255 THEN 
    NoMoreNum
     
    Result = Result + 
    (InputString[xN]*YY)
     
    YY = YY 
    *10
    NoMoreNum:            
     
            Next 
    XN
     
     
            If Isneg 
    then
              If result < 
    32768 
    then
     
    Result = 0 - Result
     
    else
    Charles Linquist

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


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    Charles,

    Attach your code (BAS/PBP) so I can try it if you do not mind.
    Dave
    Always wear safety glasses while programming.

  3. #3
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    I have to agree with others here ...not only posting code snippets can be plain ugly....also reading others' old snippets with with the wrong (now incompatible) formatting renders them virtually unreadable.

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


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    Just testing...

    Copy/Paste from Programmers Notepad
    Code:
    ' 12F675 
    ' 09/05/2011  
    DEFINE OSC 4
    ' SET FOR INTERNAL OSC AND MCLRE OFF
    #CONFIG
        __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    ANSEL=%00000000         ' ADC SET FOR DIGITAL 
    CMCON=7                 ' COMPARATOR SET FOR DIGITAL
    SWITCH_OFF  VAR GPIO.1  ' PUSH BUTTON FOR PUMP OFF
    SWITCH_RUN	VAR	GPIO.2  ' PUSH BUTTON FOR PUMP ON  
    LED_RUN     VAR GPIO.0  ' INDICATE RUN SIGNAL SENT  
    LED_OFF     VAR GPIO.5  ' INDICAT STOP SIGNAL SENT
    TX          VAR GPIO.4  ' DATA SEND PIN
    BAUD        CON 18030	' 18030 = 600 BAUD
    
    START:     ' SEND OFF SIGNAL AT POWER UP
        PAUSE  1000
        HIGH LED_OFF  
        SEROUT2 TX, BAUD,["OFF",13]  
    DO  ' LOOP TO CHECK PUSH BUTTONS
    	DO WHILE SWITCH_RUN = 1     ' CHECK ON PUSH BUTTON
    	   ' IF BUTTON PUSHED
    		HIGH LED_RUN          ' RUN INDICATOR ON
    		LOW LED_OFF           ' OFF INDICATOR OFF
    		PAUSE 50              ' SETTLE TIME
    		SEROUT2 TX, BAUD,["RUN",13]
    '		SEROUT2 TX, BAUD,[HEX CNT,13]
    	LOOP
    	DO WHILE SWITCH_OFF = 1    ' CHECK OFF PUSH BUTTON
    	   ' IF BUTTON PUSHED
            HIGH LED_OFF          ' OFF INDICATOR ON
    		LOW LED_RUN           ' RUN INDICATOR OFF
    		PAUSE 50              ' SETTLE TIME
    		SEROUT2 TX, BAUD,["OFF",13]
    '		SEROUT2 TX, BAUD,[DEC CNT,13]
    	LOOP
    LOOP
    Copy/Paste from MCS
    Code:
    ' 12F675 
    ' 09/05/2011  
    DEFINE OSC 4
    ' SET FOR INTERNAL OSC AND MCLRE OFF
    #CONFIG
        __config _INTRC_OSC_NOCLKOUT & _WDT_ON & _MCLRE_OFF & _CP_OFF
    #ENDCONFIG
    
    ANSEL=%00000000         ' ADC SET FOR DIGITAL 
    CMCON=7                 ' COMPARATOR SET FOR DIGITAL
    SWITCH_OFF  VAR GPIO.1  ' PUSH BUTTON FOR PUMP OFF
    SWITCH_RUN	VAR	GPIO.2  ' PUSH BUTTON FOR PUMP ON  
    LED_RUN     VAR GPIO.0  ' INDICATE RUN SIGNAL SENT  
    LED_OFF     VAR GPIO.5  ' INDICAT STOP SIGNAL SENT
    TX          VAR GPIO.4  ' DATA SEND PIN
    BAUD        CON 18030	' 18030 = 600 BAUD
    
    START:     ' SEND OFF SIGNAL AT POWER UP
        PAUSE  1000
        HIGH LED_OFF  
        SEROUT2 TX, BAUD,["OFF",13]  
    DO  ' LOOP TO CHECK PUSH BUTTONS
    	DO WHILE SWITCH_RUN = 1     ' CHECK ON PUSH BUTTON
    	   ' IF BUTTON PUSHED
    		HIGH LED_RUN          ' RUN INDICATOR ON
    		LOW LED_OFF           ' OFF INDICATOR OFF
    		PAUSE 50              ' SETTLE TIME
    		SEROUT2 TX, BAUD,["RUN",13]
    '		SEROUT2 TX, BAUD,[HEX CNT,13]
    	LOOP
    	DO WHILE SWITCH_OFF = 1    ' CHECK OFF PUSH BUTTON
    	   ' IF BUTTON PUSHED
            HIGH LED_OFF          ' OFF INDICATOR ON
    		LOW LED_RUN           ' RUN INDICATOR OFF
    		PAUSE 50              ' SETTLE TIME
    		SEROUT2 TX, BAUD,["OFF",13]
    '		SEROUT2 TX, BAUD,[DEC CNT,13]
    	LOOP
    LOOP
    Last edited by mackrackit; - 14th September 2011 at 15:38.
    Dave
    Always wear safety glasses while programming.

  5. #5
    Join Date
    Dec 2005
    Posts
    1,073


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    The problem lies with most of the IDEs/editors. They use a varying number of spaces for tabs - some allow users to specify the value. As Mr. E noted, most have a convert tabs to spaces function but the simplest thing to do is to make your various editors agree on how many spaces make a tab or always use spaces rather than tabs.

  6. #6
    Join Date
    Oct 2009
    Location
    Utah, USA
    Posts
    427


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    OK... I'll jump in here with a question

    In order to go back and reclaim those posts that were originally posted with HTML (when it was allowed)...

    Would it be possible to re-enable HTML, for a short time, then copy the code that was posted and paste back in the code, without the HTML, then dis-able the HTML??

    Thus re-covering the VALUABLE code examples that have for now been lost.

    This might entail a lot of manual work... but there are VALUABLE code examples, like LCD BARGRAPHS, that are currently unusable.

    I sure hate to see all those posts and thread lost.
    Dwight
    These PIC's are like intricate puzzles just waiting for one to discover their secrets and MASTER their capabilities.

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


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    As time permits and when I run across those I convert them to BB code.

    Let me know where they are and I will add them to the list.
    Dave
    Always wear safety glasses while programming.

  8. #8
    Join Date
    Mar 2009
    Posts
    653


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    Quote Originally Posted by Heckler View Post

    This might entail a lot of manual work... but there are VALUABLE code examples, like LCD BARGRAPHS, that are currently unusable.
    Yeah, that was the one I gave up trying to read just a couple of nights ago...

    http://www.picbasic.co.uk/forum/show...?t=2359&page=1

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


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    I always have MCSP set to "convert tabs to spaces". I tried to attach my source but, it didn't "take". I'll send you a link instead.

    http://dl.dropbox.com/u/566712/GetNumber_part.bas
    Charles Linquist

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


    Did you find this post helpful? Yes | No

    Default Re: Nice neat code posts

    Straight from the link...
    Who knows... but what browser do you use? It maybe something other than the code editor.
    Code:
    '**************************************************************** 
    '*  Name    : Number Input routine                             *
    '*  Author  : Charles Linquist                                  *
    '*  Notice  : Copyright (c) 2011 Charles Linquist               *
    '*          : All Rights Reserved                               *
    '*  Date    : 9/13/2011                                         *
    '*  Version : 1.0                                               *
    '*  Notes   : For the forum                                     *
    '*          :                                                   *
    '****************************************************************
    
            DEFINE OSC 40                                                           
            DEFINE NO_CLRWDT 1
            DEFINE _18F8723 1
            DEFINE HSER_RCSTA 90H
            DEFINE HSER_TXSTA 20H                                                   
            DEFINE HSER_CLROERR 1
            define HSER_BAUD 9600
    
           
    
            Keyin var byte
            Maxnum var byte
            YY var word
            POSN var byte
            IsNeg var bit
            AllowNeg var bit
            TimedOut var bit
            InputString var byte [10]
            Result Var word
            XN var byte
    
            BS CON 8
            ESC cON 27
            LF con 10
            SP con 32
            CR con 13
    
            TRISC = %10111111  ; Just for the serial port
    topp:        
           allowneg = 1
            
      hserout [CR,Lf,"Input a number "]
       gosub getnum
       hserout [CR,LF,"You entered "]
       
       IF isneg = 1 then 
          Hserout [SDEC Result,CR,LF]
        ELSE
          hserout [dec Result,CR,LF]
        ENDIF  
       
       Goto topp        
            
    
    GetNum:
            Result = 0
            MaxNum = 5     ; set for max number of digits you will accept
            YY = 1
            POSN=0
            IsNeg = 0
            TimedOut = 0
            ArrayWrite InputString,[Rep $FF\8]  ; Fill with $FF
     
    GetMoreN:              
            HSERIN 9000,InputTimeout,[Keyin]
                IF Keyin = ESC THEN ZeroNum
                If Keyin = CR THEN DunInputn
                If allowNeg then
                IF Keyin = "-" and Posn = 0 THEN 
                    IsNeg = 1
                    Hserout ["-"]
                    goto GetMoreN
                ENDIF 
                endif   
                IF Keyin = BS THEN
                   HSEROUT [BS,SP,BS]
                   IF POSN > 0 THEN
                      POSN = POSN - 1
                      InputString [POSN] = $FF
                      GOTO GetMoreN
                   ENDIF   
                ENDIF
            IF Keyin < "0" or Keyin > "9" THEN GetMoreN 
            HSEROUT [Keyin]
            InputString [POSN] = Keyin - "0"
            IF POSN >= (MaxNum -1) THEN DunInputN
            POSN = POSN +1
            GOTO GetMoreN
    DunInputN:
    
           More code here
    Dave
    Always wear safety glasses while programming.

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