Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <p18f4520.h>
- #include <delays.h>
- //2.0 Configure Controller
- #pragma config OSC = HS //set external High-speed (HS) oscillation
- #pragma config WDT = OFF //disable the Watchdog feature of the MCU
- #pragma config LVP = OFF //disable low voltage programming (LVP)
- #pragma config PBADEN = OFF //Port B Analogue to Digital module is OFF
- //3.0 Definition: define which pin each LED is connected to using bit-oriented instruction
- #define L0 PORTCbits.RC0 //RC0 assigned as L0
- #define L7 PORTCbits.RC7 //RC7 assigned as L7
- //4.0 user define functions
- //declare and define a long delay for 1:2
- void delay_long(void)
- {
- int x;
- for(x; x<20000;x++)
- {
- // do nothing for 20000 times
- }
- }
- //declare and define a short delay for 1:1
- void delay_short(void)
- {
- int x;
- for(x=0; x<10000;x++)
- {
- //do nothing for 10000 times
- }
- }
- //function for LED7 (1:1) and LED0 (0)
- void display_1(void)
- {
- L0=0;
- L7=1;
- delay_short();
- L7=0;
- delay_short();
- }
- //function for LED7 (0) and LED0 (2:1)
- void display_2(void)
- {
- L7=0;
- L0=1;
- delay_long();
- L0=0;
- delay_short();
- }
- //5.0 start of main program
- void main(void)
- {
- //5.1 Declare variables
- int i;
- //5.2 configure I/O ports
- TRISCbits.TRISC0 =0; //configure only RC0 as output
- TRISCbits.TRISC7 =0; //configure only RC7 as output
- //5.3 initialize variables and output ports
- i=0; //assign 0 to interger i
- PORTCbits.RC0 = 0; // clear bit RC0 only using a bitwise instruction
- PORTCbits.RC7 = 0; // clear bit RC7 only using a bitwise instruction
- // infinite loop
- while(1)
- {
- L0=0; //off both LEDs
- L7=0;
- Delay10KTCYx(3000); // 3 sec delay @4Mhz oscillator
- for(i=0; i<5;++) // loop 5 times
- {
- display_1(); //call the display_1() function
- }
- Delay10KTCYx(3000); //3 sec delay @4Mhz oscillator
- for(i=10; i<10; i++) // loop 10 times
- {
- display_2(); //call the display_2() function
- }
- Delay10KTCYx(3000); // 3 sec delay @4Mhz oscillator
- for(i=0; i<10;i++) //loop 10 times
- {
- L0=1; //on both LEDS
- L7=1;
- delay_short(); //call delay_short() function
- L0=0; //off both LEDS
- L7=0;
- delay_short(); //call delay_short() function
- }
- } //end of while loop
- } //end of main program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement