Program not functioning correctly

I am working on a program to control a relay from the Dragon 12-plus board. I have 5v from the board fed through the on board relay to control a RF switch. I set up a test program to cycle the relay on and off. I now added a on board switch to control the relay, the relay will not come on unless the switch is pressed. The goal of the program is to look at the switch, if the switch is pressed the relay will be energized.After compiling the program and downloading it to the board I press run. The relay cycles once through the while status and then stops. After the initial cycle the relay status is then able to be controlled by SW5 closure. My question is why does the relay cycle once when the the program is initially placed in run mode? I attempted to initially set the relay to off but this had no effect.Here is the code,



//Relay control program
// Press SW5 to energize relay

#include <hidef.h> /* common defines and macros */
#include "derivative.h" /* derivative-specific definitions */


void MSDelay(unsigned int);
void main(void)
{



DDRE = DDRE | 0b00000100; // PE2 as output
DDRH = 0X00; // Port H as inputs
PORTE = PORTE & 0b11111011; // PE2=turn off relay



while (1)
{
while ((PTH & 0x01) == 0) // while pressing SW5

{

PORTE = PORTE | 0b00000100; //PE2=1,turn on relay
MSDelay(1500); //on delay
PORTE = PORTE & 0b11111011; //PE2=0,turn off relay
MSDelay(1500); // off delay
}
}
}


void MSDelay(unsigned int itime) // msec delay
{
unsigned int i; unsigned int j;
for(i=0;i<itime;i++)
for(j=0;j<4000;j++);
}
Topic archived. No new replies allowed.