I mentioned some basic PIC programming in the last article, now it is time to do something interesting. Lets build a small robotic arm. We can make a PIC rotate a web cam in horizontal as well as vertical direction. A servo motor attatched to the bottom side of the web cam facilitates the horizontal rotation and the servo attatched to the side is responsible vertical rotation. You can get a nice picture here, which is the inspiration of this project.
A servo motor has 3 inputs, a red wire a white one and a black wire. As usual the red and black are for Vcc and GND. We provide the control pulse to the white wire. The total on-off time of the control pulse should be 20 ms and the on-time controls the postiion of the servo. In the servo i am using an on time of .5 ms positions the servo to one end and and an on-time of 2.2 ms to the other end. Hence we could make the servo turn to any position between these by providing the control pulse with on-time varying between .5 ms and 2.2 ms.
PIC in action…..
Let us create a 255 microsecond delay:
We can create a 255 microsecond delay by using the Timer0 module of the PIC (Refer my last article about PIC to know how the Timer works). The code for the delay is given below.
init_timer:
bsf STATUS, RP0
bcf OPTION_REG,T0CS
bcf STATUS,RP0
return
delay:
btfss INTCON,T0IF
goto delay
bcf INTCON,T0IF
return
This wont be sufficient to make the servo work,so lets define another function
delay_n:
movwf n
delay_n_loop:
call delay
decfsz n,1
goto delay_n_loop
return
This function calls the function delay k times where k is the value stored in the memory location n ie., we get a delay .5ms if we store 2 in n.
Now what we need is an I/O port…. I prefer PORTB to be used for this purpose. The 6th and 7th pin of PORTB is used for providing the pulse generated for the servo, the first 4 bits are used for getting the value to be stored at n which decides the servo action and the fifth pin which is also an input pin is used for selecting the servo…
Reading the input
get_n:
movlw 0x1f
andwf PORTB,0
return
get_ncomp:
sublw 0×50
return
The get_n function reads the input value for n and store it in accumulator(‘w’) register. The get_ncomp function gets the value 80 – n and store it in ‘w’, this is for the off-time of the pulse.
Now wrap up all these in main…
n equ 0×20
main:
call init_ports
call init_timer
infinite:
btfsc PORTB,5
goto L1
bsf PORTB,6
call get_n
call delay_n
bcf PORTB,6
call get_ncomp
call delay_n
goto L2
L1:
bsf PORTB,7
call get_n
call delay_n
bcf PORTB,7
call get_ncomp
call delay_n
L2:
goto infinite
end
Now what we need is an input device for the PIC which decides the position of the servo. As a first step we can make the PC do this for us. We can use the 4 characters ’4′, ’6′, ’8′, ’9′ of the numpad to make the web cam rotate horizontally and vertically by outputting certian values to the parallel port which is connected to the input pins of the PIC. Try writing the C program yoursel(its very simple)
Looking Forward:
I intend to control the webcam with a remote control as the input device. Hope i will get it soon….
Hello…
The delay program looks a bit familiar although the mc I used was different…One of us tried to do the same system by using 8051 programming. Pretty long I would say. Something like you had to divide the whole area into grids and write a seperate one for each grid..This looks more easier.Do the web cam part part fast ok.
leya
The delay procedure is a standard one, we see the same at different places in different languages :-). I just noticed that some text were missing in the article.. anyway its updated now. You could connect the webcam to servos and rotate it with your PC..