delay

Status
Not open for further replies.
Code:
/*****************************************************

******/

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

PORTB is never 0 or 1. You have it set to 0x5D (93 decimal), so neither statement is ever true.
 
Code:
#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
 
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.
 
DDR is Data Direction Register

Ah, right. I'm used to PICs, where it's called the TRIS ("tri-state") register.

PORTD stay ON still and does not change.

Read my previous comment:

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

You're never changing PORTB, so you're never actually telling PORTD to change.
 
Code:
#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.
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. Sorry for my ignorance
 

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.
 
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.
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…