Thread: programming a pic18f45k20

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    14

    programming a pic18f45k20

    Hi all,

    I have a simple question but I have tried for days to resolve my issue. All I wish to do is set a pin of my pic18f45k20 high, have a short delay,then set it to go low. I am directly programming a pic18f45k20 chip on a breadboard.

    The pin goes to a high state as desired but it never returns low after the delay. I have attached the code. It is so simple but im really confused! It also appears to work in simulation mode, I have also tried more than one pic. Many thanks for help!
    Attached Files Attached Files

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You have a "delay1s()" function that is not defined in your code. Do you have the code associated with "delay.h" and/or "delays.h" (wherever this function resides)? Did you write the source for these headers, or were they provided for you?

    A few other things:

    - The code in your "main()" function should be in a loop - e.g. "while(1)"
    - Try not writing directly to the port, but instead to the output latch register (see about "LATC" on page 127 of the data sheet)
    Last edited by Matticus; 03-03-2014 at 01:29 PM. Reason: added more information

  3. #3
    Registered User
    Join Date
    Jan 2014
    Posts
    14
    Thanks for the reply! delay1s() is defined in a delay.c which associated to delay.h and delays.h. These header files are pre written and have been given to me.

    At this stage I only want it to execute the code once in other words, I want the pin to go high to low only once. This is why I have not used a while loop. I have tried using the latch but I get the same result.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    4,183
    If I understand this post correctly https://www.microchip.com/forums/m701533-print.aspx
    You are having read-modify-write problem.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Still, you should have such a loop in your code.

    I suspect that the device might be rebooting when falling off of "main()" - if so, then the following might be taking place:

    You set RC5 high, delay two seconds (giving you time to measure it), set RC5 low ... and a few clock cycles later (faster than you can perceive), you start back at the beginning and set RC5 high again.

    If you only want your code to run once, try putting an empty infinite loop after your code of interest.

    Code:
    void main(void)
    {
        // code of interest
    
        while(1)
            ;
    }

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You are having read-modify-write problem.
    I believe that the latch registers (LATx) are designed to overcome this problem (these are present in many new PIC devices - originally, programmers had to implement this themselves with the use of "shadow registers").

  7. #7
    Registered User
    Join Date
    Jan 2014
    Posts
    14
    IT WORKS

    Matticus I used the loop at the end and I used the LAT. Seriously you guys have helped me so much, thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 09-11-2012, 01:03 AM
  2. Replies: 4
    Last Post: 12-11-2011, 04:25 PM
  3. small programming job VCPP / Object Oriented Programming
    By calgonite in forum Projects and Job Recruitment
    Replies: 10
    Last Post: 01-04-2006, 11:48 PM
  4. Total newb to programming here... Question about the many programming languages. Ty!
    By tsubotakid1 in forum A Brief History of Cprogramming.com
    Replies: 11
    Last Post: 10-05-2003, 10:32 AM