Getting Blinky to work

ErnieM

Joined Apr 24, 2011
8,377
Any ideas how to read data off a switch? Does it just toggle between one and zero when you push?

It either toggles between one and zero, or zero and one.

Seriously... some switches are wired to be normally high until pressed, some normally low.

Best read the schematic for the board. It's back in the appendix AFAIK.
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
I tried pausing the program, pressing the switch, and then stepping into the next line of code, and monitoring PORTA and PORTB and neither altered their value when I pushed the switch in. There must be something I need to do first???
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Here is my code. If anyone is able to help me get the LEDs change direction upon receiving a switch press it will be most helpful

/*
* File: Lab4inC.c
* Author: David
*
* Created on 18 June 2013, 20:45
*/


#include <xc.h>

void main(void)
{

//Declaring and Defining Variables

char Blink;
char Counter;

Blink = 0x01;
Counter = 0x00;

//Configuring the Input and Output Ports


PORTD = Blink;
TRISD = 0x00;
TRISB = 0x01;
TRISA = 0x01;

//Stimulus Dependant Rotating Bit
//Bit shifting left

while(1)
{
if(PORTB==0)
{
if(Counter<7)
{
Blink = Blink << 1; //
Counter++ ;
//CLRWDT();
}

else
{
Blink = 0x01; //
Counter = 0x00;
//CLRWDT();
}
}

//Bit shifting right

if(PORTB==1)
{
if(Counter>0)
{
Blink = Blink >> 1; //
Counter = Counter - 1 ;
//CLRWDT();
}

else
{
Blink = 0x80; //
Counter = 0x07;
//CLRWDT();
}
}

}
}
 

DerStrom8

Joined Feb 20, 2011
2,390
First of all, don't forget the pull-up/down resistors on the switch. otherwise you might get a floating pin.

Second, I highly recommend NOT referring to PORTA and PORTB with a decimal value. Instead, I've found it easiest to use binary: 0b00000000. Each bit signifies a pin in that register. It makes things much easier to visualize and to change. Then, all you have to do to turn on bit 0 of the PORTA register (for example) is to set PORTA=0b00000001.

Is your switch NO or NC? That is VERY important, and it's critical that you set it up correctly, or else it WILL NOT WORK AT ALL.

Regards,
Matt
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Ok thanks I will change it to binary now.

Regarding the pull up/down resistors, isnt this something hardwired to the board? Dont forget I am not doing any soldering.

Im not sure what NO and NC is but that might be important. You mentioned set up correctly but I havent dont anythign except alter the TRIS registers so far. Is there any more setting up to do?
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
One thing I dont get is that currently the value of PORTB is zero yet the If statement seems to not execute the command that comes after if(PORTB==0)
 

DerStrom8

Joined Feb 20, 2011
2,390
NO = Normally Open
NC = Normally Closed

Those are two different types of switches. This is what Ernie was saying when he said to check whether they're normally high or normally low. You NEED to look in the datasheet FIRST to make sure you understand how the switch is set up. Chances are since the switch is on PORTB, PORTB is not actually 0. If the switch is normally closed and connected to +5v (normally high), then your PORTB is actually 0b00000001.
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Hmm... This is rather confusing.

The datasheet appears to show it as normally open but then it also shows it as connecting to ground from the pin RB0, yet the actual board it is connected V+ and not GND.

But either way, nothing is changing when I push the button
 

DerStrom8

Joined Feb 20, 2011
2,390
If the button is normally open, and connects between the pin and ground, then it is active low, meaning it does something if it is 0. If it is connected between the pin and 5v, then it is active high, which means it does something if it is 1.

Also you'll need a debounce delay. Do a quick google search for "switch debouncing" and you'll understand why. Besides the debouncing delay, you'll need to "poll" the pin, which means check it periodically to see if it is set. If you check it more frequently, the debounce delay won't need to be as long.
 

ErnieM

Joined Apr 24, 2011
8,377
This is what Ernie was saying...
No, not really. Most micro switches are normally open, so it depends if it's a pull up resistor (unpushed is high) or pull down resistor (unpushed is low).

Port A has the additional complication of sharing pins with analog functions. Pins default to analog, so it needs be turned off as any pin with analog enabled will ALWAYS read as zero.

I believe Ports B & D do not have analog functions, but a check of the data sheet is always in order.

I leave it as an exercise to the OP to determine how the resistor is wired, and to check the data sheet.
 

DerStrom8

Joined Feb 20, 2011
2,390
Seriously... some switches are wired to be normally high until pressed, some normally low.
No, not really. Most micro switches are normally open, so it depends if it's a pull up resistor (unpushed is high) or pull down resistor (unpushed is low).

Port A has the additional complication of sharing pins with analog functions. Pins default to analog, so it needs be turned off as any pin with analog enabled will ALWAYS read as zero.

I believe Ports B & D do not have analog functions, but a check of the data sheet is always in order.

I leave it as an exercise to the OP to determine how the resistor is wired, and to check the data sheet.
Oops, I guess I read the bold statement incorrectly....

Good point about the A/D selection. Dawud, make sure you read your datasheet and set the ADCON (that's what they are in the 18Fs, not sure about 16Fs) register to configure the pins to digital. otherwise it will not work.
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
No, not really. Most micro switches are normally open, so it depends if it's a pull up resistor (unpushed is high) or pull down resistor (unpushed is low).

Port A has the additional complication of sharing pins with analog functions. Pins default to analog, so it needs be turned off as any pin with analog enabled will ALWAYS read as zero.

I believe Ports B & D do not have analog functions, but a check of the data sheet is always in order.

I leave it as an exercise to the OP to determine how the resistor is wired, and to check the data sheet.
I did already take a look at the diagram and it apeared to me that both RA0 and RB0 were electrically connected to the switch, and that on the other side of the switch was a connection to ground.

But either way, whether it is pull up or pull down wouldnt matter as I am not seeing a change in the value of RA0 or RB0 either way, its not changing from a one to a 0 or vice versa. It just stays static.

Is the main issue that it is currently not configured as a digital input?
 

ErnieM

Joined Apr 24, 2011
8,377
Is the main issue that it is currently not configured as a digital input?
I/O pins default to their analog functions (if they have one) and a digital input, so RB0 is an input unless you change it.

RA0 is only connected to the switch thru the +V 5 volts an ground, so the switch is not driving that pin.

With a voltmeter you should be able to read the switch voltage change no matter what RB0 is set as (input or output).

Also, I find your code nearly impossible to read. Code without white space (spaces, tabs, line feeds) to block out portions is like works without sentences or paragraphs: one big mass that hides it's content.

If your code as written looks better... DO learn to use that # button on the reply window. It sets up code tags to keep such formatting.
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Isnt RB0 supposed to be an inpit anyway? As im wanting it to read a switch??? Also Im really struggling to get the ucontroller to recognise a switch press still, please do help if you can
 

t06afre

Joined May 11, 2009
5,934
The switch on your board is connected to the RB0. That also is an analog input pin. After a power on reset. Pins with analog input functions. Will be set up as analog input. Not digital input as default. And pins set up analog input will always read "0" The RB0 pin is also ANS0 input. As long as you do not use analog input. Make it a habbit of setting ANSEL and ANSELH register to 0x0 as one of the first thing in your program. The datasheet http://ww1.microchip.com/downloads/en/DeviceDoc/41291G.pdf will give you more information. For PORTB see section 3.3
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Ok here is my program so far, for some reason if IF statement doesnt even work, PORTB starts as 0 yet it just keeps jumping between the two IF statements. Any ideas why this wont work? Also I checked ANSELH and it is already set to 1 which means its already configured as a digital input according to the data sheet in 3.3. Here is the code:

Rich (BB code):
/*
* File:   Lab4inC.c
* Author: David
*
* Created on 18 June 2013, 20:45
*/


#include <xc.h>

void main(void)
{

//Declaring and Defining Variables

char Blink;
char Counter;

Blink = 0x01;
Counter = 0x00;

//Configuring the Input and Output Ports


PORTD = Blink;
ANSELH = 0b11111111;
TRISD = 0x00;
TRISB = 0b11111111;
TRISA = 0b11111111;

//Stimulus Dependant Rotating Bit
//Bit shifting left

    while(1)
    {
    if(PORTB==0)
        {
            if(Counter<7)
            {
                Blink = Blink << 1;    //
                                PORTB=Blink;
                Counter++ ;
                //CLRWDT();
            }

            else
            {
                Blink = 0x01;    //
                                PORTB=Blink;
                Counter = 0x00;
                //CLRWDT();
            }
        }

//Bit shifting right

    if(PORTB==1)
        {
            if(Counter>0)
            {
                Blink = Blink >> 1;    //
                                PORTB=Blink;
                Counter = Counter - 1 ;
                //CLRWDT();
            }

            else
            {
                Blink = 0x80;    //
                                PORTB=Blink;
                Counter = 0x07;
                //CLRWDT();
            }
        }

    }
}
 

t06afre

Joined May 11, 2009
5,934
Bealer.....Bealer....NO!:p Read section "REGISTER 3-4: ANSELH: ANALOG SELECT HIGH REGISTER"
Analog select between analog or digital function on pins AN<13:8>, respectively.
1 = Analog input. Pin is assigned as analog input(1).​
0 = Digital I/O. Pin is assigned to port or special function.
ANSEL and ANSELH active bits are after reset/power on reset by default set to 1. You must make it a habit of CLEARING them, and then set the corresponding pins you want analog to 1
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Oh ok I was going on this bit:

Note: The ANSELH register must be initialized
to configure an analog channel as a digital
input. Pins configured as analog inputs
will read ‘0’.

Is it both ANSEL and ANSELH that need to be cleared for my purpose?
 
Top