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. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    The way that the key read works is by keeping a copy of how the key was 10mS ago. If it was not pressed previously and it is now pressed then we have a new keypress. Next time around the previous value is pressed and the new value is pressed and so it isn't a new keypress.

    Did you try all the code. I haven't tried it but I think it should work.

    Mike.
     
  2. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    it works! indeed!! I really thank u Pommie. Let see on Monday how could I learn more about this LED driving with MCU. Good weekend for u guys!!
     
  3. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    Have you extended it to flash all the LEDs? Do you now understand it?

    Mike.
     
  4. dave

    Dave New Member

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


     
  5. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia


    Yes, Pommie. Now I have my flashing modes ON and OFF by pressing the SAME Push Button.

    Another thing is "Why my MC is now cannot programmable!?!" I clicked program several times but it wouldn't PROGRAM.
     
  6. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    Need more info, what sort of programmer?
     
  7. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    I am using MPLab ICD2. I am using P18F4520 MC. Is this device already damaged or what? Care to help? Thanks!

    Btw, what do I mean by cannot be programmable is:

    I have my last code inside, but now I want to overwrite it, it appears to remember always the last code I had programmed.
     
  8. blueroomelectronics

    blueroomelectronics Well-Known Member

    Joined:
    Jan 21, 2007
    Messages:
    12,536
    Likes:
    168
    Location:
    Toronto, Canada
    The ICD2 will erase before programming. Is it erasing, there are diagnostics in the programming tab.
    PICs can withstand thousands of reprogrammings, not likely you've worn it out.
    Have you tried another PIC, does the ICD2 program it?
     
  9. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Hmm, when I received this kit, my colleagues had worked with it several months, even 1 year maybe? So worn out thingy seems normal. Me myself I had reprogrammed it around 80-100 times. Hmm, anything else to check. Now I can't find any MC around this office, ppl are not here yet (Software).
     
  10. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Okay guys, a new problem has arrived and I need your comments regarding this!! :D :D

    I want to simplify my Flashing command. As you can see I made it by assigning LATD and delay manually.

    Now I want to make this using BIT SHIFTING method. I have tried this code:

    Code (text):
    main()
      {
        unsigned int Value=4;          /*  4 = 0000 0100 */  
        unsigned int Shift=2;

        Value = Value << Shift;        /* 16 = 0001 0000 */  

        Value <<= Shift;               /* 64 = 0100 0000 */  

        printf("%d\n", Value);         /* Prints 64      */  
      }
    But I am not sure, because all I want to have is:

    Code (text):

    /*PORTD*/
    0000 0000 /*Initial condition*/
    0000 0001 /*RD0 turned on*/
    |
    |
    |
    |
    1000 0000 /*RD7 turned on*/

    and then looping again to Initial Condition

     
    This thing runs the same way like previous. So, care to give comments? Thanks!!!
     
  11. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    Try this
    Code (text):

    char shift, value = 1;

    LATD = value << shift;
    shift ++;
    if (shift == 8)
    {
        shift = 0;
    }
    You need to add delay to make it visible. Or put it in the timer ISR is simpler.
     
    Last edited: Apr 28, 2008
    • Like Like x 1
  12. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Yes, it works...

    How about shifting backwards, from 8 >> 1??
     
  13. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Right shift instead of left shift, >>
    Also, you can preset bit 7 of value.

    If you use the whole PORTD as output, you can just shift the port without using any variable.
     
  14. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    how about the cShift++ increment?

    My logic says, I should make another cValue, (cValue2 for example). This is considered 1000 0000 or equal to =128. Then, shifting the bit backwards from 7 to 0 with the same cShift or shift interval of 1 bit.

    If cShift reaches 8, the cValue2 now begins to execute with right shift.

    But another problem is coming up, check whether I made mistake somewhere, thanks!

    Code (text):

    /*Flashing Mode*/
    void vFlashing(void)

    {
        LATD=cValue1<<cShift; /*Forward flash*/
        Delay10KTCYx(20); /*Delays*/
        cShift++; /*Increment cShift up to..*/

        if (cShift==8) /*When cShift reaches 8*/
        {
            LATD=cValue2>>cShift; /*Reverse flash*/
            Delay10KTCYx(20); /*Delays*/
            cShift++;
        }
    }
     
     
  15. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Hi,
    That won't work because the Reverse flash part is executed only when cShift is 8, with cShift++, you won't get into it anymore.

    Do you need the LED to light go and back and go and back.... ? Try this:
    Code (text):

    char shift;

    if (shift == 15)
    {
        shift = 0;
    }
    if (shift < 8)
    {
        LATD = 0x01 << shift;
    }
    else
    {
        LATD = 0x80 >> (shift - 7);
    }
    shift ++;
    and you'll add the delay.
     
    • Like Like x 1
  16. Pommie

    Pommie Well-Known Member Most Helpful Member

    Joined:
    Mar 18, 2005
    Messages:
    10,011
    Likes:
    317
    Location:
    Brisbane Australia
    Bananasiong,

    Nice bit of code. However, shouldn't it be if(shift==16) and (shift-8).

    Mike.
     
  17. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    My code gives the sequence like this (in term of bit in PORTD):
    0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 and over and over.

    If if(shift==16) and (shift-8), it will be
    0 1 2 3 4 5 6 7 7 6 5 4 3 2 1 0 and over and over.

    I think for better illusion for the bouncing, it should be like this:
    Code (text):

    char shift;

    if (shift == 15)
    {
        shift = [B]1[/B];
    }
    if (shift < 8)
    {
        LATD = 0x01 << shift;
    }
    else
    {
        LATD = 0x80 >> (shift - 7);
    }
    shift ++;
    which gives the sequence of
    0 1 2 3 4 5 6 7 6 5 4 3 2 1 0 1 2 3 4 5 6 .......
     
  18. mrfunkyjay

    mrfunkyjay New Member

    Joined:
    Mar 26, 2008
    Messages:
    123
    Likes:
    0
    Location:
    Jakarta, Indonesia
    Very nice, I will try this tommorow, hmm... I would like to know the 0x00 thingie here.

    I saw 0x01 and 0x08. What is this represents? and the shift - 7. Care to explain how could you come out with that?

    one more question will be: what is that shift == 15??? Thanks!
     
  19. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Yep. 0x01 is also 0b00000001. Since shift is incrementing, by doing 0x01 << shift, 0x01 will be left shifted according to the number in shift.

    It is the same for right shifting 0x80, which is 0b10000000. shift - 7 determines how many times 0x80 get right shifted.

    if (shift == 15) <-- this means if shift equals to 15.

    Basically shift is counting from 1 to 15, and the LED sequence is determined by shift.
     
  20. arhi

    arhi Member

    Joined:
    Apr 17, 2008
    Messages:
    887
    Likes:
    12
    Location:
    Belgrade, .rs
    I'm not ASM guy but from my old days of 8086 asm development, isn't there some rolf and rorf pandams on pic ? to rotate the register ?

    something like
    Code (text):

    ...
    x:  
     RR LATD, 1
     nop
     nop
    ...
     jmp x
    ...
     
     
  21. bananasiong

    bananasiong New Member

    Joined:
    Mar 7, 2006
    Messages:
    1,893
    Likes:
    7
    Location:
    Malaysia
    Yes, there're instructions for rotating the register in asm. But this is the discussion about C.

    For PIC16F, rrf and rlf.
    For PIC18F, rrcf, rrncf, rlcf, rlncf which have taken care of the carry bit.
     

Share This Page