Tuesday, April 12, 2016

We can control the speed of a DC motor through a microcontroller. All we need to know about PWM .That means " Pulse Width Modulation ". It is a very popular technique and most important part of a microcontroller. In this tutorial we will use PWM to create signal. By changing pulse width of a signal ,we can control the speed .

Basic Description:

At first we will connect a DC motor with PIC microcontroller and we will also connect two push buttons with it. One of those buttons will be used to increase speed and another one for decreasing speed. That's all .

Pulse Width Modulation (PWM)

We can create various signal through PWM. If we want to create 36KHz signal, that can be done by PWM .We can create any type signal. When we write program for creating signal through MCU, the CPP pin of microcontroller generate the signal according to the instructions. Lets consider, we have a 2Hz frequency signal. That means time period is 1/2=0.5 sec. This signal can be in various form. Look at the pictures :

Pulse width modulation

when Set_Duty(127);

PWM

Pic microcontroller's has two PWM or CPP module look at the picture .

In L293D's EN1 pin we provide the signal. If you will provide highest pulse width, the speed will be highest. When you will decrease pulse width, the speed will also be decreased.
So if you don't know how to interface DC motor with microcontroller. It's recommended to read this tutorial ,

Time Period = Times of ON state + Times of OFF state .

PWM has four functions

1. PWM1_Init( Set_Frequency );

2. PWM1_Start();

3.PWM1_Set_Duty( Count );

4.PWM1_Stop();

Here most important thing is PWM1_Set_Duty(count) , If we increase the value of count, the speed increases. If we decrease the value of count, the speed decreases. Another important thing is the highest value of count is 255 and lowest is 0.

IR Controlled LED Switching : 5th & LAST PART

I have to make it quick and i think we have got all required information . I ain't going to describe source code . You have to help your self. It has become already too long !!.I can honestly say that i tried from my best. Today I am going to finish this series and it's the last part.So first of all, take a look on a calculation part.

Final Calculation :

To identify each bit uniquely, we will use another calculation method. You may use as you want . I ain't going to use millisecond .you can use millisecond or microsecond . If you want to use millisecond to measure pulse , then for logical 0 you get (562us_on+562us_off)=1.124ms. and for logical 1 you get (562us_on+1687us_off)=2.249ms. You know, for each task microcontroller needs time. That's why reading can bevaried from calculation. So if you use integer number, you will get 1ms for logical 0 and 2ms for 1. By applying this you may successfully able to make it. Alright.

So, for logical 1 :

(562+1687) or (421.5+1265.75)=1686 pulsesNow , we know 255 pulses means 1 interrupt or Count=1 . So , 1 ,, ,, (1/255) so , 1686 ,, ,, (1686/255)=6.62So, we can say 6.62 interrupts means logical 1 . In coding we i will use 5 or 6 or 7 to identify logical 1 in proteus.

So, for logical 0 :

(562+562) or (421.5+421.5)=843 pulsesNow , we know 255 pulses means 1 interrupt or Count=1 .OK! So , 1 ,, ,, (1/255) or count=(1/255) so , 843 ,, ,, (843/255)=3.31 or count=3So, we can say 3.31 interrupts means logical 0. In coding we I will use 2 or 3 or 4 to identify logical 0 in proteus. By using this technique , from count variable we can identify logical 1 or logical 0.

Consider that we have main 4bit command 0001.So the reverse command should be 1000 command .Now we have to attach these and that will be 10000001. Finally we get 8bit command in which a main command and a reverse of that command are saved .

We need some knowledge aboutPWM(Pulse Width Modulation)

We will use PWM to generate 36KHz pulse and that's why we will connect positive(+) pin of Transmitter unit Microcontroller's CPP1 pin . Because using that pin of Microcontroller will generate 36KHz pulse .

PWM1_Init(36000);will set up the CPP1 pin for PWM .

PWM1_Set_Duty(127);

set_duty(127) used to provide same for both ON and OFF .That means (T/2) time it will ON and (T/2) time it will OFF .If we use set_duty(255) , then total Time period (T) will be remain ON. If we use set_duty(0) , then total Time period (T) will be remain OFF.

Look at this photo.

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )

IR (Infrared) Remote Controlled Communication Between Two Microcontroller -Step By Step Tutorial : LAST PART ( END )

Again look at another picture given below

Here in if statement , an AND operation performed between Command and 0x01 . We are using this to identify each bit of data command.

Suppose , our command is 10000001 and 0x01=00000001.So ,when an AND operation performs the result will be like this.

Shifting Operation in Programming

Shifting Operation in Programming

So, if result is 1 ,then the related bit is 1 and delay should be 1687us.

So, if result is 0 ,then the related bit is 0 and delay should be 562us.

***Receiver Section(Important):

Here this part of code is comparing that Data signal commands received in receiver unit are correct .

Calculation :

First , we know that clock frequency will be divided by 4 and secondly it will go to prescaler. Finally clock goes to timer register .

So, if we use 12MHz clock and 1:4 prescaler. First we will get system clock (12/4)=3MHz clock or (12000000/4)=3000000Hz clock. Secondly it goes to prescaler and divided by 4 again. So we get(3000000/4)=750000Hz clock.

We know that for each pulse TMR0L(Timer0) register is incremented by one. So when 750000 Hz clock successfully enters on timer0 register and the TMR0L register increments 750000 times. Is it clear? We have a restriction, our TMR0L register can count from 0 to 255.

As we need to count 750000 or greater that 750000.We have to walk in another way. We will set an integer variablecount which will be incremented for each overflow interrupt. That means, TMR0L counts 255 and overflow interrupt occurs . So it gets reset and count is incremented by one. Actually when overflow interrupt occurs, it increases the value of count variable by one. That means count=count+1;

Now consider an External Interrupt situation on RB0 pin where count=3 and TMR0L(8bit Register)=123.

How many pulses has been passed on that External Interrupt situation ?

Total pulses will be [On that situation]tp= (count * 255) + TMR0L;

Think about that, We are using 12MHz clock and finally we get 750000Hz clock from 12MHz clock.

So , What is the meaning of this 750000 Hz clock frequency ?

It means ,when 750000 pulses has been received, time is 1 second . We read the definition of frequency in physics. So the Time period t =(1/f); The time required for one pulse .

Proteus Circuit of the Project :

Note:Perhaps , we can measure highiest time : probably it is [{(255*255)+255}/750000]*1000=87.04 milliseconds .So we can measure accurate time for 87 milliseconds probably . Actually Push button is not perfect for this . You may use other variable to increase measuring capacity .You may send pulses having time period less than 87 milliseconds on RB0 pin. I am suggesting you to provide signal on RB0 pin from another microcontroller using PWM. It will be good for you! Thank You.

Timer :Part-3

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 usetimer0 of pic18f2550and 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 T0CONregister 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 .

Now we will see how timer works

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 ?

Prescaler:

Prescaler 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 prescaler . Alright! There is another scaler "Postscaler" . Postscaler works after signal has come to the microcontroller .It isn't very necessary in this tutorial.

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".

After this stage signal comes to prescaler. In this tutorial we will use 1:4 prescaler. That means we are dividing the system clock by 4. So, it means when 4 system clock pulse comes to prescaler, it sends out 1 pulse to Timer0 register . In this way timer0 register is being incremented .

Basically, when one pulse enters into theTimer0 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 inCalculation(4th Part)

Setup Timer0 Register :

Bit 0 to 2 are used to select prescaler. As we are using 1:4 prescaler , 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).

Leave Your Message Here

Google+ Followers

Like on Facebook

Feed

Recent Posts

Circuit Diagram

Schematics is the language of electronics. It provides a concise and comprehensive diagrammatic description of a circuit. Schematics.com allows users to connect and share designs and ideas in a like-minded community.