lab#3 mikroc - site.iugaza.edu.pssite.iugaza.edu.ps/mhennawi/files/2017/09/lab3.pdf · notes about...

16
Eng.Mohammed Elasmer miKroC 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. How to write MicroC code: How to make infinite loop in MicroC : To invert the bin RB3 of PORTB? PORTB.F3=~ PORTB.F3 LAB#3

Upload: vungoc

Post on 13-Dec-2018

285 views

Category:

Documents


15 download

TRANSCRIPT

Eng.Mohammed Elasmer

miKroC

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.

How to write MicroC code:

How to make infinite loop in MicroC :

To invert the bin RB3 of PORTB? PORTB.F3=~ PORTB.F3

LAB#3

Eng.Mohammed Elasmer

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

Eng.Mohammed Elasmer

OPERATORS

Eng.Mohammed Elasmer

Eng.Mohammed Elasmer

Eng.Mohammed Elasmer

Int i,j;

void main(){

trisb=0;

i=0b00001111;

j=0b11110000;

portb=i| j;

}

Int i,j;

void main(){

trisb=0;

i=0b00001111;

j=0b11111111;

portb=i & j;

}

Eng.Mohammed Elasmer

Rotate Operator:

void main( ){

trisa=0;

porta=4;

}

void main( ){

trisa=0;

porta=4;

porta=porta<<1;

}

void main( ){

trisa=0;

porta=3;

porta=porta<<2;

}

void main( ){

trisa=0;

porta=0x0c;

porta=porta>>2;

}

Eng.Mohammed Elasmer

Logical negation (!) and complement (~) operant's:

void main() {

trisb=0;

portb=0;

portb.f0=0;

portb=!portb;

}

void main() {

trisb=0;

portb=0;

portb.f0=0;

portb=~portb;

}

void main() {

trisb=0;

portb=0b11110000;

portb=~portb;

}

int i;

void main()

{

i= 0x06;

portb=i^0x01;

}

0110

0001

XORـــــــــــ

0111

Eng.Mohammed Elasmer

int i;

void main()

{

porta= 0x06;

portb=porta^0x01;

}

int i;

void main()

{

porta= 0x06;

portb=porta^0xff

;

}

int i;

void main()

{

porta= 0x06;

i=porta^0xff;

portb=i;

}

Wrong answer!!!!

int i;

void main()

{

i= 0x06;

portb=i^0x01;

}

0110

1111

XORـــــــــــ

1001

Eng.Mohammed Elasmer

There are two types of selection statements in C: if and

switch

Eng.Mohammed Elasmer

Eng.Mohammed Elasmer

Eng.Mohammed Elasmer

Notes about Programming in MikroC Language

• 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 an infinite loop, do as the following

While(1){

}

Since this sentence is always true.

• To a delay you can use the following built in functions:

Delay_ms(const int) delay_ms(1000) 1 sec

Vdelay_ms(int) vdelay_ms(1000) 1 sec

• To make inverting use ~.

• To access an individual bit, use the following: //If RB0 is set ,set RA

if(portb.f0==1) porta.f0=1;

//clear TMR0 flag Intcon.tmr0f=0;

Labels in Micro C

Goto 'label name'

'label name' :

Eng.Mohammed Elasmer

Note:

void main()

{

trisb=0;

while(1){

}

}

void main()

{

trisb=1;

while(1){

}

}

void main()

{

trisb=255; //0xff

while(1){

}

}

Eng.Mohammed Elasmer

Example::

int separate(int x){

bit3=x/100;

bit2=(x/10)%10;

bit1=(x%100)%10;

return bit1,bit2,bit3;

}

Eng.Mohammed Elasmer

int bit1,bit2,bit3;

int separate(int x){

bit3=x/100;

bit2=(x/10)%10;

bit1=(x%100)%10;

return bit1,bit2,bit3;

}

void main()

{ separate (951 );

porta=bit1;

portb=bit2;

eedata=bit3;

}