Searching \ for '[PIC] help! another newbie's code question' in subject line. ()
Make payments with PayPal - it's fast, free and secure! Help us get a faster server
FAQ page: www.piclist.com/techref/microchip/begin.htm?key=pic
Search entire site for: 'help! another newbie's code question'.

Exact match. Not showing close matches.
PICList Thread
'[PIC] help! another newbie's code question'
2008\06\07@130830 by mossy173

picon face

I hate to keep coming to this list with handwringing questions about my code,
but I'm very much a newbie (this is my first pic project) and when things
don't work, i'm pretty much at a loss.  So here's my code, and here's the
problem:  GPIO ports 0, 1 and 2 control R, G and B channels (not
respectively) of a tri-color LED.  i want them each to fade on in
succession, then fade off, leaving the LED off at the end.  Instead, all
three ports immediately output high, causing the LED to appear white, and
then the first one goes to 0 before it begins to ramp up.  after they ramp
down, they afterwards immediately go back to high.  I'm at a loss, and
vaguely suspect that this is due to some obscure PIC behavior that I'm just
not familiar with.  Why won't my ports stay off?  Help!

the code:


#include <htc.h>
#define pin1 GPIO0
#define pin2 GPIO1
#define pin3 GPIO2

void rampoff(int time, int purpose);
void rampon(int time, int purpose);
void delay(int msec);

int i;


void main(void){
//        ANSEL = 0x00;
       TRISIO = 0x00;
       GPIO=0x00;

       while(1){
               
               rampon(100, 1);
               rampon(100, 2);
               rampon(100, 3);
               rampoff(100, 1);
               rampoff(100, 2);
               rampoff(100, 3);
       }


               
       

       
}

void rampon(int time, int purpose){
       if (purpose == 1){
               int x;
               int rampcounter1;
               for (rampcounter1 = 0; rampcounter1 < 40; rampcounter1++){
                       for (x = 0; x < time; x++){
                               pin1 = 0;        
                               delay(40 - rampcounter1);
                               pin1 = 1;
                               delay(rampcounter1);
                       }
               }
       }
       
       else if (purpose == 2){
               int y;
               int rampcounter2;
               for (rampcounter2 = 0; rampcounter2 < 40; rampcounter2++){
                       for (y = 0; y < time; y++){
                               pin2 = 0;        
                               delay(40 - rampcounter2);
                               pin2 = 1;
                               delay(rampcounter2);
                       }
               
               }
       }

       else if (purpose == 3){
               int z;
               int rampcounter3;
               for (rampcounter3 = 0; rampcounter3 < 40; rampcounter3++){
                       for (z = 0; z < time; z++){
                               pin3 = 0;        
                               delay(40 - rampcounter3);
                               pin3 = 1;
                               delay(rampcounter3);
                       }
               
               }
       }
}
void rampoff(int time, int purpose){
       if (purpose == 1){
               int a;
               int rampcounter4;
               for (rampcounter4 = 0; rampcounter4 < 40; rampcounter4++){
                       for (a = 0; a < time; a++){
                               pin1 = 1;
                               delay(40 - rampcounter4);
                               pin1 = 0;        
                               delay(rampcounter4);
                               
                       }
               
               }
       }
       
       else if (purpose == 2){
               int b;
               int rampcounter5;
               for (rampcounter5 = 0; rampcounter5 < 40; rampcounter5++){
                       for (b = 0; b < time; b++){
                               pin2 = 1;
                               delay(40 - rampcounter5);
                               pin2 = 0;        
                               delay(rampcounter5);
                       }
               
               }
       }

       else if (purpose == 3){
               int c;
               int rampcounter6;
               for (rampcounter6 = 0; rampcounter6 < 40; rampcounter6++){
                       for (c = 0; c < time; c++){
                               pin3 = 1;
                               delay(40 - rampcounter6);
                               pin3 = 0;        
                               delay(rampcounter6);
                       }
               
               }
       }
}



void delay(int msec){

       for (i = 0; i < msec; i++){
               NOP();
       }
}

--
View this message in context: www.nabble.com/help%21--another-newbie%27s-code-question-tp17710966p17710966.html
Sent from the PIC - [PIC] mailing list archive at Nabble.com.

2008\06\07@141819 by Bruce

flavicon
face
{Quote hidden}

Add CMCON = 0x07, and ANSEL = 0x00 to your init section to disable
analog.

Regards,

-Bruce

spam_OUTtechTakeThisOuTspamrentron.com
Reynolds Electronics




2008\06\07@152040 by Timothy J. Weber

face picon face
mossy173 wrote:
> Why won't my ports stay off?  Help!

I'm not sure... but it MIGHT be a read-modify-write problem.

These statements:

>                                pin1 = 0;        
>                                delay(40 - rampcounter1);
>                                pin1 = 1;

may end up translating into something that looks more like this:

       GPIO &= 0xFE;
       delay(...);
       GPIO |= 1;

To find out if it's RMW, do something like this instead:

       unsigned char gpio_  // shadow register
       ...
       gpio_ = 0;
       gpio = gpio_
       ...
       gpio_ &= 0xFE;  // turn off pin 1
       gpio = gpio_;
       delay(...);
       gpio_ |= 1;  // turn on pin 1
       gpio = gpio_;

Does that make sense?
--
Timothy J. Weber
http://timothyweber.org

2008\06\07@173752 by William \Chops\ Westfield

face picon face

On Jun 7, 2008, at 10:08 AM, mossy173 wrote:
> void main(void){
> //        ANSEL = 0x00;
>        TRISIO = 0x00;
>        GPIO=0x00;

If this is like a 12f675, then you (also?) need to turn off the analog
comparator peripheral before you can make all the pins be normal IO.

Here's my init from a similar LED-driving piece of code (for 12f675):

void init(void){
    /*
     * set all pins to real, program controlled outputs
     */
    ANSEL = 0x0;
    VRCON = 0;             //Turn Off Voltage Reference Peripheral
    CMCON = 0x07;          //Turn Off Comparator Peripheral
    TMR0 = 0;              //Clear Timer0
    INTCON = 0;
    OPTION = 0x80;
    TRISIO = 0b001000;
    GPIO = 0;
}

(I'm not sure ALL of that is necessary, but it didn't hurt...)

BillW

2008\06\07@180933 by Jinx

face picon face
> I hate to keep coming to this list with handwringing questions

Please don't say that. There's plenty of free skin cream here,
bottles of it

I'd go with Bruce - check ANSEL and CMCON. And how's
your banking ?

2008\06\07@190438 by mossy173
picon face

Well, I initialized ANSEL and CMCON according to the whole init() method in
one of the replies, but that didn't quite do it.  Now the LEDs turn off
after ramping on.  I've hooked up an independent LED to each of the pins to
better visualize what it's doing, and here's a stepwise program execution:
led1 fades on, then shuts off.  led2 fades on, then shuts off.  led3 fades
on, but then STAYS on.  LED1 switches on, then fades off, and stays off.
then LED2.  then LED3, which is still on, fades off.  So it seems like
having a different LED fade on after another will cause the first to shut
off, because the third one operates exactly as I wish they all would.
So...perhaps it's in the code?  I'm coming to C from java, which sort of
feels like switching to the flintstone mobile after a cadillac.

Here's the code as it is now (ignore the checkfinger stuff...I'm going to
try to implement a capacitive touch-sensing switch).

Eli



Jinx-4 wrote:
>
>> I hate to keep coming to this list with handwringing questions
>
> Please don't say that. There's plenty of free skin cream here,
> bottles of it
>
> I'd go with Bruce - check ANSEL and CMCON. And how's
> your banking ?
>
> --

2008\06\07@190541 by mossy173

picon face

Well, I initialized ANSEL and CMCON according to the whole init() method in
one of the replies, but that didn't quite do it.  Now the LEDs turn off
after ramping on.  I've hooked up an independent LED to each of the pins to
better visualize what it's doing, and here's a stepwise program execution:
led1 fades on, then shuts off.  led2 fades on, then shuts off.  led3 fades
on, but then STAYS on.  LED1 switches on, then fades off, and stays off.
then LED2.  then LED3, which is still on, fades off.  So it seems like
having a different LED fade on after another will cause the first to shut
off, because the third one operates exactly as I wish they all would.
So...perhaps it's in the code?  I'm coming to C from java, which sort of
feels like switching to the flintstone mobile after a cadillac.

Here's the code as it is now (ignore the checkfinger stuff...I'm going to
try to implement a capacitive touch-sensing switch).

#include <htc.h>
#define pin1 GPIO0
#define pin2 GPIO1
#define pin3 GPIO2

void rampoff(int time, int purpose);
void rampon(int time, int purpose);
void delay(int msec);
void checkFinger(void);
void init(void);


int i;
int contShifting;

void main(void){
       init();

       contShifting = 1;

       while(1){
       //        checkFinger(void);
               
               delay(1000);
               
               rampon(60, 1);
               rampon(60, 2);
               rampon(60, 3);
               rampoff(60, 1);
               rampoff(60, 2);
               rampoff(60, 3);  
               

               delay(1000);
       }


}

void checkFinger(void){
       GPIO3 = 1;
       
}
               
       

       
void rampon(int time, int purpose){
       if (purpose == 1){
               int x;
               int rampcounter1;
               for (rampcounter1 = 0; rampcounter1 < 40; rampcounter1++){
                       for (x = 0; x < time; x++){
                               pin1 = 0;        
                               delay(40 - rampcounter1);
                               pin1 = 1;
                               delay(rampcounter1);
                       }
               }
       }
       
       else if (purpose == 2){
               int y;
               int rampcounter2;
               for (rampcounter2 = 0; rampcounter2 < 40; rampcounter2++){
                       for (y = 0; y < time; y++){
                               pin2 = 0;        
                               delay(40 - rampcounter2);
                               pin2 = 1;
                               delay(rampcounter2);
                       }
               
               }
       }

       else if (purpose == 3){
               int z;
               int rampcounter3;
               for (rampcounter3 = 0; rampcounter3 < 40; rampcounter3++){
                       for (z = 0; z < time; z++){
                               pin3 = 0;        
                               delay(40 - rampcounter3);
                               pin3 = 1;
                               delay(rampcounter3);
                       }
               
               }
       }
}
void rampoff(int time, int purpose){
       if (purpose == 1){
               int a;
               int rampcounter4;
               for (rampcounter4 = 0; rampcounter4 < 40; rampcounter4++){
                       for (a = 0; a < time; a++){
                               pin1 = 1;
                               delay(40 - rampcounter4);
                               pin1 = 0;        
                               delay(rampcounter4);
                               
                       }
               
               }
       }
       
       else if (purpose == 2){
               int b;
               int rampcounter5;
               for (rampcounter5 = 0; rampcounter5 < 40; rampcounter5++){
                       for (b = 0; b < time; b++){
                               pin2 = 1;
                               delay(40 - rampcounter5);
                               pin2 = 0;        
                               delay(rampcounter5);
                       }
               
               }
       }

       else if (purpose == 3){
               int c;
               int rampcounter6;
               for (rampcounter6 = 0; rampcounter6 < 40; rampcounter6++){
                       for (c = 0; c < time; c++){
                               pin3 = 1;
                               delay(40 - rampcounter6);
                               pin3 = 0;        
                               delay(rampcounter6);
                       }
               
               }
       }
}


void delay(int msec){

       for (i = 0; i < msec; i++){
               NOP();
       }
}

void init(void){
    /*
     * set all pins to real, program controlled outputs
     */
    ANSEL = 0x0;
    VRCON = 0;             //Turn Off Voltage Reference Peripheral
    CMCON = 0x07;          //Turn Off Comparator Peripheral
    TMR0 = 0;              //Clear Timer0
    INTCON = 0;
    OPTION = 0x80;
    TRISIO = 0x00;
    GPIO = 0;
}

Eli



Jinx-4 wrote:
>
>> I hate to keep coming to this list with handwringing questions
>
> Please don't say that. There's plenty of free skin cream here,
> bottles of it
>
> I'd go with Bruce - check ANSEL and CMCON. And how's
> your banking ?
>
> --

2008\06\07@202122 by Bruce

flavicon
face
part 1 1403 bytes content-type:text/plain; (decoded 7bit)


>
> Well, I initialized ANSEL and CMCON according to the whole init() method in
> one of the replies, but that didn't quite do it.  Now the LEDs turn off
> after ramping on.  I've hooked up an independent LED to each of the pins to
> better visualize what it's doing, and here's a stepwise program execution:
> led1 fades on, then shuts off.  led2 fades on, then shuts off.  led3 fades
> on, but then STAYS on.  LED1 switches on, then fades off, and stays off.
> then LED2.  then LED3, which is still on, fades off.  So it seems like
> having a different LED fade on after another will cause the first to shut
> off, because the third one operates exactly as I wish they all would.
> So...perhaps it's in the code?  I'm coming to C from java, which sort of
> feels like switching to the flintstone mobile after a cadillac.
>
> Here's the code as it is now (ignore the checkfinger stuff...I'm going to
> try to implement a capacitive touch-sensing switch).
>
> Eli

