Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

How can we write a code for motor control in c++ in micropro using pic-18f452

user-image
Question added by MUHAMMAD OBAID KHALID SHAH SHAH , INTERNEE , CHASHMA HYDEL POWER STATION
Date Posted: 2013/10/09
Prabhakaran Pillai Balu
by Prabhakaran Pillai Balu , Computer Operator , CVM Computers

void main(void) { unsigned char ldr_left; unsigned char ldr_right; int ldr_diff; OSCCON=0x70; // Select8 Mhz internal clock /* Initial Port Used */ TRISC =0x03; // Set RC0 and RC1 as input others as Output ANSEL =0x30; // Set PORT AN4 and AN5 as analog input ANSELH =0x00; // Set PORT AN8 to AN11 as Digital I/O PORTC =0x00; // Turn Off all PORTC /* Init Servo Pulse */ pulse_max=0; pulse_top=0; top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop /* Initial ADC */ ADCON1=0b; // Select the FRC for8 Mhz /* Init TIMER0: Period: Fosc/4 x Prescaler x TMR00.0005 ms x2 *100 =0.1 ms */ OPTION =0b; //1:2 Prescaller TMR0=156; // Interupt every0.1 ms T0IE =1; // Enable interrupt on TMR0 overflow GIE =1; // Global interrupt enable for(;;) { /* Read the ADC here */ ADCON0=0b; // select left justify result. ADC port channel AN4 GODONE=1; // initiate conversion on the channel4 while(GODONE) continue; // Wait for ldr_left conversion done ldr_left=ADRESH; // Read8 bits MSB, Ignore2 bits LSB in ADRESL ADCON0=0b; // select left justify result. ADC port channel AN5 GODONE=1; // initiate conversion on the channel5 while(GODONE) continue; // Wait for ldr_right conversion done ldr_right=ADRESH; // Read8 bits MSB, Ignore2 bits LSB in ADRESL /* Get the different */ ldr_diff=ldr_left - ldr_right; if ((ldr_diff >= -THRESHOLD_VALUE) && (ldr_diff <= THRESHOLD_VALUE)) { top_value = MAX_VALUE; // Stop the Servo Motor } else { if (ldr_diff > THRESHOLD_VALUE) { top_value = CCW_ROTATION; // Counterclockwise Rotation } else { top_value = CW_ROTATION; // Clockwise Rotation } } } }

More Questions Like This

Do you need help in adding the right keywords to your CV? Let our CV writing experts help you.