Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Here is my code, can someone please help me in changing it to were it can run in

ID: 1839823 • Letter: H

Question

Here is my code, can someone please help me in changing it to were it can run in one direction continuously, then by pressing the second button it runs in the other direction. CCW to CW. TYIA

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

//Unipolar stepper motor controller
void delay(int x);
unsigned int IN, OUT;
void main(void)
{
/* put your own code here */
  
DDRC = 0xF0; /*Init PORT C bits 4 - 7 output*/
ATDDIEN = 0x00F0; /*Init bits 4 - 7 PortAD digital inputs*/
PER1AD = 0xF0; /*Enable internal pullups for PAD4 - PAD7*/
  
for(;;)
{
    IN = PT1AD; // Read PortAD;
   if (IN == 0xDF) //Is switch 2 closed (logic 0), rotate motor counter clockwise
   {
       PORTC = 0x80; //CCW
       delay(50); //wait
  
       PORTC = 0x40; //
       delay(50); //wait

       PORTC = 0x20; //
       delay(50); //wait
        PORTC = 0x10; //
       delay(50); //wait
  
   } else if (IN == 0x7F) //Is switch 2 closed (logic 0), rotate motor counter clockwise
   {
       PORTC = 0x10; //CCW
       delay(50); //wait
  
       PORTC = 0x20; //
       delay(50); //wait

       PORTC = 0x40; //
       delay(50); //wait
  
       PORTC = 0x80; //
       delay(50); //wait
   }
   else
PORTC = 0x00; //Turn off MOTOR if button is released (logic 1)
}
}
void delay(int c) //Delay function
{
int d;
  
for (; c != 0 ; c-- )
for ( d = 1 ; d <= 3270 ; d++ )
{}
}

Explanation / Answer

#include<16F877A.h>

#fuses HS,NOWDT

#use delay(clock=10000000)

#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,stream=RS232,bits=8)

void main()

{

while(true){

set_tris_a(0x06);

set_tris_b(0x00);

output_b(0x00);

if(input(PIN_A1) == 0){

int i;

for(i=0; i<6; i++)

{

output_b(0x01); //1 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

output_b(0x02); //2 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

output_b(0x04); //3 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

output_b(0x08); //4 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

}

for(i=0; i<6; i++)

{

output_b(0x04); //3 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

output_b(0x02); //2 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

output_b(0x01); //1 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

output_b(0x08); //4 No. PIN

delay_ms(500);

output_b(0x00);

if(input(PIN_A2) == 0){

goto stop;

}

}

stop: continue;

} //end if

} //end while

} //end main

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote