Advertisement
Guest User

writing user-defined functions and 'for loop'

a guest
Apr 23rd, 2016
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.19 KB | None | 0 0
  1. #include <p18f4520.h>
  2. #include <delays.h>
  3.  
  4. //2.0 Configure Controller
  5. #pragma config OSC = HS //set external High-speed (HS) oscillation
  6. #pragma config WDT = OFF //disable the Watchdog feature of the MCU
  7. #pragma config LVP = OFF //disable low voltage programming (LVP)
  8. #pragma config PBADEN = OFF //Port B Analogue to Digital module is OFF
  9.  
  10. //3.0 Definition: define which pin each LED is connected to using bit-oriented instruction
  11. #define L0 PORTCbits.RC0 //RC0 assigned as L0
  12. #define L7 PORTCbits.RC7 //RC7 assigned as L7
  13.  
  14. //4.0 user define functions
  15.  
  16. //declare and define a long delay for 1:2
  17. void delay_long(void)
  18. {
  19.  int x;
  20.  for(x; x<20000;x++)
  21.  {
  22.    // do nothing for 20000 times
  23.  }
  24. }
  25. //declare and define a short delay for 1:1
  26. void delay_short(void)
  27. {
  28.  int x;
  29.  for(x=0; x<10000;x++)
  30.  {
  31.   //do nothing for 10000 times
  32.  }
  33. }
  34.  
  35. //function for LED7 (1:1) and LED0 (0)
  36. void display_1(void)
  37. {
  38.  L0=0;
  39.  L7=1;
  40.  delay_short();
  41.  L7=0;
  42.  delay_short();
  43. }
  44.  
  45. //function for LED7 (0) and LED0 (2:1)
  46. void display_2(void)
  47. {
  48.  L7=0;
  49.  L0=1;
  50.  delay_long();
  51.  L0=0;
  52.  delay_short();
  53. }
  54.  
  55. //5.0 start of main program
  56. void main(void)
  57. {
  58. //5.1 Declare variables
  59. int i;
  60.  
  61. //5.2 configure I/O ports
  62.  TRISCbits.TRISC0 =0; //configure only RC0 as output
  63.  TRISCbits.TRISC7 =0; //configure only RC7 as output
  64.  
  65. //5.3 initialize variables and output ports
  66. i=0; //assign 0 to interger i
  67. PORTCbits.RC0 = 0; // clear bit RC0 only using a bitwise instruction
  68. PORTCbits.RC7 = 0; // clear bit RC7 only using a bitwise instruction
  69.  
  70. // infinite loop
  71. while(1)
  72. {
  73.  L0=0; //off both LEDs
  74.  L7=0;
  75.  
  76.  Delay10KTCYx(3000); // 3 sec delay @4Mhz oscillator
  77.  
  78.  for(i=0; i<5;++) // loop 5 times
  79.  {
  80.   display_1(); //call the display_1() function
  81.  }
  82.  Delay10KTCYx(3000); //3 sec delay @4Mhz oscillator
  83.  
  84.  for(i=10; i<10; i++) // loop 10 times
  85.  {
  86.  display_2(); //call the display_2() function
  87.  }
  88.  
  89.  Delay10KTCYx(3000); // 3 sec delay @4Mhz oscillator
  90.  
  91.  for(i=0; i<10;i++) //loop 10 times
  92.  {
  93.   L0=1; //on both LEDS
  94.   L7=1;
  95.   delay_short(); //call delay_short() function
  96.   L0=0; //off both LEDS
  97.   L7=0;
  98.   delay_short(); //call delay_short() function
  99.   }
  100.  } //end of while loop
  101. } //end of main program
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement