Anzeige:

Problems writing a program for AtMega16

Hier finden sie die archivierten Beiträge des alten PHPBB Forums von www.loetstelle.net

Problems writing a program for AtMega16

Von masoud66 am 16.07.2010 13:42

Could anyone help me on writing this program?
I've tried so much in codevision, and I've wrote some programs, but any of them had no correct answer!

I want to use a Mega16 as micro.
The goal of program is as follow:
A 7bit input (for example PORTA), really, it's 8bit, but last bit (PORTA.7) is always High=1.
A 3bit output (for example PORTC).
An iterrupt0 can be used. If used, could simplify the process!

What should happen:
At first, by default, the value that is connected to input port is F7. The supposed output for this value is F8.
So:
Input: F7
Output: F8

The acceptable values for input are:
11111000-F8
11111001-F9
11111010-FA
11111011-FB
11111100-FC
11111101-FD
11111110-FE
11111111-FF
For all of this group, output should be FE.

11110111-F7
Only for this input, output is F8

10001111-8F
10011111-9F
10101111-AF
10111111-BF
11001111-CF
11011111-DF
11101111-EF
11111111-FF
For this group, output should be FD.


The specified Output for F8 to FF, is FE
The specified Output for 8F to FF is FD

The best output is F8, which is only produced by F7 as input. So I should try to achieve this input. But how? In this way:

The default input is F7, so if it changes to those two groups, the output goes to specified value (FE or FD). By changing output, because of hardware designing, input also starts to change in range of two groups+F7. (Other values are impossible). NOW the micro should keep desired value (FE or FD) in output and synchronic, should pursuit the input to catch F7! At the moment that it saw F7 in input, rapidly change output to F8.
As you see, the FF is common in two groups. So to decide to produce which output, micro should do nothing and only waits until it change to one of two groups. If input returned to F7, as the before, anything doesn't happen!

For simplify the process, the 3rd bit of input (which is always 1 in two groups and only is 0 in F7), is attached to INT0 of micro. So if it was 0, means that input had no change. And if goes 1, means that input had changed. It trigs the INT0, so micro bewares of changing and starts to read inputs, and in order of the received input, produce specified output. Keep it at FE or FD until it can read F7 from input!!

This is Total of program. Because I'm a beginner, I've tried these incorrect programs, but any of them acts correct.
I know this is almost easy, but I can't do this. The codevision take no error and it compile and hex, but in Proteus, it's incorrect.

I SOLICIT YOU TO HELP ME, AND IF YOU CAN, WRITE IT FOR ME. I HOPE YOU ANSWER ME AND HELP ME. IT'S VERY IMPORTANT FOR ME BECAUSE OF MY PROJECT.

I'M WISTFUL YOUR HELP AND ANSWER….


THESE ARE MY TRIES WHICH ARE INCORRECT:

1]
include <mega16.h>
unsigned int x;
interrupt [EXT_INT0] void ext_int0_isr(void)
{
switch (x)
{
case 0xFE: PORTC=0xFE;
break;
case 0xFD: PORTC=0xFE;
break;
case 0xFB: PORTC=0xFE;
break;
case 0xEF: PORTC=0xFD;
break;
case 0xDF: PORTC=0xFD;
break;
case 0xBF: PORTC=0xFD;
break;
default: PORTC=0xF8;
}
while (PORTA!=0xF7);
}
void main (void)
{
{
PORTA=0x00;
DDRA=0x00;
PORTC=0xF8;
DDRC=0xFF;

GICR=0x40;
MCUCR=0x01;
MCUCSR=0x00;
GIFR=0x40;
}

#asm("sei")
while (1)
{
x=PORTA;
PORTC=0xF8;
}
}
1]

Second:
1]
#include <mega16.h>
unsigned int x;
void main (void)
{
PORTA=0x00;
DDRA=0x00;

PORTC=0xF8;
DDRC=0xFF;

x=PORTA;
PORTC=0xF8;

while (1)
{
while (PORTA!=0xF7);
{
if (x>247);
{
PORTC=0xFE;
}
if (x<247);
{
PORTC=0xFD;
}
}
};
}
1]

Third one:
1]
#include <mega16.h>
unsigned int x;
void main (void)
{
PORTA=0x00;
DDRA=0x00;

PORTC=0xF8;
DDRC=0xFF;

x=PORTA;
PORTC=0xF8;

while (1)
{
while (x!=0xF7)
{
if (x>0xF7);
{
PORTC=0xFE;
}
if (x<0xF7);
{
PORTC=0xFD;
}
};
};
}
1]

Von TheBug am 16.07.2010 16:07

The first problem is that you are defining two different results for the same input value: $FF

