Skip to main content

PIC Countdown Timer (0-99)
















This project describes how to program PIC16F628A to function as a 00-99 min programmable timer. User can set any time between 00-99 minutes and can turn ON a device for that period. The device will be automatically turned OFF after the time expires. For demonstration, the ON/OFF condition of device is simulated by switching LED ON and OFF. With the use of three input switches (unit, ten, start/stop) the user can set ON time of the timer and can also control Start/Stop operation. The two time set switches are for selecting unit and tens digit of minute time interval (00-99). Once you set the value of minute interval, pressing the Start/Stop will turn the timer ON (LED will glow), and pressing the same button again at any point of time during timer operation will interrupt the process (LED will turn OFF) and the timer will be reset. LCD display will provide timer status and user interface for setting time.






Circuit




















Code
Compiled using MikroC for PIC




/*
  ############################################


  MCU:16F628A
  Project: PIC Countdown Timer (0-99)
  Vishal K M
  Jan 10, 2012
  ############################################

 
*/

// LCD module connections
sbit LCD_RS at RA0_bit;
sbit LCD_EN at RA1_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISA0_bit;
sbit LCD_EN_Direction at TRISA1_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connections

// Tact switches and Relay ports
sbit Relay at RA3_bit;
sbit SS_Select at RB0_bit;    // Start Stop Timer Select
sbit Unit_Button at RB1_bit;  // Set unit min
sbit Ten_Button at RB2_bit;   // Set ten min


// Messages
char Message1[]="Timer by VISHAL";
char Message2[]="Device ON";
char Message3[]="Device OFF";
char Message4[]="Set Time:    min";
char Message5[]="Time Left:   min";
unsigned short i, j, unit=0, ten=0, ON_OFF=0, index=0, clear, time;
char *digit = "00";
// 300ms Delay
void Delay_300(){
 Delay_ms(300);
}

void Display_Digits(){
 digit[1]=unit+48;
 digit[0]=ten+48;
 Lcd_Out(2,11,digit);
}

void start_timer(unsigned short MinVal){
 unsigned short temp1, temp2;
 Relay = 1;
 ON_OFF = 1;
 Lcd_Cmd(_LCD_CLEAR);
 Lcd_Out(1,1,Message2);
 Lcd_Out(2,1,Message5);
 OPTION_REG = 0x80 ;
 INTCON = 0x90;
 for (i=0; i<MinVal; i++){
  temp1 = (MinVal-i)%10 ;
  temp2 = (MinVal-i)/10 ;
  Lcd_Chr(2, 12, temp2+48);
  Lcd_Chr(2, 13, temp1+48);
  j=1;
  do {
  Delay_ms(1000);
  j++;
  } while(((j<=60) && (Clear ==0)));
  if (Clear) {
   Relay = 0;
   Delay_ms(500);
   Lcd_Out(1,1,Message3);
   INTCON = 0x00;
   goto stop;
   }
 }
 stop:
 Relay = 0;
 ON_OFF = 0;
 unit = 0;
 ten = 0;
 clear = 1;
}

void interrupt(void){
  if (INTCON.INTF == 1)   // Check if INTF flag is set
   {
    Clear = 1;
    INTCON.INTF = 0;       // Clear interrupt flag before exiting ISR
   }
  }

void main() {
  CMCON  |= 7;                       // Disable Comparators
  TRISB = 0b00001111;
  TRISA = 0b11110000;
  Relay = 0;

  Lcd_Init();                        // Initialize LCD
 start:
  clear = 0;
  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Cmd(_LCD_CURSOR_OFF);          // Cursor off
  Lcd_Out(1,1,Message1);
  Lcd_Out(2,1,Message4);
  Display_Digits()  ;
 do {

     if(!Unit_Button){
     Delay_300();
     unit ++;
     if(unit==10) unit=0;
     Display_Digits();
    } // If !Unit_Button

    if(!Ten_Button){
     Delay_300();
     ten ++;
     if(ten==10) ten=0;
     Display_Digits();
    } // If !Ten_Button

    if(!SS_Select){
     Delay_300();
     time = ten*10+unit ;
     if(time > 0) start_timer(time);
    } // If !SS_Select

    if(clear){
     goto start;
    }
   } while(1);
}








Facebook page 

Comments

  1. Which cristal frequency are you using? 4MhZ, 8Mhz, 20Mhz...

    ReplyDelete
  2. I used 4Mhz, compiler will take care of the Cristal frequency u can use any of them.

    ReplyDelete
  3. hi, hw to modify this circuit & coding if I don't want to use lcd? I want a 10-30 mins countdown timer with alarm (piezo buzzer) as audio output.

    ReplyDelete
  4. How do you set the timer? If you are using a fixed timer its ok.

    ReplyDelete
  5. sir i want its 4 digit for micro second, so what the program please help me

    ReplyDelete
  6. Is it possible to get the code in asm format?

    ReplyDelete

Post a Comment

Popular posts from this blog

PIC 16F877A Microcontroller Based Electronic Lock 16x2LCD 4x3 Keypad

 Circuit Diagram Security is a prime concern in our day-today life. Everyone wants to be as much secure as possible. An access control for doors forms a vital link in a security chain. The microcontroller based digital lock for Doors is an access control system that allows only authorized persons to access a restricted area. An electronic lock or digital lock is a device which has an electronic control assembly attached to it. They are provided with an access control system. This system allows the user to unlock the device with a password. The password is entered by making use of a keypad. The user can also set his password to ensure better protection. The major components include a Keypad, LCD and the controller PIC16F877A. This article describes the making of an electronic code lock using the 16F877A microcontroller. The system is fully controlled by the 8 bit microcontroller 16F877A which has a 8Kbytes of ROM for the program memory. The password is stored in the

Electronic Voting Machine Using 8051 Microcontroller (AT89C51)

  Circuit Electronic voting machine has now replaced the traditional mechanism of voting due to several advantages like security, automatic counting etc. This project presents a way to develop an electronic voting machine which displays the count of votes on a 16x2 LCD interface. A user can get his/her vote register through a set of switches (one for each candidate). After every cast of vote, the subsequent count can be seen on LCD. The circuit uses AT89C51 microcontroller and the code for the project has been written in C. This LCD based electronic voting machine is designed for four candidates. The input part consists of a set of six tactile switches. The switches and 16x2 LCD are interfaced to microcontroller AT89C51 for various operations and displays. The provision of casting votes for the candidates has been provided through four of these switches. These switches are made active high and connected to pins 2-5 (P1^1 – P1^4) of the controller. The remaining two

89C51 Based Digital Thermometer Using DS1820

Introduction The hardware configuration when using multiple 1-Wire temperature sensors like the DS1820 is very simple, as illustrated in the block diagram below. A single-wire bus is used for communication between the microcontroller and the temperature sensor. It is also possible to power the devices direclty via this 1-Wire bus. An almost unlimited number of 1-WireTM devices can be connected to the bus because each device has a unique 64-bit ROM code identifier which is used to address each sensor   Temperature measurement using DS1820 sensor. Use of ‘1-wire’ protocol... Temperature measurement is one of the most common tasks performed by the microcontroller. A DS1820 sensor is used for measurement here. It is capable of measuring temperature in the range of -55 °C to 125 °C with 0.5 °C accuracy. For the purpose of transferring data to the microcontroller, a special type of serial communication called 1-wire is used. Due to a simple and wide use of these sensors, commands us