How do I add Code into a Thread

Thread Starter

RodneyB

Joined Apr 28, 2012
697
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
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
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.
 

Thread Starter

RodneyB

Joined Apr 28, 2012
697

  • C:
    [/LIST]
    #include <xc.h>
    
    // PIC10F200 Configuration Bit Settings
    #pragma config WDTE = OFF  // Watchdog Timer (WDT disabled)
    #pragma config CP = OFF  // Code Protect (Code protection off)
    #pragma config MCLRE = ON  // Master Clear Enable (GP3/MCLR pin function  is MCLR)
      // Because PIC10F200 doesn't have configurable POR or BOR,
      // I advise an external RC reset on /MCLR.
    
    
    #define  _XTAL_FREQ  4000000  //4Mhz INTOSC
    #define RUN_TIME  (60*1)
    #define START_TIME  (30*1)
    
    // Hardware Support
    
    #define SW  GPIObits.GP0
    #define LED1 sGPIObits.GP1
    #define LED2 sGPIObits.GP2
    
      //  6543210
    #define myTRIS 0b1001  // sets input output bits.
    
    // Port Shadowing
    volatile unsigned char sGPIO=0; // Port shadow register (global)
    #define sGPIObits (*(GPIObits_t * volatile) &sGPIO)
    #define updGPIO()  (GPIO = sGPIO)
    
    // Time to Ticks scaling for Timer 0 half period
    #define Time2Ticks(t) ((unsigned int)((t)*(float)_XTAL_FREQ/(4.0*256*128
    )))
    
    unsigned int ticks;
    
    void main (void) {
    
      GPIO = 0;
      OPTION = 0xC7; // /GPWO off, /GPPU off, T0CS internal, -, PSA TMR0, PS 1:256
      TRISGPIO = myTRIS;
    
      ticks = Time2Ticks(START_TIME);
    
    while(1) { // loop forever
    
      //Handle timer
    
      if (TMR0&0x80){ //check the high bit
    
      TMR0&=0x7F; //clear it
    
      if(ticks) --ticks; //decrement tick count
      }
    
      //Handle Switch
    
      if(SW==1 && ticks==0){
    
      ticks =Time2Ticks(RUN_TIME);
      }
      //Handle LED
    
      if(ticks) {
    
      LED1 =1;
    
      }
      else {
    
      LED1 = 0;
    
      }
    
      updGPIO();  // Actual update the GPIO port from the shadow register
      __delay_ms(10); //*MUST* maintain loop rate >30Hz to avoid missing Timer 0 half periods
      // Caution: using the compiler generated delays costs two bytes of RAM
    }
    }
    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

    C:
      //Handle Switch
    
      if(SW==0 && ticks==1){
      ticks =Time2Ticks(RUN_TIME);
      }
      //Handle LED
    
      if(ticks) {
    
      LED1 =0;
    
      }
      else {
    
      LED1 = 1;
    
      }
    Any direction is greatly appreciated

    Moderators note : used code tags in stead of the quote tags.
 
Last edited:

tjohnson

Joined Dec 23, 2014
611
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.
 

bertus

Joined Apr 5, 2008
22,278
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:
.
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
.
Bertus

PS I used a piece of pics asm code for example.
 

tjohnson

Joined Dec 23, 2014
611
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)".
 

MaxHeadRoom

Joined Jul 18, 2013
28,698
I am really confused. Have these replys any bearing on the question I asked about changing my code
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.
 

Thread Starter

RodneyB

Joined Apr 28, 2012
697
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:

Chuck E.

Joined Nov 24, 2014
2
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?
 
Top