Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

18f1320

Status
Not open for further replies.

GreenP

New Member
Hi
Could someone give me a brief explaination on how to use the stack pointer so I am able to jump to a desired location within my program?

Thanks
GreenP
 
To jump to the address that is on the stack you execute a return instruction. To jump to an fixed address you execute a goto instruction.

Mike.
 
Hi Mike

Thank you for your reply but I'm still having problems with the return and goto instructions, I think it is better that I give you my code and a brief explaination of It's functionallity.

What I am trying to achieve is to start 8 LED,s flashing (all connected to PORTB) from the push of a button (RA2) which is not a problem as is seen from the code, but stopping them seems to be as this is stopped from a seperate push button. This is connected to RA3 and the code for it is within the flash delay, but I am unable to get the program to return to the main_0 location. Here is my section of code excluding the debounce routines

Thanks
GreenP

#include <p18f1320.h>
//#pragma config WDT = OFF
#pragma romdata CONFIG
_CONFIG_DECL(0x88,0x01,0x00,0x00,0x80,0x03,0xC0,0x03,0xE0,0x03,0x40);
#pragma romdata

//*****************************************//
void flash_delay (void)
{
if (PORTA = 0x08)
{
return; // the program should return to main_0
}
else
{
int j;
for (j = 0; j < 1; j++);
}
}
//****************************************//
void main_0 (void)
{
if (PORTA = 0x04) // should return to here
{
while (1)
{
PORTB = 0xFF; //turning all the LED,s on
flash_delay();
PORTB = 0x00; //turning all the LED’s off
flash_delay();
}
}
main_0();
}
//*************************************************//
void main (void)
{
ADCON0 = 0x1C;
ADCON1 = 0xFF;
OSCCON = 0xA6;
TRISA = 0x2C;
TRISB = 0x00;
PORTA = 0x00;
PORTB = 0x00;

main_0();
}
 
How about;

Code:
while (PORTA == 0x04)
{
   PORTB = 0xFF;        //turning all the LED,s on
   flash_delay();
   PORTB = 0x00;        //turning all the LED’s off
   flash_delay();
}
 
Thanks Diver300

I will try using while (PORTA = 0x04)

But my problem is breaking that while loop while in the subroutine flash_delay,
so that if PORTA= 0x08 the while loop is broken and the program returns to main_0

Thanks
GreenP
 
You need to know the differance between = and ==.

if you want to test to see if portb is 4 use

Code:
if(PORTB == 0x04)

When you post code use the # to put
Code:
tags around your code. This will preserve the formating. You have assembler and c confused. What you want is close to this. I have tried to make it simple to help with learning. Note that return can be used to pass a value back to the caller and that we do not need to use the keywords goto or call. I moved the code from main_0() into main().

Code:
#include <p18f1320.h>
//#pragma config WDT = OFF
#pragma romdata CONFIG
_CONFIG_DECL(0x88,0x01,0x00,0x00,0x80,0x03,0xC0,0x 03,0xE0,0x03,0x40);
#pragma romdata

//*****************************************//
char flash_delay (void)
{
  if (PORTA == 0x08)
  {
   // return with a value of 1
    return 1;  // 1 is passed back to the caller
  }
  else 
  {
    int j;
    for (j = 0; j < 1; j++);
    // returning zero means button is not pushed
    return 0;
  }
}


//*************************************************// 
void main (void)
{
  ADCON0 = 0x1C;
  ADCON1 = 0xFF;
  OSCCON = 0xA6;
  TRISA = 0x2C;
  TRISB = 0x00;
  PORTA = 0x00;
  PORTB = 0x00;

  if (PORTA == 0x04) 
  {
    while (1)
    {
      PORTB = 0xFF; //turning all the LED,s on
      if ( flash_delay() == 1)
      {
        break; // exit while loop because button is pressed
      }
      PORTB = 0x00; //turning all the LED’s off
      if (flash_delay() == 1)
      {
        break; // exit while loop because button is pressed
      }
    }
  }
}

It would be nice to move the test to stop back to main() like this

Code:
#include <p18f1320.h>
//#pragma config WDT = OFF
#pragma romdata CONFIG
_CONFIG_DECL(0x88,0x01,0x00,0x00,0x80,0x03,0xC0,0x 03,0xE0,0x03,0x40);
#pragma romdata

//*****************************************//
char flash_delay (void)
{
  int j;
  for (j = 0; j < 1; j++);
}

//*************************************************// 
void main (void)
{
  ADCON0 = 0x1C;
  ADCON1 = 0xFF;
  OSCCON = 0xA6;
  TRISA = 0x2C;
  TRISB = 0x00;
  PORTA = 0x00;
  PORTB = 0x00;

  if (PORTA == 0x04) 
  {
    while (1)
    {
      PORTB = 0xFF; //turning all the LED,s on
      flash_delay();
      if (PORTA == 0x08)
      {
        break; // exit while loop because button is pressed
      }
      PORTB = 0x00; //turning all the LED’s off
      flash_delay();
      if (PORTA == 0x08)
      {
        break; // exit while loop because button is pressed
      }
    }
  }
}

I have not tested this. I have given you more code/help then I would like too but you seem lost. Study how return works. Study the c operators. etc.. Most of all if you can not understand the code after reading up on these things, ask how it works.

3v0
 
Last edited:
Thanks 3v0

I see what you mean about the return function, and I will study a bit more about the operands.

But I'm still a bit confused about pposting code onto the site and the use of tags.

Thanks

GreenP
 
It is the operators you want to look at. = == + ++ & && - -- etc.
Operands are the things (like numbers) that the operators work on.

When you type
Code:
 you are telling the posting engine what follows is a code listing and you do not want it to mess up the formating, mostly the indenting.

When you type
you are telling the posting engine that you are done with the code listing and want it to resume normal formating.

Thanks 3v0

I see what you mean about the return function, and I will study a bit more about the operands.

But I'm still a bit confused about pposting code onto the site and the use of tags.

Thanks

GreenP
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top