question archive Stepper motors are used in DVD drives to open/close the DVD tray
Subject:Computer SciencePrice:4.87 Bought7
Stepper motors are used in DVD drives to open/close the DVD tray. By using PIC18F458 micro-controller(or any micro-controller you can use i.e with PORTS initialization)using C program, write code to open/close of stepper motor and an LCD to display whether the tray is open or closed.
Requirement
Two push buttons are to be used, one to open the DVD tray (run the stepper motor in clockwise)
and the other to close the DVD tray (run the stepper motor in anti-clockwise direction).
When the open push button is pressed, the stepper motor should run in the clockwise direction and stop when it is released. When the close push button is pressed the stepper motor should run in the anticlockwise direction and stop when it is released.
An LCD is used to display the open (clockwise rotation) /close (anti-clockwise rotation) status of the DVD tray.
When the stepper motor is not running the LCD should display your name(any-name) (first sixteen characters if your name is longer than 16 characters) on the first line and your student ID(any-number) number on the second line.
a) Construct a C program to implement this task.
b) Explain how your code works (include comments). Your explanation should include the port initialization, LCD initialization and the logic you have used to implement this given task.
Answer: //LCD Module Connections sbit RS = P0^0; sbit EN = P0^1; sbit D0 = P2^0; sbit D1 = P2^1; sbit D2 = P2^2; sbit D3 = P2^3; sbit D4 = P2^4; sbit D5 = P2^5; sbit D6 = P2^6; sbit D7 = P2^7; //End LCD Module Connections TRISB0 = 0; TRISB1 = 0; TRISB2 = 0; TRISB3 = 0; RB0 = 0; RB1 = 0; RB2 = 0; RB3 = 0; void main () { TRISB = 0x00; PORTB = 0x00; // Initialized to be LOW while (1) { // Rotate CW // Create Bit-Shifting Variableunsigned char i=0;// Turn One Side for (int j=0; j<48; j++) { PORTB = (1<<i); i++; // Advance to the next output pin __delay_ms(100); // StepDelay if (i==4) i=0; Lcd8_Clear() ; Lcd8_Write_String("OPEN"); } // Rotate CCW // Create Bit-Shifting Variableunsigned char i=0;// Turn The Other Side for (int j=0,i=0; j<48; j++) { PORTB = (8>>i); i++; __delay_ms(100); if (i==4) i=0; Lcd8_Clear() ; Lcd8_Write_String("CLOSE"); } }}
Step-by-step explanation
Our task is to output the bit-pattern shown previously in order to make the stepper motor rotate a complete rotation in CW direction, then reverse the output bit-pattern in order to achieve a CCW rotation, and keep repeating this behavior.
The unipolar stepper motor we're using has a resolution of 7.5°/step. Which means a complete rotation will obviously require 360/7.5 = 48 Full-Step!
Let's say we'll be using PORTB as the output port. As we'll hook the least significant 4-pins (B0, B1, B2 & B3) to the 4-coils' driver transistors within the ULN2803 IC chip. To set these pins [RB0 to RB3] to be output pins and initialize these pins' states to be LOW (logic 0), we'll write the following code.
TRISB0 = 0;TRISB1 = 0;TRISB2 = 0;TRISB3 = 0;RB0 = 0;RB1 = 0;RB2 = 0;RB3 = 0;
Well, this could be done in a much simpler way. As long as we aren't using pins [RB4 to RB7], by setting the whole PORTB to be output port and initializing all it's pins to be LOW. Only 2 LOC will do the hack.
void main () { TRISB = 0x00; PORTB = 0x00; // Initialized to be LOW while (1) { // Rotate CW ... // Rotate CCW ...}}
NoteIt's a preferred practice to give initial values (states) to your output pins right after setting them to be output pins. This ensures a glitch-free output line which is substantial for sensitive systems in particular.
And now, we're done with the configurations part. Let's move next to the system's main Loop. In which we'll rotate CW & CCW.
CW Rotation
The order of sequentially activated coils determines the motor's direction of rotation. If activating the coils in this order [1 -> 2 -> 3 -> 4] results in a CW rotation, then activating them in this order [4 -> 3 -> 2 -> 1] results in a CCW rotation. Each consecutive 2-Steps are separated with a time delay called StepDelay. The following code will show you how we can implement these concepts in C.
// Create Bit-Shifting Variableunsigned char i=0;// Turn One Sidefor (int j=0; j<48; j++){ PORTB = (1<<i); i++; // Advance to the next output pin __delay_ms(100); // StepDelay if (i==4) i=0;}
Which will result in the activation of each coil at a time exactly in the same order shown previously in this diagram.
After 48-round executing this for loop, the stepper will complete a Full-Rotation 360° in one direction.
CCW Rotation
In order to reverse the rotation direction, we'll make a few changes to the previous code. As shown below
// Create Bit-Shifting Variableunsigned char i=0;// Turn The Other Sidefor (int j=0,i=0; j<48; j++){ PORTB = (8>>i); i++; __delay_ms(100); if (i==4) i=0;}
NoteThe decimal value that corresponds to this bit-pattern (1000) is 8 which will be right-shifted to get the following patterns (0100), (0010) and (0001).
Now, plug this code in your main.c file or download the full project code from the attachments section. Cross your fingers! Hit the compile button! And we're done with the firmware.