I am trying to make a security system at my house. I am using a rid switch at door which is connected to RF tx module.
I want to send the data of door status every time it changes, but I don't want to spend power continuously so I am putting my micro controller in sleep mode. now my main problem is that in my PIC 12F675 I used below code to complete my task but I in practice 99 out of 100 times it wakes up and go to loop but one time it misses that inturupt which I can't afford to happen.
my code is as follows where
OP_TX GP0
IP_REED_SW GP3
IP_PANIC_SW GP1
OP_LED GP2
Any suggestion on my code or any other way to forcefully wake him up? any suggestions will be help full,
Thanks
Mod edit: added 'C' to code tag
I want to send the data of door status every time it changes, but I don't want to spend power continuously so I am putting my micro controller in sleep mode. now my main problem is that in my PIC 12F675 I used below code to complete my task but I in practice 99 out of 100 times it wakes up and go to loop but one time it misses that inturupt which I can't afford to happen.
my code is as follows where
OP_TX GP0
IP_REED_SW GP3
IP_PANIC_SW GP1
OP_LED GP2
C:
#include <htc.h>
#include <math.h>
#include "HardwareProfile.h"
#include "delay.h"
// Configuration Bits
__CONFIG(FOSC_INTRCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_OFF & CP_ON & CPD_ON);
const char Open = 0x31 ; //1
const char Close = 0x32 ; //2
const char First = 0x41; // A
const char Second = 0x31; // 1
const char Third = 0x38; // 8
const char Header = 0x20;
const char Stop = 0x21;
volatile unsigned char i,j;
volatile unsigned int k=0,m=0, var=0;
volatile unsigned char *data;
static bit bit_once1=0,bit_once=0;
unsigned char bit_door_open=0, bit_door_close=0;
unsigned char buff[6];
void sw_open(void);
void sw_close(void);
void InitializeSystem();
void interrupt isr()
{
if(INTCONbits.GPIF==1)
{
INTCONbits.GPIF = 0;
if((GPIObits.GP3==0)&&(bit_door_open==1))
{
sw_close();
bit_door_close = 1;
bit_door_open = 0;
}
if((GPIObits.GP3==1)&&(bit_door_close==1))
{
sw_open();
bit_door_open = 1;
bit_door_close = 0;
}
}
}
void sw_open()
{
//-------------------------------
if(IP_REED_SW==1) //on
{
for(k=0;k<10;k++)
{
DelayMs(15);
}
if(IP_REED_SW==1) //on
{
for(k=0;k<10;k++)
{
DelayMs(15);
}
if(IP_REED_SW==1) //on
{
OP_LED=1;
//---------------------------
// 1st
//------------------------------
k1 :
buff[0]=Header;
buff[1]=First; // channel 1
buff[2]=Second;
buff[3]=Third;
buff[4]=Open;
buff[5]=Stop;
data=&buff;
//------------------------
for (i=0;i<16;i++) // send preamble 16 times
{
OP_TX=1;
DelayBigUs(4890); // 5 mS (5.860ms)
OP_TX=0;
DelayBigUs(4890); // 5 mS
}
DelayBigUs(14738); // syn bit
OP_LED = 0; // For less current led is zero early
OP_TX=1; // start bit
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
for (j=0;j<6;j++)
{
for(i=0;i<8;i++)
{
if ((data[j] & 0x80) == 0x80)
{
OP_TX=1;
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
}
else
{
OP_TX=0;
DelayBigUs(4890); // 5 mS
OP_TX=1;
DelayBigUs(4890); // 5 mS
}
data[j]=data[j]<<1;
}
}
//------ send stop bit -------
OP_TX=1; // stop bit
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
for(k=0;k<30;k++)
{
DelayMs(15);
}
if(bit_once==0)
{
bit_once=1;
goto k1;
}
if(IP_REED_SW==0) //on
{
OP_LED=0;
DelayMs(15);
if(IP_REED_SW==0)
{
OP_LED=1;
bit_once=1;
}
}
bit_once=0;
OP_LED = 0;
}
}
}
}
/////////////////// SW CLOSE //////////////////////////
void sw_close()
{
//-------------------------------
if(IP_REED_SW==0) //on
{
for(k=0;k<10;k++)
{
DelayMs(15);
}
if(IP_REED_SW==0) //on
{
for(k=0;k<10;k++)
{
DelayMs(15);
}
if(IP_REED_SW==0) //on
{
OP_LED=1;
//---------------------------
// 1st
//------------------------------
k2 :
buff[0]=Header;
buff[1]=First; // channel 1
buff[2]=Second;
buff[3]=Third;
buff[4]=Close;
buff[5]=Stop;
data=&buff;
//------------------------
for(i=0;i<16;i++) // send preamble 16 times
{
OP_TX=1;
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
}
DelayBigUs(14738); // Delay 15000 uS // syn bit
OP_LED = 0; // For less current led is zero early
//-----------------
OP_TX=1; // start bit
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
//-----------------
for(j=0;j<6;j++)
{
for(i=0;i<8;i++)
{
if((data[j] & 0x80) == 0x80)
{
OP_TX=1;
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
}
else
{
OP_TX=0;
DelayBigUs(4890); // 5 mS
OP_TX=1;
DelayBigUs(4890); // 5 mS
}
data[j]=data[j]<<1;
}
}
//------ send stop bit -------
OP_TX=1; // stop bit
DelayBigUs(4890); // 5 mS
OP_TX=0;
DelayBigUs(4890); // 5 mS
for(k=0;k<30;k++)
{
DelayMs(15);
}
if(bit_once1==0)
{
bit_once1=1;
goto k2;
}
if(IP_REED_SW==1)
{
OP_LED=0;
DelayMs(15);
if(IP_REED_SW==1)
{
OP_LED=1;
bit_once1=1;
}
}
bit_once1=0;
OP_LED=0;
}
}
}
}
void main()
{
InitializeSystem();
bit_once=0;
bit_once1=0;
OP_LED=0;
if(IP_REED_SW==1)
{
bit_door_open = 1;
}
if(IP_REED_SW==0)
{
bit_door_close = 1;
}
while(1)
{
GPIE = 1;
SLEEP(); // Controller goes into sleep mode (To reduce current in IDLE MODE)
}
}
void InitializeSystem()
{
OPTION_REG = 0XC0;
ANSEL = 0x00; // Configure AN pins as digital I/O
INTCON = 0X88;
ADCON0 = 0X00;
CMCON = 0X07;
VRCON = 0X00;
TRISIO = 0b00001000; // Only GP3(REED SW) is input others are output
IOC = 0b00001000;
GPIO = 0X00;
Thanks
Mod edit: added 'C' to code tag
Last edited by a moderator: