How do I add Code into a Thread

Discussion in 'Embedded Systems and Microcontrollers' started by RodneyB, Aug 22, 2015.

  1. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13
    I want to ask for some help with some c code, However I would like to know how to post it within the thread. Is it alright to cut and paste it into the thread
     
  2. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    Use the header logos above your message 5th from the right and select code.
    Alternatively you can use [] with QUOTE and /QUOTE inside brackets at each end of code.
    Max.
     
  3. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13

    • Code (C):
      1.  
      2. [/LIST]
      3. #include <xc.h>
      4.  
      5. // PIC10F200 Configuration Bit Settings
      6. #pragma config WDTE = OFF  // Watchdog Timer (WDT disabled)
      7. #pragma config CP = OFF  // Code Protect (Code protection off)
      8. #pragma config MCLRE = ON  // Master Clear Enable (GP3/MCLR pin function  is MCLR)
      9.   // Because PIC10F200 doesn't have configurable POR or BOR,
      10.   // I advise an external RC reset on /MCLR.
      11.  
      12.  
      13. #define  _XTAL_FREQ  4000000  //4Mhz INTOSC
      14. #define RUN_TIME  (60*1)
      15. #define START_TIME  (30*1)
      16.  
      17. // Hardware Support
      18.  
      19. #define SW  GPIObits.GP0
      20. #define LED1 sGPIObits.GP1
      21. #define LED2 sGPIObits.GP2
      22.  
      23.   //  6543210
      24. #define myTRIS 0b1001  // sets input output bits.
      25.  
      26. // Port Shadowing
      27. volatile unsigned char sGPIO=0; // Port shadow register (global)
      28. #define sGPIObits (*(GPIObits_t * volatile) &sGPIO)
      29. #define updGPIO()  (GPIO = sGPIO)
      30.  
      31. // Time to Ticks scaling for Timer 0 half period
      32. #define Time2Ticks(t) ((unsigned int)((t)*(float)_XTAL_FREQ/(4.0*256*128
      33. )))
      34.  
      35. unsigned int ticks;
      36.  
      37. void main (void) {
      38.  
      39.   GPIO = 0;
      40.   OPTION = 0xC7; // /GPWO off, /GPPU off, T0CS internal, -, PSA TMR0, PS 1:256
      41.   TRISGPIO = myTRIS;
      42.  
      43.   ticks = Time2Ticks(START_TIME);
      44.  
      45. while(1) { // loop forever
      46.  
      47.   //Handle timer
      48.  
      49.   if (TMR0&0x80){ //check the high bit
      50.  
      51.   TMR0&=0x7F; //clear it
      52.  
      53.   if(ticks) --ticks; //decrement tick count
      54.   }
      55.  
      56.   //Handle Switch
      57.  
      58.   if(SW==1 && ticks==0){
      59.  
      60.   ticks =Time2Ticks(RUN_TIME);
      61.   }
      62.   //Handle LED
      63.  
      64.   if(ticks) {
      65.  
      66.   LED1 =1;
      67.  
      68.   }
      69.   else {
      70.  
      71.   LED1 = 0;
      72.  
      73.   }
      74.  
      75.   updGPIO();  // Actual update the GPIO port from the shadow register
      76.   __delay_ms(10); //*MUST* maintain loop rate >30Hz to avoid missing Timer 0 half periods
      77.   // Caution: using the compiler generated delays costs two bytes of RAM
      78. }
      79. }
      80.  
      I was helped some time ago with this code I want to change it to using a normally closed switch If I change the quoted to this for some reason it does not work and I don't know why

      Code (C):
      1.  
      2.  
      3.  
      4.   //Handle Switch
      5.  
      6.   if(SW==0 && ticks==1){
      7.   ticks =Time2Ticks(RUN_TIME);
      8.   }
      9.   //Handle LED
      10.  
      11.   if(ticks) {
      12.  
      13.   LED1 =0;
      14.  
      15.   }
      16.   else {
      17.  
      18.   LED1 = 1;
      19.  
      20.   }
      21.  
      Any direction is greatly appreciated

      Moderators note : used code tags in stead of the quote tags.
     
    Last edited: Aug 23, 2015
  4. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13
    I have failed on the QUOTE assistance
     
  5. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
    nerdegutta likes this.
  6. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    It should work if you enter your code between the tags [CODE=C] and [/CODE]. You can also paste code into the edit box, select it, click on the + button in the formatting toolbar, and select Code from the menu.
     
  7. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    What is it for Asm ?
    Max.
     
  8. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    I don't know. I would have thought it should be easy to find out, but I can't find a list of GeSHi language names and the GeSHi website seems to be down right now.
     
  9. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
    Hello,

    There is an asm available. Try this for example:


    [code=asm]
    .
    org
    0x80
    ;code location in
    ; page 0
    movlw
    HIGH Table ;load PCLATH with hi
    ; address
    movwf
    PCLATH
    ;
    /
    movlw
    offset,F
    ;load offset in w reg
    call
    Table
    .
    .
    org
    0x02ff
    ;Table located end of
    ; page 2
    Table:
    addwf
    PCL,F
    ;value in pc will not
    ; roll over to page 3
    retlw
    ’A’
    ;return the ASCII
    ; char A
    retlw
    ’B’
    ;return the ASCII
    ; char B
    retlw
    ’C’
    ;return the ASCII
    ; char C
    .
    [/code]


    This will result in:
    Code (ASM):
    1.  
    2. .
    3. org
    4. 0x80
    5. ;code location in
    6. ; page 0
    7. movlw
    8. HIGH Table ;load PCLATH with hi
    9. ; address
    10. movwf
    11. PCLATH
    12. ;
    13. /
    14. movlw
    15. offset,F
    16. ;load offset in w reg
    17. call
    18. Table
    19. .
    20. .
    21. org
    22. 0x02ff
    23. ;Table located end of
    24. ; page 2
    25. Table:
    26. addwf
    27. PCL,F
    28. ;value in pc will not
    29. ; roll over to page 3
    30. retlw
    31. ’A’
    32. ;return the ASCII
    33. ; char A
    34. retlw
    35. ’B’
    36. ;return the ASCII
    37. ; char B
    38. retlw
    39. ’C’
    40. ;return the ASCII
    41. ; char C
    42. .
    43.  
    Bertus

    PS I used a piece of pics asm code for example.
     
    tjohnson likes this.
  10. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    Duh! I actually tried CODE=asm, but just with the code "test" so I didn't see any syntax highlighting and didn't think it worked. I also incorrectly assumed that it would label the code as "Code (Assembly)".
     
  11. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13
    I am really confused. Have these replys any bearing on the question I asked about changing my code
     
  12. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13
    ????
     
  13. MaxHeadRoom

    Expert

    Jul 18, 2013
    10,566
    2,379
    I though it was regards to displaying it?
    If you have a problem with the code, where is the question?
    I had no success with ASM it did not indent as well as highlighting etc?
    Max.
     
  14. tjohnson

    Active Member

    Dec 23, 2014
    614
    121
    @RodneyB: Post #6 was written in answer to your question, and the posts after it were related to Max's.
     
  15. RodneyB

    Thread Starter Active Member

    Apr 28, 2012
    633
    13
    Ok I can see the confusion. My question was included in the quote when the moderator assisted me. I posted my original code and then I posted my question followed by more code

    I have amended it.

    So my question is why after changing the code does it not work with a normally closed switch
     
    Last edited: Aug 22, 2015
  16. bertus

    Administrator

    Apr 5, 2008
    15,649
    2,348
  17. Chuck E.

    New Member

    Nov 24, 2014
    2
    0
    First off it would help if you define the objective of this program.
    Are you trying to toggle LED's on and off at a specific rate only when the switch is in a specific state?
    In your full listing if switch is closed and the counter has reached zero the counter will be reset.
    In your change snippit if the switch is open and the counter has NOT reached zero the counter will be reset every iteration of the loop.
    Is that your desire (objective)?
    Also in either case the LED will be OFF only when the counter reaches zero and for only one iteration of the while loop which appears to be 10 milliseconds.
    With that in mind TMR0 is useless.
    So you see why a plan should be put forth first before writting the code. What is your objective?
     
Loading...