Upload
phamnga
View
222
Download
11
Embed Size (px)
Citation preview
MicroC Programming
Outlines of part A:
1) MicroC Basics
2) Simple Examples
3) Lab#8:Introduction to MicroC programming
1) MicroC Basics
It is easy to write your code in C Language, but MikroC
Language is slower than the Assembly language because it
needs two steps to be transformed to machine language.
1) MicroC Basics
To clear TMR0 interrupt flag bin?
INTCON.T0IF=0;
To make a delay of 2 seconds?
DELAY_MS(2000)
Set RB5 if RA2 is set?
IF(PORTA.F2==1) PORTB.F5=1;
Int k = 200; delay_ms(K); Not correct
const int k = 200; delay_ms(K); correct
Int k = 200; Vdelay_ms(K);
portb=0x03^0x02; 0011 (XOR) 0010 =0001
Notes:
1) MicroC Basics
In MikroC language you don't need to be in bank0, or in bank1, you
can deal directly with Special Function Registers (SFR).
To make inverting use ~.
To access an individual bit, use the following:
//If RB0 is set ,set RA0
if(portb.f0==1) porta.f0=1;
Notes:
1) MicroC Basics
Selection statements:
There are two types of selection statements in C:
1. if
2. Switch
1) MicroC Basics
Open -Drain RA4 on PIC16F84ASome PICs like PIC16F84A have RA4 with open drain output instead of CMOS output!
RA4 is different; it is configured as an open drain MOSFET. When set to low, it
performs identically with the other pin architectures.
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.
That means it cannot source current and it will be high impedance
when assigned logic '1' to it.
You cannot set this pin as an output pin by using 'TRISA=0x00'.
The solution is attaching a pull up resistor (10K) to the RA4 pin in
order to use this pin as an output pin.
1) MicroC Basics
constants:
Example: const float pi=3.14;
The constants stored in ROM.
The value of the constant can’t change if for example say pi++.
General formula:
const constant type constant name= constant value
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#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)
{
Here we write the code when the switch is
press.
}
If(porta.f0==1)
{
Here we write the code when the switch is
press.
}
2) Simple Examples
Example#7 :Write a code to make program
consists of 2 switches and 3
LED’s.
When you press to switch#1 the
LED begin to turn ON from LED1
then LED2 and finally LED3.
When you press to switch#2 the
LED begin to turn OFF from LED3
then LED2 and finally LED1.
2) Simple Examples
Example#8:You have one switch and 4 LED’s.
If you press the switch the first
time all LED’s become ON.
If you press the switch the second
time, the LED’s begin to light from
right to left.
If you press the switch the third
time, the LED’s begin to light from
left to right.
2) Simple Examples
Example#9 :
You have switch in A0 and
LED in B0.
When you press to switch
the LED will light, but when
you press the switch there
is nothing unless you
released your finger from
switch.
2) Simple Examples
Example#12:Make a multiplication table of 3 in binary, and display the result in portB.
2) Simple Examples
Example#14:Make a counter by using 7-
segment .
When you press to switch#1 the
counter will increment, but when
you press to switch#2 the counter
will decrement.
2) Simple Examples
Example#14:You have a switch. At every press to switch make the LED light as
the following shape:
2) Simple Examples
Example#15:Write a code when you press to switch in A1 you run the
buzzer, and increase the counter in portB.
2) Simple Examples
Example#16 sol:
Learn:
Sound library consist of:
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#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.
PIC16F877A:It consists from 40 pin’s, we
can use 33 pin’s as input or
output.
You can connect source#1
only, or source#2 only, or to
connect 32 with Vcc and 12 with
ground, or 31 with ground and
11 with Vcc.
You must begin your program
with adcon1=0x06; or
adcon1=0x07; in case you don’t
use analog equipments, to use
port A and port E normally.
Rest:
PIC
16F877A
+5V
Port A
Port E
Source#1
Crystal
4Mhz
Port C
Port D Port D
Port C
Port D
Source#2
Port B
Lab #8Introduction to MikroC Language
Design traffic lights to the intersection consists of four directions.(10 marks)
Do this project software
and hardware.
Attach the CD for the
MicroC code and
simulation and send to me
hard copy report.
Only 3 students in every
group.
Deadline: after 5 days
from discussion the lab.
Lab #8Introduction to MikroC Language
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.