Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /////////////////////////////////////////////////////////
- //-------Made by Dr. Rodolfo Castello------------------//
- /////////////////////////////////////////////////////////
- #include <xc.h>
- //OSCILLATOR SOURCE AND DIGITAL I/O CONFIGURATION BITS
- //====================================================
- #pragma config OSC = IRCIO67 // CONFIG1H (0-3) = 0010: INTIO2 oscillator, Internal oscillator block, port function on RA6 and RA7.
- #pragma config MCLRE = ON // CONFIG3H.7 = 1: Pin de RESET habilitado y Entrada RE3 desactivado.
- #pragma config PBADEN = OFF // CONFIG3H.1 = 0: PORTB.0 -- PORTB.4 as Digital I/O.
- #pragma config LVP = OFF // CONFIG3H.2 = 0: Single-Supply ICSP disabled so that PORTB.5 works as Digital I/O.
- //PICIT-3 DEBUGGER SETUP CONFIGURATION BITS
- //=========================================
- #pragma config WDT = OFF // CONFIG2H (0) = 0: Watchdog Timer Disabled.
- //GLOBAL VARIABLES
- //================
- unsigned char ons = 0; // One-shot flag.
- unsigned char acc = 0; // Counter.
- // GLOBAL COMPILER?S CONSTANTS
- //=============================
- #define Button1 PORTBbits.RB0
- #define Button2 PORTBbits.RB1
- #define LED PORTBbits.RB3
- //FUNCTION' PROTOTYPE
- //===================
- void One_Shot(void);
- void Reset_One_Shot(void);
- void Delay(void);
- void main(void){
- //CLOCK FREQUENCY CONFIGURATION
- //============================
- OSCCON = 0x60; // 4 MHz internal oscillator
- //DISABLE PORT's ANALOG FUNCTIONS
- //===============================
- CMCON = 0xFF; // Comparators OFF, to use PORT_Ds LSN
- CVRCONbits.CVREN = 0; // Comparator Voltge Reference Module OFF
- ADCON1 = 0x0F; // All ports as DIGITAL I/O
- //CONFIGURATION OF PORT B
- //=======================
- PORTB = 0x00; // Initialize PORTB
- LATB = 0x00; // Clear PORTB
- TRISBbits.TRISB0 = 1; // PORTB.0 as Input
- TRISBbits.TRISB1 = 1; // PORTB.1 as Input
- TRISBbits.TRISB3 = 0; // PORTB.3 as Output
- while(1){
- if (Button1 == 1) // IF RB0 ON THEN trigger the One-Shot.
- One_Shot();
- if (ons == 1) // IF One-Shot?s Trigger is ON THEN Reset-One-Shot
- Reset_One_Shot();
- if (Button2 == 1){ // IF Reset ON THEN:
- LED = 0; // Turn-OFF LED.
- acc = 0; // Restart Counter.
- }
- }
- }
- void One_Shot(){
- if (ons == 1) // IF One-Shot has been triggered THEN EXIT.
- return;
- Delay(); // Eliminate Bouncing.
- if (Button1 == 1){ // IF RB0 still ON THEN:
- acc++; // Increase Counter.
- if (acc == 5) // IF Counter = 5 Then Turn-ON LED.
- LED= 1;
- ons = 1; // Turn-ON one-shot flag.
- }
- }
- void Reset_One_Shot(){
- if (Button1 == 0) // IF RB0 OFF THEN Eliminate Bouncing
- Delay();
- else // ELSE EXIT
- return;
- if (Button1 == 0) // IF RB0 OFF THEN reset one-shot flag.
- ons = 0;
- }
- void Delay(){
- unsigned char L1REG = 0;
- unsigned char L2REG;
- while(L1REG++ < 13){
- L2REG = 0;
- while(L2REG++ < 255);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement