- Trong bài này mình hướng dẫn các bạn cách đuổi Led. Cái này ứng dụng nhiều trong các mạch quảng cáo, hay Led trái tim.
- Ảnh mô phỏng protues.
- Đây là code.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _XTAL_FREQ 8000000
// 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 = 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 main(void)
{
CMCON = 0x07; // To turn off comparators
ADCON1 = 0x06; // To turn off adc
TRISB = 0x00; // Sets all pins in PORTB as output
PORTB = 1; // Set RB0 to high 00000001
do // To set infinite loop
{
__delay_ms(300); // 300 mili seconds delay
PORTB = PORTB<<1; //Shifting right by one bit // dịch sang phải 1 bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
__delay_ms(350); // 350 mili seconds delay
PORTB = 1;
}
}while(1); // To set infinite loop
}
- Link download Project.
http://www.mediafire.com/download/ie52b6jjri0t7fk/chaser_led.rar
- Ảnh mô phỏng protues.
- Đây là code.
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define _XTAL_FREQ 8000000
// 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 = 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 main(void)
{
CMCON = 0x07; // To turn off comparators
ADCON1 = 0x06; // To turn off adc
TRISB = 0x00; // Sets all pins in PORTB as output
PORTB = 1; // Set RB0 to high 00000001
do // To set infinite loop
{
__delay_ms(300); // 300 mili seconds delay
PORTB = PORTB<<1; //Shifting right by one bit // dịch sang phải 1 bit
if(PORTB >= 0b10000000) //To reset to 00000001
{ //when the count becomes 10000000
__delay_ms(350); // 350 mili seconds delay
PORTB = 1;
}
}while(1); // To set infinite loop
}
- Link download Project.
http://www.mediafire.com/download/ie52b6jjri0t7fk/chaser_led.rar
EmoticonEmoticon