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

bits at startup

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



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

bits at startup
PostPosted: Wed Mar 25, 2020 5:33 pm     Reply with quote

Dear Friends,

I have PIC18F25J11 and I have an LED connected on Pin B2. I made a simple circuit, when an interrupt is triggered on B0, the led goes high. In the main routine I made the code below:
Code:
set_tris_b(0b00000001);
   enable_interrupts(INT_EXT_H2L);
   enable_interrupts(GLOBAL);


I set B0 for interrupt input and the rest are output. The problem is that when I turn on the circuit many times, B2 turns on and sometime did not light up. What could be the problem? I want it to remain off until the interrupt. I also added the code:

Code:
output_low(PIN_B2);

but still no difference. Your help is highly appreciated.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 25, 2020 5:57 pm     Reply with quote

Add the line shown in bold below:
Quote:

set_tris_b(0b00000001);
enable_interrupts(INT_EXT_H2L);
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);

Also add this fuse statement:
Code:
#fuses BROWNOUT
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

PostPosted: Wed Mar 25, 2020 6:04 pm     Reply with quote

Thanks for your reply. Can i ask you why you added those lines please?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 25, 2020 8:59 pm     Reply with quote

Quote:
set_tris_b(0b00000001);
enable_interrupts(INT_EXT_H2L);
clear_interrupt(INT_EXT);
enable_interrupts(GLOBAL);

Can i ask you why you added those lines please?

See section 9.1 in the PIC data sheet:
http://ww1.microchip.com/downloads/en/DeviceDoc/39932D.pdf
Quote:

9.1 INTCON Registers

Note:
Interrupt flag bits are set when an interrupt
condition occurs regardless of the state of
its corresponding enable bit or the Global
Interrupt Enable bit. User software should
ensure the appropriate interrupt flag bits
are clear prior to enabling an interrupt.
This feature allows for software polling.

This means you clear it before trying to use it.

Quote:

The problem is that when I turn on the circuit many times, B2 turns on and sometimes did not light up.

#fuses BROWNOUT

Can i ask you why you added those lines please?

Because the power supply for the PIC may not drop to 0 volts if you
rapidly turn the power on and off. If it doesn't drop to 0v, then the PIC
may not start up correctly. But if you enable BROWNOUT, and it will hold
the PIC in reset until the power voltage rises up to a usable level for
correct operation.
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

PostPosted: Thu Mar 26, 2020 1:37 am     Reply with quote

The compiler is telling me that the fuse is unknown when i insert #fuses BROWNOUT
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Thu Mar 26, 2020 2:05 am     Reply with quote

Your chip is one of the very few PIC's that doesn't have a BROWNOUT fuse.
On yours it is always enabled on chips where the internal voltage regulator
is enabled (the F variant). So ignore this. Very Happy
temtronic



Joined: 01 Jul 2010
Posts: 9081
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Thu Mar 26, 2020 5:44 am     Reply with quote

re:
bits at startup....

One should always read the uChip datasheet to see what bits are set/cleared or UNDEFINED at reset or startup.
RAM for instance are UNDEFINED, so any RANDOM value could be there.

Adding the preprocessor '#zero_ram' line ensures all RAM is cleared or full of zeros. If you only use a few variables, 'preset' them ( ie char a='a'; )

SFRs (Special Function Registers) , like timer or UART settings NEED to be properly initialized. Some bits of some registers may have defaults of set or cleared , others -U-, undetermined. YOU need to make sure your program configures them as YOU need. Don't trust a 'wizard' ! The wizard creates code based on someone else's idea of a 'default' probably NOT what you consdier the default should be, so it's always better to do it yourself.

Another area that can cause headaches is the actual IDE and programmer. MPLAB defaults to 'debug' mode, so it sets up the PIC for that however you need to compile in 'release' mode for the real World.The 'programmer' also has 'defaults' so be sure to check those ! You can either have the PIC main() program or the programmer control 'fuses'.
aaronik19



Joined: 25 Apr 2011
Posts: 296

View user's profile Send private message

PostPosted: Thu Mar 26, 2020 5:11 pm     Reply with quote

I really appreciate your replies, but unfortunately, the bit continued to light up during starup. This is the program. I am sorry but I used a library and some of the lines might be useless.

Code:
#include <mainpic18F25j11.h>
#use fast_io(b)
#include <FlexLCD.c>
#include <DS3231.c>  // include DS3231 driver source file

signed int16 chip_temp;
// DS3231 library variable declaration
RTC_Time *mytime, *alarm1;

#INT_EXT
void  EXT_isr(void)
{
  output_high(PIN_B2);
  clear_interrupt(INT_EXT);
}

void dow_print()
{
  lcd_gotoxy(17, 2);
  switch(mytime->dow)
  {
    case SUNDAY   :  printf(lcd_putc, "Sun");  break;
    case MONDAY   :  printf(lcd_putc, "Mon");  break;
    case TUESDAY  :  printf(lcd_putc, "Tue");  break;
    case WEDNESDAY:  printf(lcd_putc, "Wed");  break;
    case THURSDAY :  printf(lcd_putc, "Thu");  break;
    case FRIDAY   :  printf(lcd_putc, "Fri");  break;
    default       :  printf(lcd_putc, "Sat");
  }
}

void main()
{
   
   Alarm1_Disable();
   Alarm2_Disable();
   
   
   enable_interrupts(INT_EXT_H2L);
   clear_interrupt(INT_EXT);
   enable_interrupts(GLOBAL);   
   
   IntSqw_Set(OUT_INT);  // DS3231 INT/SQW pin configuration (interrupt when alarm)
   Disable_32kHZ();      // disable DS3231 32kHz output
   Alarm1_Enable();
   
   
   delay_ms(100);
   
   mytime->hours   = 13;
   mytime->minutes = 03;
   mytime->seconds = 55;
   mytime->dow     = TUESDAY;
   mytime->day     = 24;
   mytime->month   = MARCH;
   mytime->year    = 20;
   // write time and date to the RTC chip
   RTC_Set(mytime);
   delay_ms(100);  // wait a second
   
   //set up alarm
   
   alarm1->hours    = 13;
   alarm1->minutes  = 04;
   alarm1->seconds  = 00;
   Alarm1_Set(alarm1, HOURS_MINUTES_SECONDS_MATCH);
   Alarm1_IF_RESET();
   
   lcd_init();
   lcd_clear();
   
   set_tris_b(0b00000001);
   
   while(true)
   {
   
    mytime = RTC_Get();
    // print time
    lcd_gotoxy(1, 1);
    printf(lcd_putc, "T: %02u:%02u:%02u", mytime->hours, mytime->minutes, mytime->seconds);
    // print date
    dow_print();       // print week day
    lcd_gotoxy(1, 2);
    printf(lcd_putc, "D: %02u/%02u/20%02u", mytime->day, mytime->month, mytime->year);
   
    // read chip temperature
    signed int16 chip_temp = Get_Temperature();
    // print chip temperature
    lcd_gotoxy(13, 1);
    if (chip_temp < 0)  // if temperature is negative
        printf(lcd_putc, "-%02Lu.%02Lu%cC", abs(chip_temp) / 100, abs(chip_temp) % 100, 223);
    else
    printf(lcd_putc, " %02Lu.%02Lu%cC", chip_temp / 100, chip_temp % 100, 223); 
   
    alarm1 = Alarm1_Get();
    lcd_gotoxy(1, 3);
    printf(lcd_putc, "Alarm: %02u:%02u:%02u", alarm1->hours, alarm1->minutes, alarm1->seconds);
     
    delay_ms(500);
    lcd_clear();
   }

}


is there a way how I can make sure that all specifics bit are all Low?
Ttelmah



Joined: 11 Mar 2010
Posts: 19195

View user's profile Send private message

PostPosted: Fri Mar 27, 2020 12:43 am     Reply with quote

This line:
set_tris_b(0b00000001);

Will then mean that every pin of portB (except B0), will be driven by what is in the LATB register.
This register wakes with 'uuuuuuuu'. Undefined values. May be 0 or 1,
depending on luck, different chips etc. etc...
If you want to set TRIS, then _you_ have to load the register first.
Easier. Just use output_b, then load the TRIS. So:
Code:

    output_b(0); //sets all the output latches to '0'
    set_tris_b(0b00000001); //now set B0 as an input


This way the pins will all drive low.
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