วันศุกร์ที่ 8 กุมภาพันธ์ พ.ศ. 2562

ไมโครคอนโทลเลอร์ 1

ไมโครคอนโทลเลอร์ 1

Toggle/Blink led on specific delay with pic microcontroller using timers: MPLABX and xc8 compiler



Pic microcontroller specific delay generation to toggle leds.


Project circuit diagram

Pic16f887 microcontroller is used in the project. I am going to toggle the led's connected to Port-B of pic16f887 microcontroller. Port-B is an 8-bit port. 8 led's are connected with port-b. Whole port is going to be toggled after 10 ms delay. An external 20 Mhz crystal is used as input clock source to pic16f887 microcontroller.  Timer-1 of pic16f887 is used for generating specific delay. Timer-1 is used in 16-bit mode. 

 Code


/************************************************** 
 * Property of: www.microcontroller-project.com   *
 * Author: Usman Ali Butt                         *
 * Created on 11 April, 2017, 2:30 PM             *
 **************************************************/

#include < stdio.h>
#include < stdlib.h>
#include < pic16f887.h>          

void Timer0Delay(void);       //Delay Function

int main(int argc, char** argv) {

    TRISB=0x00;               //Port-B as Output
      
    while(1){
        PORTB= ~PORTB;       //Toggle Output
        Timer0Delay();       //Delay
    }
    return (EXIT_SUCCESS);
}

void Timer0Delay(void){     //10ms delay
    T1CON=0x01;             //Timer-1 16-bit mode Prescaler 1:4
    TMR1H=0x30;             //Count Hight Byte
    TMR1L=0xD4;             //Count Low Byte
    
    T1CONbits.TMR1ON=1;              //Run timer
    while(INTCONbits.TMR0IF==0);     //Wait for flag to over flow
    T1CONbits.TMR1ON=0;              //Switch off timer
    INTCONbits.TMR0IF=0;             //Clear Interrupt
    
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น