Presentation is loading. Please wait.

Presentation is loading. Please wait.

PIC16F887.

Similar presentations


Presentation on theme: "PIC16F887."— Presentation transcript:

1 PIC16F887

2 Main Features Only 35 instructions to learn: Operating speed:
All single-cycle instructions except branches Operating speed: DC – 20 MHz oscillator/clock input DC – 200 ns instruction cycle Interrupt capability 8-level deep hardware stack Direct, Indirect and Relative Addressing modes

3 Main Features Precision Internal Oscillator: Power savings
Factory calibrated to ±1% Software selectable frequency range of 8 MHz to 31 kHz Software tunable Two-Speed Start-up mode Crystal fail detect for critical applications Clock mode switching during operation for Power savings Power-Saving Sleep mode Wide operating voltage range (2.0V-5.5V) Industrial and Extended Temperature range Power-on Reset (POR) Power-up Timer (PWRT) and Oscillator Start-up Timer (OST) • Brown-out Reset (BOR) with software control Option

4 Main Features • Enhanced low-current Watchdog Timer (WDT) with on-chip oscillator (software selectable nominal 268 seconds with full prescaler) with software enable • Multiplexed Master Clear with pull-up/input pin • Programmable code protection • High Endurance Flash/EEPROM cell: 100,000 write Flash endurance 1,000,000 write EEPROM endurance Flash/Data EEPROM retention: > 40 years Program memory Read/Write during run time In-Circuit Debugger (on board)

5 Main Features Low-Power Features: Standby Current:
50 2.0V, typical • Operating Current: 11 μ 32 kHz, 2.0V, typical 220 μ 4 MHz, 2.0V, typical • Watchdog Timer Current: 1 μ 2.0V, typical

6 Pinout and Family Differences

7

8

9 Oscillator

10 FOSC REGISTER

11 Parallel I/O (PIO) Ports

12

13 Parallel I/O (PIO) Ports

14 Timer 0

15 OPTION_REG = 0x84;

16 INTCON = 0xA0; TMR0IF_bit = 0;

17 /* * Project name: LED_Blinking (Simple 'Hello World' project) * Copyright: (c) Mikroelektronika, 2009. * Revision History: : - initial release; modified by Slavisa Zlatanovic; * Description: This is a simple 'Hello World' project. It turns on/off LEDs connected to PORTA, PORTB, PORTC and PORTD. * Test configuration: MCU: PIC16F887 Dev.Board: EasyPIC6 Oscillator: HS, MHz Ext. Modules: - SW: mikroC PRO for PIC * NOTES: - Turn ON the PORT LEDs at SW9. */

