CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Low voltage detect on PIC18F46K22 not working

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
borseyogesh1436



Joined: 30 Nov 2017
Posts: 29

View user's profile Send private message

Low voltage detect on PIC18F46K22 not working
PostPosted: Sat Dec 09, 2017 4:41 am     Reply with quote

hi friends i want to monitor power down, i found that code on ccs forum,

HLVDIN Pin (RA5) is connected to the supply voltage
(RA3) is connected to to RE&DE of max485


but it will display only
Quote:
Start
Low Voltage Detect module didn't stabilize


can any one solve that problem,

Code:
#include <18F46K22.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(crystal=7.372800MHz)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#bit LVDIF_BIT = getenv("SFR:PIR2").2

int8 lvd_flag;

#int_hlvd
void hlvd_isr(void)
{
lvd_flag = TRUE;
disable_interrupts(INT_HLVD);
}

int8 wait_for_lvd_to_stabilize(void)
{
int16 timeout_count;   
timeout_count = 0xFFFF;   // Many milliseconds

set_tris_a(0x10);
output_high(Pin_a3);  //rs485 tx enable

while(timeout_count--)
  {
   if(LVDIF_BIT)
   return(TRUE);
  }       
return(FALSE);
}

void main(void)
{
printf("Start\n\r");

setup_low_volt_detect(LVD_TRIGGER_BELOW | LVD_43);

LVDIF_BIT = 0;   // Clear the LVD interrupt flag
enable_interrupts(INT_HLVD);
enable_interrupts(GLOBAL);


if(wait_for_lvd_to_stabilize() == FALSE)
{
printf("Low Voltage Detect module didn't stabilize\n\r");
while(1);   // Wait here forever   
}       

while(TRUE)
  {
   if(lvd_flag == TRUE)
     {
      printf("\n\rLow voltage\n\r");
      lvd_flag = FALSE;
      LVDIF_BIT = 0;   // Clear the LVD interrupt flag
      enable_interrupts(INT_HLVD);
     }
   delay_ms(1000);
   printf("A");
  }
}
Ttelmah



Joined: 11 Mar 2010
Posts: 19215

View user's profile Send private message

PostPosted: Sat Dec 09, 2017 5:06 am     Reply with quote

Think about what you are doing in your initial wait.....

You are enabling the interrupt, with a handler. When the interrupt triggers, that handler is called, and the interrupt flag is then cleared. How can LVDIF_BIT ever be 'TRUE' in the main code?. The interrupt handler will always be called when it goes true...


You either need to not enable the interrupt (the flag will still set), and get rid of the interrupt handler, or test the 'lvd_flag' bit the handler sets.

I'd suspect the code you are basing on, did the former, and simply did not enable the interrupt till after the initial stabilise time.
borseyogesh1436



Joined: 30 Nov 2017
Posts: 29

View user's profile Send private message

[solved] Low voltage detect on PIC18F46K22 not working
PostPosted: Sat Dec 09, 2017 5:31 am     Reply with quote

Thanks for reply Ttelmah
code is working

Quote:
Low voltage < 4.3
voltage > 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
voltage > 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
Low voltage < 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
voltage > 4.3
Low voltage < 4.3
Low voltage < 4.3



Code:
#include <18F46K22.h>
#device ADC=16

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)

#use delay(crystal=7.372800MHz)

#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#bit LVDIF_BIT = getenv("SFR:PIR2").2

int8 lvd_flag;

#int_hlvd
void lvd_isr(void)
{
lvd_flag = TRUE;
disable_interrupts(INT_HLVD);
}

void main(void)
{
set_tris_a(0x10);
output_high(Pin_a3);  //rs485 tx enable

printf("Start\n\r");

setup_low_volt_detect(LVD_TRIGGER_BELOW | LVD_43);

delay_ms(500);
LVDIF_BIT = 0;   // Clear the LVD interrupt flag
enable_interrupts(INT_HLVD);
enable_interrupts(GLOBAL);


while(TRUE)
  {
   if(lvd_flag == TRUE)
     {
      printf("\n\rLow voltage < 4.3\n\r");
      lvd_flag = FALSE;
      LVDIF_BIT = 0;   // Clear the LVD interrupt flag
      enable_interrupts(INT_HLVD);
     }
     else
     {
     printf("\n\r voltage > 4.3\n\r");
     }
       delay_ms(1000);
  }

}
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group