Author Topic: Dumbest PIC program ever...  (Read 5655 times)

0 Members and 1 Guest are viewing this topic.

Offline gregoMTopic starter

  • Newbie
  • Posts: 3
Dumbest PIC program ever...
« on: June 10, 2013, 05:39:52 pm »
I've programmed ARM7 microcontrollers at school and now I'm trying to start programming very simpler PICs on my own.
I've got a 12F510 and I tried to code (in C) a program that shows on 4 leds the result of the ADC's conversion result. I tried it and as I expected it didn't work (never does on the first try) so I tried to see if I was able to get the simplest program I could think of to work.
Here it is:

#include <htc.h>

void main(){
   TRISGPIO=0;       //pins are outputs
   GPIO=0b111111;    //and are high
   while(1){               //forever
   }
}

And when I try this I can see the LEDs turn on just a blink of an eye and then they're off forever.
Why ? What have I possibly done wrong is so few lines ?
 

Offline sspj9

  • Contributor
  • Posts: 37
Re: Dumbest PIC program ever...
« Reply #1 on: June 10, 2013, 05:59:38 pm »
Try putting the line:        GPIO=0b111111;    //and are high
within the second scope.
 

Offline MikeK

  • Super Contributor
  • ***
  • Posts: 1314
  • Country: us
Re: Dumbest PIC program ever...
« Reply #2 on: June 10, 2013, 06:00:40 pm »
Does your compiler generate an ASM file?  If so, post it.
 

Offline alanb

  • Frequent Contributor
  • **
  • Posts: 344
  • Country: gb
Re: Dumbest PIC program ever...
« Reply #3 on: June 10, 2013, 06:07:26 pm »
GregoM

Are you jumping to conclusions in assuming that it is a software problem? Have you double checked that the LEDs are connected to the correct pins and that there are no other hardware  issues.
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Dumbest PIC program ever...
« Reply #4 on: June 10, 2013, 06:08:17 pm »
Try putting the line:        GPIO=0b111111;    //and are high
within the second scope.

If that works I'll be even more curious. There's no reason that should do anything.

Does your compiler generate an ASM file?  If so, post it.

Yes. If you're using XC8, give it the --ASMLIST option to do this.

Also, I know it probably won't help, but it can't hurt to double-check - but how are the LEDs connected?
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: Dumbest PIC program ever...
« Reply #5 on: June 10, 2013, 06:13:04 pm »
You need to turn off the analog pin functions in the ADC and comparator control registers.

Pins with analog functions power up in analog mode.
« Last Edit: June 10, 2013, 06:14:40 pm by Rufus »
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Dumbest PIC program ever...
« Reply #6 on: June 10, 2013, 06:15:58 pm »
You need to turn off the analog pin functions in the ADC and comparator control registers.

Pins with analog functions power up in analog mode.

:-+ Yep. I missed that. Open up the datasheet and look for "Analog Mode Selection" - they're active even when the ADC is turned off!
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline gregoMTopic starter

  • Newbie
  • Posts: 3
Re: Dumbest PIC program ever...
« Reply #7 on: June 10, 2013, 06:51:06 pm »
You need to turn off the analog pin functions in the ADC and comparator control registers.

Pins with analog functions power up in analog mode.

Oh right I didn't see the analog mode bits from the ADCON register were set by default.
Anyway I'm glad I chose the "beginners" section because I just realized the PIC was quickly getting hot if I let it run and I actually mixed up Vdd and Vss so this didn't have much chance to begin with. My pickit still can access it, does that mean the PIC's still up and running or should I avoid using that one from now on ?
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: Dumbest PIC program ever...
« Reply #8 on: June 10, 2013, 07:29:14 pm »
You need to turn off the analog pin functions in the ADC and comparator control registers.

Pins with analog functions power up in analog mode.
But he wants to use the ADC, so he needs to configure at least one pin as analog input, with ANS and ADC of ADCON0. And of course he needs to set ADON, read the result and then control the LEDs. The code he posted does just what the comment says: spins forever, without doing anything (unless he programmed some interrupt, too).
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Dumbest PIC program ever...
« Reply #9 on: June 10, 2013, 07:31:45 pm »
Yes, but how's he going to use the ADC properly if he doesn't know how it works? Basic "I can't get the GPIO to work" problems ought to be solved before tackling the ADC just to gain an understanding of the chip's architecture.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline FrankBuss

  • Supporter
  • ****
  • Posts: 2365
  • Country: de
    • Frank Buss
