U
U
Username2018-04-13 14:51:59
Microcontrollers
Username, 2018-04-13 14:51:59

How to make an interrupt after a minute (working with PIC16F877A timer)?

I need to implement an automatic watering system, I have 5 RA outputs, each of which is responsible for opening the tap.
Each port has a trigger time and run duration. How can I apply a logical unit to the desired RA output after a given time and keep this level for a certain time.
Help, please, with the code of 1000 sites, nothing works.
I need, for example, every minute for the RA1 port to light up. So it will be easier for me to understand the logic of work.
I tried to DO SOMETHING but the compiler swears at PR1
This is code with a timer of 10 seconds. I have it not work

#include <p24fj128ga010.h> 
_CONFIG2(FCKSM_CSDCMD&OSCIOFNC_ON&POSCMOD_HS&FNOSC_PRI) 
#define SYSCLK 8000000 
#define t1 0.5 
#define PREG SYSCLK/2*t1/256 
#define DELAY 20 
#define PORTB_0 PORTBbits.RB0 
void main(void) 
{ 
   int cnt = 0; 
   AD1PCFG = 0xffff; 
   TRISB = 0xfffe; 
   PR1 = PREG; 
   TMR1 = 0; 
   T1CON = 0x8030; 
   while (1) 
   { 
     if (_T1IF == 1) 
     { 
       _T1IF = 0; 
       if (cnt == DELAY) 
       { 
         cnt = 0; 
         PORTB_0 = ~PORTB_0; 
       } 
       cnt++; 
     } 
   } 
}

5ad099aa3add2848800674.png
Project code
#define _XTAL_FREQ 8000000
 
#define RS RD2
#define EN RD3
#define D4 RD4
#define D5 RD5
#define D6 RD6
#define D7 RD7
 
#include <xc.h>
#include "lcd.h";
#include <stdio.h>
 
// BEGIN CONFIG
#pragma config FOSC = HS // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming)
#pragma config CPD = OFF // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF // Flash Program Memory Code Protection bit (Code protection off)
//END CONFIG
 
 
 
char* plants[] = {//??????  ???????? ????????
    "Ogurec",
    "Yabloko",
    "Apel'sin",
    "Pomidor",
    "Morkov'",
};
const int N_PLANTS = 5;
int plantsTime = {48};
int FlagTextChanged = 0;
 
int activeItem = 0;//???????? ????: ?????????????? ?? ??????
char num[3];
 
int plantsItems[5] = {0,3,0,0,0};//????????? ???????? ??? ??????? ????
 
void updateItem() {
    Lcd_Clear();
    Lcd_Set_Cursor(1, 1);
    sprintf(num , "%d%c", activeItem + 1, '.');
    Lcd_Write_String(num);
    
    Lcd_Set_Cursor(1, 3);
    Lcd_Write_String(plants[plantsItems[activeItem]]);
    FlagTextChanged = 1;
}
 
void interrupt isr() {
    if (INTF) {
        INTF = 0; // reset interrupt flag/
        updateItem();
    }
    if (RBIF || RBIE) {
        if (RB6) {//????????????? ???????? ? ?????
            plantsItems[activeItem] = (plantsItems[activeItem] + 1) % N_PLANTS;
            updateItem();
        }
        if (RB7) {//????????? ???? (Next item)
            activeItem = (activeItem + 1) % N_PLANTS;
            updateItem();
        }
        
        RBIF = 0;
    }
}
 
int main() {
    TRISA = 0b00001111;
    TRISB = 0B11000111;
    PORTA = 0;
    PORTB = 0;
 
    INTF = 0; //reset the external interrupt flag
    INTEDG = 1; //interrupt on the rising edge
    INTE = 1; //enable the external interrupt
    GIE = 1; //set the Global Interrupt Enable
    RBIE = 1;
    RBIF = 0;
    unsigned int a;
    TRISD = 0x00;
    Lcd_Init();
    while (1) {
        if (FlagTextChanged == 1) {
            FlagTextChanged = 0;
            __delay_ms(10000);
            __delay_ms(10000);
            __delay_ms(10000);
            __delay_ms(10000);
        }
        Lcd_Clear();
        Lcd_Set_Cursor(1, 1);
        Lcd_Write_String("Ogurec");
        Lcd_Set_Cursor(2, 1);
        Lcd_Write_String("MPLAB XC8");
        __delay_ms(2000);
 
    }
    return 0;
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dizel995, 2018-04-26
@Dizel995

Read about the work of the timers of your mk. There is nothing complicated. I would go into the timer overflow interrupt, set the value of the counting register so that the next interrupt would occur in a second, etc. In the main loop, I would check what time it is and perform this or that work.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question