EmbeddedRelated.com
Forums

LPC2103 GPIO interfering with PWM

Started by soufeazie December 6, 2006
Hello, I am trying to control a small dc motor along with two servos
using an LPC2103. I am having a strange problem where the servos go
crazy when the dc motor is actuated. This dc motor is driven by an
h-bridge circuit, so it is controlled with 2 GPIO pins from the
microcontroller. I chose pin 8 and pin 9 because it didn't look like
they interfered with the pins needed for PWM. Could someone please
suggest a possible cause for this problem? The code works fine when
the dc motor is not connected (even when motor(0) or motor(1) is
called). Any help would be much appreciated. Thank you in advance.

----start of relevant code (this is not the entire program)-----
servo(1500, 1500);

SecondCnt=0;
while(SecondCnt<2);

motor(0);
SecondCnt1=0;
while(SecondCnt1<2);
motor(1);

for(blademod=-500;blademod {
SecondCnt=0;
servo(blademod+1500,1500-blademod);
while(SecondCnt<1);
}
} //end of infinite main loop

} //end of main program

void servo(int micropw1,int micropw2)
{
// GPIO0.12 -> MAT1.0 = PWM0 = 1.0mS/20mS
// GPIO0.13 -> MAT1.1 = PWM1 = 1.5mS/20mS
// GPIO0.19 -> MAT1.2 = PWM2 = 2.0mS/20mS
// GPIO0.20 -> MAT1.3 = Not Connect = PWM Period Count (20mS)

int cycletime1 = 20000 - micropw1;
int cycletime2 = 20000 - micropw2;

int cycles1 = 29491*cycletime1/1000;
int cycles2 = 29491*cycletime2/1000;
timeval=0;
SecondCnt=0;

// Initial Timer1 Pin Connect
PINSEL0 &= 0xFCFFFFFF; // 1111 1100 1111 1111 1111 1111 1111 1111 Reset P0.12 Function
PINSEL0 |= 0x02000000; // 0000 0010 0000 0000 0000 0000 0000 0000 P0.12 -> MAT1.0
PINSEL0 &= 0xF3FFFFFF; // 1111 0011 1111 1111 1111 1111 1111 1111 Reset P0.13 Function
PINSEL0 |= 0x08000000; // 0000 1000 0000 0000 0000 0000 0000 0000 P0.13 -> MAT1.1
PINSEL1 &= 0xFFFFFF3F; // 1111 1111 1111 1111 1111 1111 xx11 1111 Reset P0.19 Function
PINSEL1 |= 0x00000080; // 0000 0000 0000 0000 0000 0000 1000 0000 P0.19 -> MAT1.2
PINSEL1 &= 0xFFFFFCFF; // 1111 1111 1111 1111 1111 11xx 1111 1111 Reset P0.20 Function

// Initial Timer Operate
T1TCR &= 0xFFFFFFFC; // Timer1 = Timer Mode Count By Rising PCLK
Edge
T1TC = 0x00000000; // Timer Start = 0
T1PR = 0x00000000; // Prescale = 0
T1PC = 0x00000000; // Prescale Count = 0

// Initial Timer1 Match Operate
T1MCR &= 0xFFFE; // Disable Interrupt on Match-0 (1111 1111 1111
111x)
T1MCR &= 0xFFFD; // Disable Reset TC on Match-0 (1111 1111 1111
11x1)
T1MCR &= 0xFFFB; // Disable Stop TC on Match-0 (1111 1111 1111 1x11)
T1MCR &= 0xFFF7; // Disable Interrupt on Match-1 (1111 1111 1111
x111)
T1MCR &= 0xFFEF; // Disable Reset TC on Match-1 (1111 1111 111x
1111)
T1MCR &= 0xFFDF; // Disable Stop TC on Match-1 (1111 1111 11x1 1111)
T1MCR &= 0xFFBF; // Disable Interrupt on Match-2 (1111 1111 1x11
1111)
T1MCR &= 0xFF7F; // Disable Reset TC on Match-2 (1111 1111 x111
1111)
T1MCR &= 0xFEFF; // Disable Stop TC on Match-2 (1111 111x 1111
1111)
T1MCR &= 0xFDFF; // Disable Interrupt on Match-3 (1111 11x1 1111
1111)
T1MCR |= 0x0400; // Enable Reset TC on Match-3 (0000 0x00 0000 0000)
T1MCR &= 0xF7FF; // Disable Stop TC on Match-3 (1111 x111 1111
1111)

// Initial PWM Function
// Set Output By Timer Match
// Reset Output By TC Reset
T1PWMCON |= 0x00000001; // Enable MAT1.0 = PWM
T1PWMCON |= 0x00000002; // Enable MAT1.1 = PWM
T1PWMCON |= 0x00000004; // Enable MAT1.2 = PWM

// Initial Timer1 Interrupt
T1IR &= 0xFE; // Disable MAT0 Interrupt (1111111x)
T1IR &= 0xFD; // Disable MAT1 Interrupt (111111x1)
T1IR &= 0xFB; // Disable MAT2 Interrupt (11111x11)
T1IR &= 0xF7; // Disable MAT3 Interrupt (1111x111)
T1IR &= 0xEF; // Disable CAP0 Interrupt (111x1111)
T1IR &= 0xDF; // Disable CAP1 Interrupt (11x11111)
T1IR &= 0xBF; // Disable CAP2 Interrupt (1x111111)
T1IR &= 0x7F; // Disable CAP3 Interrupt (x1111111)

T1MR0 = cycles1; // MAT1.0 = 0..560332.8 (19.0 mS) = Set Output
(PWM 1.0 mS)
T1MR1 = cycles2; // MAT1.1 = 0..545587.2 (18.5 mS) = Set Output
(PWM 1.5 mS)
T1MR2 = 530842; // MAT1.2 = 0..530841.6 (18.0 mS) = Set Output
(PWM 2.0 mS)
T1MR3 = 589824; // MAT1.3 = 0..589824 (20.0 mS) = Reset Output
(PWM Period)

// Initial Timer-1 Control
T1TCR |= 0x01; // Timer-1 Enable (0000000x)
T1TCR |= 0x02; // Timer-1 Press Reset (000000x0)
T1TCR &= 0xFD; // Timer-1 Release Reset (111111x1)

while(SecondCnt<1);
lcd_init();
}

void motor(int a) //when motor is connected, this causes a problem
with servos
{

IODIR |= 0x00000300;
SecondCnt1 =0;
if(a==1) //rotate motor one direction
{
IOSET = 0x00000100;
IOCLR = 0x00000200;
}
else if(a==0) //rotate motor opposite direction
{
IOSET = 0x00000200;
IOCLR = 0x00000100;
}

while(SecondCnt1<1); //delay

IOCLR = 0x00000300; //set pins low
}

An Engineer's Guide to the LPC2100 Series

----- Original Message -----
From: "soufeazie"
To:
Sent: Wednesday, December 06, 2006 2:46 PM
Subject: [lpc2000] LPC2103 GPIO interfering with PWM
> Hello, I am trying to control a small dc motor along with two servos
> using an LPC2103. I am having a strange problem where the servos go
> crazy when the dc motor is actuated. This dc motor is driven by an
> h-bridge circuit, so it is controlled with 2 GPIO pins from the
> microcontroller. I chose pin 8 and pin 9 because it didn't look like
> they interfered with the pins needed for PWM. Could someone please
> suggest a possible cause for this problem? The code works fine when
> the dc motor is not connected (even when motor(0) or motor(1) is
> called). Any help would be much appreciated. Thank you in advance.

Noise from the motor is probably getting onto the MCU supply. Try using a
separate supply.

Leon
--- In l..., "Leon Heller" wrote:
>
> ----- Original Message -----
> From: "soufeazie"
> To:
> Sent: Wednesday, December 06, 2006 2:46 PM
> Subject: [lpc2000] LPC2103 GPIO interfering with PWM
> > Hello, I am trying to control a small dc motor along with two servos
> > using an LPC2103. I am having a strange problem where the servos go
> > crazy when the dc motor is actuated. This dc motor is driven by an
> > h-bridge circuit, so it is controlled with 2 GPIO pins from the
> > microcontroller. I chose pin 8 and pin 9 because it didn't look like
> > they interfered with the pins needed for PWM. Could someone please
> > suggest a possible cause for this problem? The code works fine when
> > the dc motor is not connected (even when motor(0) or motor(1) is
> > called). Any help would be much appreciated. Thank you in advance.
>
> Noise from the motor is probably getting onto the MCU supply. Try
using a
> separate supply.
>
> Leon
>
Leon,

Thanks for the response. I should have mentioned that I am already
using two power supplies; a standard dc power supply (plugged into the
wall) is used to power the servos and dc motor, and I have a 5V
battery pack powering the microcontroller. Is it possible that noise
from the motor is affecting the servos through the common +5V power
connection? Maybe I will try running both the servos and the
microcontroller from the battery pack, and isolate the dc motor on the
wall power supply. Thanks for the suggestion.

-David