0% found this document useful (0 votes)
2 views

codeLEDdon

The document contains C code for controlling LED patterns using a microcontroller. It includes various functions to create different lighting effects such as moving lights, fading in and out, and illuminating LEDs in sequences. The main function continuously cycles through these effects, allowing for dynamic LED displays.

Uploaded by

minhdung1282005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

codeLEDdon

The document contains C code for controlling LED patterns using a microcontroller. It includes various functions to create different lighting effects such as moving lights, fading in and out, and illuminating LEDs in sequences. The main function continuously cycles through these effects, allowing for dynamic LED displays.

Uploaded by

minhdung1282005
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

//led sáng ĐUỔI từ TRÁI sang PHẢI

#include <xc.h>
#define _XTAL_FREQ 4000000
unsigned char DATA[]={0x80,0x40,0x20,0x10,0x08,0x04,0x02,0x01};
unsigned char i;
void main(void)
{
TRISC=0x00;
TRISD=0x00;
while(1)
{
for(i=0;i<8;i++)
{
PORTC=DATA[i];
PORTD=DATA[i];
__delay_ms(100);
}
}
return;
}
//ĐUỔI từ TRÁI sang PHẢI và ngược lại
void main(void)
{
TRISD = 0x00;
TRISC=0x00;
PORTD=0x00;
PORTC=0x00;
while(1)
{
for(unsigned char i = 0; i < 8; i++)
{
PORTD = 0x80 >> i;
__delay_ms(100);
}
PORTD = 0x00;
for(unsigned char i = 0; i < 8; i++)
{
PORTC = 0x80 >> i;
__delay_ms(100);
}
PORTC = 0x00;
for(unsigned char i = 0; i < 8; i++)
{
PORTC = 0x01 << i;
__delay_ms(100);
}
PORTC = 0x00;
for(unsigned char i = 0; i < 8; i++)
{
PORTD = 0x01 << i;
__delay_ms(100);
}
PORTD = 0x00;
}
return;
}
//sáng ĐUỔI 1 LED từ NGOÀI vào TRONG và ngược lại
#include <xc.h>
#include <math.h>
#define _XTAL_FREQ 4000000
#define LED_DELAY 250

void setup() {
TRISC = 0x00;
TRISD = 0x00;
PORTC = 0x00;
PORTD = 0x00;
}

