sheetansh
sheetansh

Reputation: 195

Loops and microcontrollers

This is what I am trying to figure out, I have a MC9S08AW60. It has two timers and I am trying to configure or just check for a tick from the timer, ie , I just want to check when the overflow flag changes. the basic design of the code is:

PTFDD = 0xFF;
int j,ch_nbr;
// LED mask values:
#define mLED0    0x01
#define mLED1    0x02
#define mLED2    0x04
#define mLED3    0x08
#define mLED4    0x10
#define mLED5    0x20
#define mLED6    0x40
#define mLED7    0x80



void main()
{

  While(j>0){

  ch_nbr++;
  if (ch_nbr == 8)  ch_nbr = 0;
  if (ch_nbr == 0)  PTFD = mLED0;  // Turn LED0 on, other LEDs off
  if (ch_nbr == 1)  PTFD = mLED1;  // Turn LED1 on, other LEDs off
  if (ch_nbr == 2)  PTFD = mLED2;  // Turn LED2 on, other LEDs off
  if (ch_nbr == 3)  PTFD = mLED3;  // Turn LED3 on, other LEDs off
  if (ch_nbr == 4)  PTFD = mLED4;  // Turn LED4 on, other LEDs off
  if (ch_nbr == 5)  PTFD = mLED5;  // Turn LED5 on, other LEDs off
  if (ch_nbr == 6)  PTFD = mLED6;  // Turn LED6 on, other LEDs off
  if (ch_nbr == 7)  PTFD = mLED7;  // Turn LED7 on, other LEDs off
  Mydelay();
   }
   } 
 void Mydelay(int *j)
{

 if (TPM1SC_TOF == 0) j=0;
 else j=1;
 return j;
 }

The idea is to check if the value "j" AND concurrently decide if the LED's should light up or not! This code doesnt work.Its been sometime since I used the C programming. I am a little shaky and need some help. A proper explanation would be nice. Please consider the following link for the Datasheet of the MCU: http://cache.freescale.com/files/microcontrollers/doc/data_sheet/MC9S08AW60.pdf

Can the Following function be replaced instead of the existing Mydelay:

int Mydelay(int& j)
{
 if(TPM1SC_TOF == 0) j=0;
 else j=1;
 return j;
}
void main()
{
  PTFDD = 0xFF;
int j,ch_nbr;
// LED mask values:
#define mLED0    0x01
#define mLED1    0x02
#define mLED2    0x04
#define mLED3    0x08
#define mLED4    0x10
#define mLED5    0x20
#define mLED6    0x40
#define mLED7    0x80   
  While(j==1)
 {
  ch_nbr++;
  if (ch_nbr == 8)  ch_nbr = 0;
  if (ch_nbr == 0)  PTFD = mLED0;  // Turn LED0 on, other LEDs off
  if (ch_nbr == 1)  PTFD = mLED1;  // Turn LED1 on, other LEDs off
  if (ch_nbr == 2)  PTFD = mLED2;  // Turn LED2 on, other LEDs off
  if (ch_nbr == 3)  PTFD = mLED3;  // Turn LED3 on, other LEDs off
  if (ch_nbr == 4)  PTFD = mLED4;  // Turn LED4 on, other LEDs off
  if (ch_nbr == 5)  PTFD = mLED5;  // Turn LED5 on, other LEDs off
  if (ch_nbr == 6)  PTFD = mLED6;  // Turn LED6 on, other LEDs off
  if (ch_nbr == 7)  PTFD = mLED7;  // Turn LED7 on, other LEDs off
 }
 Mydelay();
} 

Upvotes: 2

Views: 2023

Answers (3)

vgru
vgru

Reputation: 51264

First of all, you might want to use bit shifting to simplify your code significantly (because that's what you are essentially doing, shifting a bit).

This means you don't need to have 8 constants for each position, but simply use the << operator to move the bit one position to the left on each iteration:

int MoveToLeft(int bitPosition)
{
    // shift left
    bitPosition = bitPosition << 1;

    // reset to '1' when we shift through all 8 bits
    if ((bitPosition & 0xFF) == 0)
        bitPosition = 1;

    return bitPosition;
}

Next, better way than polling the timer value is to use interrupt routines. You need to setup a function which will be invoked automatically whenever your timer finishes counting.

I am not sure if your code even works like this. Is your timer's purpose to create a delay between LED changes? Does your timer automatically resets itself? Something like this would be more logical if the timer only serves to wait before transition:

while (1) // repeat indefinitely
{
     // shift the bit
     ch_nbr = MoveLeft(ch_nbr);
     PTFD = ch_nbr;

     // while the timer is counting, do nothing
     while (TPM1_TOF == 0) 
     { }

     // reset the timer
     (not sure about this part, check your docs)
}

Upvotes: 3

F&#225;bio Perez
F&#225;bio Perez

Reputation: 26088

  1. You have more closing brackets than opening brackets. Try correcting this by matching brackets (indentation will help).

  2. You didn't declare the variables j and cn_nbr anywhere, but you use them.

  3. C statements are lowercase (while, not While).

  4. You are calling Mydelay() without parameters, when Mydelay(int &j) expect a parameter. Also, &j returns the address of the variable j. I think you want int *j here. Read more about pointers.

  5. Use switch or if ... else if instead of if ... if ... if .... Your code work both ways, but using switch or if else is faster.

Consider learning more about C programming here: http://www.cprogramming.com/

Upvotes: 0

Miles Alden
Miles Alden

Reputation: 1540

Well if this is a cut-paste of your code you've got a few things to fix:

  • 1st this line: While(j>0){ I don't see j declared or defined anywhere. The compiler should've generated an error on that, and While should be lowercase while ( j > 0 ).

  • Same with ch_nbr.

  • When you call MyDelay() you wrote it to take an &int and you probably meant (int *j), then you didn't pass it anything.

  • If needs to be lowercase if

  • And yes, as the comments showed you have 3 opening brackets { and 5 closing ones }.

Fix those, make sure it compiles now and then tackle any logic.

Upvotes: 1

Related Questions