0% found this document useful (0 votes)
16 views1 page

chớp tắt đèn

This document is a C program for a microcontroller that controls two LEDs connected to pins RB0 and RB7. It initializes the GPIO pins and alternates the state of the LEDs with delays, turning one LED on while turning the other off. The program runs indefinitely in a loop, creating a blinking effect for the LEDs.

Uploaded by

ngauyenluong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

chớp tắt đèn

This document is a C program for a microcontroller that controls two LEDs connected to pins RB0 and RB7. It initializes the GPIO pins and alternates the state of the LEDs with delays, turning one LED on while turning the other off. The program runs indefinitely in a loop, creating a blinking effect for the LEDs.

Uploaded by

ngauyenluong
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd

#include <stdio.

h>
#include <stdlib.h>
#define _XTAL_FREQ 4000000
#include <xc.h>
// CONFIG
#pragma config FOSC = XT // Oscillator Selection bits (XT 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 = OFF // Brown-out Reset Enable bit (BOR disabled)
#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)
void GPIO_init (void);
void Anode (void);
void Cathode (void);
void main (void)
{
GPIO_init();
while(1)
{
__delay_ms(100);
Anode();
Cathode();
__delay_ms(100);
}
}
void GPIO_init(void)
{
TRISBbits.TRISB0 = 0; // RB0 LÀ OUTPUT
TRISBbits.TRISB7 = 0; // RB7 LÀ OUTPUT
PORTBbits.RB0 = 1; // OFF LED RB0
PORTBbits.RB7 = 0; // OFF LED RB7
}
void Anode (void)
{
PORTBbits.RB0 = 0;
__delay_ms(500);
PORTBbits.RB0 = 1;
}
void Cathode (void)
{
PORTBbits.RB7 = 1;
__delay_ms(500);
PORTBbits.RB7 = 0;
}

You might also like