Dumbest PIC program ever... - Page 1
Forum section: electronics / beginners

author: gregoM
author_rank: Newbie
timestamp: June 10, 2013, 05:39:52 pm
content: 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 outputsGPIO=0b111111; //and are highwhile(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 ?

author: sspj9
author_rank: Contributor
timestamp: June 10, 2013, 05:59:38 pm
content: Try putting the line: GPIO=0b111111; //and are highwithin the second scope.

author: MikeK
author_rank: Super Contributor
timestamp: June 10, 2013, 06:00:40 pm
content: Does your compiler generate an ASM file? If so, post it.

author: alanb
author_rank: Frequent Contributor
timestamp: June 10, 2013, 06:07:26 pm
content: GregoMAre 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.

author: c4757p
author_rank: Super Contributor
timestamp: June 10, 2013, 06:08:17 pm
content: Quote from: sspj9 on June 10, 2013, 05:59:38 pmIf that works I'll be even more curious. There's no reason that should do anything.Quote from: MikeK on June 10, 2013, 06:00:40 pmYes. 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?

author: Rufus
author_rank: Super Contributor
timestamp: June 10, 2013, 06:13:04 pm
content: 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.

author: c4757p
author_rank: Super Contributor
timestamp: June 10, 2013, 06:15:58 pm
content: Quote from: Rufus on June 10, 2013, 06:13:04 pmYep. I missed that. Open up the datasheet and look for "Analog Mode Selection" - they're active even when the ADC is turned off!

author: gregoM
author_rank: Newbie
timestamp: June 10, 2013, 06:51:06 pm
content: Quote from: Rufus on June 10, 2013, 06:13:04 pmOh 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 ?

author: FrankBuss
author_rank: Unknown
timestamp: June 10, 2013, 07:29:14 pm
content: Quote from: Rufus on June 10, 2013, 06:13:04 pmBut 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).

author: c4757p
author_rank: Super Contributor
timestamp: June 10, 2013, 07:31:45 pm
content: 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.

author: FrankBuss
author_rank: Unknown
timestamp: June 10, 2013, 07:32:57 pm
content: Right. First "Hello World" program on every microcontroller should be a blinking LED

author: c4757p
author_rank: Super Contributor
timestamp: June 10, 2013, 07:36:09 pm
content: My usual "hello world" is a UART echo. That way I get at least one peripheral running as well.

author: gregoM
author_rank: Newbie
timestamp: June 10, 2013, 07:40:10 pm
content: Quote from: FrankBuss on June 10, 2013, 07:32:57 pmIf 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.

author: mikeselectricstuff
author_rank: Super Contributor
timestamp: June 10, 2013, 08:15:48 pm
content: Quote from: Rufus on June 10, 2013, 06:13:04 pmthat only affects input functions, not outputsalso remember that the watchdog timer default setting is ON, so you want to putCode:asm("clrwdt");in your wait loop

author: smashIt
author_rank: Contributor
timestamp: June 10, 2013, 08:19:48 pm
content: Quote from: gregoM on June 10, 2013, 06:51:06 pmthat chip can only source 100mA in total (max 25mA per pin)maybe you are asking a bit too much from it

author: c4757p
author_rank: Super Contributor
timestamp: June 10, 2013, 08:29:31 pm
content: Quote from: smashIt on June 10, 2013, 08:19:48 pmHe just said he crossed power and ground...

author: mariush
author_rank: Super Contributor
timestamp: June 10, 2013, 08:35:32 pm
content: 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.

author: Rufus
author_rank: Super Contributor
timestamp: June 10, 2013, 08:52:01 pm
content: Quote from: mikeselectricstuff on June 10, 2013, 08:15:48 pmThe 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.