0% found this document useful (0 votes)
132 views

5.1 Pic C Getting Started

The document discusses C programming basics for PIC microcontrollers. It covers essential components like header files, main functions, comments, output functions, variables, looping, and decision making. Examples are provided to demonstrate how to use various functions like output, input, and set pins high or low in code and flowcharts. Key aspects covered include using ANSI C syntax, declaring and assigning variables, writing conditional statements, and drawing equivalent assembly instructions and flowcharts.

Uploaded by

Hamzah Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

5.1 Pic C Getting Started

The document discusses C programming basics for PIC microcontrollers. It covers essential components like header files, main functions, comments, output functions, variables, looping, and decision making. Examples are provided to demonstrate how to use various functions like output, input, and set pins high or low in code and flowcharts. Key aspects covered include using ANSI C syntax, declaring and assigning variables, writing conditional statements, and drawing equivalent assembly instructions and flowcharts.

Uploaded by

Hamzah Mohammed
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 16

prepared by : Maher AL-Omari

PIC C programing
Chapter 2 bates c
prepared by : Maher AL-Omari
The CCS compiler
The CCS compiler uses ANSI standard syntax
and structures.
prepared by : Maher AL-Omari
The essential source code components
#include " 16F877A.h " // MCU header file
void main() // Main block start
{
statements;
}
contains information
about library functions
such as what rgument(s)
the function accepts and
what argument (s) the
function returns or the
location of registers for a
specific PIC
Every program must
have a main function
which can appear only
once.
all code which follows
must be placed within a
pair of braces { }
prepared by : Maher AL-Omari
The essential source code
components-2
#include " 16F877A.h " // MCU header file
void main() // Main block start
{
statements;
}
tells the compiler to
incorporate the
header file for the PIC
Comments can be enclosed
between slash/star (/*...*/) control
characters or can follow a double
slash (//), in which case the
comment is terminated with a line
return.
All statements are
terminated with a
semicolon.
prepared by : Maher AL-Omari
output_x (value)
OUTPUT_B(0xf0);
OUTPUT_C(data);
Examples:
Nothing Requires
Output an entire byte to a port. Function:
value :is a 8 bit int, or 0-255 Parameters:
A,B,C,D,E
MOVLW 0xf0
MOVWF PORTB
prepared by : Maher AL-Omari
output_x ()
example
#include " 16F877A.h " // MCU header file
void main() // Main block start
{
output_D(255); // Switch on outputs
}
MOVLW 0xff
BCF 0x3, 0x5
MOVWF 0x6
SLEEP
prepared by : Maher AL-Omari
PIC16 C Program Basics_ Variables
a label attached to the memory location where the
variable value is stored.
automatically assigned to the next available location .
name and type must be declared at the start of the
program block,
values are in decimal by default;
Only alphanumeric characters and underscore, can be
used.
key words in C, must not be used as variable names.
prepared by : Maher AL-Omari
Variables example
#include " 16F877A.h "
void main()
{
int x;
x = 99;
output_D(x) ;
}
Variable type: int (0-255)
Variable name: x
Assign a value to variable
x
Default is decimal
// Display the value in
binary
prepared by : Maher AL-Omari
PIC16 C Program Basics_ looping
// Program function: Outputs variable count
------------------------------------------------------------------
#include " 16F877A.h "
void main()
{ int x; // Declare variable
while(1) // Loop endlessly
{ output_D(x); // Display value
x ++ ; // Increment value
}
}
The block is
executed if the
value, or result of
the expression, in
the parentheses is
not zero.
Write the assembly equivalent
instructions
Loop:
Movf x,0
Movwf portd
Incf x
Goto loop
prepared by : Maher AL-Omari
value = input (pin)
Function: This function returns the state of the
indicated pin.
Returns:
0 (or FALSE) if the pin is low,
1 (or TRUE) if the pin is high
Requires: Pin constants are defined in the
16F877A.h file
#define PIN_A0 40
#define PIN_A1 41
#define PIN_A2 42
#define PIN_A3 43
#define PIN_A4 44
#define PIN_A5 45
#define PIN_B0 48
#define PIN_B1 49
#define PIN_B2 50
#define PIN_B3 51
#define PIN_B4 52
#define PIN_B5 53
#define PIN_B6 54
#define PIN_B7 55
prepared by : Maher AL-Omari
input (pin): examples
Example1:
while ( !input(PIN_B1) );
Example2:
while ( !input(PIN_A3) )
{ output_d(128); }
waits for B1 to go high
Note the ;
Keep sending 128 to
PORTD while RA3=0
Note NO ;
Draw the flowchart
RA3=0?
PORTD=128
Y
N
prepared by : Maher AL-Omari
output_high (pin)

Pin constants are defined in the 16F877A.h file
Requires
Sets a given pin to the high state. Function:
Example:
while (input(PIN_A3) )
{output_high(PIN_A0);}
Keep setting RA0 while
RA3=1
RA3=1?
RA0=1
Y
N
Draw the flowchart
prepared by : Maher AL-Omari
PIC16 C Program Basics_ Decision Making
void main()
{
int x; // Declare variable
output_D(0); // Clear all outputs
while(1) // Loop always
{
x = input(PIN_C0); // Get input state
if(x = = 1)output_high(PIN_D0);
}
}
if(input(PIN_C0))output_high(PIN_D0);
RC0=1?
RD0=1
N
PORTD=00
Read RC0 to X
Draw the flowchart
Re-write the statement
prepared by : Maher AL-Omari
output_LOW(pin)

Pin constants are defined in the 16F877A.h file
Requires
Sets a given pin to the low state. Function:
Example:
while (!input(PIN_C3) )
{output_low(PIN_D0);}
Keep RD0 low while
RC3=0
RC3=0?
RD0=0
Y
N
Draw the flowchart
prepared by : Maher AL-Omari
output_bit (pin, value)
Function: Outputs the specified value
(0 or1) to the specified I/O pin.
Requires: Pin constants are defined in the
16F877A.h file
prepared by : Maher AL-Omari
output_bit (pin, value) examples
output_bit( PIN_B0, 0);
output_bit( PIN_D0,input( PIN_B1 ) );
output_low(pin_B0);
Write statement /statements that sends
the data read on RB1 to RD0
Re-write the statement
int x;
x= input(pin_b1);
output_bit(pin_d0,x);

You might also like