Advertisement
DrAungWinHtut

inter.c

Jul 12th, 2023
1,199
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.57 KB | None | 0 0
  1. #include <xc.h>
  2. #include <stdint.h>
  3.  
  4. // Constants for oxygen level and water exchange
  5. #define OXYGEN_THRESHOLD 80     // Threshold for low oxygen level
  6. #define WATER_EXCHANGE_INTERVAL 5000   // Water exchange interval in milliseconds
  7.  
  8. // Function prototypes
  9. void Interrupt_Init();
  10. void __interrupt() OxygenLevel_ISR();
  11. void __interrupt() WaterExchange_ISR();
  12.  
  13. // Global variables
  14. volatile uint8_t oxygenLevel = 100;    // Initial oxygen level
  15. volatile uint8_t waterExchangeFlag = 0;    // Water exchange flag
  16. volatile uint16_t waterExchangeTimer = 0;  // Timer for water exchange interval
  17.  
  18. void main(void) {
  19.     // Initialize Interrupts
  20.     Interrupt_Init();
  21.  
  22.     while (1) {
  23.         // Main program loop
  24.         // Perform other tasks
  25.     }
  26.  
  27.     return;
  28. }
  29.  
  30. void Interrupt_Init() {
  31.     // Enable interrupts
  32.     INTCONbits.PEIE = 1;    // Peripheral Interrupt Enable bit
  33.     INTCONbits.GIE = 1;     // Global Interrupt Enable bit
  34.  
  35.     // Configure oxygen level interrupt
  36.     IOCANbits.IOCAN4 = 1;   // Interrupt on falling edge (LOW to HIGH)
  37.     PIE0bits.IOCIE = 1;     // Enable IOC (Interrupt-on-Change) interrupt
  38.  
  39.     // Configure timer interrupt for water exchange
  40.     T0CON0bits.T0EN = 1;    // Enable Timer0
  41.     T0CON0bits.T016BIT = 0; // 8-bit mode
  42.     T0CON0bits.T0OUTPS = 0b0000;    // 1:1 postscaler
  43.     T0CON1bits.T0CS = 0b010;    // FOSC/4 as the clock source
  44.     T0CON1bits.T0ASYNC = 0;     // Synchronized operation
  45.     T0CON1bits.T0CKPS = 0b1111; // 1:32768 prescaler
  46.     TMR0H = 0x06;   // Initialize Timer0 value (for 5 ms interrupt interval)
  47.     TMR0L = 0x18;
  48.     PIE0bits.TMR0IE = 1;    // Enable Timer0 interrupt
  49. }
  50.  
  51. void __interrupt() OxygenLevel_ISR() {
  52.     if (IOCAFbits.IOCAF4) {
  53.         IOCAFbits.IOCAF4 = 0;    // Clear the interrupt flag
  54.  
  55.         // Read oxygen level from sensor
  56.         // Update the oxygenLevel variable with the new value
  57.  
  58.         // Check oxygen level
  59.         if (oxygenLevel < OXYGEN_THRESHOLD) {
  60.             // Low oxygen level, take appropriate action (e.g., sound an alarm)
  61.             // Perform necessary tasks
  62.         }
  63.     }
  64. }
  65.  
  66. void __interrupt() WaterExchange_ISR() {
  67.     if (PIR0bits.TMR0IF) {
  68.         PIR0bits.TMR0IF = 0;    // Clear the Timer0 interrupt flag
  69.  
  70.         // Increment water exchange timer
  71.         waterExchangeTimer++;
  72.  
  73.         // Check if water exchange interval has elapsed
  74.         if (waterExchangeTimer >= WATER_EXCHANGE_INTERVAL) {
  75.             waterExchangeTimer = 0;     // Reset the timer
  76.             waterExchangeFlag = 1;      // Set the water exchange flag
  77.         }
  78.     }
  79. }
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement