Newbie problems

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

Hello everyone
I bought my stk500 yesterday.
And I am woundering about, why my code dosent seem to work..
I do not understand the io addressing system yet,
I woud be really happy if anyone coud explain it to me.
The problem I am having is getting the value of a button pressed and parsing it..
I can do ( outp(inp(PIND),PORTB); ) but I need to parse wich button thats pressed.

//Anders
---------------

#include

unsigned char a;

int main(void) {
outp(0xff,DDRB);
outp(0x00,DDRD);
outp(0xff,PORTB);

for (;;){
a = inp(PIND);
if ( a == 1 ) {
outp(~3,PORTB);
}
}
return(0);
}

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

i think first set data directions then ports you didnt set ddrb

clr temp
out DDRB, temp
out DDRD, temp
ser temp
out PORTB, temp
out PORTD, temp

well thats asm

admin's test signature
 

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

I did set em..
:/
they are at the top..

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

you read a port (in temp, PINB) now the bits are in temp etc now you can do something with these bits

admin's test signature
 

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

Hm the actual problem is that I dont know what they return when _high_ so I cant distinguish it from low

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

wait:
sbic temp, 0
rjmp wait
do something....

when bit0 is cleared (button pushed) then skips rjmp wait, well play with the instructions, or commandos adios

admin's test signature
 

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

activate internal pullups and button to gnd then a push = lo

admin's test signature
 

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

you can command a LED

.def LED = 6

cbi PORTD, LED

sbi PORTD, LED

on / off :)

admin's test signature
 

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

yeah I can do that.. but the problem is how do i get the value of the button when it is pushed.. I need the hex address.. of that.. since otherwise I cant make if statements

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

When you read from pind you read from an eight bit register, in the format of:

PIND.7-PIND.6-PIND.5-PIND.4-PIND.3-PIND.2-PIND.1-PIND.0

so for instance this BINARY value on pind:
1 1 1 1 1 1 1 0
means that pind.0 is low, and pind.1 to pind.7 are all high. So translating binary to hex, you get 11111110=FE

hope this helps!

-Colin

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

;***** STK500
; If PIND Bit 7 is Set then Light PORTB Bit 0

.include "8515def.inc"

.def Temp =r16

;**** Initialization

RESET:

ldi Temp,0x00
out DDRD,Temp ; Make PORT D Input
ldi Temp,0xFF
out DDRB,Temp ; Make PORT B Output
ldi Temp,0xFF ; Turn LEDs Off
out PORTB,Temp

;**** Input - PIND.7
;**** Output - PORTB.0

START:
sbis PIND,7 ; Is PIND Bit 7 Set (PIND.7)
rjmp End ; Skip this instruction if True
ldi Temp,0b11111110 ; Setup to Light PORTB Bit 0 (PORT.0)
out PORTB,Temp ; Do it
rjmp Start ; One more time, one more time
END:
ldi Temp,0b11111111 ; Otherwise turn off all LEDs
out PORTB,Temp ; Do it

rjmp Start ; One more time, one more time

admin's test signature
 

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

Thank you for your replies, The problem is that I dont program assembler
I intend to learn it later on, But I do program C, But I dont understand i/o of the atmel chips just yet, I woud be very happy if anyone coud provide me with an example source.. for reading inputs.

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

Hi,

I am not sure if it is what you are looking for but take a look at the mega128 (even though you may not be using the mega128) datasheet. I believe they give C examples regarding reading and writing to ports. Apologies if I am wrong and waste your time. I believe you can find these examples in the I/O ports section.

I have had problems downloading the 128 datasheet here but the datasheet at www.atmel.com always downloads.

Regards,
Steve

admin's test signature
 

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

I'm a newbie too, but take a look at Colin's above post.
What he's saying is that you can do something like this:

#define PIN7 0x80
#define PIN6 0x40
#define PIN5 0x20
#define PIN4 0x10
#define PIN3 0x08
#define PIN2 0x04
#define PIN1 0x02
#define PIN0 0x01

unsigned char a;

int main(void) {
outp(0xff,DDRB);
outp(0x00,DDRD);
outp(0xff,PORTB);

for (;;){
a = inp(PIND);
if (a & PIN7)
; /* Pin7 is on*/
if (a & PIN6)
; /* Pin7 is on*/

/* Skip ahead, brother */

if (a & PIN0)
; /* Pin7 is on*/
}
}
return(0);
}

admin's test signature
 

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

As being a newbie too;

Is there any other way to program the stk500 than in assembler? (what tools, and can they load the data directly into the atmega163 or so i have to crosscompile this somehow?)
--
Plus, how can i send a byte back to the com1 port if i press an switch/button? this has been my mayer problem so far.

Any help would really be appreciated
tia

admin's test signature
 

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

Hi,

First off, remeber that it is possible for more than one button to be pressed at a time, so you could get 11101110, which is EE hex.

When you use an compiler to convert the language of the compiler to assembly (actually to hex, but assembly can come out of hex). Once you get this file, you download this to the AVR. You can use any language that a compiler supports, including BASIC and C.

If you use BASIC or C it is fairly simple to communicate with the computer. Its not too bad in assembly either, check the "Academy" section of this site, then goto "users own projects", you should find something in there.

good luck!

-Colin

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

#include

int main(void) {
unsigned int a;
outp(0xff,DDRB); // set Data direction to OUT
outp(0x00,DDRD); // set Data direction to IN
for (;;) { // loop
a = inp(PORTD); // fetch from pin
if (( a & 0) > 0) { //never happens for some weird reason
outp(~1,PORTB);
} else {
outp(~2,PORTB);
}

}
return(0);
}

That is the code I am using now.. everything works until I try to get data from a pin..

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

if (( a & 0) > 0) { //never happens for some weird reason

Ooops ! 0 isn't often greater than 0 unless you're a politician.

Jon

admin's test signature
 

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

(a & 0 ) is supposed to be bit 0 of variable a

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

(a & 1) is bit 0 of variable a. Many people use BIT(0) to signify 1. It is also preferable to use inp(PIND) if you want the actual input to the pin. And 'a' may need to be declared volatile depending on which compiler you use so that the actual read is not optimized out of the loop. (Search this site for volatile.)
Dave Raymond

admin's test signature
 

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

AVRgcc is what I am using :)

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

I still havent got any success in doing this.. :( *desperate*
Thanks for all your answears

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

PLS HELP!!!!!!!!! We are very newbies in need of help. We have no idea how to change the program name to mcg308j.

admin's test signature
 

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

PLS HELP!!!!!!!!! We are very newbies in need of help. We have no idea how to change the program name to mcg308j.

admin's test signature
 

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

Anders,

Try something like this:

unsigned char a;

outb( 0xFF, DDRB );
outb( 0, DDRD );

for( ; ; )
{
a = inp(PIND);
outb( a, PORTD );
}

admin's test signature
 

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

Seems to me that Jon pointed it out, but maybe didn't make it clear enough:

a & 0 is ALWAYS 0.

What You get when reading a port, as in a = inp(PIND), is a byte where each bit represents the state of a pin in the port. If You would like to test a individual pin in the port You could isolate (or "mask off") the coresponfing bit. Going for the &- (and-) operator is correct, but You have to "and" the port-value with a byte where the bit corresponding to the pin You want to test is set.

Eg: You want to test pin 3. This corresponds to a byte 00001000 in binary (remember, convetions is that pins have numbers 7..0!) If You want that in hex that's 0x08. Now "and" this with what You read from the port and test if it is NON-ZERO, indicating that the bit is set:

if ( ( a & 0x08 ) != 0 )

or more compact like this:

if ( a & 0x08 )

You could also do

if ( inp( PIND ) & 0x08 )

thus getting rid of the intermediary variable.

In Your original post You attempted to react on a pin, but took no action if the pin was not set. Thus: Even if You succeeded in lighting up a diode when pressing a button it would stay lit "forever".

In the above getting the data from the port is AVR-specific. Nothing else is and so the problem was a (C) programming problem.

Elaborating a bit on Your original post the main loop could look something like:

for (;;){
a = inp(PIND);
if ((a & 0x01)){
outp(0x01,PORTB);
}
else {
outp(0x00,PORTB);
}
}

Caveat: I'm a C/C++ programmer when at work, but when hobbying on the AVR I'm using assembler so look out for typo's and the like above. Also - I'm convinced there are a few, probably better, ways to read a pin...

(And some "jibberish" in swedish IF thats Your first language, Anders: Om du fortfarande är desperat så skicka mej ett mejl, men var medveten om att jag inte kan garantera ett blixtsnabbt svar.)

As of January 15, 2018, Site fix-up work has begun! Now do your part and report any bugs or deficiencies here

No guarantees, but if we don't report problems they won't get much of  a chance to be fixed! Details/discussions at link given just above.

 

"Some questions have no answers."[C Baird] "There comes a point where the spoon-feeding has to stop and the independent thinking has to start." [C Lawson] "There are always ways to disagree, without being disagreeable."[E Weddington] "Words represent concepts. Use the wrong words, communicate the wrong concept." [J Morin] "Persistence only goes so far if you set yourself up for failure." [Kartman]

Last Edited: Fri. Mar 19, 2004 - 01:27 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Thank you for all your answears I came over the answear to this question in a AVR-GCC manual... :) I am going for vacation this week, Ill post the example code I used and the link to the documents afterwards