Thursday, October 7, 2010

Servo Motor Controller

 The servo motor is widely used in model hobbyist such as airplane R/C model for moving the rudder, ailerons, elevators and acceleration control or in the car R/C model for steering and acceleration control. In this tutorial we will learn how to control the servo motor as well as the simple close loop control algorithm for this servo motor.
Since the internal control electronics of each servo motor expects to have position data updates at intervals of approximately 60 times per-second (60Hz), the timeout/label helps establish this timing without using additional pause commands or other methods. Notice from the scope trace below that we have 15mS delays between the servo pulses. This corresponds to our 15mS delay time waiting for incoming serial data in the serial-input code line.
 
Pulse Position Modulation
The most important thing to remember from this servo tutorial is that all standard servos use the Pulse Position Modulation (PPM) language. A pulse is simply whether or not the control voltage is on or off. The amount of time that a pulse is on during a 2ms time frame dictates the servos position. This time frame, or sequence, repeats around 50 times per second. During the first half of this 2ms time frame, the pulse is always on. This starts a counter, so to speak, to tell the servo to start listening for the command. The second portion of this 2ms time frame is the command that dictates the position of the servo.
Take a look at the chart below to help you better understand this servo tutorial. The amount of time during the designated 2ms time frame that the pulse will be on for the three most extreme positions(far left, far right, neutral). There are an infinite amount of positions in between these values and amount of time the pulse is ON changes in proportion. This is what makes Pulse Position Modulation analogue.
Servo #1:
Far Left Command: Time ON = 1 + 0.0 ms = 1.0 ms
Servo #2:
Neutral Command: Time ON = 1 + 0.5 ms = 1.5 ms
Servo #3:
Far Right Command: Time ON = 1 + 1.0 ms= 2.0 ms

The pulsout command uses the positional data received/stored in the byte-variable pos to generate the servo control pulses. The PIC12C671 is limited to a maximum oscillator speed of 4MHz, hence, the period of the generated pulses will be in 10uS increments.Now that we have the servo-pod ready to use, we need to figure out how to send positional data to the servo-pod that will let us position the servo motor. As stated previously -- we know that the pulsout command is dependent on the oscillator frequency, and the pulse duration will be in 10uS increments when used in conjunction with an oscillator frequency of 4MHz. Since the PIC12C671 maximum operating frequency is 4MHz, we'll have to make due with this resolution. The circuit diagram is given below:

The Test Program:
Now that you're ready to start using the servo-pods, some test code to cycle the servo motors will be necessary. Here's a quick & simple Basic Stamp routine that will cycle a servo motor attached to a servo-pod hard-coded with the unique ID #5.

Stepper Motor Controller

Stepper motors, unlike ordinary DC motors, are brushless and can divide a full 360° into a large number of steps, for example 200. In robotics, stepper motors are widely used. They offer amazing precision as well as continuous rotation. Also, any inaccuracy between steps are non-cumulative; 200 steps will always be 1 revolution. These features make them ideal for driving the wheels on a robot, and creating linear motion using a leadscrew. The drawbacks are that they require current when not moving they are relatively expensive and they are quite heavy for the amount of torque they give. 
 
 By using an external stepper motor controller, such as the UCN 5804, you can simplify your programs and control as many motors as you have outputs via an array of UCN 5804's. Not only does it allow for the control of more motors, but more importantly, it simplifies the process. You now only have to output the pulse of your desired speed. Additionaly, you can switch between full and half stepping in real time via a switch on the UCN 5804 (or you may have the PIC control it), as well as reverse direction. A pinout for the UCN804 is shown in below:
The PIC Basic language has a command that allows you to read the value of potentiometers on a given scale. This is the "pot" command, and is used in the following manner:

pot pin,scale,var
Where pin is the pin the potentiometer is connected to (pins B0-7), scale is the maximum value the command will return (in this case 245, being the maximum amount of ms we want in between pulses), and var is the variable the value will be placed in. Using this command, we can determine the value of a potentiometer (we will be using a 50K ohm pot) and then use it to control the pulsewidth outputted to the UCN 5804. A schematic is shown in below:

The Code in given below
' Controlling a stepper motor with the UCN 5804 and a potentiometer
Symbol delay = B0                   ' make a delay variable
low 1                                        ' set the output enable low

start:
pot 2,245,delay                        ' read pot at pin B2
if delay < 25 min                       ' set min delay to 25
goto turn

turn:                                        ' if delay is over 25, start moving
delay = delay * 1000                ' turn delay into microseconds
pulsout 0,delay                         ' send to UCN 5804
goto start                                  ' go back to start

