Sie sind auf Seite 1von 12

PIC C programing

Chapter 2 bates c

3/16/2014

prepared by :Maher Al-omari

1.c

Continue, Break, and Goto


main()
{
int outbyte;
again: outbyte = 0;
while(1)
{
output_C(outbyte);
delay_ms(10);
outbyte ++ ;
if (!input(PIN_D0)) continue;
if (!input(PIN_D1)) break;
delay_ms(100);
if (outbyte == 100) goto again; }
output_c(255); }
3/16/2014

prepared by :Maher Al-omari

Quiz
main()
RD1,RD0
{
int outbyte;
00
again: outbyte = 0;
01
while(1)
{
10
output_C(outbyte);
delay_ms(10);
11
outbyte ++ ;
if (!input(PIN_D0)) continue;
if (!input(PIN_D1)) break;
delay_ms(100);
if (outbyte == 100) goto again; }
output_c(255); }
3/16/2014

prepared by :Maher Al-omari

OUTPUT ON PORTC

UP COUNTER 0-255
10 msec delay
FF & sleep
UP COUNTER 0-255
10 msec delay
UP COUNTER 0-99
110 msec delay

If and If..Else example


void main() {
int outbyte2 =255;
int count=100;
while(1) {
if( input(PIN_d0))
{
output_b(outbyte2 & 0x0f);
delay_ms(100);
}

RD1,RD0

if( input(PIN_d1))
{
output_b(outbyte2 & 0xf0);
delay_ms(100);
}
else
{
output_b(outbyte2);
delay_ms(100);
}
}
}
3/16/2014

prepared by :Maher Al-omari

OUTPUT ON PORTB

00

FF

01

0F & FF will toggle

10

F0 is displayed

11

0F & F0 will toggle

void main()
{
int8 inbits;
while(1)
{
inbits = input_D();
switch(inbits)
{
case 1: output_C(1);
break;
case 2: output_C(3);
break;
case 3: output_C(7);
break;
default:output_C(0);
}}}
3/16/2014

2.c

Switch..Case

prepared by :Maher Al-omari

Switch case example 1

7.c

#include "16F877A.H"
If the switches read 0 then
porb counts from 3 to- 11
#use delay(clock=4000000)
main() {
If the switches read 1 then
porb counts from 6 to- 22
int x;
for(x=3;x<12; x++)
{
switch(input_D())
{
case 0:output_B(X);delay_ms(10); break;
case 1:output_B(2*x);delay_ms(10);break;
}
}
3/16/2014
prepared by :Maher Al-omari
output_B(17); }

Switch case example2


1.
2.
3.
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.

3.c

#include "16F877A.H"
#use delay(clock=4000000)
1.
How many times will the program read
main()
portd? 3
{
2.
What will PORTB =to if PORTD is =127? 0x18
int x;
3.
What will PORTB =to if PORTD is =01? 01
for(x=3;x<=5; ++x)
{
switch(input_d())
{ case 128:output_b(0xF0); break;
case 1:output_b(input_d()); break;
case 2:output_high(PIN_b0); break;
default:output_b(0x18); }
}
}
3/16/2014

prepared by :Maher Al-omari

Basic Function Call


# include " 16F877A.H "
int8 outbyte = 1;
int16 n;
void out()
{
while (outbyte! = 0)
{ output_C(outbyte);
outbyte ++ ;
for(n = 1;n < 500; )
n++;
} }
main()
{ out();
while(1); }
3/16/2014

global variables
Start of function block,no (;)

Start loop, quit when output 0,no (;)


Portc is a binary counter 1-255

Delay so output is visible no (;)

call out()
goto $
prepared by :Maher Al-omari

What is the function of this program?


#include "16F877A.H"
#use delay(clock=4000000)
int cnt;
void cycle_forward ()
{ output_b (cnt);
delay_ms (10);
cnt++;
}
void cycle_backward ()
{ output_b (cnt);
delay_ms (10);
cnt--;
}
3/16/2014

counts up on portb if RD0=0


It counts down if RD0=1
The count starts from 0

void main() {
cnt=0;
while (TRUE)
{
if (!input_state(pin_D0))
cycle_forward ();
else
cycle_backward ();
}
}

prepared by :Maher Al-omari

4.c

Using global variables to Pass Parameters to a


Function
8.c
int16 count;
void out() {
int8
outbyte = 1;
n;
int16
While (outbyte! = 200)
{ output_C(outbyte);
outbyte ++ ;
for(n = 1;n < count; n++;)}
}

Portc is a binary counter 1-199


With a delay between counts
Controlled by the variable count
Set in the main block

main()
//// Main block
{
count = 2000; // Set variable value
out();
// Call function
while(1);
// Wait for reset
}

3/16/2014

prepared by :Maher Al-omari

Using Local Variables in Functions


int8 outbyte = 1;
int16 count;
out(int16 t)
{
int16 n;
while (input(PIN_D0))
{ outbyte ++ ;
for(n = 1;n < t;n++ );
}
}

3/16/2014

5.c

Local variable ,receives its value


from the calling function
Local variable
Pass count value to function out()

main()
{
count = 50000;
out(count); //
output_C(outbyte); // returned value
while(1);
}
prepared by :Maher Al-omari

What is the function of this program?


6.c
#include "16F877A.H"
#use delay(clock=4000000)
int count;
Int outbyte ;
main() {
out( t)
while(1)
{ outbyte = 5;
{
count =input_d();
outbyte=outbyte*t ;
out(count);
}

output_b(outbyte);

}
}
The output on portb =5*count
3/16/2014

prepared by :Maher Al-omari

Das könnte Ihnen auch gefallen