dalv_happy
@dalv_happy

Как сделать прерывание через минуту (работа с таймером PIC16F877A)?

Мне необходимо реализовать систему автополива, у меня есть 5 выходов RA каждый из которых отвечает за открытия крана.
У каждого порта есть время срабатывания и длительность полива. Как мне через заданное время подать на нужный выход RA логическую единицу и держать этот уровень определенное время.

Помогите, пожалуйста, с кодом 1000 сайтов прошерстил ничего не работает.
Мне нужно к примеру каждую минуту, чтобы порт RA1 загорался. Так мне будет легче понять логику работы.

Пробовал ЧТО-ТО ТАКОЕ СДЕЛАТЬ но компилятор ругается на PR1
Это код с таймером в 10 секунд. Он у меня не работает
#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
Код проекта
#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;
}

  • Вопрос задан
  • 91 просмотр
Пригласить эксперта
Ответы на вопрос 1
@Dizel995
Почитайте про работу таймеров вашего мк. Там нет ничего сложного. Я бы заходил в прерывание по переполнению таймера, выставлял значение счётного регистра так что бы следующее прерывание произошло через секунду и т.д. В основном цикле проверял бы сколько сейчас времени и выполнял бы ту или иную работу.
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы