C Programming Part1
C Programming Part1
programmin
g
MicroC Programming
Outlines of part A:
1) MicroC Basics
2) Simple Examples
OPERATORS :
1) MicroC Basics
Relational operators:
1) MicroC Basics
Bitwise operators:
1) MicroC Basics
Logical operators:
1) MicroC Basics
Selection statements:
There are two types of selection statements in C:
1. if
2. Switch
1) MicroC Basics
1) MicroC Basics
Do statement
1) MicroC Basics
Functions:
1) MicroC Basics
function Example:
1) MicroC Basics
Open -Drain RA4 on
PIC16F84A
1) MicroC Basics
Open -Drain RA4 on
PIC16F84A
1) MicroC Basics
Open -Drain RA4 on PIC16F84A
Some PICs like PIC16F84A have RA4 with open drain output instead of CMOS output!
However, when set to high, there is no internal connection with VDD and hence it will not
directly source voltage.
If it’s necessary to use RA4 as a sourcing output pin, you can add an external “pull-up”
resistor, typically in the range of 470 ohms–4.7K ohms.
The sourced current then comes from the pull-up resistor. Unlike all other pins that cannot
exceed VDD, RA4’s open drain is rated to 12 volts.
1) MicroC Basics
Open -Drain RA4 on
PIC16F84A
RA4 pin (pin 3 for PIC16F84a) is "Open-Drain" type output pin.
The value of the constant can’t change if for example say pi++.
1) MicroC Basics
Matrices
:
Index#0
Index#1
Index#2
1) MicroC Basics
Multi Dimension
Matrices :
1) MicroC Basics
Multi Dimension
Matrices :
Example:
1) MicroC Basics
#define:
1) MicroC Basics
Button “ Bounce”:
Counter from 0 to 0x0F, increases by one every press on RA4.
1) MicroC Basics
Button “
Bounce”:
1) MicroC Basics
Button “
Bounce”:
Solution
of bounce
by using
a small
delay
2) Simple Examples
Example#1:
Write a code in MicroC to make a LED B0
ON.
2) Simple Examples
Example#1
sol:
.
2) Simple Examples
Example#2:
Write a code in MicroC to make LED’s in B0,B1,B5
and
B7 ON.
2) Simple Examples
Example#2
sol:
Note:
Trisb=0 trisb=0b00000000
≡
portb=0XA3
Portb=0b10100011 ≡
2) Simple Examples
Example#3 :
Make flasher in all pin’s in portB.
2) Simple Examples
Example#3
sol :
or
2) Simple Examples
Example#4 :
Make a party LED’s…
The first shape make led B0 ON then
the next and so on..
The second shape make the
LED’s
begin to light ON from the middle.
The third shape make all LED’s ON
then OFF.
And so on..
2) Simple Examples
Example#4
sol :
2) Simple Examples
Example#5 :
Make all LED’s in PORTB ON and then OFF. Repeat this process 18.
2) Simple Examples
Example#5
sol :
2) Simple Examples
Example#6 :
Make a LED in B0 ON
when you press to switch
in A0, unless that the
LED is OFF.
2) Simple Examples
Example#6 sol:
There are two methods to connect the switches:
Pull-up resistor Pull-
down resistor
If(porta.f0==0) If(porta.f0==1)
{ {
Here we write the code when the switch is Here we write the code when the switch is
press. press.
} }
2) Simple Examples
Example#6
sol:
or
2) Simple Examples
Example#7 :
Write a code to make program
consists of 2 switches and 3
LED’s.
sol:
or
2) Simple Examples
Example#8:
You have one switch and 4 LED’s.
or
2) Simple Examples
Example#14:
Make a counter by using 7-
segment .
Sound_Init
Example: Sound_Init(&PORTC,3);
Sound_Play
Example: Sound_Play(1000,200)
where 1000 is the frequency in Hz and 200 is the time in
msec
2) Simple Examples
Example#16
sol:
2) Simple Examples
Example#17:
Write a C code program to make a flashing wave on PORT B, and
then control the direction, and the speed of the flashing wave,
through PORT A.
2) Simple Examples
Example#17
sol:
2) Simple Examples
not
e:
Rest
+5V
Port A Port B
:
PIC16F877A
:
PIC16F877
Port E
It consists from 40 pin’s, we Source#2
A
4Mhz
11 with Vcc.
Hints:
we want to consider the first road is green and the other roads are red for 2
minutes.
After that turn off the green signal, and still the red signal as previous step.
Turn on the yellow signal for 5 sec in first road, then turn of the yellow
signal.
Make the same process for the second road and so on.
Lab #8
Introduction to MikroC Language