Getting Blinky to work

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
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: David Baratheon
*
* 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();

}
}
}
 
Last edited:

DerStrom8

Joined Feb 20, 2011
2,390
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:

Rich (BB code):
#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
 

ErnieM

Joined Apr 24, 2011
8,415
You're close. Add two things:

Init the direction register TRISD:
Rich (BB code):
    TRISD =  0x00;    // All port D pins outputs
Then to get "the variable so that it affects the LEDs" TELL it to do so:
Rich (BB code):
after:  
    Counter = 0x00;
add: 
    PORTD = Counter;
 

DerStrom8

Joined Feb 20, 2011
2,390
You're close. Add two things:

Init the direction register TRISD:
Rich (BB code):
    TRISD =  0x00;    // All port D pins outputs
Then to get "the variable so that it affects the LEDs" TELL it to do so:
Rich (BB code):
after:  
    Counter = 0x00;
add: 
    PORTD = Counter;

Or you can do that :D:D:D

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

ErnieM

Joined Apr 24, 2011
8,415
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.
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
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?
 

DerStrom8

Joined Feb 20, 2011
2,390
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?
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.
 

ErnieM

Joined Apr 24, 2011
8,415
David post: 619322 said:
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?
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

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?
No need to do that when using a compiler. The compiler will set that for you.
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
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?
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
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
 

DerStrom8

Joined Feb 20, 2011
2,390
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.
 

DerStrom8

Joined Feb 20, 2011
2,390
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
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:
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
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
 

DerStrom8

Joined Feb 20, 2011
2,390
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
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
 

DerStrom8

Joined Feb 20, 2011
2,390
Any ideas how to read data off a switch? Does it just toggle between one and zero when you push?
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".
 
Top