1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

New using PIC18F4520 with C Programming

Discussion in 'Microcontrollers' started by mrfunkyjay, Apr 22, 2008.

  1. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    These codes are just executed once, you should have a while (1) in the main for looping, all the initialize setting (eg TRIS or disable ADC) shoule be before the while loop. Like this:
    Code (text):

    void main(void)

    {    
        /*Settings*/
        TRISD=0b00000000;
        TRISA=0b00010000;
        ADCON1=7;
        TRISB=0b00000001;
        while (1)
        {
             //..... button is checked here, this is called polling
        }
    }
    and also, you need delay for switch debounce
     
  2. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada
    Yes 2.5 is new, very nice LA + I/O tool and a standalone mode for PICkit2 (or clones with the EEPROMs)
     
  3. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Oh, I'm still in v2.4 :p Got to change it soon.

    *EDIT: Yeah just got it installed.
    When I used v2.4, with my dsPIC33FJ12GP201, it showed: PIC24 Configuration, Device: dsPIC33FJ12GP201.... and now with v2.5, it shows dsPIC33 Configuration :D
     
    Last edited: Apr 23, 2008
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    170
    Location:
    Toronto, Canada

    Here's a decent PDF on switch debouncing.
     

    Attached Files:

  6. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    I have succeeded, the LED goes off. Thanks for your patience and care!! Love it. I am getting more excited.

    Hmm.. Now I have a set of instructions for several LEDs.

    Code (text):

    void flashing(void)

            {
            LATDbits.LATD1 = 1;
            Delay10KTCYx(30);
            LATDbits.LATD1 = 0;
            LATDbits.LATD3 = 1;
            Delay10KTCYx(30);
            LATDbits.LATD3 = 0;
            LATDbits.LATD5 = 1;
            Delay10KTCYx(30);
            LATDbits.LATD5 = 0;
            LATDbits.LATD4 = 1;
            Delay10KTCYx(30);
            LATDbits.LATD4 = 0;
            LATDbits.LATD5 = 1;
            Delay10KTCYx(30);
            LATDbits.LATD5 = 0;
            LATDbits.LATD3 = 1;
            Delay10KTCYx(30);
            LATDbits.LATD3 = 0;
            }
     
    I want to activate it by pressing RA4 once, but if I press the button again, it stops, let's say making all PORTD = 0;

    Care to give me clues? Thanks! :)
     
  7. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    Do you want to activate the flashing sequence for only once or keep on running until the next press of RA4?
     
  8. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Keep it running until I press again RA4 then, it Stops ;)

    I know it is rather more advance, my colleague gave me hints about Finite State Machine. Dunno exactly what it is but it is the time for me to figure it out :) care to give me also ur version (clue)???
     
  9. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    This is what I have:
    Code (text):
    void main ()
    {
        //initialize setting

        while (1)
        {
            if (PORTAbits.RA4)
            {
                //add delay for debouncing here
                while (!PORTAbits.RA4)
                {
                    flashing ();
                }
            }
            LATD = 0;
            //add delay for debouncing here
        }
    }
    Maybe there's better idea from others?
     
  10. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Following, my source code atm.

    Code (text):

    #include <p18f4520.h>
    #include <delays.h>

    #pragma config OSC = HS
    #pragma config WDT = OFF
    #pragma config LVP = OFF

    int i;
    unsigned char PBState=1; /*State of the Push Button*/
    void flashing(void); /*Declaring flashing function*/

    /*Flashing Mode*/
    void flashing(void)

    {
        for(i=0;i<2;i++)
        {
            LATDbits.LATD1 = 1;
            Delay10KTCYx(50);
            LATDbits.LATD1 = 0;
            Delay10KTCYx(50);
        }

        for(i=0;i<2;i++)
        {
            LATDbits.LATD3 = 1;
            Delay10KTCYx(50);
            LATDbits.LATD3 = 0;
            Delay10KTCYx(50);
        }

        for(i=0;i<2;i++)
        {
            LATDbits.LATD5 = 1;
            Delay10KTCYx(50);
            LATDbits.LATD5 = 0;
            Delay10KTCYx(50);
        }

        for(i=0;i<3;i++)
        {
            LATDbits.LATD4 = 1;
            Delay10KTCYx(50);
            LATDbits.LATD4 = 0;
            Delay10KTCYx(50);
        }
    }

    /*Main Function*/
    void main(void)

    {  
        /*Settings*/
        TRISD=0b00000000;
        TRISA=0b00010000;
        PORTD=0;
        while(1)
        {
            /*Push Button RA4 Function*/
            if(PORTAbits.RA4==0)
            {
                PBState=PBState!=1;
            }
                Switch (PBState)
                {
                    Case 0:
                            flashing();
                    Break;

                    Case 1:
                           
                    Break;

                    Default Break;
                }
        }
    }
     
    So far, I have from this error, syntax error at the Switch() part. Anyone can notice I made failure or something?? Thanks!!
     
  11. ramygammal

    ramygammal New Member

    Joined:
    Apr 7, 2008
    Messages:
    6
    Likes:
    0
    Location:
    giza in egypt
    pic microcontroller

    Hello
    I am new to microcontroller ,I want to learn what should i do to be able to work with Pic microcontroller,and please i want good books in pic uc.
    I am so grateful to you.
     
  12. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    I use switch-case rarely. I'm not sure whether the "Switch" is case sensitive or not. Besides, I've noticed that there's missing a colon :) ) for the default.
    What is this doing?
    Code (text):
    PBState=PBState!=1;
    I guess you're trying to toggle PBState each time RA4 is pressed. I think you can try this:
    Code (text):

    if(PORTAbits.RA4==0)
    {
        PBState += 1;

        Switch (PBState % 2)
        {
            Case 0:
            flashing();
            Break;

            default: LATD = 0;
            break;
        }
    }
    Your RA4 input is active low ya?

    BTW, what compiler are you using? Can you use _RA4 instead of PORTAbits.RA4? It is equated in C30, but I don't know about other compilers.

    Hi ramygammal,
    Start your own thread and you don't need any book. The internet has lots of info about the PIC microcontroller.
     
  13. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    First of all, ramygammal. You may start your own thread or just read through this thread as I always check it and some of the guys here helping me out every day. It is nearly up to date most of the time.

    to bananasiong, My colleague told me about this finite-state machine. I don't really understand what he is trying to say but yes you are correct, he is telling me to toggle the PBState anytime I press the button. Hmmm... I am not sure about that line also. To answer your question.

    Yes you are correct RA4 is active low.

    I am using C18 demo compiler.

    I will try your code and comment here, thank you!
     
  14. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Yes. The concept is correct, but note that PBState is declared as char with 1 assigned to it. So it is not a single bit but a byte. By toggling it, it becomes 254. Your switch-case for 0 will not work, it goes default instead. Do you get what I mean?
    In my code, I'm using modulo 2. So the result is either 0 or 1 for sure.

    Is _RA4 equated as PORTAbits.RA4 in the include file? This will make everything to be easier.
    Code (text):

    if (!_RA4)

    instead of

    if (!PORTDbits.RA4)
     
  15. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    The problem with your code is that the flashing routine takes around 8 seconds to complete so the key is read very infrequently. Once it is read and toggles the state then it immediately gets read again and toggles it back.

    You could change your code so that state zero is all off, state 1 is D1 on, 2 is D1 off, 3 is D5 on etc. Make the code execute every 10mS to provide key debounce and timing delays. All states except zero increment a counter and increment the state when time is up (count=50). The last state would go back to 1.

    Does that make sense?

    Mike.
     
  16. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    It is back to basic time!

    Oke here, I am trying to digest what you explained earlier. See if I got it correct, this time.

    One byte is actually 8 bits, namely bit 0 - 7. And you trying to say that by toggling it, it goes to 254. Well, in total of a byte, if I'm not mistaken there are 256 namely 0-255, right?

    So by doing so I am doing the wrong thing and switch command here is useless since I toggle it to the wrong address, whatever u name it. lol. Hmm...

    According to ur question, I can't find any declaration in the header file that say, it is equal to _RA4.
     
  17. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Yes, it is 8-bit. You pre-assign 1 into it, so by toggling 0b00000001, you get 0b11111110, which is 254.
    As for the _RA4, I think it's available in C30 only, haven't tried C18.

    *Please take note to the timing issue mentioned by Pommie.
     
  18. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Guys I haven't figure it out how to make my LED System Requirement:

    1. Push button pressed once, flashing mode switched on.
    2. Push the same button once again, flashing mode switched off.

    But!! I have figured out something.

    Code (text):

    #include <p18f4520.h>
    #include <delays.h>

    #pragma config OSC = HS
    #pragma config WDT = OFF
    #pragma config LVP = OFF

    int i;
    unsigned char PBState=1; /*State of the Push Button*/
    void flashing(void); /*Declaring flashing function*/

    /*Flashing Mode*/
    void flashing(void)

    {
        for(i=0;i<1;i++)
        {
            LATCbits.LATC2 = 1;
            Delay10KTCYx(10);
            LATCbits.LATC2 = 0;
            Delay10KTCYx(10);
           
            LATDbits.LATD1 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD1 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD3 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD3 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD5 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD5 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD3 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD3 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD1 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD1 = 0;
            Delay10KTCYx(10);
        }
    }

    /*Main Function*/
    void main(void)

    {  
        /*Settings*/
        ADCON1=7;
        TRISB=0b00000001;
        TRISD=0b00000000;
        TRISA=0b00010000;
        TRISC=0b00000000;
        while(1)
        {
            /*Push Button RA4 Function*/
            if(PORTAbits.RA4==0)
            {
                PBState=0;
            }
           
            if(PORTBbits.RB0==0)
            {
                PBState=1;
            }
                switch (PBState)
                {  
                    case 0:
                    flashing();
                    break;
                   
                    case 1:
                    break;
                   
                    default:
                    break;
                }
        }
    }
     
    If you look thru my code here. I am assigning another PB which is located at RB0, for sure, when I press him, it switches off the whole LED.

    Anyway, I am looking thru now and thinking abt what Pommie is trying to deliver. Thanks thanks thanks!!!

    One question for bananasiong:

    Toggling 0b00000000, becomes 0b11111111, correct? Simply take 0 to 1 and 1 to 0. Right???
     
    Last edited: Apr 25, 2008
  19. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Yes. 0b00000000 to 0b11111111, 0b01010101 to 0b10101010, etc....
     
  20. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,161
    Likes:
    340
    Location:
    Brisbane Australia
    My explanation above isn't very clear. This is what I had in mind when I wrote it,
    Code (text):

    /*Main Function*/
    void main(void)
    {    
    int Key,Previous,Count;
        /*Settings*/
        TRISD=0b00000000;
        TRISA=0b00010000;
        PORTD=0;
        Count=0;
        Key=1;            //initialise key
        PBState=0;        //and state
        while(1)
        {
            Previous=Key;           //copy of last key value.
            Key=PORTAbits.RA4;      //get new key value.
            /*Push Button RA4 Function*/
            if(Key==0 && Previous==1)   //if previously 1 and now 0 then it is new key press
            {
                if(PBState==0)
                {
                    PBState=1;
                }else{
                PBState=0;
                }
            }
            Switch (PBState)
            {
                Case 0:
                    LATD=0;            //all off
                    Count=0;           //ensure state doesn't get incremented.
                    Break;
                Case 1:
                Case 3:
                    LATDbits.LATD1 = 1;                        
                    Break;
                Case 2:
                Case 4:
                    LATDbits.LATD1 = 0;                        
                    Break;
                Case 5:
                Case 7:
                    LATDbits.LATD1 = 1;                        
                    Break;
                Case 6:
                Case 8:
                    LATDbits.LATD1 = 0;                        
                    Break;
                Default:
                    Break;
            }
            Delay10KTCYx(1);             //delay 10mS
            if(Count++==50)              //if count gets to 50
            {
                Count=00;                //reset it
                PBState++;               //and increment state
            }
            if(PBState==9)              //change this as you add states
            {
                PBState=1;              //restart flash routine
            }
        }
    }
     
    HTH

    Mike.
     
  21. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Guys I am really excited! Look! Now I have my source code exactly like what I want.

    The thing is I still don't get it. The delay, the debouncing. I need several easy-to-understand guide regarding this.

    Where to put debouncing command in my sourcecode? How does it work?

    Care to explain?? Thanks!!!

    Code (text):
    #include <p18f4520.h>
    #include <delays.h>

    #pragma config OSC = HS
    #pragma config WDT = OFF
    #pragma config LVP = OFF

    int i, key, previous;
    unsigned char PBState=1; /*State of the Push Button*/
    void flashing(void); /*Declaring flashing function*/

    /*Flashing Mode*/
    void flashing(void)

    {
        for(i=0;i<1;i++)
        {
            LATCbits.LATC2 = 1;
            Delay10KTCYx(10);
            LATCbits.LATC2 = 0;
            Delay10KTCYx(10);
           
            LATDbits.LATD1 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD1 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD3 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD3 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD5 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD5 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD3 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD3 = 0;
            Delay10KTCYx(10);

            LATDbits.LATD1 = 1;
            Delay10KTCYx(10);
            LATDbits.LATD1 = 0;
            Delay10KTCYx(10);
        }
    }

    /*Main Function*/
    void main(void)

    {  
        /*Settings*/
        ADCON1=7;
        TRISB=0b00000001;
        TRISD=0b00000000;
        TRISA=0b00010000;
        TRISC=0b00000000;
       
        /*Initializing*/
        key=0;
        PBState=1;

        while(1)
        {
            previous=key; /*Copy the last value*/
            key=PORTAbits.RA4; /*Assigning new value*/
            Delay10KTCYx(1); /*Delay*/

            /*Push Button RA4 Function ON Button*/
            if(key==0 && previous==1) /*Getting information*/
            {
                Delay10KTCYx(1); /*Delay again*/
                if(PBState==0) /*Changing the PBState*/
                {
                    PBState=1;
                }
                else
                {
                    PBState=0;
                }
            }

                switch (PBState) /*Switch function for cases*/
                {  
                    case 0:
                    flashing(); /*If case 0, run the flashing()*/
                    break;
                   
                    case 1: /*If case 1, do nothing*/
                    break;
                   
                    default:
                    break;
                }
        }
    }
     

Share This Page