Jump to content

Delays On 18f


Recommended Posts

Hi, The following code is intended to populate EEPROM to confirm that the PIC is working.

 

When I use delay_ms(1) at 8MHz it populates the first three bytes of EEPROM as intended.

When I use delay_s(1) at 8MHz EEPROM does not get populated.

When I change the clock to 4MHz delay_s(1) EEPROM gets populated.

 

So there appears to be a problem with delay_s(1) at 8MHz. I'm using an 18F2523 with IDE 6.81 on XP.

Thanks, David.

#include <system.h>

//Core configuration (rest left at default):
#pragma DATA 0x300001, 0b1000		//Config1H: Internal oscillator with IO on RA6 and 7
#pragma DATA 0x300006, 0b10000001	//Config4L: Debug, Extended instructions and low voltage programming all off

#pragma CLOCK_FREQ 8000000			//8MHz

void eeprom_write()				//Function to write to EEPROM flash memory
{
eecon1.2 = 1;				//WREN: enable writing to memory
intcon.7 = 0;				//Disable all interrupts while writing to memory
pir2.4 = 0;					//EEIF: Clear this bit which gets set after each write

eecon2 = 0x55;				//Required before writing to memory
eecon2 = 0xAA;				//Required before writing to memory
eecon1.1 = 1;				//WR: Writes data eedata to address eeadr

while(eecon1.1==1);			//Wait for write to complete (becomes 0 when complete)
eecon1.2 = 0;				//WREN: Disable writing to memory (while not in use)
}

void main()
{
char i;

delay_s(1);

eecon1.7 = 0;				//EEPGD: Access data memory
eecon1.6 = 0;				//CFGS: Access data not config
eecon1.3 = 0;				//WRERR: clear on startup in case previously set

for(i=0; i<3; i++)			//Test to ensure PIC is working
{
	eeadr = i;
	eedata = i+1;
	eeprom_write();
}

while(1);

}

Link to post
Share on other sites
  • 3 months later...

I am also having this problem. For 18F4431 with 10MHz crystal (w/ HS PLL for 40MHz).

 

-delay_ms(500) works as expected at 10MHz or 40MHz.

-delay_s(1) does not work. Is very erratic at 10MHz and seems extremely long at 40MHz.

 

I am changing the #pragma CLOCK_FREQ appropriately and I see no delay overhead,

unit delay or delay resolution values during the linking process.

 

Did you find a solution to this problem DavidT?

Link to post
Share on other sites
I am also having this problem. For 18F4431 with 10MHz crystal (w/ HS PLL for 40MHz).

 

-delay_ms(500) works as expected at 10MHz or 40MHz.

-delay_s(1) does not work. Is very erratic at 10MHz and seems extremely long at 40MHz.

 

I am changing the #pragma CLOCK_FREQ appropriately and I see no delay overhead,

unit delay or delay resolution values during the linking process.

 

Did you find a solution to this problem DavidT?

 

 

Your delay_ms(500) should not work as expected because it only support 1byte so to have 500ms delay you should do like this

delay_ms(250);

delay_ms(250);

Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...