Hello everyone,
We have initilalized all the peripherals and created subsidiary function we need. In this note, we are going to create main.c with the interrupts routines.
First let's remember algorithm flow char to help ourselves to be able to imagine easily in mind. I marked parts we need to handle with red rectangle. Then I will give complete code of main.c and I'll try to explain line by line.
I have created struct to keep some related variables together. These variables are isButton, isMotorRotateEnough, isComplete. Their variable names explain what they are for. Also I have created two variables to use in loops. They are counterMotorRotate and counterABSPulse.
There are variable initialization up to while(1) loop in main function. while loops in while will keep program in the loop until waited action occurs.
void EXTI0_IRQHandler()function is for start button. It will wait until button is pressed then will let microcontroller to create PWM signal for motor spinning. Motor needs to overcome inertia force therefore we need to wait motor to spin a few tour. Timer 2 will play role to count these needed time. When time is up, encoder value will be set to 0 and ABS signal interrupt will be enabled. Also to count encoder pulses timer will be set as two channels input.
void EXTI1_IRQHandler() function will be executed in every ABS signal rising edge. When ABS signal generates rising edge this function will get the encoder value and evalute it according to requirements. Each time when this function executed encoder counter must be set to zero. Otherwise encoder counts over previous value and in the second execution program fails as part not OK.
And done! Here is the demonstration videos.
Comments
Post a Comment