Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

Code snippet for 1 second delay in PIC16F877A

Status
Not open for further replies.

babinton

Member level 4
Joined
Apr 8, 2006
Messages
79
Helped
7
Reputation
14
Reaction score
5
Trophy points
1,288
Location
Nigeria
Activity points
1,959
Hi everyone, I am a newbie to PIC programming, and I want to use a PIC16F877A the a 4MHz crystal oscillator to control a 5x7 scrolling Message LED Display board.
I already have by board and circuit design with components already soldered on the board. My main problem is that I am trying to code a 1 seconds delay for testing my display board to flash a column of LED once every second but I seem to be stuck at the delay routine.
Please, help me with a code snippet for the 1 second delay routine. Also, if anyone can give a detail explanation of how the scanning of the row and column of the 5x7 LED display works using CD4017 johnson counter, I will be very grateful.
Thanks,
 

Most programs need some kind of clock/timer and one way to implement it is to have a global variable that is incremented in a Timer interrupt routine.

You could use Timer 1 and pre-load it so that it interrupts every 1mS. In the interrupt routine, increment the global variable.
Use Mplab sim to check the interrupt timing.

When using unsigned integers, you don't have to worry about overflow when subtracting one from another, you always get the correct result.

Now you have a variable delay function to use in your program.
For a 1 second delay, simply call: wait_mS(1000);

Code:
volatile unsigned int sysclock;		/* 16-bit Global system clock */ 


/*--- Init Timer 1 ---*/

static void initTimer1(void)
  {
  T1CON = 0x01;
  TMR1IF = 0;
  TMR1IE = 1;
  }

/*--- Timer 1 interrupt vector ---*/

static void interrupt isr(void)
  {
  if(TMR1IF == 1)
    {
    sysclock++;

    TMR1ON = 0;		/* Pre-load for 1mS interrupt */
    TMR1L = 0;
    TMR1H = 0x3d;
    TMR1ON = 1;
    TMR1IF = 0;
    }
  }

/*--- Wait mS timer ---*/

static void wait_mS(unsigned int mS)
  {
  unsigned int timer = sysclock;

  while((sysclock - timer) < mS){
    CLRWDT();
    }
  }

/*--- End of File ---*/
 

Or Google for Picloops.

Small piece of useful software you can download and install in your PC.

I use it with good results.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top