Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <xc.h>
- #include <stdio.h>
- // Constants for oxygen level and water exchange
- #define OXYGEN_THRESHOLD 80 // Threshold for low oxygen level
- #define WATER_EXCHANGE_INTERVAL 5000 // Water exchange interval in milliseconds
- // Function prototypes
- void Interrupt_Init();
- void __interrupt() OxygenLevel_ISR();
- void __interrupt() WaterExchange_ISR();
- // Global variables
- volatile uint8_t oxygenLevel = 100; // Initial oxygen level
- volatile uint8_t waterExchangeFlag = 0; // Water exchange flag
- volatile uint16_t waterExchangeTimer = 0; // Timer for water exchange interval
- void main(void) {
- // Initialize Interrupts
- Interrupt_Init();
- while (1) {
- void main(void); // Main program loop
- ; // Perform other tasks
- }
- return;
- }
- void Interrupt_Init() {
- // Enable interrupts
- INTCONbits.PEIE = 1; // Peripheral Interrupt Enable bit
- INTCONbits.GIE = 1; // Global Interrupt Enable bit
- // Configure oxygen level interrupt
- IOCANbits.IOCAN4 = 1; // Interrupt on falling edge (LOW to HIGH)
- PIE0bits.IOCIE = 1; // Enable IOC (Interrupt-on-Change) interrupt
- // Configure timer interrupt for water exchange
- T0CON0bits.T0EN = 1; // Enable Timer0
- T0CON0bits.T016BIT = 0; // 8-bit mode
- T0CON0bits.T0OUTPS = 0b0000; // 1:1 postscaler
- T0CON1bits.T0CS = 0b010; // FOSC/4 as the clock source
- T0CON1bits.T0ASYNC = 0; // Synchronized operation
- T0CON1bits.T0CKPS = 0b1111; // 1:32768 prescaler
- TMR0H = 0x06; // Initialize Timer0 value (for 5 ms interrupt interval)
- TMR0L = 0x18;
- PIE0bits.TMR0IE = 1; // Enable Timer0 interrupt
- }
- void __interrupt() OxygenLevel_ISR() {
- if (IOCAFbits.IOCAF4) {
- IOCAFbits.IOCAF4 = 0; // Clear the interrupt flag
- // Read oxygen level from sensor
- // Update the oxygenLevel variable with the new value
- // Check oxygen level
- if (oxygenLevel < OXYGEN_THRESHOLD) {
- // Low oxygen level, take appropriate action (e.g., sound an alarm)
- TRISCbits.TRISC1 = 0; // Perform necessary tasks
- PORTCbits.RC1 = 1;
- }
- }
- }
- /*
- void __interrupt() WaterExchange_ISR() {
- if (PIR0bits.TMR0IF) {
- PIR0bits.TMR0IF = 0; // Clear the Timer0 interrupt flag
- // Increment water exchange timer
- waterExchangeTimer++;
- // Check if water exchange interval has elapsed
- if (waterExchangeTimer >= WATER_EXCHANGE_INTERVAL) {
- waterExchangeTimer = 0; // Reset the timer
- waterExchangeFlag = 1; // Set the water exchange flag
- }
- }
- }
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement