Advertisement
FahmiG

Wireless Counter A

Oct 17th, 2014
1,510
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.39 KB | None | 0 0
  1. /* Wireless counter
  2. PIC A  counting how many button pressed then send data to PIC B
  3.  
  4. Code for this video:
  5. https://www.youtube.com/watch?v=pm_PA_DsWZY
  6. */
  7. //Reset switch
  8. #define SW1 PORTB.F0        //Push = 0, release = 1
  9. #define SW2 PORTB.F1        //increase count
  10.  
  11. char uart_rd;
  12. unsigned int i=0;
  13.  
  14. //Previous Switch status
  15. int PrevStat= 0;
  16.  
  17. //Current Switch status
  18. int CurStat = 0;
  19.  
  20. //Count variable
  21. int Count = 0;
  22.  
  23.  
  24. void main()
  25. {
  26.       UART1_Init(9600); // Initialize UART module at 9600 bps
  27.       Delay_ms(100);    // Wait for UART module to stabilize
  28.  
  29.      TRISB = 0b00010111;
  30.      TRISD=0;             //Set PortD as LED output
  31.  
  32.      //Blink All LED Twice
  33.      PORTD=255;
  34.      Delay_ms(500);
  35.      PORTD=0;
  36.      Delay_ms(500);
  37.      PORTD=255;
  38.      Delay_ms(500);
  39.      PORTD=0;
  40.  
  41.      UART1_Write('C');   //Tell PIC B to Clear count
  42.      Count = 0;          //Clear count
  43.  
  44.     //get current switch state
  45.     PrevStat= SW2;
  46.  
  47.      //##################################################
  48.      while(1)
  49.      {
  50.                CurStat= SW2;
  51.  
  52.          //IF switch change state increase count
  53.          if( PrevStat !=CurStat)
  54.          {
  55.               PrevStat=CurStat;
  56.               Count++;
  57.               UART1_Write('a');
  58.          }
  59.  
  60.          //Check if reset count switch pressed
  61.          if(SW1==0)
  62.          {
  63.             Count= 0;
  64.             UART1_Write('C');
  65.          }
  66.  
  67.          //dispaly count on LED in binary format
  68.          PORTD = Count ;
  69.  
  70.  
  71.  
  72.             //PIC B request current count
  73.             if (UART1_Data_Ready())     // If data is received,
  74.             {
  75.                   uart_rd= UART1_Read();// read the received data,
  76.  
  77.                   if(uart_rd=='R')      // PIC B Request for current count
  78.                   {
  79.                        if(count>0)  //If count not zero
  80.                       {
  81.                           //Send character 'a' until it same as count value
  82.                           for(i=0; i<count; i++)
  83.                           {
  84.                              UART1_Write('a');
  85.                              Delay_ms(10);
  86.                           }
  87.                       }
  88.                       else
  89.                       {
  90.                           UART1_Write('C');  //Tell PIC B to ClearRed count
  91.                       }
  92.  
  93.  
  94.                   }
  95.  
  96.             }
  97.  
  98.      } //End of while(1)
  99.  
  100. }   // End of main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement