Connect with us

Very Simple Circuit but cannot work! Please help.

Discussion in 'Microcontrollers, Programming and IoT' started by JayJoe, Jul 31, 2015.

Scroll to continue with content
  1. JayJoe

    JayJoe

    12
    1
    Jul 31, 2015
    void initiate_main()
    {
    trisa=1; porta=0;
    trisd=0; portd=0;

    }

    void main()
    {
    initiate_main();
    while(1)
    {
    if(porta.f0==1)
    {
    portd.f0=1;
    }

    else
    {
    portd.f0=0;
    }
    }
    }

    What's wrong with the programming?
     

    Attached Files:

  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,582
    2,360
    Nov 17, 2011
    portd.f0 = 0 or 1, but the LED is on portb.
    Either change the HW port of the assignment in the SW.
     
  3. JayJoe

    JayJoe

    12
    1
    Jul 31, 2015
    oh, i uploaded the wrong picture. anyway, stick with this circuit ive uploaded, even ive change the programming code to port b, it still doesnt work!
     
  4. Harald Kapp

    Harald Kapp Moderator Moderator

    10,582
    2,360
    Nov 17, 2011
    The programming seems o.k.
    Is the oscillator running? Is the power supply o.k.? Is the LED in the correct orientation (short leg = cathode to ground)?
     
  5. JayJoe

    JayJoe

    12
    1
    Jul 31, 2015
    Ive found the answer. Port A cannot be input. It turns out working when i use port D as input. can anybody tell me why?
     
  6. Harald Kapp

    Harald Kapp Moderator Moderator

    10,582
    2,360
    Nov 17, 2011
    That should not be the case. PORT A should be useable as input or output, depending n the state of TRISA.
     
  7. JayJoe

    JayJoe

    12
    1
    Jul 31, 2015
    Hmm. I am thinking the same too, but it works using port d instead of port a. Still wondering.
    Sorry that i'm still new to PIC, thanks for your reply.
    Anyway, may i know the word 'TRIS' stands for?
     
  8. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    I believe it means "Tri-State" Register. IE a port that can have one of 3 states. High (5V), Low(0V) or Open(Hi Z).

    Chris
     
  9. davenn

    davenn Moderator

    13,672
    1,891
    Sep 5, 2009
    from this www site ..... http://www.mikroe.com/chapters/view/16/chapter-3-pic16f887-microcontroller/

    3.3 INPUT/OUTPUT PORTS
    In order to synchronize the operation of I/O ports with the internal 8-bit organization of the microcontroller, they are, similar to registers, grouped into five ports denoted by A, B, C, D and E. All of them have several features in common:

    • For practical reasons, many I/O pins are multifunctional. If a pin performs any of these functions, it may not be used as a general-purpose input/output pin.
    • Every port has its ‘satellite’, i.e. the corresponding TRIS register: TRISA, TRISB, TRISC etc. which determines the performance of port bits, but not their contents.
    By clearing any bit of the TRIS register (bit=0), the corresponding port pin is configured as an output. Similarly, by setting any bit of the TRIS register (bit=1), the corresponding port pin is configured as an input. This rule is easy to remember 0 = Output, 1 = Input.

    fig3-35.gif

    PORTA and TRISA register
    Port A is an 8-bit wide, bidirectional port. Bits of the TRISA and ANSEL registers control the Port A pins. All Port A pins act as digital inputs/outputs. Five of them can also be analog inputs (denoted by AN):

    fig3-36.gif


    Dave
     
    Gryd3 likes this.
  10. CDRIVE

    CDRIVE Hauling 10' pipe on a Trek Shift3

    4,960
    648
    May 8, 2012
    I love the graphics on the MikroElectronika website.

    Chris
     
    davenn likes this.
  11. JayJoe

    JayJoe

    12
    1
    Jul 31, 2015
    Oh, tri-state. Thanks. May I know what do you mean by 'Open(Hi Z)'?
     
  12. JayJoe

    JayJoe

    12
    1
    Jul 31, 2015
    Great. Now I understand better. Thanks for the website davenn! :)
     
    Last edited: Aug 1, 2015
    davenn likes this.
  13. davenn

    davenn Moderator

    13,672
    1,891
    Sep 5, 2009
    Hi Z = High Impedance
     
  14. jayanthd

    jayanthd

    43
    10
    Jul 4, 2015
    Try this code in your mikroC PRO PIC Compiler and it will work. If you have used 4 MHz crystal then select XT for Oscillator typr in edit project dialog box else choose HS.

    Code:
    void initiate_main()
    {
        cmcon = 7;
        cvrcon = 0;
        adcon1 = 0x87;
        trisa = 1;
        porta = 0;
    }
    
    void main()
    {
        initiate_main();
        delay_ms(200);
    
        while(1)
        {
            if(porta.f0 == 1)
            {
                delay_ms(50);
                if(porta.f0 == 1)
                {
                    portd.f0 = 1;
                }
            }
            else if(porta.f0 == 0)
            {
                delay_ms(50);
                if(porta.f0 == 0)
                {
                    portd.f0 = 0;
                }
            }
        }
    }
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-