Microcontroller inputs in C language?

I'm having problems finding and figuring how to do inputs with mirocontrollers VIA button press >> led goes on.

(I have a ATTINY2313)

Outputs are as simple as PORTB = 0x10 (being hex for pin 16). Keep in mind I'm very new to micros and C. The most I've done was fast-slow blink patterns. How can i do inputs though? SIMPLE code sample is welcomed!

sort by: active | newest | oldest
1-10 of 12Next »
angel_ad9 years ago
microcontroller 8051 as the output sensors,how to program the AVR Butterfly ATMEGA 169V to light the LED on the MCT-8051 by using port B on AVR as input.??
gmoon angel_ad9 years ago
We need a little more info. It's unclear what you're attempting here. And I won't be much help with programming the 8051. I assume the MCT-8051 is this.

You're setting up a data transfer. What direction? (from the AVR to the 8051? or the other way?)

Or is it a bidirectional transfer? That can't be answered simply, since you're describing a "protocol."

If the 8051 port is set for output, how can it be a "sensor?" And how can an "input" in the butterfly control an LED on the MCT-8051? Seems like your data directions are opposite...
westfw9 years ago
i = PORTB;if (i & 0x10) {  // pin B4 was set}
(assuming that you've set up PORTB to be an input; Otherwise you might get something else (like the contents of the output register.))
gmoon westfw9 years ago
Since PORTB is also used (with the DDR ) to set the pin state, you have to read PINB.

Here pin7 of port B is used with an internal pullup, so the pin goes low when the button is pushed (hence the NOT logic in the conditional statement):

#define FIREBUTTON	PB7// initialize registersDDRB &= ~_BV(DDB7);        // clear bit, input fire-buttonPORTB |= _BV(FIREBUTTON);  // set bit, enable pull-up resistor// read the pinif(!(PINB & _BV(FIREBUTTON))){	delay_ms(50);	// debouncing, of a sort...        if(!(PINB & _BV(FIREBUTTON)))        {        // do something here        }}

(This is code lifted from different parts of the file--the #define statement is above all the function defs, and the pin test isn't in a function at all... just snippets, of course.)
Killa-X (author)  gmoon9 years ago
When i tryed your code, it gaved a error on your &= and l= sections. Have any idea why?
gmoon Killa-X9 years ago
Sorry, I didn't see your request for an example earlier.... I could throw something together--the above code was adapted from an atmega8, but it is reusing one of the ISP pins on the 2313, which isn't ideal....Something simple for the 2313 won't be too difficult. Without seeing your code (or the error msg), I can't be sure about the error. Maybe you used the statements outside of a function? (main() or other function...) The define statement is fine outside, but not the init code... Also, can you tell us a bit more about your setup? C compiler? (assuming avrgcc) and programmer?
Killa-X (author)  gmoon9 years ago
I'm sorry i did some research and found help on it. I'm good now.

gmoon Killa-X9 years ago
OK, cool. Here's a little code I whipped up in 15 minutes. I threw it together, so I might as well post it ;-)... Sorry, I forgot to include the "tuned" delay function in my earlier example...

This will blink the LED on/off for as long as you GND PD4....the LED is connected to Vcc and PB1 (as you requested.) I tested it, it works.

/*********************************************AVR button test, 2313*********************************************/#include <avr/io.h>#include <inttypes.h>#define F_CPU 1000000UL  	// 1 MHz#include <util/delay.h>#define BUTTON	    PD4 // button#define LED         PB1 // LED outputvoid delay_ms(unsigned int ms)/* delay for a minimum of <ms> */{	// calibrated delay.	while(ms){		_delay_ms(0.96);		ms--;	}}int main(void){    /* INITIALIZE */	    DDRD &= ~_BV(DDD4);	// clear bit, input fire-button    PORTD |= _BV(BUTTON); // set bit, enable pull-up resistor    DDRB |= _BV(DDB1);	// set bit for output    PORTB |= _BV(LED);	// set bit, off	// infinite loop	while(1) {        if(!(PIND & _BV(BUTTON)))        {        	// blink			PORTB &= ~_BV(LED);	// turn on LED			delay_ms(50);		    // wait			PORTB |= _BV(LED);	    // turn it off			delay_ms(100);        }    }	return(0);}
gmoon gmoon9 years ago
Oh, and since it's using internal pullups--the button (when closed) is connected to GND (not VCC...)
Killa-X (author)  westfw9 years ago
I'm a noob for i'm just starting this. recently i've been messing with beeps and sounds also. Can you please give me a full code example? like a simple: Button on pin PD4, LED on PB1... when button is pushed, led lights. Would like if you could!
1-10 of 12Next »