Simple controlling LED blinking in PIC18f4580

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

Simple controlling LED blinking in PIC18f4580

Postby Ryu » Wed Dec 21, 2011 11:56 pm

hi all..
i have a program to control LED blinking by using 2 inputs (buttons) on SK40C. But i fail. Can u take a look of my code's problem ? For your information, the blinking part is fine because i successfully test it without adding 'button input part'. Anyway, this code is build successfully.
CODE: SELECT_ALL_CODE
#include<p18f4580.h>
//======================================================

//configuration bit
//===============================================================
#pragma config OSC = HS            //HS Oscillator
#pragma   config FCMEN = OFF         // Fail-Safe Clock Monitor disabled
#pragma   config IESO = OFF         // Oscillator Switchover mode disabled
#pragma   config PWRT = OFF         // PWRT disabled
#pragma   config BOREN = OFF         // Brown-out Reset disabled in hardware and software
#pragma   config WDT = OFF         // WDT disabled (control is placed on the SWDTEN bit)
#pragma   config MCLRE = ON         // MCLR pin enabled; RE3 input pin disabled
#pragma   config PBADEN = OFF         // PORTB<4:0> pins are configured as digital I/O on Reset
//#pragma   config CCP2MX = PORTC         // CCP2 input/output is multiplexed with RC1
#pragma   config LVP = OFF         // Single-Supply ICSP disabled
#pragma   config XINST = OFF         // Extended Instruction Set

//define
//================================================================
#define XTAL_FREQ 20MHZ            //crystal frequency in MHZ

#define MHZ *1000L               //number of KHZ in a MHZ
#define KHZ *1                  //number of KHZ in a KHZ

#if   XTAL_FREQ >= 12MHZ
   #define   DelayUs(x)   { unsigned char _dcnt; \
              _dcnt = (x)*((XTAL_FREQ)/(12MHZ)); \
              while(--_dcnt != 0) \
                 continue; }
#else
   #define   DelayUs(x)   { unsigned char _dcnt; \
              _dcnt = (x)/((12MHZ)/(XTAL_FREQ))|1; \
              while(--_dcnt != 0) \
                 continue; }
#endif

#define LED1 LATBbits.LATB6
#define LED2 LATBbits.LATB7

#define SW1 LATBbits.LATB0
#define SW2 LATBbits.LATB1

//Function Prototype
//================================================================
void DelayMs(unsigned char);

//Global Variable
//================================================================

//Main Function
//================================================================
void main(void)
{
   unsigned char i;
   TRISBbits.TRISB0 = 1;      //configure PORTB I/O direction
   TRISBbits.TRISB1 = 1;      //configure PORTB I/O direction
   TRISBbits.TRISB6 = 0;      //configure PORTB I/O direction
   TRISBbits.TRISB7 = 0;      //configure PORTB I/O direction

   LED1 = 0;         //off LED1
   LED2 = 0;         //off LED2

   while(1)
   {
      if(SW1==0)
      {
         for (i=10;i>0;i--)      //looping for 8 times (4 times blinking)
         {
            LED1 ^= 1;                   //toggle LED
                                LED2 = 0;      
            DelayMs(1000);      //delay 110ms
         }
      }

      if(SW2==0)
      {
         for (i=8;i>0;i--)
         {
            LED2 ^= 1;
                                LED1 = 0;
            DelayMs(1000);
         }
      }
   }
}

//Function
//===========================================================

void DelayMs(unsigned char cnt)   // delay function (mili second)
{
   #if   XTAL_FREQ <= 2MHZ
      do {
         DelayUs(996);
      } while(--cnt);
   #endif
   
   #if    XTAL_FREQ > 2MHZ   
      unsigned char   i;
      do {
         i = 4;
         do {
            DelayUs(250);
         } while(--i);
      } while(--cnt);
   #endif
}
Ryu
Newbie
 
Posts: 13
Joined: Sun Aug 14, 2011 10:32 pm

Re: Simple controlling LED blinking in PIC18f4580

Postby shahrul » Thu Dec 22, 2011 2:08 pm

The push button will not read until the LED have finish the sequence. For better, do a counter for LED blinking, not a delay.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 3 guests