Wednesday, March 17, 2010

Write Program


Secondly, we write the program in the PIC C Compiler. Here, we use C programming type of coding.

#include <16F877A.h> //specify the MCU
#fuses HS,NOLVP,NOWDT //high speed, no low voltage programming, no watchdog timer
#use delay (clock=20000000) //set the clock to 20MHz

The first three lines of this program define the basic hardware environment. The chip being used is the PIC16F877A, running at 20MHz.
The “output_low” turns the LED on because the other end of the LED is +5V. This is done because the chip can tolerate more current when a pin is low than when it is high.
#define fwdOn output_high (pin_b1);
#define fwdOff output_low (pin_b1);
#define revOn output_high (pin_b2);
#define revOff output_low (pin_b2);
#define leftOn output_high (pin_b3);
#define leftOff output_low (pin_b3);
#define rightOn output_high (pin_b4);
#define rightOff output_low (pin_b4);
The #define is used to enhance readability the code that assigned at the pin that we use in the program. For example, #define fwdOn output_high (pin_b1) is to recall the code that we write on the main program. So, once we simulate the program, then the output at pin_b1 will be activated. In the main program, we just write it as fwdOn. The program below is to set up for ‘spin’ move.

void spin( )
{
fwdOn
delay_ms (3000); //delay for 3s
fwdOff
delay_ms (1000); //delay for 1s
revOn
delay_ms (2000); //delay for 2s
revOff
delay_ms (100);
fwdOn
leftOn
delay_ms (5000); //delay for 5s
fwdOff
leftOff

delay_ms (1000); //delay for 1s
revOn
delay_ms (1000);
The program above is for ‘spin’ movement. The car will go forward for 5s then stop for 1s. Then it will reverse for 2s and we stop the reverse for 100 ms before the combining of forward and left button to perform spinning move in 5s.

Ex :

fwdOn
delay_ms (3000); //delay for 3s

The coding above means that the car will move forward in delay time of 3 seconds. The first row is to write the movement of the car (fwdOn) while the second row is to program the delay time of that movement, The number in the columns is the delay time written in milliseconds. [delay_ms (3000) ]

No comments:

Post a Comment