Professional Documents
Culture Documents
CMOS
Memories
RAM ROM’s
The first set of codes defines the PICmicro device. This is most
commonly done in C by
#include "16F84.h"
This statement will make reference to the header file of the 16F84
device for register address and other information. It must be noted
that the header file must be contained within the same folder as the
program codes.
PROGRAMMING A PIC USING C
One of the first tasks in the main function is to initialize and define the
ports. This is done by the following codes.
Motor = 1 ;
The following code will turn the pin low.
Motor = 0;
PROGRAMMING A PIC USING C
The rest of the operations such as generation of pulses and duty cycles
follow this. Implementing an output response is the only way of testing
inputs. Hence, there can be conditional statements to check if the bit is
high or low.
The following code will check a pin assigned the name Trigger.
If Trigger is high, motor will turn on.
If (Trigger = = l)
{
motor = 1;
}
C programs are compiled by CC5X and converted into assembly.
These then get transferred into hex files. Hex files are downloaded to
PIC.
For more information (e.g. detailed descriptions and examples of each assembly statement), refer to
the PIC16F8X data sheet available on Microchip's website (www. microchip. com).
PIC CONTROLLER -- EXAMPLE: SECURITY SYSTEM CIRCUIT
THE CC5X SOURCE CODE – EXAMPLE: SECURITY SYSTEM CIRCUIT
#include "16F84.H“ //Header of the PIC used
bit DOOR_WINDOW @ PORTB . 0 ; //assign bit 0 of Port B to DOOR_WINDOW
bit MOTION @ PORTB . 1 ;
bit C @ PORTB.2;
bit D @ PORTB.3;
bit ALARM @ PORTA. 0 ;
void main()
{
PORTA = Ob.0000.0000 ; //Initialize all Port A bits (Low Nibble)
TRISA = 0b.0000.0000 ; //Sets Port A bits as outputs (0 - output)
PORTB = 0b.0000.0000 ; //Initialize all Port B bits (Low Nibble)
TRISB = 0b.1111.1111 ; //Sets Port B bits as inputs (1 - input)
while (1) //Keeps alarm always running
if(C = = 0&&D = = l && DOOR_WINDOW = = 1) //Operating State 1
{
ALARM = 1 ;
if (C = = l && D = = 0)
{
i f (DOOR_WINDOW = = 1 | |MOTION = = l ) // Operating State 2
{
ALARM = 1 ;
}
}
}
}
PIC CONTROLLER -- EXAMPLE: LED
Design a PIC-based circuit and write a CC5X code for switching two
LEDs alternately.
THE CC5X SOURCE CODE – EXAMPLE: LED
#include "16F84.H" //Header of the PIC used
bit switch @ PORTB.O;
bit LED1 @ PORTA. 1;
bit LED2 @ PORTA.2;
void main()
{
PORTA=Ob.0000.0000 ; //Initialize all Port A bits (Low Nibble)
TRISA= 0b.0000.0000 ; //Sets Port A bits as outputs (0 - output)
PORTB=0b.0000.0000 ; //Initialize all Port B bits (Low Nibble)
TRISB=0b.1111.1111 ; //Sets Port B bits as inputs (1 - input)
int counter = 0;
while(1)
{
if (switch = = l)
{
LED1=1;
LED2 = 0;
counter++;
}
if (counter = = 10)
{
LED2 = 1;
}
else
LED1 = 0 ;
LED2 = 0;
}
}
~END~