Question
i would like to rewrite the code using 3 inputs for 4 outputs. assuming my 3 inputs are channels 1,2&3 and my outputs are motors1,2,3&4.
i would like to rewrite the code using 3 inputs for 4 outputs. assuming my 3 inputs are channels 1,2&3 and my outputs are motors1,2,3&4. motor 1 is controlled by CH1, Motor2 by CH2 to move the Car forward, backwards,left and right. but for motors3&4 there are controlled by CH3. whenever CH3 is high, motor3 will turn CW while motor4 will turn CCW. whenever CH3 is neutral(at0), both motor stops. i am using a RC controller Spektrum and a 4channel receiver.
#include
// 2. Declarations Section // Global Variables
int in1; int in2; int in3; long out1; long out2; long out3; long count1 = 0; long count2 = 0; long count3 = 0; long count4 = 0;
// Insert Function Prototypes here
void wait100us(void); //time delay function
// 3. Subroutines Section // MAIN: Mandatory for a C Program to be executable
int main(void) { // Declare variables here // Initialize the ports here
//Initialize PortF (used for testing purposes only) volatile unsigned long delay; //create delay variable SYSCTL_RCGC2_R |= 0x20; //initialize PortF clock delay = SYSCTL_RCGC2_R; //wait for response GPIO_PORTF_LOCK_R = 0x4C4F434B; //unlock register GPIO_PORTF_CR_R |= 0x1F; // GPIO_PORTF_PUR_R |= 0x11; //enable pull up resistor GPIO_PORTF_DIR_R |= 0xE; //specify output direction for pins PF1-PF3 GPIO_PORTF_DEN_R |= 0x1F; //enable digital function for pins PF0-PF4 GPIO_PORTF_AMSEL_R &= 0x0; //disable analog function GPIO_PORTF_AFSEL_R &= 0x0; //disable alternate function GPIO_PORTF_PCTL_R &= 0x0; //
//Initialize PORTB (used for GPIO) SYSCTL_RCGC2_R |= 0x2; //initialize clock delay = SYSCTL_RCGC2_R; //wait for response GPIO_PORTB_DIR_R |= 0xF0; //specify output direction for pins PB4-PB7 GPIO_PORTB_DEN_R |= 0xFF; //enable digital for pins PB0-PB7 GPIO_PORTB_AMSEL_R &= 0x0; //disable analog function GPIO_PORTB_AFSEL_R &= 0x0; //disable alternate function GPIO_PORTB_PCTL_R &= 0x0; // GPIO_PORTB_DATA_R &= 0x0; //clear data register
while(1) { //start primary control loop in1 = GPIO_PORTB_DATA_R & 0x1; //read PB0 in2 = GPIO_PORTB_DATA_R & 0x2; //read PB1 in3 = GPIO_PORTB_DATA_R & 0x4; //read PB2 //read signal from transmitter if (in1 == 0x1) { //if CH1 signal is high count1 = count1 + 1; //increment counter 1 } else { //if CH1 signal is low if (count1 != 0) { //check if counter 1 is not blank out1 = count1; //write value of counter 1 to output 1 count1 = 0; //clear counter 1 } } if (in2 == 0x2) { //if CH2 signal is high count2 = count2 + 1; //increment counter 2 } else { //if CH2 signal is low if (count2 != 0) { //check if counter 2 is not blank out2 = count2; //write value of counter 2 to output 2 count2 = 0; //clear counter 2 } } if (in3 == 0x4) { //if CH3 signal is high count4 = count4 + 1; //increment counter 3 } else { //if CH3 signal is low if (count4 != 0) { //check if counter 3 is not blank out3 = count4; //write value of counter 3 to output 3 count4 = 0; //clear counter 3 } } //output to SBC GPIO_PORTB_DATA_R &= ~0xE0; //clear PB5-PB7 if (out1 >= 30 && out2 >= 30) { //if both thumbsticks are in forward position GPIO_PORTB_DATA_R |= 0x60; //raise PB5 and PB6 } else if (out1 >= 30 && out2 < 30 && out2 > 23) { //if left thumbstick is in forward position while right thumbstick is in neutral position GPIO_PORTB_DATA_R |= 0x20; //raise PB5 } else if (out1 < 30 && out1 > 23 && out2 >= 30) { //if left thumbstick is in neutral position while right thumbstick is in forward position GPIO_PORTB_DATA_R |= 0x40; //raise PB6 } else if (out1 < 30 && out1 > 23 && out2 < 30 && out2 > 23) { //if both thumbsticks are in neutral position GPIO_PORTB_DATA_R |= 0x0; //PB5-PB7 remain blank } else if (out1 <= 23 && out2 >= 30) { //if left thumbstick is in reverse position while right thumbstick is in forward position GPIO_PORTB_DATA_R |= 0x80; //raise PB7 } else if (out1 >= 30 && out2 <= 23) { //if left thumbstick is in forward position while right thumbstick is in reverse position GPIO_PORTB_DATA_R |= 0xA0; //raise PB5 and PB7 } else if (out1 <= 23 && out2 <= 23) { //if both thumbsticks are in reverse position GPIO_PORTB_DATA_R |= 0xC0; //raise PB6 and PB7 } if (out3 >= 28) { //if right thumbstick is in left position GPIO_PORTB_DATA_R |= 0xE0; //raise PB5-PB7 } wait100us(); //call wait function } //end primary control loop } //end main program
// Insert subroutines here
void wait100us(void) { //wait function while(count3 < 50) { //while counter 3 is below 50 count3 = count3 + 1; //increment counter 3 } count3 = 0; //clear counter 3 } //end wait function
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started