1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

delay

Discussion in 'AVR' started by rostyslav, Sep 26, 2014.

  1. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine
    Code (text):
    /*****************************************************

    ******/

    #include <mega168.h>


    void main(void)
    {



    PORTB=0x5D;
    DDRB=0x00;
    PORTC=0x00;
    DDRC=0x00;
    PORTD=0xE0;
    DDRD=0xFF;


    while (1)
          {
                if (PORTB==0)
           {
           PORTD=1;
           }
           if (PORTB==1)
           {
           PORTD=0;
           }
          }
    }
     
    Why does not work? Pin does not change to High and Low.
     
  2. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    That depends on your compiler, but usually you simply set the pin = 1
     
  3. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    PORTB is never 0 or 1. You have it set to 0x5D (93 decimal), so neither statement is ever true.
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine

    Mean an small red square in AVR HIGH?
     
  6. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine
    Code (text):


    #include <mega168.h>


    void main(void)
    {

    PORTB=0xFF;
    DDRB=0x00;


    PORTD=0x00;
    DDRD=0xFF;



    while (1)
          {
       
           if  (PORTB==0xFF)
           {
           PORTD=0xFF;
           }
           else
           {
           PORTD=0x00;
           }
           }
           }
    Is not any changes:banghead:
     
  7. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    First of all, your code is very sloppy. Please be careful with your indentation. If you don't format the code in blocks, it's very difficult to follow.

    Second of all, PORTB never changes from 0xFF, so I would expect PORTD to turn on and stay on.

    What is DDRB and DDRD? I'm not familiar with that register.
     
  8. cowboybob

    cowboybob Well-Known Member Most Helpful Member

    Joined:
    Oct 22, 2011
    Messages:
    3,052
    Likes:
    479
    Location:
    James Island, SC
    Old news...
     
  9. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine
    DDR is Data Direction Register. it is mean that it set all Port B pins as HIGH when 0xFF.

    PORTD stay ON still and does not change.
     
    Last edited: Oct 11, 2014
  10. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Ah, right. I'm used to PICs, where it's called the TRIS ("tri-state") register.

    Read my previous comment:

    You're never changing PORTB, so you're never actually telling PORTD to change.
     
    • Like Like x 1
  11. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine
    how to tell in programm to change PORT when condition is set?
     
  12. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    Put it in an "if" statement and simply change the port value.
     
  13. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine
    Code (text):

    #include <mega168.h>


    void main(void)
    {

    PORTB=0xFF;
    DDRB=0x00;


    PORTD=0x00;
    DDRD=0xFF;



    while (1)
       {
       
          if  (PORTB==0x7F)
       {
           PORTD=0xFF;
       }
           else
       {
           PORTD=0x00;
       }
       }
       }
    :( something wrong. :banghead:
    let debug it step by step.
    I did declare in programm that PORTB (all is High) is input and PORTD (all is Low) is output. Than I did write a condition if PORTB receives a voltage, so Low, PORTD must be changed for HIGH.:facepalm: Sorry for my ignorance:nailbiting:
     
  14. rostyslav

    rostyslav New Member

    Joined:
    Sep 26, 2014
    Messages:
    18
    Likes:
    0
    Location:
    Ucraine
  15. DerStrom8

    DerStrom8 Super Moderator Most Helpful Member

    Joined:
    Aug 9, 2010
    Messages:
    5,515
    Likes:
    507
    Location:
    Vermont (GMT-5:00)
    I don't even know where to begin with that one. That circuit is such a mess! Your battery is backwards, and so is your LED. Your resistor connected to the pushbutton is 0.47 ohms (no idea where the logic in that is) and the overall design is just a huge mess.

    I'm afraid if you don't have any experience with circuits whatsoever, it'll be difficult to help you. You should learn the basics of electronics, such as the symbols, grounds, and so on. Read the articles and e-books provided for free here and at AllAboutCircuits. Once you learn that come back and maybe we can help you further.
     
    • Agree Agree x 1
  16. wkrug

    wkrug Active Member

    Joined:
    May 30, 2010
    Messages:
    280
    Likes:
    29
    Location:
    Germany
    When using a Port as input it is floating.
    So You have to take it to a allowed Logic Level.
    When using a Switch, You should use a pullup Resistor from +5V to the input Pin.
    Then connect the Switch to the Pin and GND.
    When the Switch is open, the Controller "see" a logic high Level, when closed a logic low Level will be seen.

    The ATMEGA serias has integrated Pullups, they can be activated by setting the according PORTx to 1.

    To readout the input Level, You have to read the PINx Register.

    The AVR Assembler supports single input readouts.
    Some "C" Compilers use the Syntax PINx.0, while x ist the Port e.g. B and 0 can be 0...7 for the whished Port.
    When Your compile is not such one, You have to figure out the Port with logical commands like
    PINB & 0b00000001

    For example

    if(PINB.0 == 0)
    {
    PORTD.0=0; //Switch on PORTD.0 = low LED get Current
    }
    else
    {
    PORTD.0=1;
    }

    That should work without any problem.
    To measure some pulses, You could use the Input Capture function.
    You have to configure a timer to count.
    When the level on the according ICP Pin changed the actual Counter Result will be written to the input capture register.
    You can read out it an get a very precise result.
    To get a longer counting period I'll suggest You to use a Timer Overflow Interrupt to count up an external variable.

    To use that function properly, You shoud get more experience by using Interrupts.
     
    • Like Like x 1

Share This Page