Programming PIC 16f676 in C language...

Discussion in 'Programmer's Corner' started by RRITESH KAKKAR, Jun 4, 2012.

  1. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    HI,

    I found this code for LED blinking test, i want to know what does asm("nop"); mean here??

    Code ( (Unknown Language)):
    1.  
    2. #include <pic.h>        // pic specific identifiers
    3. __CONFIG(0x3D18);        // Config bits
    4.  
    5. void main(void)            // program entry
    6.     {
    7.     CMCON = 0x7;        // Comparitors off
    8.     TRISA = 0x0;        // Both ports
    9.     TRISB = 0x0;        // as outputs
    10.     while(1)            // Loop forever
    11.         {
    12.         PORTB = 0xff;    // All on!!
    13.         PORTA = 0xff;
    14.         asm("nop");        // We can call asm for
    15.         asm("nop");        // precise timming
    16.         PORTB = 0x0;    // All off!
    17.         PORTA = 0x0;            
    18.         }
    19.     }                    // End!!
     
    kazi2207 likes this.
  2. absf

    Senior Member

    Dec 29, 2010
    1,492
    371
    It is either:

    1. A demonstration on how you can embed assembly codes inside a c program.
    2. To delay 2 uS (if clock = 4MHZ) before Port A and B are cleared.:D

    Allen
     
    RRITESH KAKKAR likes this.
  3. BMorse

    Senior Member

    Sep 26, 2009
    2,675
    234
  4. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    The nop instruction will cause very less delay if i we want more delay how to do that in C??
     
  5. absf

    Senior Member

    Dec 29, 2010
    1,492
    371
    If you're using HiTech C then add in these lines:

    Before void main(void), add in

    Code ( (Unknown Language)):
    1.  
    2. #include <htc.h>
    3. #define _XTAL_FREQ 20000000  //20000000 = xtal frequency
    4.  
    replace asm("nop"); with

    Code ( (Unknown Language)):
    1.  
    2. __delay_ms(2000);  // delay for 2 seconds
    3.  
     
    RRITESH KAKKAR likes this.
  6. RRITESH KAKKAR

    Thread Starter Senior Member

    Jun 29, 2010
    2,831
    89
    What the use of these both line and how 2Mhz are declared?
     
  7. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    The line:
    Code ( (Unknown Language)):
    1. #define _XTAL_FREQ 20000000  //20000000 = xtal frequency
    tells the compiler how fast your processor is running.It needs to know that to set up the delays for you automatically. Use whatever speed your chip is actually running.

    The line:
    Code ( (Unknown Language)):
    1.  __delay_ms(2000);  // delay for 2 seconds
    calls a routine that (using the processor speed) sets up a delay. These delays are only approximate, so don't build a clock depending on these to keep your time going.

    Do read the help files for your compiler to find out the other useful routines they provide you.
     
    absf and RRITESH KAKKAR like this.
  8. kazi2207

    New Member

    Dec 19, 2012
    4
    0
    code need for pic16f676 to be used in battery charge controller
     
  9. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Many C compilers allow "inline assembly" in programs.

    Sometimes it is a block, such as _asm { instructions } or called as an asm("{operation}") function.

    This allows you to have more precise control over timers, program timing, and other features not easily seen through the high level language abstraction layer.

    NOP in assembly means "No Operation", which is a NULL instruction to cause the processor to essentially do nothing for one instruction cycle.

    Calling a few hundred thousand NOP instructions results in a delay, but is a bit of a waste of processor power.

    A general rule is delays if used at all should be short. Any other timed processing tasks should use other functions as delays. Items such as scanning a keypad or other non-time dependent tasks can be computed to time for function to run. That function then can be used as a fixed "delay" period while doing something useful, thus keeping your code cleaner and running realtime.

    It IS a good deal more work and brain sweat, but once you learn C in general, then start tweaking out delays, you'll find you don't need to buy devices with a faster clock rate or more flash simply by coding smarter.

    Remember to completely comment/document your code so somebody else reading it can tell what is going on, such as an LCD output routine being used as a delay (LCDs do require precise delays for data outputs), so they work good for a short delay loop.
     
    RRITESH KAKKAR likes this.
  10. Ian Rogers

    Member

    Dec 12, 2012
    158
    29
    That code was written for a pic16f628a...

    htc has several functions that can be used in this fashion.

    asm(); is one of these functions...
     
Loading...