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 / Topics / whats wrong with my coding?

whats wrong with my coding?

|

Microcontroller › PIC › whats wrong with my coding?

  • This topic has 2 replies, 3 voices, and was last updated 14 years, 8 months ago by romel emperado.
Viewing 3 posts - 1 through 3 (of 3 total)
  • Author
    Posts
  • June 9, 2011 at 10:31 am #990
    thanikai
    Participant

    below is my coding, where i used to ON and OFF 2 LEDs using 2 push button… the conditions were as below:-

    – when push button 1 presed, LED 1 ON after 2.5 sec and followed by LED 2 ON after another 2.5 sec.

    – when push button 2 presed LED 2 OFF after 2.5 sec and followed by LED 1 OFF after another 2,5 sec…

     

    when i simulate using proteus, there was no found problem and it function as the condition above.. but when i applied it through hardware, the LEDs ON and OFF automatically without press push button..

     

    pls any1 help me to identify which part of my coding hs problem.. 10q

     

    #include <htc.h>
    #include <pic.h>
    //#include <delay.h>

    #define _XTAL_FREQ 20000000
    int count = 0;

    void main()
    {
    TRISA = 0x11100000; // RA0 to RA2 as input, rest as output
    TRISB = 0x00000000; // port B output
    PORTB = 0x00; //clear portB

    while (1)
     
     
     {

     if (RA0 == 1)
      {
       __delay_ms(1000);  //delay 2.5 s

       RB4 = 1;   //LED on
       __delay_ms(1000);  //delay 2.5 s

       RB5 = 1;   //Relay on
       count = 1;
       
      }

     if (RA1 == 1)
      {
       __delay_ms(1000);  //delay 2.5 s

       RB5 = 0;   //Relay off
       __delay_ms(1000);  //delay 2.5 s

       RB4 = 0;    //LED off
       count = 2;
      }
     }
    } 

     

     

    June 20, 2011 at 3:32 am #6362
    kari
    Participant

    Add pull down resistors to RA0 and  RA1 in HW.

    June 22, 2011 at 11:18 am #6370
    romel emperado
    Participant

    first you need to enable PORTA as input by changing the value of CMCON register.. check the datasheet of your device.

     

     

    your conditional statement is inside the while(1) loop, so if you release you button the value of the port will change and it will not satisfy your needs.. put also a pull down resistor to your input so that will toggle to 1 when the button is pressed.

     

    just make a flag that the button is pressed.. do it like this

     

    #include<htc.h>  // you dont need to include pic.h when htc.h is there..

     

    // dont forget to set the proper configuration bits.

     

    void main()
    {
        bit x = 0, y= 0;;
        //your I/0 initiallization here

      // dont forget to set CMCON
        
        while(1)
        {
            if(RA0 == 1)
            x = 1; //set as flag when RA0 is high
           
            if(RA1 == 1)
            y = 1;
           
            if(x == 1)
            {
                //led is on
                //relay is on
                // put your delay here
                x= 0; //clear your flag
            }
           
            if(y == 1)
            {
                //led is off
                //relay is off
                // put your delay here
                y= 0; //clear your flag
            }
           
        }

    }

     

     

  • Author
    Posts
Viewing 3 posts - 1 through 3 (of 3 total)
  • You must be logged in to reply to this topic.
Log In

RSS Recent Posts

  • analog logic of shmidt trigger bjt circuit February 15, 2026
  • Harman Kardon radio module BMW noise February 15, 2026
  • CR2/CR123A Batteries In Projects February 15, 2026
  • RC Electronic Speed Control Capacitors February 15, 2026
  • Convenient audio FFT module? February 15, 2026

Stay Up To Date

Newsletter Signup
EngineersGarage

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