Problem with PIC18F1220 (Delay_ms function)

Thread Starter

quangtien

Joined Mar 2, 2009
2
Hi friends,
I am a newbei to PIC18F1220. I have use MicroC to code for PIC18F1220. I have imported this simple code into the PIC18F1220. However, the delay_ms function does not work, my LED is not turned off.

Rich (BB code):
//my blinking LED code is posted here//
void main() {
  PORTA = 0;
  TRISA = 0;             // PORTA is output

  do {
    PORTA = 1;      // Bitwise negation of pins on PORTA
    Delay_ms(1000);      // 1 second delay
    PORTA = 0;
    Delay_ms(1000);
  } while(1);            // endless loop
}//~!
Could you please give me any suggestions to fix this problem?
Thanx alot!
Quang Tien
 
Last edited by a moderator:

t06afre

Joined May 11, 2009
5,934
Have you shown all your code or just some part of it. I can not see any setting of your configuration words. Or some include statements.
Read section 19.0 SPECIAL FEATURES OF THE CPU in the pic datasheet. Also remember that then after power-on reset. some pins on PORTA, and PORTB are by default set as analog input pins. Read the datasheet ;)
 
Last edited:

spinnaker

Joined Oct 29, 2009
7,830
Hi friends,
I am a newbei to PIC18F1220. I have use MicroC to code for PIC18F1220. I have imported this simple code into the PIC18F1220. However, the delay_ms function does not work, my LED is not turned off.

Rich (BB code):
//my blinking LED code is posted here//
void main() {
  PORTA = 0;
  TRISA = 0;             // PORTA is output

  do {
    PORTA = 1;      // Bitwise negation of pins on PORTA
    Delay_ms(1000);      // 1 second delay
    PORTA = 0;
    Delay_ms(1000);
  } while(1);            // endless loop
}//~!
Could you please give me any suggestions to fix this problem?
Thanx alot!
Quang Tien
What makes you think it is the delay?

Have you put a scope or logic probe on PORTA?

Is the LED off at first then turns on but not off again?

Is this port also an analog port?
 

Thread Starter

quangtien

Joined Mar 2, 2009
2
Thank you for your reply. I think it is the delay function coz:
1. In the do-while loop, If I put it as posted, LED first off, then on, then never off again no matter what value use for the delay_ms fuction.
2. In the do-while loop, If I put it as
PORTA =0;
Delay_ms(1000);
PORTA =1;
Delay_ms(1000);
LED will not turn on.
 

t06afre

Joined May 11, 2009
5,934
Have you tried to use Google and something simple like Delay_ms. From what I can see you have not told the compiler which speed you are running the MCU. And I no settings of the configuration word. I am 100% sure your problem improper setup, not the dealy function.
 

spinnaker

Joined Oct 29, 2009
7,830
Thank you for your reply. I think it is the delay function coz:
1. In the do-while loop, If I put it as posted, LED first off, then on, then never off again no matter what value use for the delay_ms fuction.
2. In the do-while loop, If I put it as
PORTA =0;
Delay_ms(1000);
PORTA =1;
Delay_ms(1000);
LED will not turn on.
I do not understand. Does it work in #2?

Try removing or commenting out the delay and place it in debug mode. Step through program and see if light goes off and on.
 

spinnaker

Joined Oct 29, 2009
7,830
From what I can see you have not told the compiler which speed you are running the MCU. And I no settings of the configuration word. I am 100% sure your problem improper setup, not the dealy function.

This is a good thought. It could be the delay is a lot longer than what you think it is.


You could replace the delay statements with a for loop

int i;

for (i=0 i<1000; i++);

You will have to play with the i<1000 part depending on your clock speed, it may need to be more or less.
 

Tahmid

Joined Jul 2, 2008
343
Hi,
Okay, for a start, the code is fine. There is NO PROBLEM with the delay function, that I'm 100% sure of.
Add this after main function declaration:
Rich (BB code):
   ADCON1 = 0x7F;
Then, goto Project Setup, choose 18F1220 and set the appropriate clock frequency, that you are using with the PIC on hardware/simulation. Goto Project > Edit Project. Click Default. Try it and then let us know.

PORTA has ADC inputs multiplexed to it, so you must declare it as digital IO by writing to ADCON1. You need to give the correct clock frequency and correct configuration settings. Check your clock frequency.

Hope this helps.
Tahmid.
 
hi Guys
I am totally new at pic programming.
I did have the same issue with the pic18f1220.
I solved the problem 2 ways.
first as mentioned i reduced the delay_ms(1000) to delay_ms(10) this gave me 2 to 3 seconds with the internal RC oscillator enabled.
secondly I tried the external HS oscillator 8MHZ . I added the crystal and capacitors and it worked fine with the delay_ms(1000) . the 1000 gave me the 1 second.
 

ErnieM

Joined Apr 24, 2011
8,377
Hi nico, welcome to the forums.

Did you notice this thread is some 5 years old and the person who started it (we call them TS for thread starter here) has never been seen since his 2 messages?

What I'm saying is it is best to let old threads die, especially if you did not start them.

Starting your own thread and pointing to an old thread is wonderfully acceptable.

Have fun, sees ya!
 
Hi nico, welcome to the forums.

Did you notice this thread is some 5 years old and the person who started it (we call them TS for thread starter here) has never been seen since his 2 messages?

What I'm saying is it is best to let old threads die, especially if you did not start them.

Starting your own thread and pointing to an old thread is wonderfully acceptable.

Have fun, sees ya!
 
you are so correct . but like me I googled the issue and I found this thread. but this thread did not give a proper answer so I just want to add it if the next person has the same problem.
I believe this forum will be good for me.
 

GopherT

Joined Nov 23, 2012
8,009
you are so correct . but like me I googled the issue and I found this thread. but this thread did not give a proper answer so I just want to add it if the next person has the same problem.
I believe this forum will be good for me.
And on this friendly site, you will be accused of hijacking for some odd reason and the thread will be locked as a "thank you" for your efforts.

Welcome to the site. I'm sure a mod will be here shortly.
 
Top