Transcript Chapter 2
CHAPTER 2
PIC 16F887 MICROCONTROLLER
Eng. Hazem W. Marar
PIC 16F887
PORTS
ANALOG PINS
IN MIKROC
ANSEL AND ANSELH
TURN LED ON
void main()
{
TRISB = 0 ;
PORTB = 0xff;
}
// set PORTB as OUTPUT
// turn all LEDs ON
WRITE A CODE TO TOGGLE AN LED
DELAYS
void main( )
{
TRISB = 0 ; // set PORTB as OUTPUT
While(1)
// forever
{
PORTB = 0xff ; // turn all LEDs ON
Delay_ms(500) ; // wait 500 ms
PORTB = 0 ; // turn all LEDs OFF
Delay_ms(500) ; // wait 500 ms
}
}
USE AN INPUT SIGNAL (PUSH BUTTON) TO
CONTROL AN LED
INPUT SIGNAL
void main( )
{
TRISB = 0x01 ; // set 1st bit of PORTB as INPUT
While(1)
// forever
{
if(PORTB.F0 ==1)
{
PORTB.F1 = 1;}
if(PORTB.F0 ==0)
{
PORTB.F1 = 0;}
}
}
USE AN INPUT SIGNAL (PUSH BUTTON) TO
TOGGLE AN LED
TOGGLE
unsigned short LastButton =0;
void main(){
TRISC = 0x01 ;
PORTC=0x00;
while(1)
{
if(PORTC.F0 ==1 && LastButton ==0)
{
PORTC.F1=~PORTC.F1;
LastButton=1;}
else
{
LastButton = PORTC.F0;
}}}
BOUNCING EFFECT
unsigned short current;
unsigned short debounce ( )
{
current = PORTB.F0;
if( LastButton != current)
{ delay_ms(5);
current = PORTB.F0;}
return current;
}
USE PUSH BUTTONS TO CHANGE FLASHING
DELAYS
USE A SINGLE PUSH BUTTON TO CHANGE
FLASHING DELAYS
MANAGING INDIVIDUAL BITS
USE AN INPUT SIGNAL (PUSH BUTTON) TO
CONTROL A LED MATRIX
USE PUSH BUTTONS TO CONTROL
MULTIPLE LED MATRICES
LAB EXERCISE
Chapter 2