220 likes | 530 Views
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 ; // set PORTB as OUTPUT PORTB = 0xff; // turn all LEDs ON }. Write a code to toggle an LED. Delays.
E N D
Chapter 2PIC 16F887 Microcontroller Eng. Hazem W. Marar
Turn LED on • void main(){TRISB = 0 ; // set PORTB as OUTPUT PORTB = 0xff; // turn all LEDs ON}
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}}
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;} }}
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; }
Lab exercise Chapter 2