Advertisement
Guest User

Untitled

a guest
Jul 26th, 2014
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.25 KB | None | 0 0
  1. #include <pic.h>
  2. #define _XTAL_FREQ 4000000 //Hz
  3. #define s1 RA0
  4. #define s2 RA1
  5. #define s3 RA2
  6. #define s4 RA3
  7. #define s5 RA4
  8. #define s6 RA5
  9. #define s7 RE0
  10. #define s8 RE1
  11.  
  12.  
  13. char bulk = 0;
  14. char blink = 0;
  15.  
  16.  
  17.  void InitUART(void)
  18. {
  19.  
  20.         SPBRG = 25; //9600 Baudrate@4MHz
  21.         BRGH  = 1;  //fast baudrate
  22.         BRG16 = 0;  //8 bit baudrate generator
  23.         SYNC  = 0;  //asynchonyous mode
  24.         SPEN  = 1;  //enable TX as output on RC6
  25.         CREN  = 1;  //enalbe RX as input on RC7
  26.         RCIE  = 1;  //Enable RX Interrupts
  27.         TXEN  = 1;  //Eanbel transmitter
  28.         PEIE  = 1;
  29.         GIE   = 1;
  30. }
  31.  
  32. void send_char_UART(char value)
  33. {
  34.         while(!TXIF);     //wait while TXREG is busy    
  35.         TXREG=value;
  36. }
  37.  
  38. int LED (int L)
  39. {
  40.     if (L == 1)
  41.     {
  42.       RB0 = 1;
  43.       __delay_ms(500);
  44.       RB0 = 0;
  45.       __delay_ms(500);
  46.     }
  47.  
  48.     if (L == 2)
  49.     {
  50.       RB1 = 1;
  51.       __delay_ms(500);
  52.       RB1 = 0;
  53.       __delay_ms(500);
  54.     }
  55.  
  56.     if (L == 3)
  57.     {
  58.       RB2 = 1;
  59.       __delay_ms(500);
  60.       RB2 = 0;
  61.       __delay_ms(500);
  62.     }
  63.  
  64.     if (L == 4)
  65.     {
  66.       RB3 = 1;
  67.       __delay_ms(500);
  68.       RB3 = 0;
  69.       __delay_ms(500);
  70.     }
  71.  
  72.     if (L == 5)
  73.     {
  74.       RB4 = 1;
  75.       __delay_ms(500);
  76.       RB4 = 0;
  77.       __delay_ms(500);
  78.     }
  79.  
  80.     if (L == 6)
  81.     {
  82.       RB5 = 1;
  83.       __delay_ms(500);
  84.       RB5 = 0;
  85.       __delay_ms(500);
  86.     }
  87.  
  88.     if (L == 7)
  89.     {
  90.       RB6 = 1;
  91.       __delay_ms(500);
  92.       RB6 = 0;
  93.       __delay_ms(500);
  94.     }
  95.  
  96.     if (L == 8)
  97.     {
  98.       RB7 = 1;
  99.       __delay_ms(500);
  100.       RB7 = 0;
  101.       __delay_ms(500);
  102.     }
  103.      
  104.     if (L == 0)
  105.     {
  106.     PORTB =0b00000000;
  107.     }
  108. }
  109.  
  110. int Sens(void)
  111. {
  112.    
  113.     if(s1 == 0)
  114.     {  
  115.     return 1;
  116.     }
  117.    
  118.     else if(s2 == 0 && s1 == 1)
  119.     {
  120.     return 2;
  121.     }
  122.    
  123.     else if(s3 == 0 && s2 == 1 && s1 == 1)
  124.     {
  125.     return 3;
  126.     }
  127.    
  128. //  else if(s4 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
  129. //  {
  130. //  return 4;
  131. //  }
  132. //  else if(s5 == 0 && s4 == 1 && s3 == 1 && s3 == 1 && s2 == 1 && s1 == 1)
  133. //  {
  134. //  return 5;
  135. //  }
  136. //  else if(s6 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
  137. //  {
  138. //  return 6;
  139. //  }
  140. //  else if(s7 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
  141. //  {
  142. //  return 7;
  143. //  }
  144. //  else if(s8 == 0 && s3 == 1 && s2 == 1 && s1 == 1)
  145. //  {
  146. //  return 8;
  147. //  }  
  148.  
  149.     else
  150.     {
  151.     return 0;
  152.     }
  153. }
  154.  
  155.  
  156. main()
  157. {
  158.  
  159.         InitUART();
  160.         ANSELH = 0;
  161.         ANSEL  = 0;
  162.         TRISC0 = 1;
  163.         TRISA  = 1;
  164.         TRISE  = 1;
  165.         TRISB  = 0;
  166.         PORTB  = 0;
  167.  
  168.  
  169. while(1)
  170. {
  171. if (blink == 1)
  172.     {
  173.     if (Sens()==1)
  174.     {
  175.     send_char_UART('A');
  176.     blink = 0;
  177.     while (Sens()==1)
  178.     {
  179.     LED(1);
  180.     }
  181.    
  182.     }
  183.     else if (Sens()==2)
  184.     {
  185.     send_char_UART('B');
  186.     blink = 0;
  187.     while (Sens()==2)
  188.     {
  189.     LED(2);
  190.     }
  191.     }
  192.  
  193.     else if (Sens()==3)
  194.     {
  195.     send_char_UART('C');
  196.     blink = 0;
  197.     while (Sens()==3)
  198.     {
  199.     LED(3);
  200.     }
  201.     }
  202.     else
  203.     {
  204.     blink = 0;
  205.     }
  206. }  
  207.  
  208.  
  209. }
  210. }
  211.  
  212. interrupt isr()
  213. {
  214.  
  215.     if(RCIF)
  216.     {
  217.         if(OERR)
  218.         {
  219.             CREN=0;
  220.             CREN=1;     //reset receiving module to recover from error
  221.  
  222.         }
  223.             else
  224.             {
  225.                 bulk = RCREG;
  226.                     if(!FERR)        //no framing error
  227.                     {
  228.                         if (RCREG == 'C')
  229.                             {
  230.                             blink = 1;  // start blinking when a 'X' is received without errors
  231.                             }
  232.                     }      
  233.  
  234.             }
  235.     }
  236. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement