Guest User

interrupts.c

a guest
Mar 14th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.87 KB | None | 0 0
  1. /******************************************************************************/
  2. /*Files to Include                                                            */
  3. /******************************************************************************/
  4.  
  5. #if defined(__XC)
  6.     #include <xc.h>        
  7. #elif defined(HI_TECH_C)
  8.     #include <htc.h>        
  9. #endif
  10.  
  11. #include <stdint.h>        
  12. #include <stdbool.h>        
  13.  
  14. #include "delay.h"
  15. #include "user.h"
  16.  
  17. unsigned short int ITERATION_SCALING_VALUE = 64; //yields 1024 to 0 iterations
  18.  
  19. /******************************************************************************/
  20. /* Interrupt Routines                                                         */
  21. /******************************************************************************/
  22.  
  23. #ifndef _PIC12
  24.  
  25. void interrupt isr(void)
  26. {
  27.     //if it's the AD interrupt
  28.     if(ADIF==1)
  29.     {                
  30.         //delay for some number of NOPs
  31.         doDelay( (((unsigned short int)ADRESH)<<8) + ((unsigned short int)ADRESL) );
  32.  
  33.         //reset AD int flag
  34.         ADIF = 0;
  35.     }
  36. }
  37. #endif
  38.  
  39. void doDelay(unsigned short int ANALOG_VALUE)
  40. {    
  41.     unsigned short int GARBAGE_VAL = 0;
  42.     unsigned char TEMP;
  43.  
  44.     //0 to 65536 value range for unsigned 16 bit int, scalingValue of 64 yields 1024 to 0 iterations
  45.     unsigned short int SCALED_ITERATIONS = ANALOG_VALUE/ITERATION_SCALING_VALUE;
  46.  
  47.     //iteration period proportional to analog value
  48.     while(GARBAGE_VAL<SCALED_ITERATIONS)
  49.     {
  50.         GARBAGE_VAL++; //to make sure we don't optimize NOPs into nothingness
  51.     }
  52.    
  53.     if(RB0==0)//toggle PORTB on
  54.     {      
  55.         PORTB = TEMP = 0xFF; //make sure PORTB has time to process the change
  56.     }    
  57.     else //toggle PORTB off
  58.     {
  59.         PORTB = TEMP = 0x00; //make sure PORTB has time to process the change
  60.     }
  61.  
  62.     INTF=0; //unset INTF if set
  63. }
Advertisement
Add Comment
Please, Sign In to add comment