Getting Blinky to work

Discussion in 'Embedded Systems and Microcontrollers' started by Dawud Beale, Jun 16, 2013.

  1. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Hi everyone I am trying to get blinky to work on a PIC16F887 demoboard that comes with the PICKIT 2. I am using MPLABX.

    I have a variable called blink but I dont know how to "link" that variable to the LEDs. The variable is successfully shifting the bit along one each time but it isnt lighting up any LEDs. How do I alter the variable so that it affects the LEDs?

    Here is my code:

    /*
    * File: Lab1inC1.c
    * Author: Dawud Beale
    *
    * Created on 16 June 2013, 21:22
    */


    #include <xc.h>

    void main(void){

    char Blink;
    char Counter;

    while(1){
    Blink = 0x01;
    Counter = 0x00;

    while(Counter<7){
    Blink = Blink << 1; //
    Counter++ ;
    CLRWDT();

    }
    }
    }
     
  2. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
  3. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Try using a #Define at the beginning.

    I'm used to C18, so I'm not sure how different XC8 is. C18 would use a line like this at the beginning of the program:

    Code ( (Unknown Language)):
    1. #DEFINE blink LATA
    This basically states that wherever you use "blink", it replaces it with the latch for portA.

    Personally, I just set the latches directly, I don't even use a variable. It's just a waste of memory for me.

    Hope this helps.
    Regards,
    Matt
     
  4. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    You're close. Add two things:

    Init the direction register TRISD:
    Code ( (Unknown Language)):
    1.     TRISD =  0x00;    // All port D pins outputs
    2.  
    Then to get "the variable so that it affects the LEDs" TELL it to do so:
    Code ( (Unknown Language)):
    1.  
    2. after:  
    3.     Counter = 0x00;
    4. add:
    5.     PORTD = Counter;
    6.  
     
    Dawud Beale and DerStrom8 like this.
  5. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328

    Or you can do that :D:D:D

    That's true though, either way you need to set the TRIS register/s.
     
  6. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Since the PIC16F887 doesn't have the latch registers you'll have a hard time using them.

    So use the port register, and due to certain "complications" when reading these they are always best "shadowed," or another memory register used to hold the value and copied over.
     
    Dawud Beale and DerStrom8 like this.
  7. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    :facepalm: You're right. My mistake--Again, I'm used to C18 (for the PIC18 series). The idea of the Define statement is the same though.
     
  8. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Hi everyone, thanks for the input so far. So from what I gather, I need to use

    PORTD = Blink

    To basically make the ports that connect to the LEDs match the Blink variable and I need to configure PORTD as an output. Correct?

    I am trying to configure the PORTD but it doesnt recognise BSR = to alter the bank select. Any ideas what it might be instead of BSR to alter the Bank Register?
     
  9. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    You need to define blink as PORTD, not just set it equal to it. That is, unless you want to re-set PORTD to blink every single time you change it. But that's very inefficient, if you ask me.

    Yes, you need to set PORTD to output. Use the TRISD register. I don't think you need to worry about the BSR. Just set TRISD = 0b00000000 (or 0x00) to set all of the PORTD pins as output.
     
  10. mitko89

    Member

    Sep 20, 2012
    123
    19
    Try using BSEL instead of BSR. And doesn't the complier take care about bank switching when you are using a C complier?
     
    Dawud Beale likes this.
  11. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    Correct. You need to add the two lines as I have told you.

    Using "PORTD = Blink" is acceptable only for the most simple of demo programs. For anything serious you should use a separate register from the port to compute and hold the value.

    Using a #define here is prone to strange occasional hard to track errors

    No need to do that when using a compiler. The compiler will set that for you.
     
    Dawud Beale likes this.
  12. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Yes, I am pretty sure that is the case, which is why I said the OP shouldn't have to worry about it.
     
  13. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Hi everyone thank you for your suggestions. They have worked and I have the program working on the board.

    I am trying to add in a switch to change the direction of flow of the lit LEDs, does anyone know which port the switch is attached to?
     
  14. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Also what would I use to cause a delay between each cycle so I can see the LEDs blink along when I run the program? AT the moment it goes really quick so you cant see it with the naked eye unless you step through the program
     
  15. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    It doesn't matter which pin the switch is attached to, provided it can operate as an output and that you set the tris register properly to configure it as such.
     
  16. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Use the delay instructions provided by the compiler. You should be able to find them in the XC8 libraries. Just put one after each instruction you need a delay after.

    Matt

    P.S. Sorry for my horrible English today guys :rolleyes:
     
  17. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Oh I meant for the 44 pin demo board as the switch is already hard wired to a certain port. I found it on the datasheet and it says RB0 is connected to the switch
     
  18. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Oops, I had no idea. Guess I should have read the link you sent regarding your board, huh? :p

    Glad you found it though :D
     
  19. Dawud Beale

    Thread Starter Member

    Feb 10, 2012
    275
    7
    Any ideas how to read data off a switch? Does it just toggle between one and zero when you push?
     
  20. DerStrom8

    Well-Known Member

    Feb 20, 2011
    2,428
    1,328
    Yes, that's the idea. Usually anything between 0 and 2v (roughly) is read as "0" or "off", and between 3 and 5v (roughly) is read as "1" or "on". Anything in between is unstable and it's highly recommended you avoid situations where a pin is "floating".
     
Loading...