LPC_GPIO_TypeDef structure pointer problem solved !!!

It took me 2 hours debugging to find out that I made a couple of mistakes.

1. lpc1114 memory address seems to be unsigned long.  So if declared just int might
cause trouble.

2. LPC_GPIO_TypeDef is a structure type.

3. LPC_GPIO1 is pointer to LPC_GPIO_TypeDef.

4. LPC_GPIO_TypeDef *p; // p is a pointer to LPC_GPIO_TypeDef.

5. Therefore the following 4 statements are equivalent.

   // LPC_GPIO1->DIR |= (1 << pinNumber);        
   // (*LPC_GPIO1).DIR |= (1 << pinNumber);      
   // (*gpio_struct_ptr).DIR |= (1 << pinNumber);
   // gpio_struct_ptr->DIR |= (1 << pinNumber);  

6. The following functions is tested good.

void setPortPinDirection(int portNumber, int pinNumber)
  {
   LPC_GPIO_TypeDef *gpio_struct_ptr;
   gpio_struct_ptr = LPC_GPIO1;

    switch( portNumber )
    {
    case 1:

      // The following 4 statements do the same thing.
      // LPC_GPIO1->DIR |= (1 << pinNumber);     
      // (*LPC_GPIO1).DIR |= (1 << pinNumber);
      // (*gpio_struct_ptr).DIR |= (1 << pinNumber);
      // gpio_struct_ptr->DIR |= (1 << pinNumber);

      gpio_struct_ptr->DIR |= (1 << pinNumber);

      break;
    case 2:
LPC_GPIO2->DIR |= (1 << pinNumber);
 break;    
    default:
      break;
    }
}

.END



// ****************************************************************************
// Program - Blinky
// Description - Blink LED
// Author - TL Fong
// Version - 0.41
// Date - 2013jul12hkt2126
// License - Free
// Hardware - Somy ARM Cortex M0 LPC1114/301 Learning Board 
// Software - Keil uVision 4.71.2.0 ARM CC
// Method - Incremental testing
// Hardware configuration notes
//   1. LED1 is connected to PIO1-8 
// ****************************************************************************

#include <stdio.h>
#include "LPC11xx.h"
#include "timer.h"

void setPortPinDirection(int portNumber, int pinNumber)
  {
   LPC_GPIO_TypeDef *gpio_struct_ptr;
   gpio_struct_ptr = LPC_GPIO1;

    switch( portNumber )
    {
    case 1:

      // The following 4 statements do the same thing.
      // LPC_GPIO1->DIR |= (1 << pinNumber);    
      // (*LPC_GPIO1).DIR |= (1 << pinNumber);
      // (*gpio_struct_ptr).DIR |= (1 << pinNumber);
      // gpio_struct_ptr->DIR |= (1 << pinNumber);

      gpio_struct_ptr->DIR |= (1 << pinNumber);

      break;
    case 2:
LPC_GPIO2->DIR |= (1 << pinNumber);
 break;    
    default:
      break;
    }
}

void setPortPinValue(int portNumber, int pinNumber, int pinValue)
{
switch( portNumber )
{
case 1:
 if (pinValue == 1)
 LPC_GPIO1->DATA &= ~(1 << pinNumber);        
else
 LPC_GPIO1->DATA |= (1 << pinNumber);
break;
    case 2:
      ;
 break;    
default:
 break;
}
}

void blinkLed(int ledNumber, int onTime, int offTime, int blinkCount)
{
int count;

switch( ledNumber )
{
case 1:
setPortPinDirection(1, 8);
 
 for (count = 0; count < blinkCount; count--)
 {
 setPortPinValue(1, 8, 0);
        delayTenthSecond(onTime);    
   setPortPinValue(1, 8, 1);
        delayTenthSecond(offTime);
 }
      break;
    case 2:
      ;
 break;    
default:
 break;
}
}

int main()
  {
setPortPinDirection(1, 8);
blinkLed(1, 5, 10, 20); // Led 1, off 0.5 sec, on 1 sec, blink 20 times

  }

// ****************************************************************************
// End of Program
// ****************************************************************************
.END

No comments:

Post a Comment