Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <pic.h>
- #define _XTAL_FREQ 4000000 //Hz
- #define s1 RA0
- #define s2 RA1
- #define s3 RA2
- #define s4 RA3
- #define s5 RA4
- #define s6 RA5
- #define s7 RE0
- #define s8 RE1
- char bulk = 0;
- char blink = 0;
- void InitUART(void)
- {
- SPBRG = 25; //9600 Baudrate@4MHz
- BRGH = 1; //fast baudrate
- BRG16 = 0; //8 bit baudrate generator
- SYNC = 0; //asynchonyous mode
- SPEN = 1; //enable TX as output on RC6
- CREN = 1; //enalbe RX as input on RC7
- RCIE = 1; //Enable RX Interrupts
- TXEN = 1; //Eanbel transmitter
- PEIE = 1;
- GIE = 1;
- }
- void send_char_UART(char value)
- {
- while(!TXIF); //wait while TXREG is busy
- TXREG=value;
- }
- int LED (int L)
- {
- if (L == 1)
- {
- RB0 = 1;
- __delay_ms(500);
- RB0 = 0;
- __delay_ms(500);
- }
- if (L == 2)
- {
- RB1 = 1;
- __delay_ms(500);
- RB1 = 0;
- __delay_ms(500);
- }
- if (L == 3)
- {
- RB2 = 1;
- __delay_ms(500);
- RB2 = 0;
- __delay_ms(500);
- }
- if (L == 4)
- {
- RB3 = 1;
- __delay_ms(500);
- RB3 = 0;
- __delay_ms(500);
- }
- if (L == 5)
- {
- RB4 = 1;
- __delay_ms(500);
- RB4 = 0;
- __delay_ms(500);
- }
- if (L == 6)
- {
- RB5 = 1;
- __delay_ms(500);
- RB5 = 0;
- __delay_ms(500);
- }
- if (L == 7)
- {
- RB6 = 1;
- __delay_ms(500);
- RB6 = 0;
- __delay_ms(500);
- }
- if (L == 8)
- {
- RB7 = 1;
- __delay_ms(500);
- RB7 = 0;
- __delay_ms(500);
- }
- if (L == 0)
- {
- PORTB =0b00000000;
- }
- }
- int Sens(void)
- {
- if(s1 == 0)
- {
- return 1;
- }
- else if(s2 == 0 && s1 == 1)
- {
- return 2;
- }
- else if(s3 == 0 && s2 == 1 && s1 == 1)
- {
- return 3;
- }
- // else if(s4 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
- // {
- // return 4;
- // }
- // else if(s5 == 0 && s4 == 1 && s3 == 1 && s3 == 1 && s2 == 1 && s1 == 1)
- // {
- // return 5;
- // }
- // else if(s6 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
- // {
- // return 6;
- // }
- // else if(s7 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
- // {
- // return 7;
- // }
- // else if(s8 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
- // {
- // return 8;
- // }
- else
- {
- return 0;
- }
- }
- main()
- {
- InitUART();
- ANSELH = 0;
- ANSEL = 0;
- TRISC0 = 1;
- TRISA = 1;
- TRISE = 1;
- TRISB = 0;
- PORTB = 0;
- while(1)
- {
- if (blink == 1)
- {
- if (Sens()==1)
- {
- send_char_UART('A');
- blink = 0;
- while (Sens()==1)
- {
- LED(1);
- }
- }
- else if (Sens()==2)
- {
- send_char_UART('B');
- blink = 0;
- while (Sens()==2)
- {
- LED(2);
- }
- }
- else if (Sens()==3)
- {
- send_char_UART('C');
- blink = 0;
- while (Sens()==3)
- {
- LED(3);
- }
- }
- else
- {
- blink = 0;
- }
- }
- }
- }
- interrupt isr()
- {
- if(RCIF)
- {
- if(OERR)
- {
- CREN=0;
- CREN=1; //reset receiving module to recover from error
- }
- else
- {
- bulk = RCREG;
- if(!FERR) //no framing error
- {
- if (RCREG == 'C')
- {
- blink = 1; // start blinking when a 'X' is received without errors
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement