IR Remote Controlled Home Automation using PIC Microcontroller

Published  May 11, 2017   38
S Saddam
Author
IR Remote Controlled Home Automation using PIC Microcontroller

In this project, we are going to use a PIC microcontroller to remotely control few AC loads by just using an IR remote. A similar project IR remote controlled Home automation has already been done with Arduino also, but here we designed it on PCB using EasyEDA’s online PCB designer and simulator, and used their PCB designing services to order the PCB boards as shown in the subsequent section of the article. 

At the end of this project you will be able to toggle (ON/OFF) any AC load using an ordinary Remote from the comfort of your Chair/Bed. To make this project more interesting we have also enabled a feature to control the speed of the fan with the help of Triac. All these can be done with simple clicks on your IR remote. You can use any of your TV/DVD/MP3 remote for this project. The different IR signals from the remote are received by the microcontroller which then controls the respective relays via a relay driver circuit. These relays are used to connect and disconnect the AC Loads (Lights/Fan).

Working Explanation:

The working of this project is fairly simple to understand. When a button is pressed on the IR Remote it sends a sequence of code in form of encoded pulses using 38Khz modulating frequency. These pulses are received by the TSOP1738 sensor and then read by the Controller. The Controller then decodes the received train of the pulses into a hex value and compares it with the predefined hex values in our program.

If any match occurs then the controller performs a relative operation by triggering the respective Relay/Triac and the corresponding result is also indicated by on-board LEDs. Here in this project, we have used 4 bulbs (small bulbs) of different colors as lighting loads and another bulb (bigger bulb) is considered to be a fan for demonstration purpose.

We have selected key 1 to toggle the relay1, 2 to toggle the relay2, 3 to toggle the relay3, 4 to toggle the relay4, and Vol+ to increase fan speed and Vol- to decrease speed of the fan.

Note: Here we have used 100watt bulb instead of a fan.

 

There are many types of IR Remotes available for different devices, but most of them work around 38KHz Frequency. Here in this project, we control home appliances using IR TV remote and for detecting the IR signals, we use a TSOP1738 IR Receiver. This TSOP1738 sensor can sense 38Khz Frequency signal. The working of IR remote and the TSOP1738 is covered in detail in this article: IR Transmitter and Receiver

Our PIC microcontroller operates at +5V and the Relays operate at +12V, Hence we use a transformer to step down the 220V AC and rectify it using a full bridge rectifier. This rectified DC voltage is then regulated to +12V and +5V by using the regulator ICs 7812 and 7805 respectively.

To trigger the relay we make use of transistors like BC547 which can act as an electronic switch to turn ON/OFF the relays based on the signal from the PIC microcontroller.Further to control the speed of the fan we are using a TRIAC. TRIAC is a power semiconductor which is capable of controlling the output voltage; this capability is used to control the speed of the fan.

We have also used a Triac Driver to control the Triac using our PIC microcontroller. This driver is used to give a firing angle pulse to Triac, so that the output power can be controlled. Here we have used 6 level of speed control. When the level is 0 then the fan will be off. When level will be 1 then speed will be 1/5th of full speed. When level will be 2 then speed will be 2/5th of full speed and respectively for others. The current level of the speed can be monitored using the on-board 7-segment display.

 

The block diagram of the project is shown below.

Working Block Diagram of IR Remote Controlled Home Automation using PIC

Components: 

The components required to build this project is given below:

  • PIC18f2520 Microcontroller           -1
  • TSOP1738                                       -1
  • IR TV/DVD Remote                      -1
  • Transistor BC547                            -4
  • Relays 12 volt                                 -4
  • Bulb with holder                             -5
  • Connecting wires                            -
  • EasyEda PCB                                 -1                    
  • 16x2 LCD
  • Power supply 12v
  • Terminal connector 2 pin     `           -8
  • Terminal Connector 3 pin                -1
  • Transformer 12-0-12                       -1                                             -
  • Voltage Regulator 7805                  -1
  • Voltage Regulator 7812                  -1
  • Capacitor 1000uf                            -1
  • Capacitor     10uf                             -1
  • Capacitor     0.1uf                            -1
  • Capacitor 0.01uf 400V       `           -1
  • 10k                                                  -5
  • 1k                                                    -5
  • 100ohm                                           -7
  • Common cathode segment -1
  • 1n4007 diode                                  -10
  • BT136 triac                                     -1
  • Male/female header             -
  • LEDs                                               -6
  • Opto-coupler moc3021                   -1
  • Opto-coupler mtc2e or 4n35           -1
  • 20Mhz crystal                                  -1
  • 33pf capacitor                                 -2
  • 5.1v zener diode                              -1
  • 47 ohm 2 watt resistor                     -1

All these components are commonly used and can be easily purchased. However if you are looking for a best buy online then we would recommend you LCSC.

LCSC is a great online store to buy your electronics components for all kinds of projects. They feature about 25,000 kinds of components and the best thing is that they sell even small quantity items for small projects and they also have Global Shipping.

 

Decoding the IR Remote:

As said earlier you can use any kind remote for your project. But we have to know what kind of signal is generated for from that particular remote. For every individual key on the remote there will be an equivalent HEX value for that key. Using this HEX value we can distinguish between each key on our microcontroller side. So before we decide to use a remote we should know the HEX value for the keys preset in that particular remote. In this project, we have used a NEC remote. The HEX values for the keys on a NEC remote is given below.

decimal hex key

As you can notice the HEX value has 7 characters out of which only the last two differs, hence we can consider only the last two digits to distinguish between each keys. 

key1

Circuit Diagram:

The schematic for the project is shown below.

Circuit Diagram for PIC Microcontroller Based Remote Controlled Home Automation

The above schematic was made easy by using esayEDA schematic editor since they provide the layouts of all components used in this project. It also does not require an installation and can be used online on the go. 

The pinouts and component values are clearly specified in the schematic above. You can also download the schematic file from here.

 

Programming:

The program for this project is done using MPLABX, the code is also pretty simple and easy to understand. The complete code will be given at the end of this tutorial, further few important chunks of the program are explained below.

In the beginning of the code, we should include required libraries, define the pins and declare the variables.

#include <xc.h>
#include<string.h>
#include<stdlib.h>
#include "config.h"

#define tric RB1
#define ir RB2

#define relay1 RC2
#define relay2 RC3
#define relay3 RC4
#define relay4 RC5

#define rly1LED RB3
#define rly2LED RB4
#define rly3LED RB5
#define rly4LED RC1
#define fanLED RC0

int flag=0;
int cmd=0;
int speed=5;
unsigned int dat[100];
int i=0;
char result[10];
int j=0;

After that, we have created a simple delay function by using the “for” loop.

void delay(int time)
{
    for(int i=0;i<time;i++)
        for(int j=0;j<800;j++);
}

After that, we have initialized the timer by using the following function

void timer()           // 10 -> 1us
{
   T0PS0=0;
   T0PS1=0;
   T0PS2=0;
   PSA=0;      //Timer Clock Source is from Prescaler
   T0CS=0;     //Prescaler gets clock from FCPU (5MHz)
   T08BIT=0;   //16 BIT MODE
   TMR0IE=1;   //Enable TIMER0 Interrupt
   PEIE=1;     //Enable Peripheral Interrupt
   GIE=1;      //Enable INTs globally
   TMR0ON=1;      //Now start the timer!
}

Now in the main function, we have give directions to the selected pins and initialize timer and external interrupt int0 to detect zero crossing.

   ADCON1=0b00001111;
   TRISB1=0;
   TRISB2=1;
   TRISB3=0;
   TRISB4=0;
   TRISB5=0;
   TRISC=0x00;
   TRISA=0x00;
   PORTA=0xc0;            
   TRISB6=0;
   RB6=1;
    relay1=0;
    relay2=0;
    relay3=0;
    relay4=0;
    rly1LED=0;
    rly3LED=0;
    rly2LED=0;
    rly4LED=0;
    fanLED=0;
        i=0;
    ir=0;
    tric=0;
    timer();
    INTEDG0 = 0; // Interrupt on falling edge
    INT0IE = 1; // Enable the INT0 external interrupt (RB0)
    INT0IF = 0; // Clears INT0 External Interrupt Flag bit
    PEIE=1;     //Enable Peripheral Interrupt
    GIE=1;      //Enable INTs globally

Now, here we are not using any interrupt or capture and compare mode to detect IR signal. Here we have just used a digital pin to read data just like we read a push button. Whenever signal goes high or low we just put debouncing method and run the timer. Whenever pin changes its state to another then time values will be saved in an array.

IR remote send logic 0 as 562.5us and logic 1 as 2250us. Whenever timer reads around 562.5us then we assume it 0 and when timer reads around 2250us then we assume it as 1. Then we convert it in hex.

The incoming signal from remote contains 34 bits. We store all the bytes in the array and then decode the last byte to use.

      while(ir == 1);
       INT0IE = 0;
       while(ir == 0);
       TMR0=0;
       while(ir == 1);
       i++;
       dat[i]=TMR0;

       if(dat[1] > 5000 && dat[1]<12000)
       {
       }
       else
       {
           i=0;
           INT0IE = 1;
       }

       if(i>=33)
       {
        GIE=0;
        delay(50);
        cmd=0;
        for(j=26;j<34;j++)
        {
            if(dat[j]>1000 && dat[j]<2000)
                cmd<<=1;

            else if(dat[j]>3500 && dat[j]<4500)
            {
                cmd|=0x01;
                cmd<<=1;
            }
        }
        cmd>>=1;

The above piece of code receives and decodes the IR signal using timer interrupts and stores the corresponding HEX value in the variable cmd. Now we can compare this HEX value (cmd variable) with our predefined HEX values and toggle the relay as shown below

if(cmd == 0xAF)
         {
             relay1=~relay1;
             rly1LED=~rly1LED;
         }

         else if(cmd == 0x27)
         {
             relay2=~relay2;
             rly2LED=~rly2LED;
         }
        
         else if(cmd == 0x07)
         {
             relay3=~relay3;
             rly3LED=~rly3LED;
         }
         else if(cmd == 0xCF)
         {
             relay4=~relay4;
             rly4LED=~rly4LED;
         }
         else if(cmd == 0x5f)
         {
             speed++;
             if(speed>5)
             {
                 speed=5;
             }
         }

         else if(cmd == 0x9f)
         {
             speed--;
             if(speed<=0)
             {
                 speed=0;
             }
         }

Now to know at which our fan is currently operating, we should use a 7-segment display. The following lines are used to instruct the pins of the 7-segment display.

if(speed == 5)        // turned off  5x2= 10ms triger    //speed 0
        {
            PORTA=0xC0;           // display 0
            RB6=1;
            fanLED=0;            
        }      

        else if(speed == 4 )    //  8 ms trigger    //speed 1
        {
            PORTA=0xfc;        // displaying 1
            RB6=1;
            fanLED=1;
        }
       
        else if(speed == 3)   //   6 ms trigger       // speed 2
        {
            PORTA=0xE4;       // displaying 2                   
            RB6=0;
            fanLED=1;
        }
       
        else if(speed == 2)   // 4ms trigger  // speed 3
        {
            PORTA=0xF0;       // displaying 3
            RB6=0;
            fanLED=1;
        }
       
        else if(speed == 1)    // 2ms trigger  // speed 4
        {
            PORTA=0xD9;          // displaying 4
            RB6=0;
            fanLED=1;
        }
    
        else if(speed == 0)        // 0ms trigger  // speed 5  full power
        {
           PORTA=0xD2;            // displaying 5
           RB6=0;
           fanLED=1;
        }

The below function is for external interrupt and time overflow. This function is responsible for detecting zero crossing and driving the Triac.

void interrupt isr()
{
    if(INT0IF)
    {
        delay(speed);
        tric=1;
        for(int t=0;t<100;t++);
        tric=0;
        INT0IF=0;
    }
  
   if(TMR0IF)  //Check if it is TMR0 Overflow ISR  
   {
      TMR0IF=0;
   }
}

The final PCB for this IR remote controlled home automation looks as shown below:

Final PCB for PIC based remote controlled home automation project

 

Circuit and PCB Design using EasyEDA:

To design this  Remote control home automation we have used EasyEDA which is a free online EDA tool for creating circuits and PCBs in a seamless manner. We have previously ordered few PCBs from EasyEDA and still using their services as we found the whole process, from drawing the circuits to ordering the PCBs, more convenient and efficient in comparison of other PCB fabricators. EasyEDA offers circuit drawing, simulation, PCB design for free and also offers high quality but low price Customized PCB service. Check here for the complete tutorial on How to use Easy EDA for making Schematics, PCB layouts, Simulating the Circuits etc.

EasyEDA is improving day by day; they have added many new features and improved the overall user experience, which makes EasyEDA easier and usable for designing circuits. They are soon going to launch its Desktop version, which can be downloaded and installed on your computer for offline use.

In EasyEDA, you can make your circuit and PCB designs public so that other users can copy or edit them and can take benefit from there, we have also made our whole Circuit and PCB layouts public for this Remote control Home automation.

Below is the Snapshot of Top layer of PCB layout from EasyEDA, you can view any Layer (Top, Bottom, Topsilk, bottomsilk etc) of the PCB by selecting the layer form the ‘Layers’ Window. 

PCB Design for PIC based IR Remote Controlled Home Automation

IR Remote Home Automation PCB Design

 

Calculating and Ordering PCB Samples online:

After completing the design of PCB, you can click the icon of Fabrication output, which will take you on the PCB order page. Here you can view your PCB in Gerber Viewer or download Gerber files of your PCB and send them to any manufacturer, it’s also a lot easier (and cheaper) to order it directly in EasyEDA. Here you can select the number of PCBs you want to order, how many copper layers you need, the PCB thickness, copper weight, and even the PCB color. After you have selected all of the options, click “Save to Cart” and complete your order, then you will receive your PCBs within few days.

Ordering PCB on EasyEDA

You can directly order this PCB or download the Gerber file using this link.

After few days of ordering PCB’s we got the PCBs. The boards that we received are shown below.

PCB boards for IR remote controlled home automation project

Once we received the PCBs I mounted all the required components over the PCB, and finally we have our IR Remote Controlled Home Automation ready, check this circuit working in demonstration video at the end of the article.

PIC home automation PCB after soldering components

PIC based home automation PCb

Code

 

#include <xc.h>
#include<string.h>
#include<stdlib.h>
#include "config.h"

#define tric RB1
#define ir RB2

#define relay1 RC2
#define relay2 RC3
#define relay3 RC4
#define relay4 RC5

#define rly1LED RB3
#define rly2LED RB4
#define rly3LED RB5
#define rly4LED RC1
#define fanLED RC0

int flag=0;
int cmd=0;
int speed=5;
unsigned int dat[100];
int i=0;
char result[10];
int j=0;
   
void delay(int time)
{
    for(int i=0;i<time;i++)
        for(int j=0;j<800;j++);
}

void timer()           // 10 -> 1us
{
   T0PS0=0; 
   T0PS1=0;
   T0PS2=0;
   PSA=0;      //Timer Clock Source is from Prescaler
   T0CS=0;     //Prescaler gets clock from FCPU (5MHz)
   T08BIT=0;   //16 BIT MODE
   TMR0IE=1;   //Enable TIMER0 Interrupt
   PEIE=1;     //Enable Peripheral Interrupt
   GIE=1;      //Enable INTs globally
   TMR0ON=1;      //Now start the timer!
}

void main(void) 
{
   ADCON1=0b00001111;
   TRISB1=0;
   TRISB2=1;
   TRISB3=0;
   TRISB4=0;
   TRISB5=0;
   TRISC=0x00;
   TRISA=0x00;
   PORTA=0xc0;             
   TRISB6=0;
   RB6=1;
    relay1=0;
    relay2=0;
    relay3=0;
    relay4=0;
    rly1LED=0;
    rly3LED=0;
    rly2LED=0;
    rly4LED=0;
    fanLED=0;
        i=0;
    ir=0;
    tric=0;
    timer();
    INTEDG0 = 0; // Interrupt on falling edge
    INT0IE = 1; // Enable the INT0 external interrupt (RB0) 
    INT0IF = 0; // Clears INT0 External Interrupt Flag bit
    PEIE=1;     //Enable Peripheral Interrupt
    GIE=1;      //Enable INTs globally

   while(1)
   {   
       while(ir == 1);
       INT0IE = 0;
       while(ir == 0);
       TMR0=0;
       while(ir == 1);
       i++;
       dat[i]=TMR0;
       
       if(dat[1] > 5000 && dat[1]<12000)
       {
       }
       else
       {
           i=0;
           INT0IE = 1;
       }

       if(i>=33)
       {
        GIE=0;
        delay(50);
        cmd=0;
        for(j=26;j<34;j++)
        {
            if(dat[j]>1000 && dat[j]<2000)
                cmd<<=1;

            else if(dat[j]>3500 && dat[j]<4500)
            {
                cmd|=0x01;
                cmd<<=1;
            }
        }
        cmd>>=1;
         
         if(cmd == 0xAF)
         {
             relay1=~relay1;
             rly1LED=~rly1LED;
         }
         
         else if(cmd == 0x27)
         {
             relay2=~relay2;
             rly2LED=~rly2LED;
         }
         
         else if(cmd == 0x07)
         {
             relay3=~relay3;
             rly3LED=~rly3LED;
         }
         
         else if(cmd == 0xCF)
         {
             relay4=~relay4;
             rly4LED=~rly4LED;
         }
         
         else if(cmd == 0x5f)
         {
             speed++;
             if(speed>5)
             {
                 speed=5; 
             }
         }
         
         else if(cmd == 0x9f)
         {
             speed--;
             if(speed<=0)
             {
                 speed=0; 
             }
         }
         
        if(speed == 5)        // turned off  5x2= 10ms triger    //speed 0
        {
            PORTA=0xC0;           // display 0
            RB6=1;
            fanLED=0;             
        }
        
        else if(speed == 4 )    //  8 ms trigger    //speed 1
        { 
            PORTA=0xfc;        // displaying 1
            RB6=1;
            fanLED=1;
        }
        
        else if(speed == 3)   //   6 ms trigger     // speed 2
        {
            PORTA=0xE4;       // displaying 2                    
            RB6=0;
            fanLED=1;
        }
        
        else if(speed == 2)   // 4ms trigger  // speed 3
        {
            PORTA=0xF0;       // displaying 3
            RB6=0;
            fanLED=1;
        }
        
        else if(speed == 1)    // 2ms trigger  // speed 4
        {
            PORTA=0xD9;          // displaying 4
            RB6=0;
            fanLED=1;
        }
        
        else if(speed == 0)        // 0ms trigger  // speed 5  full power
        {
           PORTA=0xD2;            // displaying 5
           RB6=0;
           fanLED=1;
        }
        
        else
        {
            RB6=1;
            PORTA=0xff;         // display off
            fanLED=0;
        }
        
        i=0;
        INT0IE = 1;
        GIE=1;
       }
}
}

void interrupt isr()
{
    if(INT0IF)
    {
        delay(speed);
        tric=1;
        for(int t=0;t<100;t++);
        tric=0;
        INT0IF=0;
    }
    
   if(TMR0IF)  //Check if it is TMR0 Overflow ISR   
   {
      TMR0IF=0;
   }
    
}

Video

Have any question realated to this Article?

Ask Our Community Members

Comments

Submitted by Bogere on Thu, 05/18/2017 - 16:08

Permalink

Am a beginner of the pic language, i have picked interest in your project .My biggest problem is how to start writing the instructions on the MPLABX

Submitted by Abel on Fri, 09/08/2017 - 18:10

Permalink

but the above codes are not well explained like the previous PIC tutorial with PIC16F877A... few comment lines, reason why they're used, no data sheet references like the previous ones nothing is explained ... may be he doesn't want us to understand both the PIC and codes

Submitted by Abel on Tue, 09/12/2017 - 20:03

Permalink

i do not understand all these codes ... what and what are you trying to do ? and how ,,, full explanation would do better and i'll really appreciate it..
/********************************************************************************/

while(ir == 1);
INT0IE = 0;
while(ir == 0);
TMR0=0;
while(ir == 1);
i++;
dat[i]=TMR0;

if(dat[1] > 5000 && dat[1]<12000)
{
}
else
{
i=0;
INT0IE = 1;
}

if(i>=33)
{
GIE=0;
delay(50);
cmd=0;
for(j=26;j<34;j++)
{
if(dat[j]>1000 && dat[j]<2000)
cmd<<=1;

else if(dat[j]>3500 && dat[j]<4500)
{
cmd|=0x01;
cmd<<=1;
}
}
cmd>>=1;

The full explanation maybe out of the scope of this article or the auther have to open another topic about IR communication protocol for NEC remote controle which helps you better uderstand whats going on in this part of code . i suggest you to search 

IR protocol 1.JPG

ir protocol 2.JPG

Submitted by abel on Thu, 10/12/2017 - 04:34

Permalink

Please from
* if(dat[1] > 5000 && dat[1]<12000)
{
}
Does it mean...
if(dat[1] > 5000 && dat[1]<12000)
{
//do nothing
}???
Please explain

at this part the code doing nothing only reading the value of time and register to dat[i] and count i from 1 to 33 , next will read the code according to NEC protocol ( zero and one) which  I send picture above one by one 

at theend we only need the last 8 bit to know which relay turn on or off

Submitted by Miguel on Thu, 03/22/2018 - 00:05

Permalink

Hi, can you send the .hex file, or the config.h file pls.

thanks, its a bit urgent. it's my final work for school.

Submitted by Francis on Fri, 03/30/2018 - 19:16

Permalink

Please send me the mikro c source code version for this project.
Also,in the schematic,I did not see the connection between the TSOP1738 and the PIC.

Submitted by Danny Van de Steen on Tue, 04/10/2018 - 14:54

Permalink

Hello,

Where can i find a comiled version (= *.hex file) of the software? I am using the PIC18F2520 like described on the web page.
Secondly i am looking for the exact configuration words for the PIC18F2520.
Many thanks in advance,

Danny Van de Steen

Hi Danny,

The HEX file for this program is not given directly. You have to compile this program on your IDE to generate the HEX file. 

The configuration words is explained below

 
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF        // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

Submitted by SA on Tue, 04/24/2018 - 10:46

Permalink

sir,
Is there needed to fill UN-USED memory to prevent malfunction of operation of the MCU.
And could u please let me expand the matter and how to fill the unused memory for a MCU.
TNX.

Submitted by sa on Wed, 05/30/2018 - 01:50

Permalink

In o/p i have used all triac instead of relays. but problems is, when the o/p(connected with 40w FTL, 4Feet) is turned on then all others o/p are start flickering and stop till start glowing of the FTL,
Although, i have tried using MOV in 230v i/p A/C section to prevent this. I think the rating of MOV used is not appropriate to prevent this effect of FTL causing other flickering,
Cud u please suggest, is mov right choice if so then what spec, of mov should i used for that problems????

tnx

Submitted by ahsan ullah on Fri, 06/01/2018 - 10:04

Permalink

hy i am very impressed with your project. but i need some help if you can olease.
1- can i use 4MHZ crystal with this program??
2- i want to use PIC 16F72 with MikroC can you please convert this code in mikroC??

Submitted by deb on Mon, 06/11/2018 - 23:27

Permalink

hi
Nice work. I am a senior citizen and still an electronic hobbyist . I want to make this for my own use.
I am not familiar in MPLAB but slightly worked on Micro C after retirement and also can manage writing hex files in Pics. So it will be of great help to this old man if you can send me the .hex file or suitable c file for generating hex in Micro C.
Regards

Hi ded,

Its really inspiring to see people like you around here. But I don't think the author will respond back with a hex file since it is not recommended as a good sign, so the best for you to proceed is to use a development board like Arduino. This will make your building process a 100 times easier and the code will be easily available online 

Submitted by sa on Sat, 06/30/2018 - 19:37

Permalink

can you explain as i do not understand the code
/********************************************************************************/

while(ir == 1);
INT0IE = 0;
while(ir == 0);
TMR0=0;
while(ir == 1);
i++;
dat[i]=TMR0;

if(dat[1] > 5000 && dat[1]<12000)
{
}
else
{
i=0;
INT0IE = 1;
}

if(i>=33)
{
GIE=0;
delay(50);
cmd=0;
for(j=26;j<34;j++)
{
if(dat[j]>1000 && dat[j]<2000)
cmd<<=1;

else if(dat[j]>3500 && dat[j]<4500)
{
cmd|=0x01;
cmd<<=1;
}
}
cmd>>=1;

-------------------------
and how 10 ms is get from here:
if(speed == 5) // turned off 5x2= 10ms triger //speed 0

whereas 2.5 instruction is 1usec
pl anybody can explin...

Submitted by sa on Sat, 07/07/2018 - 12:00

Permalink

void delay(int time)
{
for(int i=0;i<time;i++)
for(int j=0;j<800;j++);
}

this is wrong code for 20mhz crystal. it should be 16 mhz to get 2 ms from delay();
tcy=16/4=4mips;
period=1/4=2.5us;
when delay(1);
then 800*2.5=2000us=2ms;;;;;
otherwise, inner for loop should be 1000;
Please comment am i right or nor????

sa

NO NO . sorry for the wrong information . Usinf loops to create delay and how to calculate the the time delay need you to go to assembly language and use MPLAP watch dog timer and count how many instruction cycle take to increment 'i' from '0' to 'n' which is not as simple formula you write here . small delays using loops usually are not accurate