pia-468x60

pia-728x90

Tuesday, January 12, 2021

PIC16F887 Timer1 and Ultra Sonic Range Measurement Application

Introduction

Additionally to Timer0, PIC16F887 has an 16-bit timer - Timer1. This timer is more advanced than Timer0. It has some additional features over Timer0

  • Optional LP oscillator
  • Synchronous or asynchronous operation
  • Gate mode
  • Interrupt on overflow
  • Capture/compare function, etc.
PIC16F887 Timer1 and Ultra Sonic Range Measurement Application
Block diagram of Timer1

 There are some registers to Timer1.

PIC16F887 Timer1 and Ultra Sonic Range Measurement Application
Registers relate to Timer1


 Timer1 Control Register (T1CON) set the overall operations of Timer1.

PIC16F887 Timer1 and Ultra Sonic Range Measurement Application
Timer1 Control Register (T1CON)

 Timer1 Application of Ultra-Sonic Range Measurement

An application of timer is pulse width measurement of external incoming event. For instance Ultrasonic Ranging Module HC-SR04, is a low cost popular range measurement for electronics hobbyist.



PIC16F887 Timer1 and Ultra Sonic Range Measurement Application
Popular HC-SR04 sample (Ali Express)

This module is a non-contact measuring type of sensor. It uses sounds wave traveling in space, along with its echo signal to get the distance. Measurement ranges from 4cm to 4 meters.

To make its measurement works properly the programmer must check its timing diagram.  

PIC16F887 Timer1 and Ultra Sonic Range Measurement Application
Timing Diagram

 It has a formula to measure range in term of receiving pulse width.

Range = (high level time)*(velocity)/2

Velocity of sound is 340m/s.

In short form we use only high time in us,

Range (in cm) = high level time (in us) / 58 

To get range in Inch,

Range (in inch) = high level time (in us) / 148

Programming in MikroC

System has HC-SR04 as its input. A character LCD output displays sampling range measurement. PIC16F887 controller uses its internal 8MHz oscillator.

Timer1 measure echo signal from HC-SR04 input to RD1. RD1 of PIC16F887 is trigger pin initiating range measurement of sensor. Prescaler is assigned to 1:2 to get a 1us timing.

PIC16F887 Timer1 and Ultra Sonic Range Measurement Application
Schematic Diagram


 MikroC source code:



Click here to download its zip file.

Friday, January 8, 2021

PIC16F887 Timer0 Interrupt Driven Display

Introduction

Since timer interrupt runs in background of microcontroller program. We can use this feature to create timing tick activating some tasks.

Multiplexing a display requires an activating time ranging around 5ms for each digit to display properly. More digits require more time in milliseconds. Other remaining tasks in microcontroller program need to wait until display driving finished. Embedded controller program designed using this method is not responsive.

Using timer0 interrupt, a timing tick can activate each digit of multiplexing display regularly. Display timing doesn't consume main program loop timing. Main program just operate other remaining tasks. Any program written using interrupt is flexible.

PIC16F887 Timer0 Interrupt Driven Display
Simulation sample of this program

Timer0 Interrupt Programming

This example program multiplex a two-digit display using timer0 interrupt timing ticks. Each digit will be activated for 5ms. It's a free running timer that counts up from 0 to 59 seconds before it resets.



Timer0 clock prescaler is assigned to 1:256. Number of ticks per second is 30. Display type is common cathode. We just use an inverting buffer here due to simulation problem.

PIC16F887 Timer0 Interrupt Driven Display
Schematic Diagram



This controller pulse from its 8MHz internal oscillator.

Click here to download its source file.



Wednesday, January 6, 2021

PIC16F887 Timer0 Creating Delay Function in MikroC

Introduction

In previous post of Timer0 interrupt programming, we use its interrupt advantage to create a timer tick in a range of milli second. We can decorate some more features over this capability.

A precise timing delay function can be created using timer0 interrupt programming. In this example we create a delay function from the existing 1ms-delay routine that it was written in previous example

PIC16F887 Timer0 Creating Delay Function in MikroC
Program simulation sample

 

Programming

Delay function created by adding more routine that rely on free running counter in the ISR coding of Timer0 interrupt.

void ms_delay(unsigned count) {
     while(ms_count<count);
      ms_count=0;

}

There is no change in source code but it excepts only one additional routine that handle timing delay. 

PIC16F887 Timer0 Creating Delay Function in MikroC
Schematic Diagram

Program in MikroC



Click here to download source file.


 

Labels