Help my, please.
Testing MOD-ZIGBEE-PIR and still generate interrupt , someone advise me where I am wrong?
Environment : board MOD-ZIGBEE-PIR, programer PIC-KIT3, MPLAB X IDE v. 3.20, compiler XC8
Below it is a simple example to test interruption PIR and buttons. Interrupt buttons is OK, but comparators PIR sensor reports constantly interrupted.
Thanks!
Bystrik
*
* File: main.c
* Author: vlcab
*
* Created on Nede?a, 2016, februára 21, 11:47
*/
#include "System.h"
#include <stdio.h>
#include <stdlib.h>
#include <xc.h>
#pragma config FOSC = INTIO67 // Internal oscillator block, port function on RA6 and RA7
#pragma config FCMEN = OFF // Fail-Safe Clock Monitor disabled
#pragma config IESO = OFF // Oscillator Switchover mode disabled
#pragma config PWRT = OFF // PWRT disabled
#pragma config BOREN = OFF // Brown-out Reset disabled in hardware and software
#pragma config WDTEN = OFF // WDT is controlled by SWDTEN bit of the WDTCON register
#pragma config MCLRE = ON // RE3 input pin enabled; MCLR disabled
#pragma config PBADEN = OFF // PORTB<4:0> pins are configured as digital I/O on Reset
#pragma config CCP2MX = PORTBE // CCP2 input/output is multiplexed with RB3
#pragma config LVP = OFF // Single-Supply ICSP disabled
#pragma config XINST = OFF // Instruction set extension and Indexed Addressing mode enabled
#pragma config DEBUG = ON // Background debugger enabled, RB6 and RB7 are dedicated to In-Circuit Debug
#pragma config WDTPS = 256 // 1:256
#define RFIF INTCONbits.INT0IF
#define RFIE INTCONbits.INT0IE
#define PHY_CS LATCbits.LATC0
#define PHY_CS_TRIS TRISCbits.TRISC0
#define RF_INT_PIN PORTBbits.RB0
#define RF_INT_TRIS TRISBbits.TRISB0
#define PHY_WAKE LATCbits.LATC1
#define PHY_WAKE_TRIS TRISCbits.TRISC1
#define PHY_RESETn LATCbits.LATC2
#define PHY_RESETn_TRIS TRISCbits.TRISC2
#define SPI_SDI PORTCbits.RC4
#define SDI_TRIS TRISCbits.TRISC4
#define SPI_SDO LATCbits.LATC5
#define SDO_TRIS TRISCbits.TRISC5
#define SPI_SCK LATCbits.LATC3
#define SCK_TRIS TRISCbits.TRISC3
#define PUSH_BUTTON_1 PORTBbits.RB5
#define BUTTON_1_TRIS TRISBbits.TRISB5
#define PUSH_BUTTON_2 PORTBbits.RB4
#define BUTTON_2_TRIS TRISBbits.TRISB4
#define PUSH_BUTTON_3 PORTBbits.RB2
#define BUTTON_3_TRIS TRISBbits.TRISB2
#define PUSH_BUTTON_3_L LATBbits.LATB2
#define LED_1 LATAbits.LATA0
#define LED_1_TRIS TRISAbits.TRISA0
#define LED_2 LATAbits.LATA1
#define LED_2_TRIS TRISAbits.TRISA1
#define TMRL TMR0L
#define PIR_IN_TRIS TRISBbits.TRISB3
#define PIR_IN_PORT PORTBbits.RB3
#define PIR_IN_LAT LATBbits.LATB3
#define PIR_POWER_TRIS TRISAbits.TRISA4
#define PIR_POWER_LAT LATAbits.LATA4
#define CLOCK_FREQ 16000000
#define GetInstructionClock() (CLOCK_FREQ/4)
static volatile char pirTriggered;
static volatile char butTriggered;
void Wait( );
void main(void) {
unsigned char dummy;
BYTE oscconVal ;
WORD pirInterval = 0;
OSCCON = 0b01110010; // configure INTERNAL oscillator, 16MHz
WDTCONbits.SWDTEN = 0; //disable WDT
// disable ADC
ANSEL = 0x00;
ANSELH = 0x00;
LATB = 0x00;
TRISA = 0x00;
TRISB = 0x00;
TRISC = 0x00;
BUTTON_1_TRIS = 1; //TRISBbits.TRISB5
BUTTON_2_TRIS = 1; //TRISBbits.TRISB4
LED_1_TRIS = 0; //TRISAbits.TRISA0
LED_2_TRIS = 0; //TRISAbits.TRISA1
LED_1 = 0; //LATAbits.LATA0
LED_2 = 0; //LATAbits.LATA1
HLVDCON = 0b00001001;
INTCONbits.RBIE = 0;
INTCONbits.GIEH = 1; //+++
INTCON2bits.INTEDG0 = 0;
IOCB = 0b00110000; //IOCBbits
PIE2bits.HLVDIE = 0;
// set input as analogue pin
PIR_IN_TRIS = 1; //TRISAbits.TRISA3 B !!
PIR_IN_LAT = 0;
ANSELH |= 0x02; // C12IN2 RB3-AN9
// // tune comarator modules
CM1CON0 = 0x00;
CM2CON0 = 0x00;
// // configure, but do not enable comparators
CM1CON0 = 0b00000110; // C1VIN+ connects to C1VREF output,C12IN2- pin of C1 connects to C1VIN-
CM2CON0 = 0b00010110; //C2OUT logic is inverted,C2VIN+ connects to C2VREF, C12IN2- pin of C2 connects to C2VIN-
CM2CON1 = 0b00010000; // C1 to FVR, C2 to CVref 0b00010000
CVRCON = 0xB0; // divider of the comparator
//
// ADC
// ADCON0bits.CHS = 0b1001;
// ADCON2 = 0b10101111;
// ADCON1 = 0x00;
// ADCON0bits.ADON = 1;
// ADCON0bits.GODONE = 1;
/* PIR interrupt settings ***********************************/
PORTA = 0x00;
PORTB = 0x00;
INTCONbits.PEIE = 1;
PIR_POWER_TRIS = 0; //TRISAbits.TRISA4
PIR_POWER_LAT = 1; //LATAbits.LATA4
Wait();
// trun on comparators
CM1CON0bits.C1ON = 1;
CM2CON0bits.C2ON = 1;
Wait();
// turn on the references
CVRCONbits.CVREN = 1;
CVRCON2bits.FVREN = 1; // ref napatie
// wait for the FVR to stabilize
while(!CVRCON2bits.FVRST)
;
// interupt enable
// read output valies of the comparator to update mismatch latches
dummy = CM1CON0;
dummy = CM2CON0;
PIR2bits.C1IF = 0;
PIR2bits.C2IF = 0;
oscconVal = OSCCON;
OSCCONbits.IRCF = 0; // select the 31kHz source
while(pirInterval++ < 800);
OSCCON = oscconVal;
pirTriggered = 0;
butTriggered = 0;
LED_1 = 0;
LED_2 = 0;
// button interupt
INTCONbits.RBIE = 1;
INTCONbits.RBIF = 0;
// PIR interrupt
PIE2bits.C1IE = 1;
PIE2bits.C2IE = 1;
/* LED 2 green - cycle
* LED 1 red - interrupt
*/
while(1)
{
LED_2 = 1;
Wait( );
LED_2 = 0;
Wait( );
if (pirTriggered) // PIR interrupt
{
pirTriggered = 0;
LED_1 = 1;
Wait( );
LED_1 = 0;
Wait( );
dummy = CM1CON0;
dummy = CM2CON0;
PIE2bits.C1IE = 1;
PIE2bits.C2IE = 1;
}
if (butTriggered) // button interrupt
{
butTriggered = 0;
LED_1 = 1;
Wait( );
LED_1 = 0;
Wait( );
INTCONbits.RBIE = 1;
}
}
}
void interrupt PIR_Isr()
{
if ( ( PIR2bits.C1IF && PIE2bits.C1IE ) ||
( PIR2bits.C2IF || PIE2bits.C2IE))
{
PIE2bits.C1IE = 0;
PIE2bits.C2IE = 0;
PIR2bits.C1IF = 0;
PIR2bits.C2IF = 0;
pirTriggered = 1;
}
if (INTCONbits.RBIF)
{
INTCONbits.RBIE = 0;
INTCONbits.RBIF = 0;
butTriggered = 1;
}
return;
}
void Wait( )
{
WORD pirInterval = 0;
while(pirInterval++ < 50000);
}