loetstelle.net
Lötstelle Userforum
 
 FAQFAQ   SuchenSuchen   MitgliederlisteMitgliederliste   BenutzergruppenBenutzergruppen   RegistrierenRegistrieren 
 ProfilProfil   Einloggen, um private Nachrichten zu lesenEinloggen, um private Nachrichten zu lesen   LoginLogin 

Problems writing a program for AtMega16
Gehe zu Seite 1, 2  Weiter
 
Neues Thema eröffnen   Neue Antwort erstellen    loetstelle.net Foren-Übersicht -> Mikrocontroller
Vorheriges Thema anzeigen :: Nächstes Thema anzeigen  
Autor Nachricht
masoud66



Anmeldungsdatum: 07.03.2009
Beiträge: 13

BeitragVerfasst am: Fr 16.Jul 2010 13:42:33    Titel: Problems writing a program for AtMega16 Bauteile finden Antworten mit Zitat

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:

Code:

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;
}
}


Second:
Code:

#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;
      }       
}       
}; 
}


Third one:
Code:

#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;     
}         
};   
}; 
}
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: Fr 16.Jul 2010 16:07:25    Titel: Bauteile finden Antworten mit Zitat

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.

_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
BellaD



Anmeldungsdatum: 03.12.2006
Beiträge: 1708
Wohnort: Klinik der misshandelten Kuscheltiere

BeitragVerfasst am: Fr 16.Jul 2010 16:56:14    Titel: Bauteile finden Antworten mit Zitat

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

_________________
Eine Weiterverbreitung ohne ausdrückliche Genehmigung ist untersagt. Für Schäden, lehnen wir jegliche Haftung ab.
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
masoud66



Anmeldungsdatum: 07.03.2009
Beiträge: 13

BeitragVerfasst am: Fr 16.Jul 2010 19:07:16    Titel: Bauteile finden Antworten mit Zitat

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
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: Sa 17.Jul 2010 00:40:32    Titel: Bauteile finden Antworten mit Zitat

Still unclear what you are trying to do. Should your program exit if $F7 is read as input?
_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
masoud66



Anmeldungsdatum: 07.03.2009
Beiträge: 13

BeitragVerfasst am: Sa 17.Jul 2010 13:08:04    Titel: Bauteile finden Antworten mit Zitat

DON'T CARE MY PREVIOUS POST CONFUSING LINES!
IT'S THE SUMMARY OF WHAT I WANT
http://img697.imageshack.us/img697/4055/isinputoneofthisvalues.jpg
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
Elektron



Anmeldungsdatum: 16.03.2008
Beiträge: 1049

BeitragVerfasst am: Sa 17.Jul 2010 14:04:13    Titel: Bauteile finden Antworten mit Zitat

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


Zuletzt bearbeitet von Elektron am So 18.Jul 2010 00:01:36, insgesamt 2-mal bearbeitet
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden E-Mail senden Website dieses Benutzers besuchen
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: Sa 17.Jul 2010 23:36:15    Titel: Bauteile finden Antworten mit Zitat

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.
_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
masoud66



Anmeldungsdatum: 07.03.2009
Beiträge: 13

BeitragVerfasst am: So 18.Jul 2010 06:43:12    Titel: Bauteile finden Antworten mit Zitat

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;
         }
        }
  }
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: So 18.Jul 2010 11:39:26    Titel: Bauteile finden Antworten mit Zitat

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.

_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...


Zuletzt bearbeitet von TheBug am So 18.Jul 2010 11:42:15, insgesamt einmal bearbeitet
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: So 18.Jul 2010 11:41:41    Titel: Bauteile finden Antworten mit Zitat

Ah, and, no, I can't write this for you but I can help you write it. If I would write this (which I could in a minute or less) you would not learn from it and have to come back for the same thing again.
_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: So 18.Jul 2010 13:29:40    Titel: Bauteile finden Antworten mit Zitat

Sorry, no answers of that kind via PM.
_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: So 18.Jul 2010 13:35:42    Titel: Bauteile finden Antworten mit Zitat

Put the x=PORTA inside the while loop, assign the output values to PORC instead of y, and fix the logical expressions, then you are done.
_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
masoud66



Anmeldungsdatum: 07.03.2009
Beiträge: 13

BeitragVerfasst am: Mo 19.Jul 2010 00:11:14    Titel: Bauteile finden Antworten mit Zitat

i tried again.
i repaled x=PORTA with x=PINA
i deleted y and repalced it with PORTC=0x....
my last IF command has no ELSE after it. is it right. does it effect?
i put all of them in the while loop...
what is meaning "...change my logical statements"? if they have problem, why they can be compiled?!?

BUT when i simulate it in proteuss, the output changes only for one time. i mean when i set input to F7, and click simulate, the output goes F8, but while simulating when i change input value, output keeps in F8 and no change happen!

it's simulating:
http://www.2shared.com/file/e436JwWO/solar.html

it's my code:
Code:

#include <mega16.h>

unsigned int x;

 void  main ()
 {
  PORTC=0x00;
  DDRC=0xFF;
 
  PORTA=0x00;
  DDRA=0x00;

  while (1)
  {
 
  x=PINA;
 
   if (x==0xF7)
   {
     PORTC=0xF8;
   }
   else
   if ((x==0x8F) || (x==0x9F) || (x==0xAF) || (x==0xBF) || (x==0xCF) || (x==0xDF) || (x==0xEF))
   {
     PORTC=0xFD;
   }
   else
   if ((x==0xF8) || (x==0xF9) || (x==0xFA) || (x==0xFB) || (x==0xFC) || (x==0xFD) || (x==0xFE))
   {
     PORTC=0xFE;
   }
   };
  }
         
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden
TheBug



Anmeldungsdatum: 20.07.2007
Beiträge: 1484

BeitragVerfasst am: Mo 19.Jul 2010 00:29:13    Titel: Bauteile finden Antworten mit Zitat

I would write it this way. Indenting so that matching braces are vertically aligned simplifies reading the code.

I am not familiar with the simulator you are using, does it allow single stepping? If so you should try to go through the code line by line and see what the simulator things should happen.

Code:

#include <mega16.h>

unsigned int x;

 void  main ()
 {
  PORTC=0x00;
  DDRC=0xFF;
 
  PORTA=0x00;
  DDRA=0x00;

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

_________________
Wenn Sie nichts zu tun haben, tun Sie es bitte nicht hier...
Nach oben
Benutzer-Profile anzeigen Private Nachricht senden Website dieses Benutzers besuchen
Anzeige






Verfasst am: Heute 02:33:34    Titel: Bauteile finden

Nach oben
Beiträge der letzten Zeit anzeigen:   
Neues Thema eröffnen   Neue Antwort erstellen    loetstelle.net Foren-Übersicht -> Mikrocontroller Alle Zeiten sind GMT + 1 Stunde
Gehe zu Seite 1, 2  Weiter
Seite 1 von 2

 
Gehe zu:  
Du kannst keine Beiträge in dieses Forum schreiben.
Du kannst auf Beiträge in diesem Forum nicht antworten.
Du kannst deine Beiträge in diesem Forum nicht bearbeiten.
Du kannst deine Beiträge in diesem Forum nicht löschen.
Du kannst an Umfragen in diesem Forum nicht mitmachen.
Du kannst Dateien in diesem Forum nicht posten
Du kannst Dateien in diesem Forum nicht herunterladen


Powered by phpBB 2.x © 2001, 2002 phpBB Group

Sie sind Besucher Nr. 1222022
©opyright by Markus Vohburger 2005 All Rights reserved