/* Main.c file generated by New Project wizard
 *
 * Created:   Tue Feb 7 2023
 * Processor: PIC16F877A
 * Compiler:  CCS for PIC
 */
#include <main.h>
#include <16F877A.h>

void output_porta();
void output_portb();
void output_portc();
void output_portd();

unsigned int portnum;
unsigned int reverse;
unsigned int count=1;

int main (void)
 { 
   // Write your code here
	 portnum = 1;
	 reverse = 0;

	 while(TRUE)
	 {
	    if(portnum==1)	output_porta();
	    delay_ms(100);
	    if(portnum==2)	output_portb();
	    delay_ms(100);
	    if(portnum==3)	output_portc();
	    delay_ms(100);
	    if(portnum==4)	output_portd();
	    delay_ms(100);
	    if(portnum==0)
	       {
	       reverse=0;
	       portnum+=1;
	       count=1;
	       }
	    if(portnum==5)
	       {
		  reverse=1;
		  portnum-=1;
		  count=256;
	       }
	    delay_ms(150);
	 }
	 
 }   

void output_porta()
      {
	 output_a(count);
	 if(count>=1 && reverse==0)	count = count * 2;		// used to double the variable "count" whilst greater than or equal to 1 and the "reverse" variable is "0"
	 if(count<=128 && reverse==1)	count = count / 2;		// used to half the variable "count whilst less than or equal to 128 and the "reverse" variable is "1"
	 if(count==128 && reverse==0)
	    {
	       count=1;
	       portnum+=1;
	       output_a(0);
	    }
	 if(count==1 && reverse==1)
	    {
	       count=128;
	       portnum-=1;
	       output_a(0);
	    }
	 delay_ms(150);
      }
void output_portb()
      {
	 output_b(count);
	 if(count>=1 && reverse==0)	count = count * 2;		// used to double the variable "count" whilst greater than or equal to 1 and the "reverse" variable is "0"
	 if(count<=256 && reverse==1)	count = count / 2;		// used to half the variable "count whilst less than or equal to 128 and the "reverse" variable is "1"
	 if(count==256 && reverse==0)
	    {
	       count=1;
	       portnum+=1;
	       output_b(0);
	    }
	 if(count==1 && reverse==1)
	    {
	       count=256;
	       portnum-=1;
	       output_b(0);
	    }
	 delay_ms(150);
      }
void output_portc()
      {
	 output_c(count);
	 if(count>=1 && reverse==0)	count = count * 2;		// used to double the variable "count" whilst greater than or equal to 1 and the "reverse" variable is "0"
	 if(count<=256 && reverse==1)	count = count / 2;		// used to half the variable "count whilst less than or equal to 128 and the "reverse" variable is "1"
	 if(count==256 && reverse==0)
	    {
	       count=1;
	       portnum+=1;
	       output_c(0);
	    }
	 if(count==1 && reverse==1)
	    {
	       count=256;
	       portnum-=1;
	       output_c(0);
	    }
	 delay_ms(150);
      }
void output_portd()
      {
	 output_d(count);
	 if(count>=1 && reverse==0)	count = count * 2;		// used to double the variable "count" whilst greater than or equal to 1 and the "reverse" variable is "0"
	 if(count<=256 && reverse==1)	count = count / 2;		// used to half the variable "count whilst less than or equal to 128 and the "reverse" variable is "1"
	 if(count==256 && reverse==0)
	    {
	       portnum+=1;
	       output_d(0);
	    }
	 if(count==1 && reverse==1)
	    {
	       portnum-=1;
	       output_d(0);
	    }
	 delay_ms(150);
      }