Always High

Go To Last Post
8 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

/* Include useful pre-defined functions */
#include     // Defines pins, ports, etc to make programs easier to read
#define F_CPU 100000UL	      // Sets up the default speed for delay.h
#include 
#include 
#include 
/// Prototypes //////// 
void InitPorts (void); 

/// Defines ///////////
int main()
 {
 //InitPorts();   
 DDRD &= ~(1<<PD6);//set PORTD pin0 to zero as input
 PORTD |= (1<<PD6);//Enable pull up
 PORTD |= (1<<PD7);//led OFF
 DDRD |= (1<<PD7);//set PORTD pin1 to one as output

while(1) 
 {
 if (bit_is_clear(PIND, PD0))//if button is pressed
  {
   PORTD &= ~(PD7);//led ON
   loop_until_bit_is_set(PIND, PD6);//LED ON while Button is pressd
   PORTD |= (PD7);//led OFF	
   }
   
  }
return(0);
}

I use that code to try the high and low function, but the problem it is always HIGH. Someone knows what I am doing wrong?

Last Edited: Thu. Dec 14, 2006 - 05:25 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I don't get it - you set PD6 as the input (presumably for the button?) but then your bit_is_clear() test is reading PD0

By the way, always use '/' in #include paths and don't EVER include iom8.h - include just io.h and the right sub-header wlil be pulled in anyway.

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
   PORTD &= ~(PD7);//led ON 

Don't you want to do

PORTD &= ~(1<<PD7);

like in your motor control defines?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Offcouse, I have to code it better. I am testing it now :)
----
After testing:
Only when I press the button and reset the ATmega8, the output is high(when I release the button is going to low), but when I press again it is not high again.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well you did fix the "PORTD |= (PD7);" to be "PORTD |= (1<<PD7);" as well didn't you?

Cliff

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, I did.

int main()
 {
 //InitPorts();   
 DDRD &= ~(1<<PD6);//set PORTD pin6 to zero as input
 PORTD |= (1<<PD6);//Enable pull up
 PORTD |= (1<<PD7);//led OFF
 DDRD |= (1<<PD7);//set PORTD pin7 to one as output

while(1) 
 {
 if (bit_is_clear(PIND, PD6))//if button is pressed
  {
   PORTD &= ~(1<<PD7);//led ON
   loop_until_bit_is_set(PIND, PD6);//LED ON while Button is pressd
   PORTD |= (PD7);//led OFF
  
   }
   
  }
return(0);
}

And offcourse the oim8.h and '/'

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
PORTD |= (PD7);//led OFF

Need to turn that into a bitshift too to create the proper mask:

PORTD |= (1 << PD7);//led OFF

- Dean :twisted:

Make Atmel Studio better with my free extensions. Open source and feedback welcome!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Sorry, I thought I already did that, sorry.(ashamed)