hasithgayan
Newbie level 5
i looking for make solar tracking system using pic 16f877a.i wrote code as follow but when it simulate
using proteus.it only change rotation direction not stop at given range.please help to slove.
using proteus.it only change rotation direction not stop at given range.please help to slove.
Code C - [expand] 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 // Servo definition and variables #define MAX_VALUE 200 #define CCW_ROTATION MAX_VALUE - 20 #define CW_ROTATION MAX_VALUE - 10 #define STOP_ROTATION MAX_VALUE unsigned char pulse_max=0; unsigned char pulse_top=0; unsigned char top_value = 0; static void interrupt (void) { if(T0IF) { // TIMER0 Interrupt Flag pulse_max++; // Pulse Max Increment pulse_top++; // Pulse Top Increment /* MAX_VALUE=200 turn off the pulse */ if (pulse_max >= MAX_VALUE) { pulse_max=0; pulse_top=0; PORTB.B1=0; // Turn off RC2 } /* top_value = MAX_VALUE - n, n=10: 10 x 0.1ms = 1.0ms, n=20: 20 x 0.1ms = 2.0ms */ /* 2ms -> CCW Rotation, 1ms -> CW Rotation */ if (pulse_top == top_value) { PORTB.B1=1; // Turn On RC2 } TMR0 = 156; // Initial Value for 0.1ms Interrupt T0IF_bit = 0; // Clear TIMER0 interrupt flag } } void main() { /* Init Servo Pulse */ pulse_max=0; pulse_top=0; top_value = MAX_VALUE; // top_value = MAX_VALUE: Servo Motor Stop OPTION_REG = 0b00000000; // 1:2 Prescaller TMR0=156; // Interupt every 0.1 ms T0IE_bit = 1; // Enable interrupt on TMR0 overflow GIE_bit = 1; // Global interrupt enable TRISB=0x00; PORTB=0x00; TRISC=0x00; PORTC=0x00; ADC_Init(); PWM1_Init(1000); while (1) { unsigned int ldr_left; unsigned int ldr_right; signed int ldr_diff; ldr_left=ADC_Read(0); ldr_right=ADC_Read(2); ldr_left=ldr_left*0.488; ldr_right= ldr_right*0.488; ldr_diff=ldr_left - ldr_right; if((ldr_diff >= -50) && (ldr_diff <= 50)) {top_value = MAX_VALUE; // Stop the Servo Motor } else if(ldr_diff > 50) //ccw { top_value = CCW_ROTATION; } else //ccw { top_value = CW_ROTATION; // Clockwise Rotation } } }
Last edited by a moderator: