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 CCS Technical Support

reset() function, PIC reset state.

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







reset() function, PIC reset state.
PostPosted: Tue Nov 04, 2008 2:48 pm     Reply with quote

Hello

I have a question about the state of the registers of the pic once reset_cpu() is called. (or reset_cpu() will be a hardware wired reset of mclr)

I can not work out whether registers keep their previous states set in the program. example:

Code:

#include<18F45K20.h>
//#opt 11                       
#fuses H4    /* Config bit. HSPLL x4 enabled */
#fuses CCP2B3   /* Config bit. Multiplex PWM pins to use B3 */ 
#DEVICE ADC=10    /* Config bit. HSPLL x4 enabled */
#priority int_timer0,int_timer3  /* CCS why of setting up interrupt priorities */     
#use delay(clock=64000000)  /* CCS Used for inbuilt functions. used for delay_X() functions */
#use rs232(baud = 115000,xmit=PIN_C6,rcv=PIN_c7)   /* CCS Used for inbuilt functions. used for printf functions */

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#use fast_io(E)


void main()
{   
      delay_ms(5000) // used to give some indication of reset to slow things down
      set_trisc_c(0x00)
      output_high(PIN_C1);  /*connected to LED1 */
      output_high(PIN_C2); /* connected to a LED2*/
      delay_ms(2000) // used to give some indication of reset to slow
      reset_cpu(); // or mclr is grounded to reset

while(1);
}




In the above code does all pins registers go back to 0 state. Therefore
the sequence will be:
1- power to PIC
2- a 5 second delay no leds are on
3- Leds come on for two seconds
4- then??? I have observed that the lights stay on even though the PIC has Reset (or to be more accurate the pic seems to reset. It seems the lights stay on.)

Is this normal, as I expected the register states to reset and go back to 0, (or what state is reset).

Secondly: If I use mclr as a reset mechanism would the states of the registers stay as they were the instant before the reset. If not, what states will they be in ?
example:



// some code
set_tris_c(0x00);
output_high(PIN_C1); // connected to LED
some one pushes reset which grounds mclr to reset

The reason is because I would like some way to reset all registers back to 0 value rather than the state they were in before the reset. In particular the user will push a button that pulls mclr to ground and the pic resets with all registers back to 0.

Sorry about the long question but getting strange results and don't know if it's sofware or hardware.

Thanks
newbie22
Guest







PostPosted: Tue Nov 04, 2008 3:04 pm     Reply with quote

Sorry

Just to let you know:

I am using a PIC18F46K20 and 45K20

ccs compiler 4.80;

thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Nov 04, 2008 3:39 pm     Reply with quote

The PIC data sheet has a table which shows the state of all PIC registers
(such as TRISC and LATC) after the various kinds of resets.
Look at this table:
Quote:
TABLE 4-4: INITIALIZATION CONDITIONS FOR ALL REGISTERS

This is on page 62 in the Acrobat reader.

Go to the Microchip data sheet finder page. Select your PIC in the
drop down box. Wait for the page to load. The data sheet link will
be on the upper left side of the page:
http://www.microchip.com/stellent/idcplg?IdcService=SS_GET_PAGE&nodeId=2046
newbie22
Guest







PostPosted: Wed Nov 05, 2008 4:29 am     Reply with quote

Thanks PCM


Just one last question and I have looked on the datasheet but cannot find it, what states are the registers when the pic is not powered then powered up???

thanks

PS: i have a application that is connected to a battery and is powered off by software, ie: a pin goes high which de latches the power, however my pic seems to keep the same register values as before the switch off, I have presumed that once the PIC has NO power then switched on then all port registers go back to 0. I am I right.
dyeatman



Joined: 06 Sep 2003
Posts: 1934
Location: Norman, OK

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 8:04 am     Reply with quote

You didn't spend much time looking where PCM told you to look. In the datasheet, the entire section 4.6 is titled RESET STATE OF REGISTERS starting on page 56 of the PDF. That section contains the info you are looking for....read the whole thing. You are looking for POR (Power ON Reset)

The answer to your question is no, all registers do not go back to zero after POR.
newbie22
Guest







PostPosted: Wed Nov 05, 2008 1:36 pm     Reply with quote

hey i put this in a wrong thread

Can someone tell me the difference between latch and port please, and what output_high() works with port or latch....

Reason is this:
Code:


#include<18F45K20.h>
                   
#fuses H4               
#use delay(clock=64000000)       

void main()
{
 output_low(PIN_B4)  // connected to LED low is switched off
 delay_ms(5000);

 output_high(PIN_B4)  // connected to LED high is switched on
 while(1);
}
 


In the above program once the pic is turned on there is an 5 sec delay then led comes on and stays on. What happens is: if we do a mclr reset (ie. ground mclr)
1- PIC resets itself and goes to initialization state
- In this state PORTB goes to uuu0 0000 state:
please note:(u) stands for unchanged.
- latch goes to uuuu uuuu (all latch is unchanged)
2-the processor resets go back to the beginning of the code
- LED SHOULD BE SWICTHED OFF AS PORTB4 is 0

My question: I am seeing that my LED is still on when I do an mclr reset. why. (If the code is reset then LED will only turn on after 5 secs due to initialization state).

Also if what should happen according to the data sheet PIC18F45K20.

Please help because I am losing it.

The main reason is it seems that the datasheet says one thing and the actual result i have observed is another, therefore the confusion. Can not tell whether its a software or hardware issue
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 2:29 pm     Reply with quote

Set your IDE to make the List file in "Symbolic" format. In MPLAB
this is done in the Project/Build Options menu.

Then compile your program and look at the .LST file. It will show
what registers the output_pin() functions are writing to. You will be
able to see if it's the Port or the Latch registers.
newbie22
Guest







PostPosted: Wed Nov 05, 2008 3:01 pm     Reply with quote

Hey PCM


looking at the disassembly i get

output_high(PIN_B4);
BSF 0xf8a, 0x4, ACCESS

(latb address is 0xf8a)

which means it is acting on the latch.

SOrry but my next question is, why, and does it make any difference between setting portb1 or latb1?? i really can not get my head around the difference.....

In addition, if portb1 is high and latb1 is low and an led is connected to pin_b1 then what will the led show. I thought it would be ON.... This goes back to the mclr reset as the portb is is uuu0 0000 and lat is uuuu uuuu (u is unchanged from last state). it appears that it acts on lat state....
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 3:34 pm     Reply with quote

I put it in symbolic format and got this. You can directly see that it's
using the LATB register.
Quote:

... output_low(PIN_B4); // connected to LED low is switched off
004C: BCF TRISB.4
004E: BCF LATB.4
... output_high(PIN_B4); // connected to LED high is switched on
005E: BCF TRISB.4
0060: BSF LATB.4


See the PIC 18F Reference manual on i/o Ports:
http://ww1.microchip.com/downloads/en/DeviceDoc/39511.pdf
It's explained on page 6.

When you write to any i/o or pin, the PIC reads the entire i/o port,
sets or clears your desired pin and then writes the whole port again.
with the result. A problem can occur during the read operation.
What if one or more of the pins are loaded down by external circuits
(such as an LED or transistor) so that the value read back is a "0" ?
If the port latch was previously set to a "1" for those pins, they will be
written back (unintentionally) as a "0". This problem affects pins
that are on the same i/o port as the pin that you are changing with
your BSF or BCF instruction. For example, you may be writing to
pin B4, but the problem described above will potentially affect pins
B0-B3 and B5-B7.

The Latch register solves this problem. The "read back" is perfomed
on the port latch register, and not on the direct pins. The port latch
is insulated from the pins by a buffer, so it's unaffected by any external
load (LED or transistor) on the pin itself. If you write a "1" to the latch,
when it's read back during the pin write operation, it will be read as a "1".
newbie22
Guest







big thanks
PostPosted: Wed Nov 05, 2008 4:20 pm     Reply with quote

thanks pcm

i think i get it so its is like a memory in that if i write a high to the latch latb4 and that will save into the buffer and then pin_b4 will be set high. So if i program output_high(PIN_B4) (bsf LAT.4)
lat will be set high, and then portb4 will be set high and therefore my led will be high...


If this is the case then this might explain some of my mclr problems, because as the initialization state is uuu0 0000 for portb and lat is uuuu uuuu, consequently as output_high(PIN_B4) sets latb4 high then once mclr has occured the led will be high as lat will keep its previous state and will not act on portb4 being 0. If so I need to ensure that in the beginning of my code i need to clear all lats to 0.


again thanks has cleared up a lot of things, still got problems but will work on them.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Nov 05, 2008 4:26 pm     Reply with quote

Just use this:

Code:
output_b(0x00);
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