Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Wireless counter
- PIC A counting how many button pressed then send data to PIC B
- Code for this video:
- https://www.youtube.com/watch?v=pm_PA_DsWZY
- */
- //Reset switch
- #define SW1 PORTB.F0 //Push = 0, release = 1
- #define SW2 PORTB.F1 //increase count
- char uart_rd;
- unsigned int i=0;
- //Previous Switch status
- int PrevStat= 0;
- //Current Switch status
- int CurStat = 0;
- //Count variable
- int Count = 0;
- void main()
- {
- UART1_Init(9600); // Initialize UART module at 9600 bps
- Delay_ms(100); // Wait for UART module to stabilize
- TRISB = 0b00010111;
- TRISD=0; //Set PortD as LED output
- //Blink All LED Twice
- PORTD=255;
- Delay_ms(500);
- PORTD=0;
- Delay_ms(500);
- PORTD=255;
- Delay_ms(500);
- PORTD=0;
- UART1_Write('C'); //Tell PIC B to Clear count
- Count = 0; //Clear count
- //get current switch state
- PrevStat= SW2;
- //##################################################
- while(1)
- {
- CurStat= SW2;
- //IF switch change state increase count
- if( PrevStat !=CurStat)
- {
- PrevStat=CurStat;
- Count++;
- UART1_Write('a');
- }
- //Check if reset count switch pressed
- if(SW1==0)
- {
- Count= 0;
- UART1_Write('C');
- }
- //dispaly count on LED in binary format
- PORTD = Count ;
- //PIC B request current count
- if (UART1_Data_Ready()) // If data is received,
- {
- uart_rd= UART1_Read();// read the received data,
- if(uart_rd=='R') // PIC B Request for current count
- {
- if(count>0) //If count not zero
- {
- //Send character 'a' until it same as count value
- for(i=0; i<count; i++)
- {
- UART1_Write('a');
- Delay_ms(10);
- }
- }
- else
- {
- UART1_Write('C'); //Tell PIC B to ClearRed count
- }
- }
- }
- } //End of while(1)
- } // End of main function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement