Problem with PIC18F1220 (Delay_ms function)

Discussion in 'Embedded Systems and Microcontrollers' started by quangtien, Nov 7, 2010.

  1. quangtien

    Thread Starter New Member

    Mar 2, 2009
    2
    0
    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.

    Code ( (Unknown Language)):
    1.  
    2. //my blinking LED code is posted here//
    3. void main() {
    4.   PORTA = 0;
    5.   TRISA = 0;             // PORTA is output
    6.  
    7.   do {
    8.     PORTA = 1;      // Bitwise negation of pins on PORTA
    9.     Delay_ms(1000);      // 1 second delay
    10.     PORTA = 0;
    11.     Delay_ms(1000);
    12.   } while(1);            // endless loop
    13. }//~!
    14.  
    Could you please give me any suggestions to fix this problem?
    Thanx alot!
    Quang Tien
     
    Last edited by a moderator: Nov 7, 2010
  2. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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: Nov 7, 2010
  3. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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?
     
  4. quangtien

    Thread Starter New Member

    Mar 2, 2009
    2
    0
    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.
     
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    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.
     
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015
    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.
     
  7. spinnaker

    AAC Fanatic!

    Oct 29, 2009
    4,887
    1,015

    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.
     
  8. Tahmid

    Active Member

    Jul 2, 2008
    344
    25
    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:
    Code ( (Unknown Language)):
    1.    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.
     
  9. nico.boshoff

    New Member

    Jul 3, 2015
    3
    0
    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.
     
  10. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,392
    1,606
    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!
     
  11. nico.boshoff

    New Member

    Jul 3, 2015
    3
    0
     
  12. nico.boshoff

    New Member

    Jul 3, 2015
    3
    0
    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.
     
  13. GopherT

    AAC Fanatic!

    Nov 23, 2012
    6,059
    3,822
    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.
     
Loading...