Re: Dumbest PIC program ever...
« Reply #10 on: June 10, 2013, 07:32:57 pm »
Right. First "Hello World" program on every microcontroller should be a blinking LED :)
So Long, and Thanks for All the Fish
Electronics, hiking, retro-computing, electronic music etc.: https://www.youtube.com/c/FrankBussProgrammer
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Dumbest PIC program ever...
« Reply #11 on: June 10, 2013, 07:36:09 pm »
My usual "hello world" is a UART echo. That way I get at least one peripheral running as well.
No longer active here - try the IRC channel if you just can't be without me :)
 

Offline gregoMTopic starter

  • Newbie
  • Posts: 3
Re: Dumbest PIC program ever...
« Reply #12 on: June 10, 2013, 07:40:10 pm »
Right. First "Hello World" program on every microcontroller should be a blinking LED :)

If I can't get a LED on, I don't think I can get it to blink, and I'm aware I'll need to put at least one pin back in analog mode for the ADC that's what I started doing but as I said it didn't work and I decided to get things as simple as possible.
 

Online mikeselectricstuff

  • Super Contributor
  • ***
  • Posts: 13746
  • Country: gb
    • Mike's Electric Stuff
Re: Dumbest PIC program ever...
« Reply #13 on: June 10, 2013, 08:15:48 pm »
You need to turn off the analog pin functions in the ADC and comparator control registers.

Pins with analog functions power up in analog mode.
that only affects input functions, not outputs

also remember that the watchdog timer default setting is ON, so you want to put
Code: [Select]
asm("clrwdt"); in your wait loop
« Last Edit: June 10, 2013, 08:17:25 pm by mikeselectricstuff »
Youtube channel:Taking wierd stuff apart. Very apart.
Mike's Electric Stuff: High voltage, vintage electronics etc.
Day Job: Mostly LEDs
 

Offline smashIt

  • Contributor
  • Posts: 30
Re: Dumbest PIC program ever...
« Reply #14 on: June 10, 2013, 08:19:48 pm »
I just realized the PIC was quickly getting hot if I let it run

that chip can only source 100mA in total (max 25mA per pin)
maybe you are asking a bit too much from it
 

Offline c4757p

  • Super Contributor
  • ***
  • Posts: 7799
  • Country: us
  • adieu
Re: Dumbest PIC program ever...
« Reply #15 on: June 10, 2013, 08:29:31 pm »
I just realized the PIC was quickly getting hot if I let it run

that chip can only source 100mA in total (max 25mA per pin)
maybe you are asking a bit too much from it

He just said he crossed power and ground...
No longer active here - try the IRC channel if you just can't be without me :)
 

Online mariush

  • Super Contributor
  • ***
  • Posts: 5022
  • Country: ro
  • .
Re: Dumbest PIC program ever...
« Reply #16 on: June 10, 2013, 08:35:32 pm »
Also make sure you're not messing with MCLR pin while you have the programmer connected to it. You might keep the chip in permanent reset or something like that.

I suggest starting with only one led, on a pin that doesn't have other functions (like oscillator or MCLR) if you can't be bothered to browse the datasheet for the IC first.
 
 

Offline Rufus

  • Super Contributor
  • ***
  • Posts: 2095
Re: Dumbest PIC program ever...
« Reply #17 on: June 10, 2013, 08:52:01 pm »
You need to turn off the analog pin functions in the ADC and comparator control registers.

Pins with analog functions power up in analog mode.
that only affects input functions, not outputs

The block diagram in the 12F510 data sheet shows enable lines from the ADC and comparator gating the tristate control of GP pins with those functions. It doesn't actually show the digital inputs being disconnected although I wouldn't be surprised if they are. Microchip are great at simplifying block diagrams enough to leave you guessing.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf