Beginners: please read this post and this post before posting to the forum.
0 Members and 1 Guest are viewing this topic.
I know all I have to do as far as electronics go is connect one end to +5V, through a resistor (I have some 330ohm and some 1.62kohm laying around, would either of these be a good value?), through the button, and into an analog pin. My problem is in the programming portion. Do I just need to call a2dConvert8bit() on the port the button will be connected to? Then what would be the returned value with the button pressed and not pressed? Would it return 0 if the button is not pressed?
You don't need an A/D input for that.
then connect one end of it to +5V and the other end to a digital input line. The button/switch goes from that line to ground.
(pseudo code - look up the exact syntax in whatever language you use)
//define port functions; example: PORT_ON( PORTD, 6);#define PORT_ON( port_letter, number ) port_letter |= (1<<number)#define PORT_OFF( port_letter, number ) port_letter &= ~(1<<number)#define PORT_ALL_ON( port_letter, number ) port_letter |= (number)#define PORT_ALL_OFF( port_letter, number ) port_letter &= ~(number)#define FLIP_PORT( port_letter, number ) port_letter ^= (1<<number)#define PORT_IS_ON( port_letter, number ) ( port_letter & (1<<number) )#define PORT_IS_OFF( port_letter, number ) !( port_letter & (1<<number) )
//configure ports for input or output - specific to ATmega8void configure_ports(void) { DDRC = 0x00; //configure all C ports for input PORTC = 0x00; //make sure pull-up resistors are turned off DDRD = 0xFF; //configure all D ports for output DDRB = 0xC7; //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex') }
//configure ports for input or output - specific to ATmega8void configure_ports(void) { DDRC = 0x00; //configure all C ports for input PORTC = 0x00; //make sure pull-up resistors are turned off DDRD = 0xF7; //F7 being hex for 0b11110111 DDRB = 0xC7; //configure B ports 0, 1, 2, 6, 7 for output (google search '0b11000111 to hex') }
Basically the internal pull-up resistor is just taking the place of the external resistor that I was going to be using?