I'm guessing WDT timeout or osc config problem.

Try embedding config options in your source.

__CONFIG ( UNPROTECT & WDTDIS & BOREN & PWRTEN & MCLRDIS & INTIO);

I ran your example with 3 LEDs, and it works as advertised. Fades on/off, recycles, etc,.
See attached.

Regards,

-Bruce

.....techKILLspamspam@spam@rentron.com
Reynolds Electronics


part 2 4495 bytes content-type:text/plain;
(decoded 7bit)

#include <htc.h>
#define pin1 GPIO0
#define pin2 GPIO1
#define pin3 GPIO2

__CONFIG ( UNPROTECT & WDTDIS & BOREN & PWRTEN & MCLRDIS & INTIO);

void rampoff(int time, int purpose);
void rampon(int time, int purpose);
void delay(int msec);
void checkFinger(void);
void init(void);


int i;
int contShifting;

void main(void){
       init();

       contShifting = 1;

       while(1){
       // checkFinger(void);
               
               delay(1000);
               
               rampon(60, 1);
               rampon(60, 2);
               rampon(60, 3);
               rampoff(60, 1);
               rampoff(60, 2);
               rampoff(60, 3);  
               

               delay(1000);
       }


}

void checkFinger(void){
       GPIO3 = 1;
       
}
               
       

       
void rampon(int time, int purpose){
       if (purpose == 1){
               int x;
               int rampcounter1;
               for (rampcounter1 = 0; rampcounter1 < 40; rampcounter1++){
                       for (x = 0; x < time; x++){
                               pin1 = 0;
                               delay(40 - rampcounter1);
                               pin1 = 1;
                               delay(rampcounter1);
                       }
               }
       }
       
       else if (purpose == 2){
               int y;
               int rampcounter2;
               for (rampcounter2 = 0; rampcounter2 < 40; rampcounter2++){
                       for (y = 0; y < time; y++){
                               pin2 = 0;
                               delay(40 - rampcounter2);
                               pin2 = 1;
                               delay(rampcounter2);
                       }
               
               }
       }

       else if (purpose == 3){
               int z;
               int rampcounter3;
               for (rampcounter3 = 0; rampcounter3 < 40; rampcounter3++){
                       for (z = 0; z < time; z++){
                               pin3 = 0;
                               delay(40 - rampcounter3);
                               pin3 = 1;
                               delay(rampcounter3);
                       }
               
               }
       }
}
void rampoff(int time, int purpose){
       if (purpose == 1){
               int a;
               int rampcounter4;
               for (rampcounter4 = 0; rampcounter4 < 40; rampcounter4++){
                       for (a = 0; a < time; a++){
                               pin1 = 1;
                               delay(40 - rampcounter4);
                               pin1 = 0;
                               delay(rampcounter4);
                               
                       }
               
               }
       }
       
       else if (purpose == 2){
               int b;
               int rampcounter5;
               for (rampcounter5 = 0; rampcounter5 < 40; rampcounter5++){
                       for (b = 0; b < time; b++){
                               pin2 = 1;
                               delay(40 - rampcounter5);
                               pin2 = 0;
                               delay(rampcounter5);
                       }
               
               }
       }

       else if (purpose == 3){
               int c;
               int rampcounter6;
               for (rampcounter6 = 0; rampcounter6 < 40; rampcounter6++){
                       for (c = 0; c < time; c++){
                               pin3 = 1;
                               delay(40 - rampcounter6);
                               pin3 = 0;
                               delay(rampcounter6);
                       }
               
               }
       }
}


void delay(int msec){

       for (i = 0; i < msec; i++){
               NOP();
       }
}

void init(void){
    /*
     * set all pins to real, program controlled outputs
     */
    ANSEL = 0x0;
    VRCON = 0;             //Turn Off Voltage Reference Peripheral
    CMCON = 0x07;          //Turn Off Comparator Peripheral
    TMR0 = 0;              //Clear Timer0
    INTCON = 0;
    OPTION = 0x80;
    TRISIO = 0x00;
    GPIO = 0;
}



part 3 35 bytes content-type:text/plain; charset="us-ascii"
(decoded 7bit)

2008\06\07@210027 by mossy173

picon face

Well, ok, this is the fun, rocky part of learning something new.  First, huge
thanks to Bruce, I'm immensely grateful for the energy you're putting into
helping me.  I added the config line (could you please explain what it
does?) to the file, programmed, stuck it in the circuit, and despite being
tested by someone else, it was still doing that "thing."  Then I realized
that I had the LED's between + on the batteries and the pin.  Now I have
them between the pin and ground, and it's working.  What was I doing before?

Also, how do I read input from a pin?  My compiler (hi-tech) doesn't know
what to make of input(GPIO3);

Thanks so much to everybody,
Eli



Tech-4 wrote:
{Quote hidden}

> --

2008\06\07@232805 by Bruce

flavicon
face
> Well, ok, this is the fun, rocky part of learning something new.

We all start somewhere.

>  I added the config line (could you please explain what it
> does?

Sure. Adding config settings to your source embeds them in your .HEX file.

If you don't, then you'll need to set these with your programmer before you
program the target.

Otherwise, you're accepting the un-programmed defaults (see defaults in data sheet)
at program time. I always include config options in my source file. It's really hard to
remember what you had set each time if you're using your programmer to set these
before you program the part.

UNPROTECT = code protection off
WDTDIS = watch dog timer disabled
BOREN = brown out reset enabled
PWRTEN = power up timer enabled
MCLRDIS = /MCLR reset is disable
INTIO = internal oscillator / no clock out

If you're using Hi-Tech PICC C lite, look in your pic12f6x.h file for a list of config options
available. The data sheet & Hi-Tech manual will help too.

> Then I realized that I had the LED's between + on the batteries and the pin.  Now I have
> them between the pin and ground, and it's working.  What was I doing before?

LEDs were in backwards. Of course, you could have changed your firmware to adjust
for that.

> Also, how do I read input from a pin?  My compiler (hi-tech) doesn't know
> what to make of input(GPIO3);

You should have several code examples in your samples directory. rlight.c shows how to
read individual port bits. Just change these to whatever your port pins are.

static bit      button0 @ PORTBIT(PORTB, 0); would be >>

static bit      button0 @ PORTBIT(GPIO, 0);, etc..

if (button0 == 1), etc..

Read through the examples, and get familiar with the compiler & PIC you're using. It gets fairly
easy after that.

Regards,

-Bruce

.....techKILLspamspam.....rentron.com
Reynolds Electronics

2008\06\07@234128 by Jinx

face picon face
> that I had the LED's between + on the batteries and the pin.  Now I
> have them between the pin and ground, and it's working.  What was
> I doing before?

You would have been seeing the LEDs in the opposite state to that
you intended. When LED is between B+ and pin, a logic "1" output
will put B+ on both of the LED's terminals = it won't be lit. So you've
effectively sent it a practical "0". Similarly, a logic "0" will ground the
cathode side of the LED, with B+ on the anode as before, and so it
will light, ie a practical "1". The 12F675 manual is not quite as clear
as others re the pin description diagram, which would show a low-
and high-side FET for sinking and sourcing current respectively

2008\06\09@071953 by Michael Rigby-Jones

picon face


> -----Original Message-----
> From: EraseMEpiclist-bouncesspam_OUTspamTakeThisOuTmit.edu [piclist-bouncesspamspam_OUTmit.edu] On
Behalf
> Of mossy173
> Sent: 08 June 2008 02:00
> To: @spam@piclistKILLspamspammit.edu
> Subject: Re: [PIC] help! another newbie's code question
>
>
> Well, ok, this is the fun, rocky part of learning something new.
First,
> huge
> thanks to Bruce, I'm immensely grateful for the energy you're putting
into
> helping me.  I added the config line (could you please explain what it
> does?) to the file, programmed, stuck it in the circuit, and despite
being
> tested by someone else, it was still doing that "thing."  Then I
realized
> that I had the LED's between + on the batteries and the pin.  Now I
have
> them between the pin and ground, and it's working.  What was I doing
> before?

Just to check, you do have current limiting resistors in series with the
LED's don't you?  If not that could easily explain the Read-Modify-Write
issues since you'd be pulling too much current from the port pins and
dragging the voltage down (or up depending on LED connection).

Regards

Mike

=======================================================================
This e-mail is intended for the person it is addressed to only. The
information contained in it may be confidential and/or protected by
law. If you are not the intended recipient of this message, you must
not make any use of this information, or copy or show it to any
person. Please contact us immediately to tell us that you have
received this e-mail, and return the original to us. Any use,
forwarding, printing or copying of this message is strictly prohibited.
No part of this message can be considered a request for goods or
services.
=======================================================================

More... (looser matching)
- Last day of these posts
- In 2008 , 2009 only
- Today
- New search...