LED Blinking Sequence using PIC Microcontroller
Project Code
/ 'C' source line config statements
// CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// Use project enums instead of #define for ON and OFF.
#include <xc.h>
#define _XTAL_FREQ 20000000 //Specify the XTAL crystall FREQ
/*******Sequence blink*******/
sblink(int get) //Function definition with "get" as parameter
{
for (int i=0; i<=7 && RB0==0; i++)
{
PORTD = get << i; //LED move Left Sequence
__delay_ms(50);
}
for (int i=7; i>=0 && RB0==0; i--)
{
PORTD = get << i; //LED move Left Sequence
__delay_ms(50);
}
}
sblink(int get) //Function definition with "get" as parameter
{
for (int i=0; i<=7 && RB0==0; i++)
{
PORTD = get << i; //LED move Left Sequence
__delay_ms(50);
}
for (int i=7; i>=0 && RB0==0; i--)
{
PORTD = get << i; //LED move Left Sequence
__delay_ms(50);
}
}
/*******MAIN Function*******/
void main() //The main function
{
TRISB0=1; //Instruct the MCU that the PORTB pin 0 is used as input for button.
TRISD = 0x00; //Instruct the MCU that all pins are output
PORTD=0x00; //Initialize all pins to 0
OPTION_REG= 0b00000000;//Enable pull up R on port B
void main() //The main function
{
TRISB0=1; //Instruct the MCU that the PORTB pin 0 is used as input for button.
TRISD = 0x00; //Instruct the MCU that all pins are output
PORTD=0x00; //Initialize all pins to 0
OPTION_REG= 0b00000000;//Enable pull up R on port B
while(1) //Get into the Infinite While loop
{
if (RB0==0)
{
sblink(1); //FUNCTION CALL 1 with parameter 1
sblink(3); //FUNCTION CALL 3 with parameter 3
sblink(7); //FUNCTION CALL 7 with parameter 7
sblink(15); //FUNCTION CALL 4 with parameter 15
while(RB0==0) //If button is still pressed
{
PORTD=0xFF; //Turn ON all LEDs
}
}
}
}
{
if (RB0==0)
{
sblink(1); //FUNCTION CALL 1 with parameter 1
sblink(3); //FUNCTION CALL 3 with parameter 3
sblink(7); //FUNCTION CALL 7 with parameter 7
sblink(15); //FUNCTION CALL 4 with parameter 15
while(RB0==0) //If button is still pressed
{
PORTD=0xFF; //Turn ON all LEDs
}
}
}
}
ไม่มีความคิดเห็น:
แสดงความคิดเห็น