EngineersGarage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise
You are here: Home / Replies / Now I tried like this below

Now I tried like this below

|

Microcontroller › 8051 › Interrupt of 1s -Atmel 89C51 › Now I tried like this below

December 3, 2015 at 11:09 am #13553
Mike
Participant

Now I tried like this below but stil dont have 1 second per number which is divided with 3 . .. He put all numbers which are divided with 3 in one second

Code:

#include <reg51.h>
                 
typedef unsigned char byte;

byte a[9]= {7,5,16,3,1,4,11,9,6};
byte counter;
byte frequency;
byte displ;
byte xdata display _at_ 0x8001;
byte started = 0;
int i=0;

sbit switcher=P0^0;
 
void Inic(void)
{
    EA=1;         // enable global interrupt
    TMOD=1;       // configure timer0 to 16 bit timer
    TH0=0x3C;     // set timer0 to 0xC350
    TL0=0xAF;     // 50,000 decimal
    
    counter=1;    // set counter to 1
}

 
void timer0(void) interrupt 1 using 2
{   
    TH0 = 0x3C;           // set timer0 to C350 = 50,000
    TL0 = 0xAF;           // after each interrupt                    
    
    counter–;            // count the interrupt
    if(counter == 0)      // count 20 interrupts for 1 sec delay       
       {
       counter = 20;      // reset counter to 20
       if(started == 1)   // if timer is started
         {   
         while(i < 9)     // go through array "a" to find a multiple of 3  
           {
           if( (a[i] % 3) == 0) // multiple of 3 has remainder = 0                 
              {
              display = (a[i]);  // display a matching number
              i++;          // increment i to the next number in "a"
              break;      // break out of the while loop after displaying a number
              }
           i++;            // increment i to the next number in "a"
           }                 // end while i < 9     
         if(i == 9)       // if 9, all numbers have been tested
           {
           ET0 = 0;       // disable timer 0 interrupt
           TR0 = 0;       // stop timer0 – all of "a" has been checked
           started = 0;   // started = 0, stop until switch pressed again   
           }
         }  // end if started == 1    
       }  // end if counter == 0
}

void main(void)
{
   Inic();        // initialize timer0
   switcher = 1;  // write 1 to switcher to use as an input
                  // it should have a pullup resistor to pull it high
   while(1)       // continuous loop
     {                    // the switch should pull P0^0 low
     if(switcher == 0)    // using switcher as a start switch
       if(started == 0)   // if counter is stopped
          {         
          started = 1;    // started goes 1 when switch pressed
          i = 0;          // set i for first byte in "a" array
          counter = 1;    // counter to 1 to get first number
                          // quicker (or 20 to wait 1 second)
          TH0=0x3C;       // set timer0 to 0xC350
          TL0=0xAF;       // 50,000 decimal
          ET0=1;          // enable timer0 interrupt
          TR0=1;          // start timer0 running         
          }
     }            
}

RSS Recent Posts

  • My Advanced Realistic Humanoid Robots Project June 9, 2026
  • What is the most suitable light source for making a book light like this? June 9, 2026
  • extracting material properties from open load resonance June 9, 2026
  • Oshon Basic, programing IF statements June 9, 2026
  • How charging is done in Transformer Inverters June 9, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2026 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Engineers Garage Main Site
  • Visit our active EE Forums
    • EDABoard.com
    • Electro-Tech-Online
  • Projects & Tutorials
    • Circuits
    • Electronic Projects
    • Tutorials
    • Components
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Advertise