Help, how to write this program?

Rasprava o AVR mikrokontrolerima, AVR projekti i drugo vezano za AVR...

Moderators: pedja089, stojke369, trax, InTheStillOfTheNight

Post Reply
masoud
Pocetnik na forumu
Pocetnik na forumu
Posts: 28
Joined: 13-02-2009, 22:12

Help, how to write this program?

Post by masoud »

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….

my tries to write this which are INCORRECT:

Code: Select all

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 prog:

Code: Select all

#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: Select all

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

User avatar
trax
Administrator sajta
Administrator sajta
Posts: 3509
Joined: 08-01-2005, 18:04
Location: 75k, BA
Contact:

Re: Help, how to write this program?

Post by trax »

Hi,

I don't have much time to work on your problem, but FOR EXAMPLE, the last program you wrote has some problems. Here is what it should look like (if the flow of it is OK):

Code: Select all

#include <mega16.h>

//volatile uint8_t x;

void main (void)
{
	uint8_t x; // moved it here, type: unsigned 8bit

	DDRA=0x00; // inputs
	PORTA=0x00; // turn off pullup resistors

	DDRC=0xFF; // outputs      
	PORTC=0xF8; // set PORTC pins to: 11111000

	while(1)
	{     
		x=PINA; // read PORTA pin values in "x" variable
		if(x == 0xF7) PORTC=0xF8;
		else if(x >= 0xF8 && x <= 0xFE) PORTC=0xFE;
		else if(x >= 0x8F && x <= 0xEF) PORTC=0xFD; // this should work for your flowcart! it also works for, example: 0xC2, 0xB0...
	}
	
	// we should never get to this line! we always stay in the "while" above.
}
Post Reply