sp3ctrm5tr

PIC16F877A LCD Count UP and DOWN with range

Apr 12th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.69 KB | None | 0 0
  1. /*--------------------------------------------------------/
  2.     Name    : Mohd Syahri Fikri Bin Samusi
  3.    
  4.     LCD interfacing with PIC16F877A
  5.     with RB interrupt. switch RB7 will count up
  6.     and switch RB6 will count down. Range of
  7.     counter from 250 to 300
  8.    
  9.     Date modified : 06 April 2012
  10. /--------------------------------------------------------*/
  11.  
  12. #include <htc.h>
  13. #include <pic16f877a.h>
  14. #define _XTAL_FREQ 20000000
  15. #include <lcd.c>
  16.  
  17. int k;
  18. //----Interrupt Service function----
  19. interrupt ISR(void) // Interrupt function definition
  20. {
  21.     if(RBIF) // if RB port change interrupt flag
  22.     {
  23.         RBIE = 0; // Disable RB port change interrupt
  24.        
  25.         if(!RB7)
  26.         {
  27.             k++; // Increment of the counter
  28.             //__delay_ms(1000); //Delay for 1 second
  29.         }
  30.        
  31.         if(!RB6)
  32.         {
  33.             k--; // Decrement of the counter
  34.             //__delay_ms(1000); //Delay for 1 second
  35.         }
  36.  
  37.         RBIF = 0; // Reset RB port change interrupt flag
  38.         RBIE = 1; // Re-enable RB port change interrupt    
  39.     }
  40.     GIE=1;
  41. }
  42.  
  43. //----Main function----
  44. void main( )
  45. {  
  46.     char j[6];
  47.     lcd_init( );
  48.     TRISB = 0xF0;
  49.     nRBPU = 0;
  50.     lcd_putch ('\f');
  51.     lcd_putc ("RB7 UP, RB6 DOWN ");
  52.    
  53.     RBIE = 1;   // Enable the RB port change interrupt
  54.     GIE = 1;    // Global interrupt enable
  55.     RBIF = 0;   // Clear RB port change interrupt flag
  56.     PEIE = 1;   // Peripheral Interrupt Enable
  57.    
  58.     while (1)
  59.     {  
  60.         if(k<250) k=250;
  61.         if(k>300) k=300;
  62.        
  63.         // k++;//Increment the counter
  64.         j[0]=k/10000+'0';
  65.         j[1]=(k%10000)/1000+'0';
  66.         j[2]=(k%1000)/100+'0';
  67.         j[3]=(k%100)/10+'0';
  68.         j[4]=k%10+'0';
  69.         j[5]=0;
  70.         lcd_gotoxy(2,0); //Go to the second line of LCD panel
  71.         lcd_putc ("Now is ");
  72.         lcd_putc (j);
  73.         __delay_ms(1000); //Delay for 1 second         
  74.     }
  75. }
  76. //End of Program
Advertisement
Add Comment
Please, Sign In to add comment