min:
delay = 25                                ' bottom out the delay at 25
goto turn                                   ' start the motor

 

Wednesday, October 6, 2010

Object Replacing With Line Follow Robot


This given video representing Object Replacing With Line Follow Robot. This robot has many complex features and application. These in a robotic hand which used for replacing the object.
[I can not give the Source Code and Hex File.]

Tuesday, October 5, 2010

Line Follow Robot






The above photos are representing my hand made first “Line Follow Robot”. This robot can follow simultaneously white line or black line. It also can work in 90 degree angle type line, small circle, end of line etc.








Testing the robot
  I created courses for the robot to follow out of black and white electrical insulation tape, using one colour per course. For each course I initially balanced the robot's left and right sensors and adjusted the brightness of the headlight LEDs.
One of the tests was to determine the ability of the robot to sense lines under various conditions. For this, I measured the voltage at each of the robot's sensor test points after placing it directly over the top of a straight line in a course, with it's motors switched off. I repeated the test with the robot pointing slightly to the right and then to the left of the line.
The measurements were taken with a box covering the robot to shield it from all ambient light and then with the box removed so that I could measure the impact of the room's lighting. 

Low cost multfunctional frequency counter



We are introducing a low cost multifunctional seven digit LCD frequency counter, for your HAM radio shack. This frequency counter is designed in such a way that it can be used for direct frequency measurement or with any of the commercial / home made radio/transceiver as a digital frequency display. The power consumption of the counter is very low. It could be operated with even an ordinary 9V battery for a long period. This frequency counter contains only a few components. The brain of the unit is a PIC 16F84 micro-controller. The LCD display used is a state of the art, commonly available 16 characters dot matrix in one line unit. The other main attractive feature is the use of a single transistor as the input amplifier, which will give a sensitivity of less than 150 m.V with a very high bandwidth. With this, the counter is useful for the measurement of a wide variety of input wave forms without loosing the accuracy. The counter is capable of measuring frequency up to 35MHz in normal condition and is suitable up to 3.5GHz with pre-scalar.

MAIN FEATURES
  • 10 Hz frequency resolution.
  • Direct frequency measurement.
  • Seven offset channels selectable with a push button.
  • Each offset channel got 3 options (VFO + IF) , (VFO IF), ( IF - VFO ) selectable with a three position switch. 
  1. Eg.1 Suppose IF is 9MHz (9MHz SSB filter using with home made TRX) and VFO is 2 MHz you have to select the third option (IF-VFO) ie (9 2) = 7 for 40 mtr operation. 
  2. Eg.2 Suppose IF is 9MHz and VFO is 5MHz you have to select the first option VFO+IF ie 9 + 5 = 14 for 20 mtr operation With this offset channel selection option you can simply input the VFO frequency to the counter with out any conventional mixing for displaying the operating frequency.
  • In each of the above offset channel option, it is possible to select USB/LSB displayed as N / R (Normal / Reverse) selectable with another 2 position slide switch.
Download Hex File

Light Detection



This experiment detects the presence of light. A photo-darlington transistor is connected to the circuit via a plug and socket and exposed to light. The change in resistance of the photo-darlington transistor creates a voltage change across it and this is amplified by the circuit and delivered to an input of the microcontroller. The program turns on the 8 LEDs when the photo-darlington transistor detects change in illumination. The photo-darlington transistor is connected to the circuit via a capacitor and only CHANGES in illumination are detected.
If the illumination is decreased, a point will be obtained where the circuit is sensitive to the changes in supply rail voltage. At this point the circuit will start to oscillate or MOTOR-BOAT. The circuit must be re-designed to prevent this from occurring. If the photo-darlington transistor is placed in a room with incandescent lighting, the 50 or 60Hz from the light will be detected. These pulses must be dealt with by the program to obtain a reliable HIGH/LOW pulse.

Measuring Resistance


This experiment measures resistance. You can get very low-cost digital multimeters to measure resistance and display the value on a four digit display. We cannot compare with cost and complexity of this type of display, but our circuit has other features. It will activate a device when a particular value of resistance is detected. This allows measurement of degrees of rotation of a potentiometer or the conductivity of a liquid (via two probes), plus many other areas where resistance values change. Our demonstration program uses a potentiometer to detect resistance in the range 0k to 10k however any other range can be read, by changing the value of C. The accuracy of the circuit is determined by the tolerance of C (most capacitors are 10%). The time taken to produce a low on the input is used as a jump value in a look-up table and a display-value is obtained for the 7-segment display. For simplicity, the values 0 to 9, plus "-" overflow, are displayed. Further experiments show two and three-digit accuracy.

Download SourceCode

Pulse Detection with a coil


This experiment uses a coil to detect pulses. A magnet is moved past a coil and this creates a voltage in the turns of the coil. This is ideal for picking up the rotation of a shaft. It is non-mechanical and will have an infinite life. Reed switches have a very short life when used rapidly to detect shaft rotation and have a fairly low speed of operation. The output voltage of a coil is fairly low and needs two stages of amplification for the signal to be large enough to be detected by the input of a microcontroller. The clever arrangement on the front end of the analogue amplifier of the PIC LAB-1 board allows a microphone or coil to be fitted. The coil does not require the resistor, (it is required by the electret microphone) however it does not affect the operation. This demonstration program increments the 7-segment display.
This allows a count-of-ten however experiments on the web include a 2 and 3-digit readout from the 7-segment display and an RPM counter. The advantage of a magnetic pickup is the lack of switch-noise. The pulses from the pick-up are very clean but must be debounced for low-speed detection. The 2-stage amplifier increases the sinewave signal and over-amplifies it to produce a rail-to-rail signal commonly called a square-wave or digital signal.

Download Source Code

Analog to Digital Conversion Using PIC16F84

This experiment shows 0-256 parts of a 10k potentiometer on the 8 LEDs. It is not accurate, but shows the concept of A to D conversion. Many microcontrollers have an input that can read any value of voltage from 0v to 5v (and higher by using a voltage divider network). Normally there are 256 steps in this range to produce a resolution of approx 20mV for 0-5v scale. This is called an A to D input (A to D converter - analogue input) and is ideal for measuring voltages and other values that are classified as ANALOGUE. A very simple external circuit can be added to measure different parameters such as the change in resistance of a temperature probe and other analogue devices. The PIC16F84 does not have an internal A to D converter, however we can create an A to D feature by using two lines and a sub-routine. To create an analogue input, a capacitor "C" is connected in series with an unknown resistor (R) and charged via one of the lines of the microcontroller. The diagram below shows how this is done.
 
 
The first diagram shows a resistor and capacitor connected in series. This is called a TIME DELAY circuit. The capacitor is initially uncharged and the resistor charges the capacitor to a specified value. The time taken to reach this value is called the Time Delay. The mid-point of the two components is called the "detection point." It does not matter if the resistor is above the capacitor or below. The same Delay Time (or a similar time) will be produced.
In the second diagram the capacitor is above the resistor and if the top line is taken HIGH, the voltage at the detection point will fall to a specified value after a Delay Time. If the value of the resistor is changed, the time taken for the voltage at the detection point to reach a specified value will alter. That's exactly what happens in the third circuit above. The micro monitors the voltage on the detection point and when it reaches the lower threshold for the input line, the program displays the "count-value" on the 8 LEDs.
The other feature that has to be worked out is the time taken for the capacitor to charge. In our circuit, the capacitor has charged before 255 loops have been executed (when the pot is at maximum resistance) and we cannot same at a faster rate, so the maximum display-value is "DF." To obtain a full reading, the capacitor will need to be increased in value.
 

Hee Haw Sound

This experiment creates a Hee Haw sound for an alarm. The diagram shows the number of cycles for the HEE and the time taken for each cycle, equates to a certain length of time. The frequency of the HAW is lower and the number of cycles must be worked out so that the time for the HAW is equal to the time for the HEE. This is simple when writing the program. The values loaded into the two files for the HEE are reversed for the HAW. The routine consists of two sections: HEE and HAW. Each section has two nested loops. The inner loop creates the length of time for the HIGH and LOW to produce a single cycle and the outer loop creates the number of cycles.

Download Source Code

Saturday, October 2, 2010

Moving Message Display



It is possible to process periodically with the hardware timer. While the hardware timer is counting time, the software can do the other processing. In the software, the scroll of the message is periodically done by the interruption processing of hardware timer. And the lighting-up control processing of LEDs is executed in the waiting time of scroll. The Circuit diagram of this project is given below.
  •  LED control circuit
  • As for the left figure, all RA ports are in the L level condition. In this case, only outout-0 of 74HC154 is L level. Then, only TR1 is in the ON condition, the other transistors are in the OFF condition. So, only the LEDs which are connected with TR1 are in the lighting-up possible condition. The lighting-up of the LED is decided in the condition of the RB7-RB0 ports. In case of the left figure, because RB0 and RB4 are L level, D1 and D5(It isn't displayed in figure) light up. The other LEDs are connected with RB0 and RB4 can't be lit up, because the transistors for the power supply to those LEDs are in the OFF condition.


    The figure on the left shows the situation of the LED blink by the RA ports and the RB ports of PIC16F84A. You can find that the lighting-up time of one LED is 1/16 with this animation. If using latch registers for each LED row, the lighting-up time of the LEDs can be made long. I don't use latch registers because the printed board space needs more.
    Download Source code
    Download Hex File


Room temperature controller



This equipment uses two temperature sensors, drives external equipment, and keeps the temperature of the room at preset temperature. The purpose of this equipment is for preventing room temperature going up with the heat of the computers. Electric cost will become high if an air-conditioner is always operated. Then, I made the equipment which adjusts the temperature of the room automatically using some ventilation fans. The function below preset temperature is a function attached moreover. I think that it can use for temperature control, such as a greenhouse. Control mode(works over preset or below preset) is changed with a switch.
  •  LED display and SW reading circuit

7 bits of low ranks of PORTB are used for lighting of 7 segment LED, and reading of BCD switch in common. PORTC and the 8th bit of PORTB is used for designation of a device. The mode change of PORTB is required of LED control and BCD-SW control. This change is performed by software.
The diode connected to the BCD switch is for prevention of illegal current. With a BCD switch, a common terminal and each terminal corresponding to a bit short-circuit by setup. If there is no diode, the setting state of a switch will influence operation of other devices. In the case of a silicone diode, VF is about 0.6V. When L level detection of the PIC cannot be normally carried out with this voltage, it is better to change a diode into other kinds.(Germanium etc.)




  • External equipment drive circuit 
 
This is the circuit which controls external equipment. Although based on the drive current of the relay to be used, in this case, the drive circuit which uses a transistor is adopted.






  • Temperature sensor circuit
 A temperature sensor (LM35DZ) can measure from 0°C to 100°C. However, the output is 0V at 2°C. Therefore, the voltage of minus is required in order to measure 0°C. Since the minus power supply is not used with this equipment, the measurable temperature is above 2°C. The output of a sensor goes up by 10mV for every 0°C. The output voltage in 32°C is 300mV. The output voltage of a sensor is amplified by an operational amplifier, and is inputted into the A/D converter of PIC. Proofreading of a temperature display is performed by adjusting the gain of an operational amplifier by VR. In this circuit, in order to simplify a circuit, the operational amplifier which operates with a plus single power supply is used.
 
 

Friday, October 1, 2010

RF Remote Controller (Receiver)

 




The given circuit diagram representing the receiver section of previous RF Remote Controller.

FET amplification circuit is used for a high frequency amplification circuit of the receiver. It is because the getting of parts is easy. So, it doesn't have high sensitivity. To make the receiver with high sensitivity, the circuit which used IC for FM receiver is good.
  •  High frequency amplification circuit

The 2 stages FET amplifier is used for the high frequency amplification. The FET amplifies only at the voltage to apply to gate (G). The resistor to be putting in the source (S) of the FET is to make the bias voltage of the gate. When making a resistance value big, voltage of the source goes up to the grounding and the bias voltage becomes big. In case of the gain of the amplifier is big, when the signal of the output returns to the input, an amplifier oscillates. In the case, it makes this resistance value big and the gain must be lowered. The power of the FET connects with the center tap of the coil (L2 and L3) to be putting in the side of drain (D). This is to make do operation to have been stable, improving the characteristic of the resonance circuit. The characteristic of the resonance circuit is called queue (Q). The ideal resonance circuit picks up only a resonant frequency. However, the actual resonance circuit becomes a shape like the mountain which made a resonant frequency a top. When the Q of the resonance circuit is high, the voltage at the resonant frequency becomes high and the resonant characteristic becomes good.
  • Detection circuit
Rectifying the output of the high-frequency amplifier directly with Germanium diode and it makes the DC voltage from high frequency voltage. This circuit can get twice voltage compared with usual rectification circuit. Because the charging takes time when the value of the capacitor (C6) to use for input is big, the change of the output voltage becomes late. I decided a value of this capacitor by the cut & try.
  • Relay drive circuit

RB5 and RB7 port of PIC make do the operation of the relays. It is converting the output voltage of PIC with the transistor. It makes LED light up to find the operation of the relay. It puts a resistor in the LED in series and it is limiting the electric current which flows through the LED to about 10 mA.
It is putting a diode in parallel with the relay. When the drive electric current of the relay passes away, the back electromotive force occurs to the coil of the relay. A transistor is prevented from the high voltage by passing the electric current of the back electromotive force to this diode. Because the relay to be using this time is small, there are few gravities that the transistor breaks even if it doesn't put a diode. I put it for the safety.
  • Time Chart for Receiver

 First, the receiver confirms that the electric wave exists. A 5-millisecond timer is set when an electric wave is received and the input signal becomes ON. The signal which is sent from the transmitter is a 10-millisecond interval. To detect ON/OFF in the position of the stable state, it is shifting for 5 milliseconds  from the standing-up of the signal.




RF Remote Controller (Transmitter)


The control code does the electric wave of the transmitter to the control of ON/OFF. At design first, I examin the adoption of the frequency modulation (FM) method by the control code. However, I adopted an electric wave ON/OFF method because the circuit was simple.
A control code is controlled by the software of PIC. With this, the control code can be easily changed. There are two purposes in this control code. The 1st is to secure security. The FM electric wave which was modulated at the single frequency can be made with the other equipment, being easy. At the circuit this time, a control code with byte(8bits) is used. To recognize a control code, to detect a specific signal in front of it is needed. Also, it makes not recognize a normally control code when not detecting a normally signal more than one time. The 2nd is to change the kind of the control. The kind to control are designated by the contents of the byte. At the circuit this time, only two kinds of codes are used. More kinds can be controlled if changing the input circuit of the control.
  • Power switch and control code selection circuit



The operation of the circuit of the transmitter only when pushing a control switch. When a control switch isn't pushed, it makes all circuits stop. When a control switch isn't pushed, it makes all circuits stop to suppress the consumption of the battery cell. The control switch combines the selector switch of the control code and the power switch. A control switch is connected with the side of the positive of the cell and a power is supplied to the circuit even if it pushes any switch. Control code selection malfunction is prevented by the diodes.
  •  Time Chart for Transmitter
I will show the time chart of the control code in the figure above. This composition is the composition which I contrived and is not standard composition. The switching time of each bit is 10 milliseconds. The signal to transmit from the transmitter is composed of three blocks. In the program, three blocks are managed by transmission status (TX_STATUS). ST0, ST1, ST2 show transmission status with the figure above. A bit in each block is managed by sub status (TX_SUBSTATUS). The figure which is written under the signal in the figure above shows sub status.

Stepper Motor controller

On this page, I will explain about the operation principle of stepper motor. There are many kind of stepper motors. Unipolar type, Bipolar type, Single-phase type, Multi-phase type... Single-phase stepper motor is often used for quartz watch. On this page, I will explain the operation principle of the 2-phase unipolar PM type stepper motor.In the PM type stepper motor, a permanent magnet is used for rotor and coils are put on stator. The stepper motor model which has 4-poles is shown in the figure on the left. In case of this motor, step angle of the rotor is 90 degrees.

Thursday, September 30, 2010

Count-down timer

On this page, I will introduce the Count-down timer with PIC16F84A. I am putting a count-down timer by the hardware on Electronic Circuits Application Garage. The function of the timer is the same as it approximately. (The stop switch is added) Because this unit was made compactly, the wiring of the back becomes quite complicated.
This is PIC16F84A. In case of PIC16F84A, it is possible to use a clock frequency upto 20 MHz. The circuit this time, I am using 10-MHz resonator.

This is the IC which decodes the binary code of 3 bits. 8 conditions can be expressed by 3 bits. 74HC138 outputs only one an L level out of 8 by the 3-bit input condition.

Download Source code
Download Hex File 
  
This flasher moves, while lighting of LED drags on. The brightness of LED is four kinds, bright, less bright, almost dim and dim. These lighting states are moved with time. Control of brightness is performed in lighting time of LED like PWM(Pulse Width Modulation). There is no PWM function in PIC16F84A. Therefore, I divided lighting of LED into four cycles, and controlled them. It is "Bright" when all periodic lightings are carried out. 2 cycle lighting state is "Less bright". 1 cycle lighting state is "almost dim". It is "Dim" in OFF a total cycle. At first, I was going to prepare much more cycles. However, because PIC16F84A carried only 1K-word program memory, I made into four cycles.
   The timer function of PIC is used for the processing which moves the lighting position of LED. When man looks at the lighting situation of LED, it is not meaningful if too quick. In this circuit, it is the purpose to show so that lighting of LED may drag on. In order to lengthen time of a timer, 4MHz resonator is used. The period with 4MHz frequency is 1/4,000,000 = 0.25 microseconds. Because 4 periods (the clock) are necessary to the count of the timer, a count is downed with the timer every microsecond. Because a prescaler of 1/256 is used for the input of timer, actually, a count is downed with every 256 microseconds. When making the initial value of the timer as 195, the time-out becomes 256µsec/count x 195count = 49920µsec = 50msec.
 

Wednesday, September 29, 2010

Reading from the I/O ports

The program will only move onto ‘Carry on here’ only if bit 0 on PortA is set to a 1. Let us now write a program which will flash an LED at one speed, but if a switch is closed it will flash the LED twice as slow.  You can probably work this program out for yourself, but I have included the listing anyway.  You could try and write the whole program, just to see if you have grasped the concepts.  We are using the same circuit as before, with the addition of a switch connected RA0 of the PIC and the positive rail of our supply.
;*****Set up the Constants**** 
STATUS          equ       03h                  
TRISA              equ       85h              
PORTA            equ       05h               
COUNT1         equ       08h               
COUNT2         equ       09h                  
;****Set up the port****
bsf                    STATUS,5      
movlw              01h                   
bcf                   STATUS,5        
;****Turn the LED on**** 
Start                 movlw              02h              
movwf              PORTA                                 
;****Check if the switch is closed

BTFSC            PORTA,0            
                                                      
call                   Delay                   
;****Add a delay 
call                   Delay 
;****Delay finished, now turn the LED off****
movlw              00h                 
movwf              PORTA     
;****Check if the switch is still closed

BTFSC             PORTA,0   
call                    Delay               
;****Add another delay**** 
call                    Delay 
;****Now go back to the start of the program
goto Start               
;****Here is our Subroutine
Delay
Loop1
decfsz               COUNT1,1    
goto                  Loop1            
decfsz               COUNT2,1    
goto                  Loop1      
return
;****End of the program****
end   
                                                               
What I have done here is to turn the LED on.  I then check to see if the switch is closed.  If it is closed, then I make a call to our delay subroutine.  This gives us the same delay as before, but we are now calling it twice.  The same goes for when the LED is off.  If the switch is not closed, then we have our old on and off times.

delay loops into our program

Let us put these delay loops into our program, and finish off by making it a real program by adding comments:
;*****Set up the Constants**** 
STATUS         equ       03h
TRISA             equ       85h
PORTA           equ       05h
COUNT1        equ       08h
COUNT2        equ       09h
;****Set up the port****  
bsf                   STATUS,5
movlw              00h
movwf              TRISA
bcf                   STATUS,5
;****Turn the LED on**** 
Start               movlw        02h
movwf               PORTA                                                 
;****Start of the delay loop 1**** 
Loop1          decfsz      COUNT1,1
goto              Loop1 
decfsz           COUNT2,1
goto              Loop1   
;****Delay finished, now turn the LED off**** 
 movlw              00h 
movwf              PORTA 
;****Add another delay**** 
Loop2
decfsz             COUNT1,1
goto                 Loop2    
decfsz             COUNT2,1
goto                 Loop2
;****Now go back to the start of the program
goto                 Start
;****End of the program**** 
end                              ;Needed by some compilersand also just in case we miss the goto instruction.
 
 You can compile this program and then program the PIC.  Of course, you will want to try the circuit out to see if it really does work.  Here is a circuit diagram for you to build once you have programmed your PIC.

Delay Loops

There is one slight drawback to our flashing LED program.  Each instruction takes one clock cycle to complete.  If we are using a 4MHz crystal, then each instruction will take 1/4MHz, or 1uS to complete.  As we are using only 5 instructions, the LED will turn on then off in 5uS.  This is far too fast for us to see, and it will appear that the LED is permanently on.  What we need to do is cause a delay between turning the LED on and turning the LED off.
The principle of the delay is that we count down from a previously set number, and when it reaches zero, we stop counting.  The zero value indicates the end of the delay, and we continue on our way through the program.

first we define our constant:  
COUNT          equ       08hNext we need to decrease this COUNT by 1 until it reaches zero.  It just so happens that there is a single instruction that will do this for us, with the aid of a ‘goto’ and a label.  The instruction we will use is:
DECFSZ        COUNT,1
 This instruction says ‘Decrement the register (in this case COUNT) by the number that follows the comma.  If we reach zero, jump two places forward.’  A lot of words, for a single instruction. Let us see it in action first, before we put it into our program.
COUNT          equ 08h
LABEL           decfsz   COUNT,1
                        goto LABEL
                        Carry on here.
 What we have done is first set up our constant COUNT to 255.  The next line puts a label, called LABEL next to our decfsz instruction.  The decfsz COUNT,1 decreases the value of COUNT by 1, and stores the result back into COUNT.  It also checks to see if COUNT has a value of zero.  If it doesn’t, it then causes the program to move to the next line.  Here we have a ‘goto’ statement which sends us back to our decfsz instruction.  If the value of COUNT does equal zero, then the decfsz instruction causes our program to jump two places forward, and goes to where I have said ‘Carry on here’.  So, as you can see, we have caused the program to stay in one place for a predetermined time before carrying on.  This is called a delay loop.  If we need a larger delay, we can follow one loop by another.  The more loops, the longer the delay.  We are going to need at least two, if we want to see the LED flash..

Writing To the Ports

In the last tutorial, we I showed you how to set up the IO port pins on the PIC to be either input or output. In this tutorial, I am going to show you how to send data to the ports. In the next tutorial, we will finish off by flashing an LED on and off which will include a full program listing and a simple circuit diagram so that you can see the PIC doing exactly what we expect it to. Don’t try and compile and program your PIC with the listings here, as they are examples only.

                  First, let us set up Port A bit 2 as an output:
                  bsf 03h,5 ;Go to Bank 1
                  movlw 00h ;Put 00000 into W
                  movwf 85h ;Move 00000 onto TRISA – all pins set to output
                  bcf 03h,5 ;Come back to Bank 0

This should be familiar from the last tutorial. The only difference is that I have set all of the pins on Port A as output, by sending 0h to the tri-state register.
We define a label very simply. We type a name, say START, then type the code:

                  Start movlw 02h ;Write 02h to the W register. In binary this is
                                            ;00010, which puts a ‘1’ on pin 2 while keeping
                                            ;the other pins to ‘0’
                  movwf 05h ;Now move the contents of W (02h) onto the
                                    ;PortA, whose address is 05h
                  movlw 00h ;Write 00h to the W register. This puts a ‘0’ on
                                    ;all pins.
                  movwf 05h ;Now move the contents of W (0h) onto the Port
                                    ;A, whose address is 05h
                  goto Start ;Goto where we say Start
As you can see, we first said the word ‘Start’ right at the beginning of the program.  Then, right at the very end of the program we simply said ‘goto Start’.  The ‘goto’ instruction does exactly what it says.

The most common registers

STATUS
To change from Bank 0 to Bank 1 we tell the STAUS register. We do this by setting bit 5 of the STATUS register to 1. To switch back to Bank 0, we set bit 5 of the STATUS register to 0. The STATUS register is located at address 03h (the ‘h’ means the number is in Hexadecimal).

TRISA and TRISB
These are located at addresses 85h and 86h respectively. To program a pin to be an output or an input, we simply send a 0 or a 1 to the relevant bit in the register. Now, this can either be done in binary, or hex. I personally use both, as the binary does help visualize the port. If you are not conversant with converting from binary to hex and vice versa, then use a scientific calculator.

PORTA and PORTB
To send one of our output pins high, we simply send a ‘1’ to the corresponding bit in our PORTA or PORTB register. The same format follows as for the TRISA and TRISB registers. To read if a pin is high or low on our port pins, we can perform a check to see if the particular corresponding bit is set to high (1) or set to low (0)
 
W
The W register is a general register in which you can put any value that you wish. Once you have assigned a value to W, you can add it to another value, or move it. If you assign another value to W, its contents are overwritten.

Tuesday, September 28, 2010

PIC 16F84A PIN diagram



The given diagram showing the pin-outs of the PIC 16F84. I will go through each pin, explaining what each is used for. Microchip manufacture a series of microcontrollers called PIC. There are many different flavours available, some basic low memory types, going right up through to ones that have Analogue - To- Digital converters and even PWM built in. I am going to concentrate on the 16F84 PIC. Once you have learnt how to program one type of PIC, learning the rest is easy.
RA0 To RA4 : RA is a bidirectional port. That is, it can be configured as an input or an output. The number following RA is the bit number (0 to 4). So, we have one 5-bit directional port where each bit can be configured as Input or Output.
RB0 To RB7 : RB is a second bidirectional port. It behaves in exactly the same way as RA, except there are 8 - bits involved.
VSS And VDD : These are the power supply pins. VDD is the positive supply, and VSS is the negative supply, or 0V. The maximum supply voltage that you can use is 6V, and the minimum is 2V
OSC1/CLK IN And OSC2/CLKOUT : These pins is where we connect an external clock, so that the microcontroller has some kind of timing.
MCLR : This pin is used to erase the memory locations inside the PIC (i.e. when we want to re-program it). In normal use it is connected to the positive supply rail.
INT : This is an input pin which can be monitored. If the pin goes high, we can cause the program to restart, stop or any other single function we desire. We won't be using this one much.
T0CK1 : This is another clock input, which operates an internal timer. It operates in isolation to the main clock. Again, we won't be using this one much either.

Multi PIC Programmer

"Multi PIC Programmer 5 Ver.2" is a PIC programmer, which can program to 8-pin to 40-pin devices using single ZIF socket. I built "Multi PIC programmer 5 Ver.1", in order to enable it to program 40-pin devices like PIC16F877 with a ZIF socket. Lately I improved this PIC programmer. The main improvements are having made it suit "VPP before VDD" and changed wiring of a ZIF socket for accepting devices with LVP (Low Voltage Programming) mode.
Before you build this "PIC programmer", I recommend checking to see if there is enough output voltage at the serial port your personal computer. If TxD, DTR, and RTS do not have more than +7.5V(or -7.5V), this programmer will not work well, especially, with the latest laptop computers that using low power RS232 interface ICs.
Other important matters are as:
1. The GND line of a serial port forms relative VDD on a PIC programmer. All the GND symbols in a circuit diagram are a PIC programmer's GND. Never connect them with GND line of a serial port.
2. This PIC programmer changes VPP in accordance with the device selected (8-18pin) or (28-40pin) with one sliding switch. So, if the insertion position of a device and slide switch is not set correctly, your PIC may be damaged by over voltage.
3. This PIC programmer does not support all PIC MCUs. (PIC16C5x is not programmable with this programmer. By using an adapter, the 20 pin PIC 16C770/771 can be programmed.
4. I did not try all PICs since and I do not have all them. The PICs, which I successfully programmed and verified, are PIC12F629, PIC12F675, PIC16F627, PIC16F628, PIC16F630, PIC16F676, PIC16F818, PIC16F819, PIC16F84A, PIC16F873, PIC16F877A, PIC18F2320, PIC18F452.
5. The programming software used is IC-Prog of Bonny Gijzen.
6. "Hardware settings" of IC-Prog are the same as the JDM programmer.

Monday, September 27, 2010

PIC Programming through WinPic

WinPic now supports a large variety of PICs with different programming algorithms. Programmable devices are listed on the Features page.
Note that most programming adapters supported by WinPic do not meet Microchip's requirements for a "production grade" programmer. If you think you need a production grade programmer (which can verify the PIC at different voltages), look below.
WinPic lets you ...
  • program a HEX-file into a PIC microcontroller
  • read the contents of a PIC and save it as a HEX file
  • read and modify the configuration word(s) of the PIC
Keep in mind that this program is still far from being "professional" software ! Last not least because this program is freeware, the entire risk of its use is with you - read the disclaimer if you haven't yet. 

How generate a Hex file and Burn it into PIC??

Now that you know how to write code, you need to know how to get your code into the PIC. The process of programming a PIC is often referred to as “burning”. To burn code into the PIC, we will be using the windows version of MPLAB.

In the MPLAB program, all the information about your program is stored in a project files. Project files contain information about the program, the device you’re using, one or more assembly language codes and compiled hex files. The process of burning a PIC contains three major steps. The first is to write the code in assembly language. Once the code is written, it must be compiled into a hex file for programming into the device. After successfully compiling the code, the final step is to program the device.

Begin by opening up the program MPLAB.

Create a new project by going to Project>New Project. In the new project dialogue box select a directory to place the project in and give the new project a name. When you’ve finished, click “Ok”.

You will now see the Edit Project dialogue box. Select the appropriate device, and set the development mode to MPLAB-SIM Simulator. In the project files window click the file that has the name of your file with a .hex extension. Click on the Node Properties button and in the window that appears, click “Ok”. This sets default node properties and allows you to add nodes later.

Exit the Edit Properties box by clicking “Ok”.

Now you may create assembly code. Got to File>New. A blank text editor box should appear. Enter your assembly code into this window. When you are done, save the code (File>Save, give the code a name and click “Ok”).

You must now assign the source code you just made to the project. Go to Project>Edit Project. In the Edit Project dialogue box, click the Add Node button. Browse to find the assembly code you just found. Select it and click “Ok”. The file name for the assembly code should now appear in the window. Click “Ok” to close the Edit Project box.

Now that the source code is associated with the project, its time to compile the code. Go to Project>Make Project. If the compile is successful, you will see the Build Results window appear with the message “Build completed successfully”. If there were errors they will be listed in the window. After compiling successfully, save the project (Project>Save Project).

The final step is to program the device. Select Picstart Plus>Enable Programmer. The Programmer Status dialogue box should appear. At this point, you should have the Picstart Plus device programmer plugged in and th serial cable connected to the serial port on your computer. Place the PIC in the ZIF socket with pin 1 in the top left corner, and lock it in place.