Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
December 03, 2016, 10:49:55 22:49


Login with username, password and session length


Pages: [1]
Print
Author Topic: ATMEGA48 and ATMEGA88 conflict  (Read 1795 times)
0 Members and 1 Guest are viewing this topic.
jeniusj
Junior Member
**
Offline Offline

Posts: 38

Thank You
-Given: 11
-Receive: 7


« on: June 21, 2008, 07:30:17 19:30 »

I have worked on interfacing the NS-45/P inclinometer with ATMEGA88 using USART0 in DOUBLE SPEED mode via interrupt driven communication,I have done it successfully on atmega88 but later when I have to do the same thing with atmega48,the program not runs although I have altered the include directories and all necessary things as per the CHIP(i m using IMAGECRAFT ICCAVR),after debugging I come to know that a flag variable which is created by me in the program to know that I have received 9 bytes successfully and then to call the   Display routine in order to display the measured angle,have lost its value when it returns from the ISR(although the same routine works fine on atmega88).I think this is most probably due to the stack overflow.I have solved the problem by using a pointer to the flag variable and now the program works fine.
I want to know the reason behind it (i.e.) is it due to the stack overflow or due to any-other reason.
All useful comments have been welcomed

« Last Edit: June 21, 2008, 07:35:03 19:35 by jeniusj » Logged
hate
Hero Member
*****
Offline Offline

Posts: 556

Thank You
-Given: 156
-Receive: 354


« Reply #1 on: June 22, 2008, 03:38:25 15:38 »

I think people will have to see the source for both chips or it'll be just guessing but I don't think this can be a stack issue!

Regards...
Logged

Regards...
jeniusj
Junior Member
**
Offline Offline

Posts: 38

Thank You
-Given: 11
-Receive: 7


« Reply #2 on: June 28, 2008, 06:05:27 18:05 »

ok I will upload the code soon
Logged
belepojegy
Guest
« Reply #3 on: June 28, 2008, 07:24:57 19:24 »

Use "unsigned" when you declare your flag.
Logged
telyadi
Newbie
*
Offline Offline

Posts: 17

Thank You
-Given: 0
-Receive: 0


« Reply #4 on: June 30, 2008, 05:41:43 05:41 »

first suspect is stack overflow. I thought ICCAVR show the memory usage after project compile?
Logged
jeniusj
Junior Member
**
Offline Offline

Posts: 38

Thank You
-Given: 11
-Receive: 7


« Reply #5 on: June 30, 2008, 07:02:51 07:02 »

code that is running on ATMEGA88 successfully on AVR STUDIO4
#include <avr/io.h>
#include <avr/iom88.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define ATMEGA_88


//To go in DEBUG Mode, comment the following line
//#define RELEASE

//#ifndef RELEASE
#define DEBUG
//#endif

//static int bPacketReady = 0;//Setting false as default
int iRecieveBuffer [10];
int iOutputBuffer [10];
int i, j, k, l;
const int iLEDCodes [10] = { 0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09 };
const int iMUXCodes [6] = { 0b010000,0b001000,0b000100,0b000100,0b000001,0b000010 };
int a=0;

//Function Declaration
void Delay ( int iDelay );
void DisplaySegments ( int iDisplayData [10] );

