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 / Hello i wanted to write to

Hello i wanted to write to

|

Microcontroller › 8051 › Understanding of Timer and Counter › Hello i wanted to write to

April 4, 2017 at 8:47 pm #14555
ismail alper köylü
Participant

Hello i wanted to write answer your questions but i was not sure about the some things but now i am also with some code :)

Timers and counters uses the same registers. They operate different. Using keil with c if you do not stop timers/counters they will reaload themselves with the first values loaded on a overflow (Also applies for cleaing flags). Best way to understand is to try some simple codes with LED inputs outputs. Explaning them here will only give you superficial information therefore i am leaving you a timer0/timer, timer0/counter to try out :) 

timer0/timer ***********************

Once you load the registers and start it will repeatedly reload itself whenever the interrupt ends. You can end it with TR0=0; somewhere in your code, this is the basic principle of how timers works.

#include <REGX51.H>
 
 
 
void delay(){
int i;
for(i=0;i<20000;i++);
 
 
}
 
 
 
 
 
void timer0() interrupt 1{
P2++;
 
delay();
 
}
 
 
 
 
 
 
void main(){
  P2=1;
  TMOD=0x00;
  TH0=0xE0;
  TL0=0x18;
  ET0=1;
  EA=1;
TR0=1;
  while(1){
  
}
 
 
 
 
 
}

timer0/counter********************

As counter you are using T0 pin as input and it count high to low edges of transmissions it does not count both high to low and low to high so one of your answer to your questions is here. You can implement the input with pullup resistor and with button enabled ground. This code will keep incrementing P2 port whenever it detects a high to low transmission on P0 port.
 

#include <REGX51.H>
 
 
void delay()
{
int i;
for(i=0;i<1250;i++);
 
 
}
 
void intter() interrupt 1
{
 P2++;
 delay();
 
 
}
 
 
void main()
{
       P2=0;
  TMOD=0x06;
  TH0=-1;
  TL0=-1;
  ET0=1;
  ET1=1;
  EA=1;
  TR0=1;
  while(1);
 
 
 
 
 
 
}

**************************
These are from 89s52. 89s52 also has extended timer2 but i have never used it/needed it before. It is a little bit different from other timers in 89s52. It is best to master timer0 and timer1 before moving on to extimer2 or you will have confussions. 89s52 is failry basic microcontroller to start on C51 architecture it has very few sfr and easy to use with enough ram and flash. 
 

RSS Recent Posts

  • Fun with AI and swordfish basic June 22, 2025
  • Microinverters and storeage batteries? June 22, 2025
  • FFC connector white June 22, 2025
  • Is AI making embedded software developers more productive? June 22, 2025
  • Can I make two inputs from one?? June 22, 2025

Stay Up To Date

Newsletter Signup
EngineersGarage

Copyright © 2025 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