696Views12Replies
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!
Comments
12 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.??
Reply 12 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...
13 years ago
Reply 13 years ago
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):
(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.)
Reply 13 years ago
When i tryed your code, it gaved a error on your &= and l= sections. Have any idea why?
Reply 13 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?
Reply 13 years ago
I'm sorry i did some research and found help on it. I'm good now.
http://www.avrtutor.com/tutorial/thermo/ioports.php
I use AVRDUDE
Reply 13 years ago
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.
Reply 13 years ago
Oh, and since it's using internal pullups--the button (when closed) is connected to GND (not VCC...)
Reply 13 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!
Reply 13 years ago
gmoon looks like he's got more recent experience with AVR than I, since I missed the PINB vs PORTB issue. I had also assumed that you already knew about changing pin direction since you were already outputting to pins, and the default power-up state is that pins are INPUTs.
13 years ago
Thank you all very much for the help!