Please assume the following program structure for a moving bot with 2 motors (using an 8951 chip):
Code:
...corresponding C and 8951 libraries included here...
...some initialization here. also TR0 and EA and ET0 are all set...
...some global variables here, most of which are 'volatile'
void PIcontroller(void); // the controller for making wheels rotate at same speed
void RotationCounter(void); // counts the number of turns of the wheels via 2 rotary encoders connected to each wheel
void forward(void);
void backward(void);
void stop(void);
void turn(void);
void MainController(void) interrupt 1 using 1 // an interrupt is needed for the PIcontroller() and RotationCounter() to work properly, I think
void main(void);
- Almost all of the functions use global volatile variables.
- Each function may call another function. also pins maybe set or clear or may need PWM applied to them (for the motors) in each function. For example, forward() needs to call PIcontroller() and RotationCounter() in order to have some PWM. Then some pins are needed to be configured to use PWM.
- the MainController() interrupt calls each function with some conditions. e.g.
forward() until rotary counters reach 200;
stop();
backward() until rotary counters reach 200;
etc.
- main() would simply call the interrupt in an infinite loop, e.g. while(1) { MainController(); }
Now my question is that does the above coding structure do what it is intended to do? Is it correct coding? Are the interrupts being used correctly? Is there a way to implement the same idea in main() instead of the interrupt function?
P.S> This is not a part of the main question, but would be appreciated if someone could tell why this is happening: in the MainController() interrupt when I call forward() the bot moves forward alright. But when I call forward() again, it acts weird (e.g. both wheels start going backwards with very low PWM value)...