int main ( void )
{
   //Setting up General purpose Configuration
   PRR = PRR & 0b11111101;   //Enable USART0
   SREG = 0x80;      //Enabling All Interupts

   //Setting up USART Configuration
   UCSR0B = 0x00;      //Disable Transmit n Recieve while setting baud rate
   UCSR0A = 0x02;      //U2X = 1
   UCSR0C = 0x06;      //Transmit 8-bit Data
   UBRR0L = 0x67;      //
   UBRR0H = 0x00;      //Baud Rate
   UCSR0B = 0xF8;      //Transmit Enable, Recieve Enable

   //Port Direction Registers
   DDRB = 0xFF;
   DDRC = 0xFF;
   
   i = 0;
   j = 0;
   k = 0;
   l = 0;

   while ( 1 )
   {
      //Code Body here   
      if  ( a == 1 )
      {
         //Uncomment the following block when in DEBUG Mode
#ifdef DEBUG
         for( j = 0; j < 9; j++ )
         {
            while (!(UCSR0A & 0x20));
             UDR0 = iRecieveBuffer[j];
            iOutputBuffer [j] = iRecieveBuffer [j];
         }
#endif
        a   = 0;
      }

/*      if ( k == 0 )    //Chacking sign of the Packet
      {
         if ( iOutputBuffer
  • == '-' )
         {
            PORTC = 0x01;
            PORTB = 0xFD;
         }

         k++; l++;
         continue;
      }
          
      if ( k == 3 )
         l++;
      
      iOutputBuffer [l] &= 0x0F;
      PORTB = 0xFF;
      PORTC = iMUXCodes [k++];

      if ( k == 3 )
         PORTB = iLEDCodes [iOutputBuffer[l++]] & 0xFE;   
      else
         PORTB = iLEDCodes [iOutputBuffer[l++]];

      //delay
      for ( j = 0; j < 50; j++ );


      if ( k == 6 ) k = 0;
      if ( l == 7 ) l = 0;
*/
      DisplaySegments ( iOutputBuffer );
   }

   return 1;
}

void DisplaySegments ( int iDisplayData [10] )
{
   int iter_i;
   for ( iter_i = 0; iter_i < 7; iter_i++ )
   {
      if (iter_i==0)
        {
         if ( iDisplayData [iter_i] == '-' )
          {
            PORTC = iMUXCodes [iter_i] ;
            PORTB = 0xFD;
            Delay(100);
         }
       else
          {
            PORTC = iMUXCodes [iter_i] ;
            PORTB = 0xFF;
            Delay(100);
         }
      }

        if (( iter_i != 0 ) && ( iter_i != 3 ) )
        {
         iDisplayData [iter_i] &= 0x0F;
          
         if ( iter_i == 2 )
           {
              PORTC = iMUXCodes [iter_i] ;
              PORTB = ( iLEDCodes [ iDisplayData [iter_i] ] & 0xFE );
              Delay(100);
           }
         else
         {
              PORTC = iMUXCodes [iter_i];
               PORTB = iLEDCodes [ iDisplayData [iter_i] ];
              Delay(100);
         }
      }            
   }
    
   PORTC=0x00;
}

void Delay ( int iDelay )
{
    unsigned int iter_l;
   for ( iter_l = iDelay; iter_l > 0; iter_l-- );
}


ISR ( USART_TX_vect )
{
   //USART Transmission complete routine
}

ISR ( USART_RX_vect )
{
   //USART Reception complete routine
#ifdef DEBUG
   iRecieveBuffer [i++] = UDR0;
#endif

#ifdef RELEASE
   iOutputBuffer [i++] = UDR0;
#endif

   if( i == 9 )
   {   
      a = 1;
       i = 0;
   }
}

ISR ( USART_UDRE_vect )
{

}


Code that is running successfully on ATMEGA48

#include <avr/io.h>
#include <avr/iom48.h>
#include <avr/interrupt.h>
#include <util/delay.h>

#define ATMEGA_48


//To go in DEBUG Mode, comment the following line
//#define RELEASE

//#ifndef RELEASE
#define DEBUG
//#endif

//static int bPacketReady = 0;//Setting false as default
int iRecieveBuffer [10];
int iOutputBuffer [10];
int i, j, k, l;
int *b;
const int iLEDCodes [10] = { 0x03, 0x9F, 0x25, 0x0D, 0x99, 0x49, 0x41, 0x1F, 0x01, 0x09 };
const int iMUXCodes [6] = { 0b010000,0b001000,0b000100,0b000100,0b000001,0b000010 };
int a=0;

//Function Declaration
void Delay ( int iDelay );
void DisplaySegments ( int iDisplayData [10] );

int main ( void )
{
   //Setting up General purpose Configuration
   PRR = PRR & 0b11111101;   //Enable USART0
   SREG = 0x80;      //Enabling All Interupts

   //Setting up USART Configuration
   UCSR0B = 0x00;      //Disable Transmit n Recieve while setting baud rate
   UCSR0A = 0x02;      //U2X = 1
   UCSR0C = 0x06;      //Transmit 8-bit Data
   UBRR0L = 0x67;      //
   UBRR0H = 0x00;      //Baud Rate
   UCSR0B = 0xF8;      //Transmit Enable, Recieve Enable

   //Port Direction Registers
   DDRB = 0xFF;
   DDRC = 0xFF;
   
   b=&a;
   i = 0;
   j = 0;
   k = 0;
   l = 0;

   while ( 1 )
   {
      //Code Body here   
      if  ( *b == 1 )
      {
         //Uncomment the following block when in DEBUG Mode
#ifdef DEBUG
         for( j = 0; j < 9; j++ )
         {
            while (!(UCSR0A & 0x20));
             UDR0 = iRecieveBuffer[j];
            iOutputBuffer [j] = iRecieveBuffer [j];
         }
#endif
        a   = 0;
      }

/*      if ( k == 0 )    //Chacking sign of the Packet
      {
         if ( iOutputBuffer
  • == '-' )
         {
            PORTC = 0x01;
            PORTB = 0xFD;
         }

         k++; l++;
         continue;
      }
          
      if ( k == 3 )
         l++;
      
      iOutputBuffer [l] &= 0x0F;
      PORTB = 0xFF;
      PORTC = iMUXCodes [k++];

      if ( k == 3 )
         PORTB = iLEDCodes [iOutputBuffer[l++]] & 0xFE;   
      else
         PORTB = iLEDCodes [iOutputBuffer[l++]];

      //delay
      for ( j = 0; j < 50; j++ );


      if ( k == 6 ) k = 0;
      if ( l == 7 ) l = 0;
*/
      DisplaySegments ( iOutputBuffer );
   }

   return 1;
}

void DisplaySegments ( int iDisplayData [10] )
{
   int iter_i;
   for ( iter_i = 0; iter_i < 7; iter_i++ )
   {
      if (iter_i==0)
        {
         if ( iDisplayData [iter_i] == '-' )
          {
            PORTC = iMUXCodes [iter_i] ;
            PORTB = 0xFD;
            Delay(100);
         }
       else
          {
            PORTC = iMUXCodes [iter_i] ;
            PORTB = 0xFF;
            Delay(100);
         }
      }

        if (( iter_i != 0 ) && ( iter_i != 3 ) )
        {
         iDisplayData [iter_i] &= 0x0F;
          
         if ( iter_i == 2 )
           {
              PORTC = iMUXCodes [iter_i] ;
              PORTB = ( iLEDCodes [ iDisplayData [iter_i] ] & 0xFE );
              Delay(100);
           }
         else
         {
              PORTC = iMUXCodes [iter_i];
               PORTB = iLEDCodes [ iDisplayData [iter_i] ];
              Delay(100);
         }
      }            
   }
    
   PORTC=0x00;
}

void Delay ( int iDelay )
{
    unsigned int iter_l;
   for ( iter_l = iDelay; iter_l > 0; iter_l-- );
}


ISR ( USART_TX_vect )
{
   //USART Transmission complete routine
}

ISR ( USART_RX_vect )
{
   //USART Reception complete routine
#ifdef DEBUG
   iRecieveBuffer [i++] = UDR0;
#endif

#ifdef RELEASE
   iOutputBuffer [i++] = UDR0;
#endif

   if( i == 9 )
   {   
      *b = 1;
       i = 0;
   }
}

ISR ( USART_UDRE_vect )
{

}

Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC