Getting Blinky to work

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Excellent PORTB is now changing when I push the button. Thanks for the help. Well happy. Plus I learned a bunch of stuff about how to configure ports. My If statements still aren't working though for some odd reason
 

t06afre

Joined May 11, 2009
5,934
Is it both ANSEL and ANSELH that need to be cleared for my purpose?
No in your case strictly speaking only the ANS10 bit need to be cleared. But making it a habit to clear those registers can save you a lot of problems.
Also look up the __DELAY_MS, __DELAY_US functions. But then using them. You MUST tell the compiler your clock frequency like this
Rich (BB code):
#define _XTAL_FREQ 8000000 //Using 8MHz clock speed
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
OK I just realised that the value for PORTB is all over the place, one minute its 17, the next its something else (Without receiving any stimulus). So thats why my if statement wont work. RB0 is stable though. If there a way just to check RB0 whilst ignoring the value of RB1-7?

Thanks for the info on delays,really useful
 
Last edited:

t06afre

Joined May 11, 2009
5,934
If you are only changing RB0, then you should only test that bit, as you do not know anything about the rest of PORTB. Like this
Rich (BB code):
IF(!RB0)//same as IF(RB0==0)
IF(RB0)//same as IF(RB0==1) 
//use what you like best
Then reading more than one bit, you must use a bit mask, and the bitwise AND operator Here is link http://www.cprogramming.com/tutorial/bitwise_operators.html But also Google bit twiddling for more info
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
ok great thanks I will try that now. One final problem. I am trying the delay you suggested and I am getting an error. I did put in the #define you suggested also. The error is unable to resolve identifier
 

t06afre

Joined May 11, 2009
5,934
ok great thanks I will try that now. One final problem. I am trying the delay you suggested and I am getting an error. I did put in the #define you suggested also. The error is unable to resolve identifier
Post you code just so we are sure what you are doing. A common mistake not to use TWO underscores like this __DELAY_MS(500) But making to long delays will also make an error
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Here is the code I was testing a delay in:

Rich (BB code):
/*
* File:   Lab1inC1.c
* Author: David
*
* Created on 16 June 2013, 21:22
*/


#include <xc.h>
#define _XTAL_FREQ 8000000 //Using 8MHz clock speed

void main(void){

char Blink;
char Counter;


//Configuring PortD as an Input
TRISD = 0x00;

//Blinky
    while(1){
    //__delay_ms(100)
            Blink = 0x01;
                PORTD=Blink;
                __delay_ms(1000) // request a delay in milliseconds
        Counter = 0x00;

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

                  }
         }
}
 

t06afre

Joined May 11, 2009
5,934
this code compiled just fine at my end
Rich (BB code):
/*
* File:   Lab1inC1.c
* Author: David
*
* Created on 16 June 2013, 21:22
*/

#include <xc.h>
#define _XTAL_FREQ 8000000 //Using 8MHz clock speed
void main(void){
char Blink;
char Counter;

//Configuring PortD as an Input
TRISD = 0x00;
//Blinky
while(1)
{
//__delay_ms(100)
            Blink = 0x01;
                PORTD=Blink;
                __delay_ms(1000); // request a delay in milliseconds
  Counter = 0x00;
while(Counter<7){
  Blink = Blink << 1;
                PORTD=Blink;
  Counter++ ;
  CLRWDT();
            }
      }
}
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Ah yes its the PICKIT 2. Ok so I guess that is the reason. I didnt realise you could get problems with that.

Btw my program only changes direction temporarily when I push the button. As soon as I let go it reverses direction again.

The delay works, thank you for all your help on that
 

Thread Starter

David_Baratheon

Joined Feb 10, 2012
285
Although the delay works, MPLABX still gives a red error warning. Any idea why?

Also how do I create a latch so that when i push the button the change in direction remains?
 

ErnieM

Joined Apr 24, 2011
8,377
It could be that your PIC do not support more than one breakpoint with your programmer(PICKIT 2?)
He said PIC, not PICkit. You need to check how many breakpoints your PIC supports. They take hardware to do that.

Most PICs I've used with either the PICkit 2 or 3 (and even the ICD3) support 3 breakpoints.

BTW, inside the Microchip library there is a lovely file called ":\Microchip Solutions\Microchip\Common\TimeDelay.c." There's a header file too in the \include folder.
 

t06afre

Joined May 11, 2009
5,934
He said PIC, not PICkit. You need to check how many breakpoints your PIC supports. They take hardware to do that.

Most PICs I've used with either the PICkit 2 or 3 (and even the ICD3) support 3 breakpoints.

BTW, inside the Microchip library there is a lovely file called ":\Microchip Solutions\Microchip\Common\TimeDelay.c." There's a header file too in the \include folder.
ICD3 and Real ICE units support what is named "software breakpoints" PICKIT 2 and 3 do not support this only HW breakpoints. HW breakpoints is limeted as follows
Devices
Number of Breakpoints
PIC12F/16F
1
PIC16F1xxx enhanced
3
PIC18F
1
PIC18F enhanced
3
PIC18FxxJ
3 or 5 (Note 1)
dsPIC30F
2
dsPIC33F/PIC24H/F
4
dsPIC33EP/PIC24EP
2
dsPIC33EP/PIC24EPxxxxx8xx:
3
PIC32MX
6

Note 1: There is a limitation for these devices that only 1 data capture is available.
 
Top