Esy15 1

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

A switch is connected to pin P2.7.

Write a C program to monitor the status of SW and perform the


following:

1. If SW=0, the stepper motor moves clockwise.

2. If SW=1, the stepper motor moves counter clockwise.

#include <reg51.h>

#define DELAY_TIME 10 // Adjust for desired motor speed

sbit SW = P2^7; // Define switch at P2.7

void delay(unsigned int time);

void rotateClockwise();

void rotateCounterClockwise();

void main() {

// Set the initial state of the switch as input

SW = 1; // Initialize switch as input

while (1) {

if (SW == 0) { // If switch is pressed (SW = 0)

rotateClockwise(); // Rotate motor clockwise

} else { // If switch is not pressed (SW = 1)

rotateCounterClockwise(); // Rotate motor counterclockwise

void delay(unsigned int time) {

unsigned int i, j;

for (i = 0; i < time; i++) {


for (j = 0; j < 1275; j++); // Adjust for clock speed of 89C51

// Function to rotate motor clockwise

void rotateClockwise() {

P1 = 0x09; // 1001

delay(DELAY_TIME);

P1 = 0x0C; // 1100

delay(DELAY_TIME);

P1 = 0x06; // 0110

delay(DELAY_TIME);

P1 = 0x03; // 0011

delay(DELAY_TIME);

// Function to rotate motor counterclockwise

void rotateCounterClockwise() {

P1 = 0x03; // 0011

delay(DELAY_TIME);

P1 = 0x06; // 0110

delay(DELAY_TIME);

P1 = 0x0C; // 1100

delay(DELAY_TIME);

P1 = 0x09; // 1001

delay(DELAY_TIME);

You might also like