- Đây là ảnh mô phỏng protues.
#include <xc.h>
#define _XTAL_FREQ 4000000
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void GPIO_init(void);// goi ham xuat nhap
void delay_ms(unsigned int i);
unsigned int i;
void main (void)
{
GPIO_init(); //
unsigned char i,I,J,CD,D,Y,X;
while(1)
{
// left
PORTC = 0X01;
for(i=0;i<8;i++)
{
delay_ms(300);
PORTC = PORTC<<1;
}
// right
delay_ms(300);
PORTC = 0X80;
for(i=0;i<8;i++)
{
delay_ms(300);
PORTC = PORTC>>1;
}
// left 2
PORTC = 0X03;
for(i=0;i<4;i++)
{
delay_ms(300);
PORTC = PORTC<<2;
}
// right 2
PORTC = 0XC0;
for(i=0;i<4;i++)
{
delay_ms(300);
PORTC = PORTC>>2;
}
// chase left
delay_ms(300);
PORTC = 0X00;
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = PORTC>>1;
PORTC = PORTC | 0X80;
}
// chase right
delay_ms(300);
PORTC = 0X00;
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = PORTC<<1;
PORTC = PORTC | 1;
}
// chase on left
delay_ms(300);
PORTC = 0X00;
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = (PORTC<<1) | 0X01;
}
// chase off left
delay_ms(300);
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = PORTC<<1;
}
// chase right on and off
delay_ms(300);
X=0X00000000;
for(I=0;I<8;I++)
{
X=(X<<1)+0X01;
PORTC = X;
delay_ms(300);
}
for(I=0;I<8;I++)
{
X=(X>>1);
PORTC = X;
delay_ms(300);
}
// don 1
delay_ms(300);
CD=0X00000000;
for ( J=8;J>0;J--)
{
D=0X00000001;
for(I=0;I<J;I++)
{
Y=CD+D;
PORTC = (Y);
delay_ms(300);
D=(D<<1);
}
CD=Y;
}
// don 2
delay_ms(300);
CD=0X00;
for ( J=4;J>0;J--)
{
delay_ms(300);
D=0X03;//0x03
for(I=0;I<J;I++)
{
delay_ms(100);
Y=CD+D;
PORTC = (Y);
delay_ms(300);
D=(D<<2);
}
CD=Y;
}
}
}
void GPIO_init(void)
{
TRISC = 0X00; // cho porta la output
PORTC = 0X00; //
}
void delay_ms(unsigned int i)
{
for(i=0;i<10;i++)
{
__delay_ms(30);
}
}
- Link download project : Click here
#define _XTAL_FREQ 4000000
// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.
// CONFIG1
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // RE3/MCLR pin function select bit (RE3/MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF // Brown Out Reset Selection bits (BOR disabled)
#pragma config IESO = OFF // Internal External Switchover bit (Internal/External Switchover mode is disabled)
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
#pragma config LVP = OFF // Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
void GPIO_init(void);// goi ham xuat nhap
void delay_ms(unsigned int i);
unsigned int i;
void main (void)
{
GPIO_init(); //
unsigned char i,I,J,CD,D,Y,X;
while(1)
{
// left
PORTC = 0X01;
for(i=0;i<8;i++)
{
delay_ms(300);
PORTC = PORTC<<1;
}
// right
delay_ms(300);
PORTC = 0X80;
for(i=0;i<8;i++)
{
delay_ms(300);
PORTC = PORTC>>1;
}
// left 2
PORTC = 0X03;
for(i=0;i<4;i++)
{
delay_ms(300);
PORTC = PORTC<<2;
}
// right 2
PORTC = 0XC0;
for(i=0;i<4;i++)
{
delay_ms(300);
PORTC = PORTC>>2;
}
// chase left
delay_ms(300);
PORTC = 0X00;
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = PORTC>>1;
PORTC = PORTC | 0X80;
}
// chase right
delay_ms(300);
PORTC = 0X00;
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = PORTC<<1;
PORTC = PORTC | 1;
}
// chase on left
delay_ms(300);
PORTC = 0X00;
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = (PORTC<<1) | 0X01;
}
// chase off left
delay_ms(300);
for(i=0;i<9;i++)
{
delay_ms(300);
PORTC = PORTC<<1;
}
// chase right on and off
delay_ms(300);
X=0X00000000;
for(I=0;I<8;I++)
{
X=(X<<1)+0X01;
PORTC = X;
delay_ms(300);
}
for(I=0;I<8;I++)
{
X=(X>>1);
PORTC = X;
delay_ms(300);
}
// don 1
delay_ms(300);
CD=0X00000000;
for ( J=8;J>0;J--)
{
D=0X00000001;
for(I=0;I<J;I++)
{
Y=CD+D;
PORTC = (Y);
delay_ms(300);
D=(D<<1);
}
CD=Y;
}
// don 2
delay_ms(300);
CD=0X00;
for ( J=4;J>0;J--)
{
delay_ms(300);
D=0X03;//0x03
for(I=0;I<J;I++)
{
delay_ms(100);
Y=CD+D;
PORTC = (Y);
delay_ms(300);
D=(D<<2);
}
CD=Y;
}
}
}
void GPIO_init(void)
{
TRISC = 0X00; // cho porta la output
PORTC = 0X00; //
}
void delay_ms(unsigned int i)
{
for(i=0;i<10;i++)
{
__delay_ms(30);
}
}
EmoticonEmoticon