Programming PIC 16f676 in C language...

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
HI,

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

Rich (BB code):
#include <pic.h>        // pic specific identifiers
__CONFIG(0x3D18);        // Config bits

void main(void)            // program entry
    {
    CMCON = 0x7;        // Comparitors off
    TRISA = 0x0;        // Both ports
    TRISB = 0x0;        // as outputs
    while(1)            // Loop forever
        {
        PORTB = 0xff;    // All on!!
        PORTA = 0xff;
        asm("nop");        // We can call asm for
        asm("nop");        // precise timming
        PORTB = 0x0;    // All off!
        PORTA = 0x0;            
        }
    }                    // End!!
 

absf

Joined Dec 29, 2010
1,968
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
 

absf

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

Before void main(void), add in

Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 20000000  //20000000 = xtal frequency
replace asm("nop"); with

Rich (BB code):
__delay_ms(2000);  // delay for 2 seconds
 

Thread Starter

RRITESH KAKKAR

Joined Jun 29, 2010
2,829
If you're using HiTech C then add in these lines:

Before void main(void), add in

Rich (BB code):
#include <htc.h>
#define _XTAL_FREQ 20000000  //20000000 = xtal frequency
replace asm("nop"); with
What the use of these both line and how 2Mhz are declared?
 

ErnieM

Joined Apr 24, 2011
8,377
What the use of these both line and how 2Mhz are declared?
The line:
Rich (BB code):
#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:
Rich (BB code):
 __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.
 

kazi2207

Joined Dec 19, 2012
4
code need for pic16f676 to be used in battery charge controller
HI,

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

Rich (BB code):
#include <pic.h>        // pic specific identifiers
__CONFIG(0x3D18);        // Config bits

void main(void)            // program entry
    {
    CMCON = 0x7;        // Comparitors off
    TRISA = 0x0;        // Both ports
    TRISB = 0x0;        // as outputs
    while(1)            // Loop forever
        {
        PORTB = 0xff;    // All on!!
        PORTA = 0xff;
        asm("nop");        // We can call asm for
        asm("nop");        // precise timming
        PORTB = 0x0;    // All off!
        PORTA = 0x0;            
        }
    }                    // End!!
 

thatoneguy

Joined Feb 19, 2009
6,359
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.
 

Ian Rogers

Joined Dec 12, 2012
1,136
That code was written for a pic16f628a...

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

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