When you have resolved this problem I would recommned to take a closer look at logical functions. Your requirements can easily be solved with a logical masking in two if statements.

Von BellaD am 16.07.2010 16:56

moin

Make sure the JTAG Interface on Port C are disabled !
Read the Datasheet
http://www.atmel.com/dyn/resources/prod_documents/doc2466.pdf
site 260

hv

Von masoud66 am 16.07.2010 19:07

thanks alot for your help.

1-there is no problem. i can delete the FF input by changing my hardware design. i mean, FF exits from two groups of inputs. but, as an interrupt activator, is active.
i mean:
when we have F7 in input, the output is F8...
If the 3rd bit of input, goes high(1), for some moments, the input goes to FF.
At this time, micro takes any action, And only its INTERRUPT is Trigged.
So it start reading input.
It may read FF, so do ANY thing; ONLY continue reading input (which is temporary FF) and Waits Until If input chage to those two groups, take the specified action due to input value.
in real, the FF value, would only trigs Interrupt and has no command for producing output. By this method, the problem of FF value, is solved.

2- i've seen the problem of portc before. it could be solved by disabling JTAG fusebit. However, i can use any other Ports, even for Input. i mean there is no limit in selecting which port as In or Out.

as you see in my tries, (those 3 incorrect programs) i'm beginner in program writing, however i've studied in this case, but i can't act as a skilful programmer.

if you can do this for me, Embarassed i don't know how can i appreciate you. i wish i could do for you any thing to recompense your try.

thanks alot

Von TheBug am 17.07.2010 00:40

Still unclear what you are trying to do. Should your program exit if $F7 is read as input?

Von masoud66 am 17.07.2010 13:08

DON'T CARE MY PREVIOUS POST CONFUSING LINES!
IT'S THE SUMMARY OF WHAT I WANT
http://img697.imageshack.us/img697/4055/isinputoneofthisvalues.jpg

Von Elektron am 17.07.2010 14:04

Hello masoud66,

you're welcome to join our forum and though the main language is German, posting in English as well is accepted here. However, we have put up some rules to make sure threads remain readable. One of these is to use proper grammar which, for instance, includes not using uppercase letters all along the way. Please understand that this restrictions are intended to grant a high quality of information for future users that probably are looking for a similar solution.

I thank you for your cooperation and wish you well to solve your problem as fast as possible.

Greetings,
Elektron

Von TheBug am 17.07.2010 23:36

You already have the algorithm, just code it into whatever language you want. Easiest would be three if/else statements put into a while(1) LOOP.

Von masoud66 am 18.07.2010 06:43

hi

excuse me for bad english grammer. because English is not my native language, and i don't know german language too. Embarassed

yes, i have algoritm, but i can't write it in C. i'm beginner, you saw my programs in first post. they have much more mistakes. So i've tried this commands too, but not in a proper way. i mean i can't regularize them. how to make some "if" commands gather in a loop? how to make some "or" command in one "if" command? how to make a while(1) loop, when input changes? how refresh it?... how!!? Sad

can you write me this? Question
i said my problem. i've tried much, but i'm disable to do this Crying or Very sad Crying or Very sad

this is what i guess. codevision compiled it+hex file. but in the proteuss, take any action. nothing happen! i've used a 8pin DIP-switch as input device and 8 LEDs as output.
((in the first "IF" command, i've put two equals (x==0xF7) . when i put one =, it takes Error that said: possibly incorrect assignment!))

Code:

#include<mega16.h>
unsigned  int x;
unsigned  int y;


void main (void)
   { 

        DDRC = 0xFF;
        PORTC= 0x00;
       
        DDRA=0x00;
        PORTA=0x00;
 
        x=PORTA;
        PORTC=y;
       
        while(1)
        {
         if (x==0xF7)
         {
         y=0xF8;
         }
         else if ((x=0xF8) || (x=0xF9) || (x=0xFA) ||(x=0xFB) ||(x=0xFC) ||(x=0xFD) || (x=0xFE))   y=0xFE;
         else if ((x=0x8F) || (x=0x9F) || (x=0xAF) ||(x=0xBF) ||(x=0xCF) ||(x=0xDF) || (x=0xEF))   y=0xFD;
         else
         {
         y=0xF8;
         }
        }
  }

Von TheBug am 18.07.2010 11:39

Your basic idea is right now.

== is the equality comparison
= is assigning a value to a variable

Using "=" inside an if statement is a tricky thing to do and you don't want this in your case.

In your program you will never see any change in the status. You are not reading the input and you are not writing the output.

The
x=PORTA;
PORTC=y;
statements assign the value of PORTA to x and the value of y to PORTC. These statements do not make X behave like it is PORTA or PORTC like it is y.

Anzeige: