Thanks to all who help on this forum!
I’m a retired guy, old enough to have started on vacuum tubes in the 60’s. My son gave me Atmel 168 microcontrollers & parts for Christmas and I’m having a great time learning to use it. I really enjoyed the first two tutorials at Sparkfun.com (many thanks to their writer) and I got it up and running without difficulty.
I’m interested in using switches to input to the microcontroller. Couldn’t find much information on how to do that, so I’ve puzzled it out by trial and error. I’m using 10k resistors to ground to pull down each of the PB0 to PB2 pins and I touch the pin side of a resistor with a wire connected to the 5 V supply to switch it on (no pushbutton switches handy). My program simply reads PORTB and writes to PORTC repeatedly:
int x;
int main (void)
{ ioinit(); //Setup IO pins and defaults
DDRB = 0b00000000; // set port B for input
while(1)
{ x = PINB; // read portB
PORTC = x; // output to portC
delay_ms(500);
}
return(0);
I have the Port C pins connected through resistors and LED’s to ground to show the output.
It works! When I pull PB0 high, the LED on PC0 lights! And the other two inputs also work. I read about switch debouncing, but had no trouble at all with it so far.
I’d like to write this up and make it available to other newbies interested in connecting their microcontrollers to switch inputs, but first I have lots of questions for you experienced people.
Why do I have to put “x = PINB” instead of “x = PORTB”? Where was I supposed to find this information - I must have missed lots of other stuff with it. I got the PINB idea from a program that had this line for reading:
pin_status = ~PINB & (_BV(PINB0) | _BV(PINB1));
Why would that be used when PINB alone works?
Can I use PB3 through PB5 as well, even though they are connected to my serial programmer? Is it safe to program with the pull down resistors in place on those pins?
In some programs I see “volatile int x” instead of “int x”. What does the word “volatile” do? Sorry, I took a C course years ago but C doesn’t stick in the mind well. Pascal is much better for amateurs and old people!
It would be much nicer to use interrupts to respond to switches - is there somewhere to find help on that?
The censor declined to post this the first time I wrote; hope it works this time.
Thanks!