void main()
{
setup();
while (1)
{
PORTC = 0x80;
PORTD = 0x01;
__delay_ms(LED_DELAY);

for (unsigned char i = 0; i < 7; i++)


{
PORTC = PORTC | (PORTC >> 1);
PORTD = PORTD | (PORTD << 1);
__delay_ms(LED_DELAY);
}

//__delay_ms(LED_DELAY);

PORTC = 0x0;
PORTD = 0x0 ;

PORTD = 0x80;
PORTC = 0x01;__delay_ms(LED_DELAY);
for (unsigned char i = 0; i < 7; i++)
{
PORTC = (PORTC << 1) ;
PORTD = (PORTD >> 1) ;
__delay_ms(LED_DELAY);
}

PORTC = 0x00;
PORTD = 0x00;
__delay_ms(LED_DELAY);
}
}
//sáng dần từ TRÁI sang PHẢI/tắt dần ngược lại
unsigned char DATA2[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF};
unsigned char i;
void main(void)
{
TRISC=0x00;
PORTC=0x00;
TRISD=0x00;
PORTD=0x00;
while(1)
{
for ( i=0;i<8; i++)
{
PORTD = DATA2[i];
__delay_ms(200);
}
for ( i=0;i<8;i++)
{
PORTC=DATA2[i];
__delay_ms(200);
}
for (signed char i=7; i>=0;i--)
{
PORTC=DATA2[i];
__delay_ms(200);
}
for (signed char i=7; i>=0;i--)
{
PORTC=0x00;
PORTD = DATA2[i];
__delay_ms(200);
}
PORTD=0x00;
__delay_ms(200);
}
return; }
//sáng dần từ PHẢI sang TRÁI/tắt dần ngược lại
unsigned char DATA2[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
unsigned char i;
void main(void)
{
TRISC=0x00;
PORTC=0x00;
TRISD=0x00;
PORTD=0x00;
while(1)
{
for ( i=0;i<8; i++)
{
PORTC = DATA2[i];
__delay_ms(200);
}
for ( i=0;i<8;i++)
{
PORTD=DATA2[i];
__delay_ms(200);
}
for (signed char i=7; i>=0;i--)
{
PORTD=DATA2[i];
__delay_ms(200);
}
for (signed char i=7; i>=0;i--)
{
PORTD=0x00;
PORTC = DATA2[i];
__delay_ms(200);
}
PORTC=0x00;
__delay_ms(200);
}
return; }
//sáng dồn từ trái sang phải
void main(void)
{
TRISC=0x00;
PORTC=0x00;
unsigned char i,j,a=0;
while(1)
{
for(i=0;i<8;i++)
{
for (j=0;j<8-i; j++)
{
PORTC=(0x80>>j)|a;
__delay_ms(200);
}
a=PORTC;
__delay_ms(200);
}
a=0x00;
PORTC=0x00;
__delay_ms(200);
}
}

//sáng DỒN 16LED C,D từ TRÁI sang PHẢI


unsigned char
DATA[]={0x0001,0x0003,0x0007,0x000F,0x001F,0x003F,0x007F,0x00FF,0x01FF,0x03FF,
0x07FF,0x0FFF,0x1FFF,0x3FFF,0x7FFF,0xFFFF};
unsigned char i;
void main(void)
{
TRISC = 0x00;
PORTC = 0x00;
TRISD = 0x00;
PORTD = 0x00;
unsigned int a = 0;
unsigned int b = 0;
unsigned int i = 0;
int j;
while (1) {
a = 0;
b = 0;

for (j = 0; j < 16; j++) {


i = 0x8000;
for (int k = 0; k < 16 - j; k++) {
a = i | b;
PORTC = (a & 0xFF); // Xuất 8 bit thấp ra PORTC
PORTD = ((a >> 8) & 0xFF); // Xuất 8 bit cao ra PORTD
__delay_ms(LED_DELAY);
i = i >> 1;
}
b = a;
}
}
return;
}
TỔNG HỢP
#include <xc.h>
#define _XTAL_FREQ 4000000
#define LED_DELAY 100
unsigned char DATA_IN[]={0x81,0x42,0x24,0x18};
unsigned char DATA_OUT[]={0x18,0x24,0x42,0x81};
unsigned char i;

void sangduoi()
{
for(unsigned char i = 0; i < 8; i++)
{
PORTD = 0x80 >> i;
__delay_ms(100);
}
PORTD = 0x00;
for(unsigned char i = 0; i < 8; i++)
{
PORTC = 0x80 >> i;
__delay_ms(100);
}
PORTC = 0x00;
for(unsigned char i = 0; i < 8; i++)
{
PORTC = 0x01 << i;
__delay_ms(100);
}
PORTC = 0x00;
for(unsigned char i = 0; i < 8; i++)
{
PORTD = 0x01 << i;
__delay_ms(100);
}
PORTD = 0x00;
}

void ngoaivaotrong()
{
for (unsigned char i = 0; i < 8; i++)
{
PORTC = 0x80 >> i;
PORTD = 0x01 << i;
__delay_ms(LED_DELAY);
}
for (unsigned char i = 0; i < 8; i++)
{
PORTC = 0x01 << i;
PORTD = 0x80 >> i;
__delay_ms(LED_DELAY);
}
PORTC = 0x00;
PORTD = 0x00;
}
unsigned char DATA2_TP[]={0x80,0xC0,0xE0,0xF0,0xF8,0xFC,0xFE,0xFF};
void sangdanTP()
{
for (unsigned char i = 0; i < 8; i++)
{
PORTD = DATA2_TP[i];
__delay_ms(100);
}
for (unsigned char i = 0; i < 8; i++)
{
PORTC = DATA2_TP[i];
__delay_ms(100);
}
for (signed char i = 7; i >= 0; i--)
{
PORTC = DATA2_TP[i];
__delay_ms(100);
}
for (signed char i = 7; i >= 0; i--)
{
PORTC = 0x00;
PORTD = DATA2_TP[i];
__delay_ms(100);
}
PORTD = 0x00;
__delay_ms(100);
}
unsigned char DATA2_PT[]={0x01,0x03,0x07,0x0F,0x1F,0x3F,0x7F,0xFF};
void sangdanPT()
{
for (unsigned char i = 0; i < 8; i++)
{
PORTC = DATA2_PT[i];
__delay_ms(100);
}
for (unsigned char i = 0; i < 8; i++)
{
PORTD = DATA2_PT[i];
__delay_ms(100);
}
for (signed char i = 7; i >= 0; i--)
{
PORTD = DATA2_PT[i];
__delay_ms(100);
}
for (signed char i = 7; i >= 0; i--)
{
PORTD = 0x00;
PORTC = DATA2_PT[i];
__delay_ms(100);
}
PORTC = 0x00;
__delay_ms(100);
}
void sangdon8led()
{
unsigned char i,j,a=0;
{
for(i=0;i<8;i++)
{
for (j=0;j<8-i; j++)
{
PORTC=(0x80>>j)|a;
__delay_ms(100);
}
a=PORTC;
__delay_ms(100);
}
a=0x00;
PORTC=0x00;
__delay_ms(100);
}
}
unsigned char
DATA[]={0x0001,0x0003,0x0007,0x000F,0x001F,0x003F,0x007F,0x00FF,0x01FF,0x03FF,
0x07FF,0x0FFF,0x1FFF,0x3FFF,0x7FFF,0xFFFF};
void sangdon16led()
{
TRISC = 0x00;
PORTC = 0x00;
TRISD = 0x00;
PORTD = 0x00;

unsigned int a = 0;
unsigned int b = 0;
unsigned int i = 0;
int j;
a = 0;
b = 0;

for (j = 0; j < 16; j++)


{
i = 0x8000;
for (int k = 0; k < 16 - j; k++) {
a = i | b;
PORTC = (a & 0xFF); // Xuất 8 bit thấp ra PORTC
PORTD = ((a >> 8) & 0xFF); // Xuất 8 bit cao ra PORTD
__delay_ms(LED_DELAY);
i = i >> 1;
}
b = a;
}
}
void main()
{
TRISC = 0x00;
TRISD = 0x00;
PORTC = 0x00;
PORTD = 0x00;
while(1)
{
sangduoi();
ngoaivaotrong();
sangdanTP();
sangdanPT();
sangdon8led();
sangdon16led();
}
}

You might also like