Atmega 164 C code

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

As a part of my project work i was given a ckt diagram and also a program.
This is a small internal project so i was just asked to tell what the program does.
I am not able to understand the program.Please help me to understand it

Attachment(s): 

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

The program only seems to have a distant passing relationship to the far more complex circuit it's running on.

Also the code is making use of inputs that in the schematic are not connected to anything.

The code also seems to suggest that the relays are on the bottom 4 bits of PORTB while the circuit shows them on middle bits of PORTC.

I'd have said these two things do not go together.

Clearly the program is reading a key matrix whereas the circuit looks like something that would connect to a phone and decode inbound DTMF tones to control the various relays.

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

clawson wrote:
The program only seems to have a distant passing relationship to the far more complex circuit it's running on.

Also the code is making use of inputs that in the schematic are not connected to anything.

The code also seems to suggest that the relays are on the bottom 4 bits of PORTB while the circuit shows them on middle bits of PORTC.

I'd have said these two things do not go together.

Clearly the program is reading a key matrix whereas the circuit looks like something that would connect to a phone and decode inbound DTMF tones to control the various relays.

Please help me understand the.logic being checked.
Thanx,i did believe the circut ant code did not go together but was not sure.i am interested in understanding the project code.provided as I did not understand the code at all !

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

Well, the code looks fairly straightforward. But there are virtually no comments at all. There are several ways that the code could be edited to make it easier to understand.

If your teacher wants you to understand 'foreign' code, you must simply add comments to explain what is happening.

If your teacher wants you to re-write the code, you make your alterations to the code.

If your teacher wants you to spot the deliberate mistake, you take the heavy hint given earlier. (does the code match the circuit wiring ?)

I would print the code. Then scribble all over it with a pencil, adding arrows and explanations.

When I have understood it, I would type in some comments. Finally presenting your teacher with 'annotated' code. Note that your 'annotations' should be in English. Do not comment things that are 'obvious' like 'a = b + c'

David.

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

Quote:

i did believe the circut ant code did not go together but was not sure.

Scene from "Ocean's 47". George Clooney's hair is not only shot with grey, but virtually gone on top. Meg Ryan is the love interest but now bears a remarkable similarity to Estelle Getty in the Golden Girls. The whole gang ain't what it used to be.

Danny: "OK, are we all set to knock over the Majestic?"

Henchman #1: "Yeah, boss. Here are the blueprints, and we got these security codes by paying off a guy."

Danny: "But these blueprints are for the cathedral downtown, not the casino on the Strip!?! And the security codes are the ones used at the dementia ward at the nursing home to keep the Alzheimer's patients from wandering."

Henchman #2: "Well, boss, you weren't specific about the security codes, and the buildings kind of look alike."

Lee

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

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

:lol:

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

david.prentice wrote:
Well, the code looks fairly straightforward. But there are virtually no comments at all. There are several ways that the code could be edited to make it easier to understand.

If your teacher wants you to understand 'foreign' code, you must simply add comments to explain what is happening.

If your teacher wants you to re-write the code, you make your alterations to the code.

If your teacher wants you to spot the deliberate mistake, you take the heavy hint given earlier. (does the code match the circuit wiring ?)

I would print the code. Then scribble all over it with a pencil, adding arrows and explanations.

When I have understood it, I would type in some comments. Finally presenting your teacher with 'annotated' code. Note that your 'annotations' should be in English. Do not comment things that are 'obvious' like 'a = b + c'

David.

Hmm,can u help me understand this foreign code,i am very very poor in c

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

As David says, break things down into smaller and smaller parts until you can decode each individual element.

Take a line such as:

			if((PINB&0x10) && aa)aa=0;

Now it doesn't help that the author has used such a pointless variable name as 'aa' so we cannot know the real purpose of the variable but you can see it defined above as:

unsigned char aa=1;

At a guess it looks like the author intends to use this as a boolean where 1=true and 0=false. So it's going to be used to turn something on and off.

The result of the if() statement is to assign 0 to 'aa' if the condition is true - if effect it's turning this switch "off".

The condition itself is:

((PINB&0x10) && aa)

So there are really two conditions there and they both have to be true (that is what '&&' means) for the conditional statement to take effect (aa to be switched off). The second part is the easiest to decode. It's just saying 'aa' - in other words, if aa already has a non-zero value.

The first condition is (PINB&0x10) which means read the PINB input register and use bitwise AND to test only the state of bit 4 (0x10 is a byte in which only bit 4 is set). So this is just testing that PB4 within PINB is set to 1.

So that's one line decoded. Now just repeat this process on every line in the program and you are done.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
  if((PINB&0x10) && aa)aa=0;

I did not understand this part at al ?

Why is an aa outside the bracket ?

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

I think you need to read some beginners guides to C yourself...

if (condition) statement;

...means if the condition is true the statement is performed.

...and from <5 seconds of work with Google...
http://www.loirak.com/prog/ctuto...

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

Quote:

Why is an aa outside the bracket ?

If it had been written as:

if((PINB&0x10) && aa)    aa=0;

or even:

if((PINB&0x10) && aa) {
  aa=0;
}

would it be easier to see what's going on?

If this whole exercise is about learning to program C then this code and this circtuit are NOT the right place to start. The circuit is too complex for a beginner to understand and the code is truly awful and not a good example for a beginner either.

Why not get yourself an AVR with nothing more than a resistor and an LED connected then see if you can write a program to make the LED flash. It's clear you are about a year or more away from the point where you could hope to take on the reprogramming of the control software for the circuit presented.

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

As always when we get these "Help me with the absolute basics of for an assignment/project", I have a hard time believing that the assignment was given without lectures/classes on the language as such.

- So, where there no C classes given?
- If so: Did you pay attention?

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]

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

Johan,

Do you really think a course tutor would present the beginner with such awful code? I know that one of the major skills of engineering is quickly being able to decipher someone else's algorithm and implementation but this test seems to be outside the remit of anything you really encounter in the "real world" when doing maintenance on someone else's code.

Cliff

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

Well, I would quite believe a teacher giving a student that code.

Probably as an exercise for the student to see how awful it is. And the teacher is so proud to show how clever she is.

It does pick out the men from the boys. The student that slept in class is identified immediately.

Personally, I think that a teacher should give her students examples of good coding practice. Admittedly, they could include 'deliberate mistakes', and the student exercise is to find and correct the mistakes.

@Dilip,

When you want an explanation, first say what you think. Then point to the parts you are unsure of.

Just saying 'I don't understand' implies that not only did you sleep in class, but you want someone else to do your work.

David.

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

OP wrote:
...,i am very very poor in c
So get a book, learn C and get rich quick. That's the real problem, not the code.

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1

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

Quote:
learn C and get rich quick.
I wish :(

Thirty years and then some I've been a software engineer and yet today, when my car failed the MOT and I was told the repairs would cost over 580 quid, I had to dust off the credit card!

Sod programming, the money is in car mechanics :)

--greg
Still learning, don't shout at me, educate me.
Starting the fire is easy; the hardest part is learning how to keep the flame!

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

Yeah, and politcs and being a lawyer ! Hmm..look at that all three professions gets one dirty ( USUALLY :wink: ).

1) Studio 4.18 build 716 (SP3)
2) WinAvr 20100110
3) PN, all on Doze XP... For Now
A) Avr Dragon ver. 1
B) Avr MKII ISP, 2009 model
C) MKII JTAGICE ver. 1