Pic Microcontroller Programming Tutorial For Beginners using MikroC With Step By Step Guide.

Saturday, January 2, 2016

IR (Infrared) Communication Between Two Microcontroller -Step By Step Tutorial : Part-3



PART 1 : Basic Introduction 

PART 2 : Interrupt 

PART 3 : Timer

PART 4 : Calculation

PART 5 : LED Switching Project [END

 

Timer :Part-3


 

 

Microcontroller Clock tutorial

Microcontroller Clock tutorial



Today I will discuss about Timer. It's very important part of microcontroller . Basically Timer is used to measure time. Microcontroller performs time related operations using timer . As we need to measure the time of each falling edge of IR-Signal .In this tutorial we will use timer0 of pic18f2550 and we will use 8bit mode. You may use 16bit mode, but first time skip complexity. Both TMROL and TMROH registers works with 16bit mode.
PIC 18f2550 microcontroller has 4 timer modules. Timer0,Timer1,Timer2 and Timer3 . I will only describe about Timer0 and i will try to make it easy so that you can make it by yourself . 

Generally Timer0 is controlled by T0CON register of pic18f2550 and TMR0L is Timer0 register low byte . In 16bit mode both TMR0L(8bit) and TMR0H(8bit) works. But in 8bit mode we can use only TMR0L. This timer0 register is incremented for each pulse comes to it and can count from 0 to 255 (0xFF).


Now we need to take a look about Timer0's control register and some necessary information . Next we will see how timer works and that will be good for us . 

Read all information carefully provided on these images!!



IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

Just keep in your mind , all those given in previous steps.We will need those in source code .

Now we will see how timer works 

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3


Generally We connect crystal clock with microconteroller so that it can work with time. Consider that we are using 12MHz clock. So, that means 12000000 pulses are passing to the microcontroller . Is that clear ?

Prescalar:

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

Prescalar is used to scale frequency. Look at the picture named scaling ratio. When (fosc/4) or (12/4=3MHz) frequency pulse comes to the the microcontroller we need make this smaller to make the calculation easy. So at this stage we need prescalar . Alright! 
There is another scaler "Postscalar" . Postscalar works after signal has come to the microcontroller .It isn't very necessary in this tutorial.

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

On that picture clock cycle  represents (Clock/4) or (12/4=3MHz) frequency . If we use 1:2 to scale , then 2 pulses is considered as 1 pulse . In 1:4 , 4 pulses is considered as one pulse. For 1:8 , eight pulses will be considered as one pulse.

Working of Timer0




Note:To Understand Clearly about Timer0 . You have to keep this picture in your memory . All we see in that part are the details of this Picture and save the Picture .

For Clock frequency we could use T0CKL pin (External Source) but Crystal frequency (12 MHz Crystal Clock) is good for us .

When we connect a crystal (we are using 12MHz Crystal Clock) with our microcontroller , microcotnroller receives pulse continuously. At this case we know the frequency and we can calculate the time period of each pulse from (T=1/f). So if we know how many pulse are received, we can calculate time by multiplying. We will see this later .
Generally , Crystal clock frequency divided by 4. It's a rule. That means first time clock frequency prescaled by 4. Look at the picture and you can see that "clock/4" or "System Clock"

IR (Infrared) Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
After this stage signal comes to prescalar. In this tutorial we will use 1:4 prescalar. That means we are dividing the system clock by 4. So, it means when 4 system clock pulse comes to prescalar, it sends out 1 pulse to Timer0 register . In this way timer0 register is being incremented .

Basically, when one pulse enters into the  Timer0 register, TMR0L  register is incremented by 1. The capacity of this register is 0 to 255. So when TMR0L will count 255, there will be an  overflow interrupt. In the interrupt function routine we will reset TMR0L register and we will increase (int count variable) by 1. We need this variable to remember that how many times interrupt occurred. When we perform [count * TMR0L], we get the total number of pulses. From this information we can measure time of "1" and "0"


We will create a project  in Calculation(4th Part)

Setup Timer0 Register :

Bit 0 to 2 are used to select prescalar. As we are using 1:4 prescalar , we need to set 001

Now set 3rd bit to 0 . So bit comes 0001.

Now set 4th bit to 0 and  we get   00001.

Now set 5th bit to 0 and we get   000001

Now set 6th bit to 1 , because we are using 8 bit mode . You may use 16bit mode. and  we get      1000001

Now set 7th bit to  1, because we need to start timer . Finally we get : 11000001 or 0xc1 (hexadecimal).


IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3
IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : Part-3

 Setup Timer0 overflow flag :

When overflow happens timer overflow interrupt flag bit becomes 1. When Overflow Register flag bit becomes 1 and microcontroller understand that interrupt occurred. So microcontroller goes to perform timer interrupt routine. When flag becomes 0 , microcontroller understand that everything is fine and there is no interrupt.
To enable overflow interrupt we need to set that enable. Unless the overflow interrupt will not work.

So first set INTCON.TMR0IE=1;

Note: you can find INTCON register on Interrupt section in Datasheet or in Interrupt tutorial i had described about INTCON register.


We set INTCON.TMR0IF bit to 0 and that sets the Flag as 0 .

First we need to enter on the INTCON register and then set TMR0IF=0;


 >>Continue to 4th PART<<

Reactions:

0 comments:

Post a Comment

Share and Bookmark this Blog

Tags

: (1) 18F2550 (1) 36KHz (3) and (1) Based (1) battery (1) Bipolar (1) Blinking (1) blinks (1) Bluetooth (1) bluetooth device interfacing (1) bluetooth module (1) button (1) circuit (1) clock (1) control (1) crystal oscillator (3) Db9 (1) DC Motor (2) digital (2) Digital Voting Machine (1) digital voting machine using pic (1) display (2) DS1307 (1) electronic (1) embedded c programming tutorial (11) embedded c tutorial (11) experiment kit (4) external interrupt (4) flash (1) flashing (1) Gas Leakage detector (1) HC-06 (1) home (1) how (1) How to (10) i2c tutorial (1) in (1) indicator (1) infrared Connection (3) interface (8) interfacing (3) Interrupt (3) Introduction (1) IR Connection (3) IR Receiver (4) IR Transmitter (4) key pad (1) keypad (1) lavel (1) Lcd 16x2 (2) lcd 2x16 (2) led (1) lm35 (2) LPG (1) machine (1) make (1) Make bootloader (1) making (1) matrix (1) max232 (1) membrane keyboard (1) meter (2) Micocontroller (1) microchip (4) microchip pic (2) microchips (3) microcontroller (9) microcontroller based (3) microcontroller programming (3) Microcontroller Project (4) Microcontroller Projects (1) microcontroller_project (2) microcontrollers (4) Microprocessor (2) mikroC (5) mikroc code to start and stopstart and stop dc motor (1) mikroc pro for pic (2) Motion detector (1) MQ-9 Gas Sensor (1) musical (1) NEC Protocol (4) pcb (5) PIC (3) pic controller (11) pic microcontroller (11) pic microcontroller tutorial (11) pic programming (1) pic programming in c (12) pic proteus (1) Pic Tutorial (12) pic18 (2) pic18f2550 (11) picmicrocontroller (4) picRFモジュール (1) PIR Motion Sensor (1) printed circuit board (1) proteus (6) pulse width modulation (1) push (1) push button (1) PWM (1) real (1) rf transmitter (3) Rs 232 (1) Rs232 (1) scroll (1) scrolling (1) Serial communication (1) Serial Connection (1) Serial Port (1) serial port rs232 (1) Servo Motembedded c programming tutorial (1) simulation (2) Soil Moisture Meter (1) speed control (1) step by step (7) step bystep (1) Stepper Motor (2) text (2) Thief Detector (1) time (1) timer (4) timer0 (4) tone (1) TSOP38236 Receiver (4) tutorial (2) Unipolar (1) USART Connection (1) USB (1) usb 1.0 (1) USB bootloadere (1) USB HID (1) using (9) voltmeter (1) voting (1) water level indicator (3) with (2) work (1)

Definition List

Comment Option isn't working properly

Comment option isn't working properly. If you have anything to say, you are requested to use the "Contact Form" below here.

Leave Your Message Here

Name

Email *

Message *

Google+ Followers

Like on Facebook