18 void main() { ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; TRISA = 0x00; // set direction to be output TRISB = 0x00; // set direction to be output TRISC = 0x00; // set direction to be output TRISD = 0x00; // set direction to be output do { PORTA = 0x00; // Turn OFF LEDs on PORTA PORTB = 0x00; // Turn OFF LEDs on PORTB PORTC = 0x00; // Turn OFF LEDs on PORTC PORTD = 0x00; // Turn OFF LEDs on PORTD Delay_ms(1000); // 1 second delay PORTA = 0xFF; // Turn ON LEDs on PORTA PORTB = 0xFF; // Turn ON LEDs on PORTB PORTC = 0xFF; // Turn ON LEDs on PORTC PORTD = 0xFF; // Turn ON LEDs on PORTD } while(1); // Endless loop }

19 /* * Project name: Timer0_Interrupt (Using Timer0 to obtain interrupts) * Copyright: (c) Mikroelektronika, 2009. * Revision History: : - initial release; modified by Slavisa Zlatanovic; * Description: This code demonstrates how to use Timer0 and it's interrupt. Program toggles LEDs on PORTB. * Test configuration: MCU: PIC16F887 Dev.Board: EasyPIC6 Oscillator: HS, MHz Ext. Modules: - SW: mikroC PRO for PIC * NOTES: - Turn on LEDs on PORTB (switch SW9.2). */

20 unsigned cnt; void interrupt() { if (TMR0IF_bit) { cnt++; // Increment counter TMR0IF_bit = 0; // Clear TMR0IF TMR0 = 96; } void main() { OPTION_REG = 0x84; // Assign prescaler to TMR0 ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; TRISB = 0; // PORTB is output PORTB = 0xFF; // Initialize PORTB TMR0 = 96; // Timer0 initial value INTCON = 0xA0; // Enable TMRO interrupt cnt = 0; // Initialize cnt do { if (cnt >= 400) { PORTB = ~PORTB; // Toggle PORTB LEDs cnt = 0; // Reset cnt } while(1);

21 unsigned cnt; void interrupt() { if (TMR0IF_bit) { TMR0IF_bit = 0; // Clear TMR0IF TMR0 = 255; PORTB = ~PORTB; } void main() { OPTION_REG = 0x80; // Assign prescaler to TMR0 ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; TRISB = 0; // PORTB is output PORTB = 0xFF; // Initialize PORTB TMR0 = 255; // Timer0 initial value INTCON = 0xA0; // Enable TMRO interrupt cnt = 0; // Initialize cnt

22 _interrupt: MOVWF R15+0 SWAPF STATUS+0, 0 CLRF STATUS+0 MOVWF ___saveSTATUS+0 MOVF PCLATH+0, 0 MOVWF ___savePCLATH+0 CLRF PCLATH+0 ;Timer0_Interrupt.c,28 :: void interrupt() { ;Timer0_Interrupt.c,29 :: if (TMR0IF_bit) { BTFSS TMR0IF_bit+0, BitPos(TMR0IF_bit+0) GOTO L_interrupt0 ;Timer0_Interrupt.c,31 :: TMR0IF_bit = 0; // Clear TMR0IF BCF TMR0IF_bit+0, BitPos(TMR0IF_bit+0) ;Timer0_Interrupt.c,32 :: TMR0 = 255; MOVLW MOVWF TMR0+0 ;Timer0_Interrupt.c,33 :: PORTB = ~PORTB; COMF PORTB+0, 1 ;Timer0_Interrupt.c,34 :: } L_interrupt0: ;Timer0_Interrupt.c,35 :: } L_end_interrupt: L__interrupt2: MOVF ___savePCLATH+0, 0 MOVWF PCLATH+0 SWAPF ___saveSTATUS+0, 0 MOVWF STATUS+0 SWAPF R15+0, 1 SWAPF R15+0, 0 RETFIE ; end of _interrupt

23

24 CAPTURE/COMPARE/PWM MODULES (CCP1 AND CCP2)
This device contains one Enhanced Capture/Compare/PWM (CCP1) and Capture/Compare/PWM module(CCP2). The CCP1 and CCP2 modules are identical in operation, with the exception of the Enhanced PWM features available on CCP1 only. CCPRx and CCPx refer to CCPR1 or CCPR2 and CCP1 or CCP2, respectively.

25

26

27

28

29

30

31 Pinout and Family Differences

32 /* * Project name: PWM (PWM library Demonstration) * Copyright: (c) MikroElektronika, 2008. * Revision History: initial release; modified by Slavisa Zlatanovic; * Description: This is a simple demonstration of PWM library, which is being used for control of the PIC's CCP module. The module is initialized and started, after which the PWM1 and PWM2 Duty Ratios can be adjusted by means of 4 buttons connected to pins RA0, RA1, RA2 and RA3. The changes can be monitored on the CCP output pins (RC1 and RC2). * Test configuration: MCU: PIC16F887 Dev.Board: EasyPIC6 Oscillator: HS, MHz Ext. Modules: - SW: mikroC PRO for PIC * NOTES: - Pull-down PORTA and connect button jumper (J17) to Vcc. - Turn on LEDs on PORTC SW9.3. */

33 PWM2_Init(5000 seem to override PWM1_Init(5000
unsigned short current_duty, current_duty1; void InitMain() { ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; PORTA = 255; TRISA = 255; // Configure PORTA pins as input PORTB = 0; // Set PORTB to 0 TRISB = 0; // Designate PORTB pins as output PORTC = 0; // Set PORTC to 0 TRISC = 0; // Designate PORTC pins as output PWM1_Init(5000); // Initialize PWM1 module at 5KHz PWM2_Init(5000); // Initialize PWM2 module at 5KHz } PWM2_Init(5000 seem to override PWM1_Init(5000

34 void main() { InitMain(); current_duty = 30; // Initial value for current_duty current_duty1 = 30; // Initial value for current_duty1 PWM1_Start(); // Start PWM1 PWM2_Start(); // Start PWM2 PWM1_Set_Duty(current_duty); // Set current duty for PWM1 PWM2_Set_Duty(current_duty1); // Set current duty for PWM2 while (1) { // Endless loop if (RA0_bit) { // Button on RA0 pressed Delay_ms(40); current_duty++; // Increment current_duty PWM1_Set_Duty(current_duty); } if (RA1_bit) { // Button on RA1 pressed current_duty--; // Decrement current_duty if (RA2_bit) { // Button on RA2 pressed current_duty1++; // Increment current_duty1 PWM2_Set_Duty(current_duty1); if (RA3_bit) { // Button on RA3 pressed current_duty1--; // Decrement current_duty1 if (RA5_bit) { // Button on RA3 pressed PORTC.B0 = ~ PORTC.B0; // current_duty = current_duty; Delay_ms(1000); Delay_ms(5); // Slow down change pace a little

35 /* * Project name: Button (Demonstration of using Button Library) * Copyright: (c) Mikroelektronika, 2009. * Revision History: : - initial release; modified by Slavisa Zlatanovic; * Description: This program demonstrates usage on-board button as PORTB input. On every RB0 one-to-zero transition PORTC is inverted. * Test configuration: MCU: PIC16F887 Dev.Board: EasyPIC6 Oscillator: HS, MHz Ext. Modules: - SW: mikroC PRO for PIC * NOTES: - Turn ON the PORTC LEDs. - Put button jumper (J17) into VCC position and pull-down PORTB. */

36 bit oldstate; // Old state flag
void main() { ANSEL = 0; // Configure AN pins as digital I/O ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; TRISB0_bit = 1; // set RB0 pin as input TRISB = 0b ; PORTB = 0x00; TRISC = 0x00; // Configure PORTC as output PORTC = 0xAA; // Initial PORTC value oldstate = 0; do { if (Button(&PORTB, 0, 1, 1)) { // Detect logical one oldstate = 1; // Update flag } if (oldstate && Button(&PORTB, 0, 1, 0)) { // Detect one-to-zero transition PORTC = ~PORTC; // Invert PORTC oldstate = 0; // Update flag if (RB1_bit == 1) { // Detect one-to-zero transition } while(1); // Endless loop

37 Switch + LED (SWITCH12) void main () { ADPCFG = 0xFFFF;
TRISF=0b000001; PORTF=0x00; while(1) if((PORTF&0b000001)==1) // Switch at PORTF Bit 0 {PORTF.F1=PORTF.F1^1;} // Toggles PORTF Bit 1 }

38 Key DEBOUNCING

39 Key DEBOUNCING – 1 (SWITCH2)
void main () { int i; const int Tenms = 16667; ADPCFG = 0xFFFF; TRISF=0b000001; PORTF=0x00; while(1) i = 0; // Wait 10 ms for Button Up while (i < Tenms) if (1 == PORTF.F0) // Button Down/Start over i = 0; } else // Button Up/Increment Count i = i + 1; } // i = 0; // Wait 10 ms for Button Down if (0 == PORTF.F0 ) // Button Up/Start over else // Button Down/Increment Count PORTF.F1=PORTF.F1^1; // Toggle LED to Turn ON/OFF LED Adapted from 123 PIC Experiments for the EVIL GENIOUS by MYKE PREDKO

40 Key DEBOUNCING – 1 (SWITCH2)
;SWITCH2.c,24 :: while (i < Tenms) $0144 $ L_main_6: $0144 $ ADD W14, #0, W0 $0146 $ MOV [W0], W2 $0148 $4700E2 ADD W14, #2, W1 $014A $ SUB W2, [W1], W0 $014C $3D000D BRA GE L_main_7, L_main_7 ;SWITCH2.c,25 :: if (0 == PORTF.F0) // Button Up/Start over $014E $ MOV PORTF, W0 $0150 $6000E1 AND W0, #1, W1 $0152 $ SUBR W1, #0, W0 $0154 $3A BRA NZ L_main_8, L_main_8 ;SWITCH2.c,26 :: i = 0; $0156 $ MOV #0, W0 $0158 $780F00 MOV W0, [W14] $015A $ GOTO L_main_9 $015E $ L_main_8: ;SWITCH2.c,28 :: i = i + 1; $015E $ MOV #1, W1 $0160 $ ADD W14, #0, W0 $0162 $ ADD W1, [W0], [W0] $0164 $ L_main_9: $0164 $ GOTO L_main_6 $0168 $ L_main_7: ;SWITCH2.c,30 :: PORTF.F1=PORTF.F1^1; // Toggle LED to Turn

41 Capture Mode

42 Compare Mode

43 PWM

44 Period in multiples of 4/fosc
Resolution in multiples of 1/fosc Least significant bits of extended TMR2 Change at oscillator frequency

45

46

47

48

49

50 void InitMain() { ANSEL = 0; // Configure AN pins as digital ANSELH = 0; C1ON_bit = 0; // Disable comparators C2ON_bit = 0; PORTC = 0; // Set PORTC to 0 TRISC = 0; // Designate PORTC pins as output } void main() { InitMain(); PR2 = 0x65; T2CON = 0X04; CCP2CON = 0X3F; CCPR2L = 0X0F;

51 PWM

52

53

54

55


Download ppt "PIC16F887."

Similar presentations


Ads by Google