1. Preprocessor Directives:
#include <pic.h>: Includes the header filepic.h, which contains definitions and declarations specific to PIC microcontrollers.#define _XTAL_FREQ 4000000: Defines a preprocessor constant_XTAL_FREQwith the value 4000000, representing the crystal frequency in Hertz. This value is used by the compiler to calculate delay times and other timing-related calculations.
2. main() Function:
-
Initialization:
PORTC = 0xff;: Sets all bits of PORTC to 1.TRISC = 0x00;: Configures all pins of PORTC as outputs.
-
Main Loop:
-
First For Loop:
- Iterates from 0 to 1 (effectively executing once).
- Sets individual bits of PORTC to 1, one at a time, and delays for 100ms between each bit setting. This creates a sequential lighting effect from left to right.
-
Second For Loop:
- Iterates from 0 to 1 (effectively executing once).
- Sets individual bits of PORTC to 1, starting from the rightmost bit and moving left, and delays for 100ms between each bit setting. This creates a sequential lighting effect from right to left.
-
Explanation:
The code implements a simple LED-based lighting effect on a PIC microcontroller, controlling the individual bits of PORTC to create a sequential lighting pattern.
- The first for loop lights up the LEDs from left to right, starting with the leftmost LED and gradually illuminating the others.
- The second for loop lights up the LEDs from right to left, starting with the rightmost LED and gradually illuminating the others.
#include<pic.h>
#define _XTAL_FREQ 4000000
void main()
{
unsigned char i;
PORTC=0xff;
TRISC=0x00;
for(i=0; i<1; i++)
{
PORTC=0X01;
__delay_ms(100);
PORTC=0X02;
__delay_ms(100);
PORTC=0X04;
__delay_ms(100);
PORTC=0X08;
__delay_ms(100);
PORTC=0X100;
__delay_ms(100);
PORTC=0X20;
__delay_ms(100);
PORTC=0X40;
__delay_ms(100);
PORTC=0X80;
__delay_ms(100);
}
for(i=0; i<1; i++)
{
PORTC=0X80;
__delay_ms(100);
PORTC=0X40;
__delay_ms(100);
PORTC=0X20;
__delay_ms(100);
PORTC=0X100;
__delay_ms(100);
PORTC=0X08;
__delay_ms(100);
PORTC=0X04;
__delay_ms(100);
PORTC=0X02;
__delay_ms(100);
PORTC=0X01;
__delay_ms(100);
}
}
#define _XTAL_FREQ 4000000
void main()
{
unsigned char i;
PORTC=0xff;
TRISC=0x00;
for(i=0; i<1; i++)
{
PORTC=0X01;
__delay_ms(100);
PORTC=0X02;
__delay_ms(100);
PORTC=0X04;
__delay_ms(100);
PORTC=0X08;
__delay_ms(100);
PORTC=0X100;
__delay_ms(100);
PORTC=0X20;
__delay_ms(100);
PORTC=0X40;
__delay_ms(100);
PORTC=0X80;
__delay_ms(100);
}
for(i=0; i<1; i++)
{
PORTC=0X80;
__delay_ms(100);
PORTC=0X40;
__delay_ms(100);
PORTC=0X20;
__delay_ms(100);
PORTC=0X100;
__delay_ms(100);
PORTC=0X08;
__delay_ms(100);
PORTC=0X04;
__delay_ms(100);
PORTC=0X02;
__delay_ms(100);
PORTC=0X01;
__delay_ms(100);
}
}

what is it
ReplyDelete