pic 12f683 - Page 1
Forum section: electronics / beginners

author: derekwdempsey
author_rank: Newbie
timestamp: August 05, 2012, 08:29:08 pm
content: Hey I'm trying to get an LED to blink using a PIC 12f683. the led just stays lit and doesnt blink. any help would be appreciatedcode:#include <stdio.h>#include <stdlib.h>#include <htc.h>#include <delays.h>#define delay_ms/***/int main(int argc, char** argv) {TRISIO = 0;while(1)GPIO = 1;delay_ms(500);GPIO = 0;delay_ms(500);return (EXIT_SUCCESS);}

author: mariush
author_rank: Super Contributor
timestamp: August 05, 2012, 08:40:54 pm
content: You're missing the { } for the while .If you don't pt { } then only the first instruction will be considered in a loop... your code is actuallywhile(1) {GPIO = 1;}delay_ms(500);GPIO = 0;delay_ms(500);... meaning keep the led on forever.change towhile(1) {GPIO = 1;delay_ms(500);GPIO = 0;delay_ms(500);}

author: derekwdempsey
author_rank: Newbie
timestamp: August 05, 2012, 08:50:38 pm
content: thanks, that makes sense, but it still isnt working

author: mariush
author_rank: Super Contributor
timestamp: August 05, 2012, 08:59:32 pm
content: Test the led.Make sure you put the led the right way, if you put it the other way you probably burned it out.Use a resistor to limit the current going through the led... the pins of the PIC shouldn't output more than 20-25mA but still it's good practice.Check the datasheet of that pic and make sure you don't have to disable AD (analogue-digital converters on it) before.Make sure that pin is actually input-output and can output stuff, some PICs have several I/O and one input only (usually the mclr pin)

author: derekwdempsey
author_rank: Newbie
timestamp: August 05, 2012, 09:03:25 pm
content: the led works and is in the correct way.a resistor is being used.the pic is all set with the other stuff.i programmed it with mikroc and it worked fine but in mplab, it wont work.

author: mariush
author_rank: Super Contributor
timestamp: August 05, 2012, 09:25:51 pm
content: I'm not familiar with that PIC series, I worked mainly with high pin pic16 chips.With these pic16 chips, they use TRISA, TRISB etc ... did you check the pic12f683.h file in the hi-tech compiler include folder to see what variables and names are used?See the datasheet :http://ww1.microchip.com/downloads/en/devicedoc/41211d_.pdfSo if you say GPIO =1 , you're setting GP0 to 1 ... GP0 is the pin below VssAlternatively you can say GPIObits.GP0 = 1 to change only that bit.Otherwise... I don't know what to say... you're not setting the oscillator frequency in that code so I'm thinking maybe delay_ms doesn't work right and actually you are flipping the led on and off so fast in each loop you don't see it turning on.With Pic16 I use, I have them running at 16 Mhz, so each instruction takes 4 Hz or cycles, so there are 4 operations per uS (microsecond) (some instructions take 2 cycles)Therefore, I made my own delay procedures which are fixed for 16 Mhz:Code:in delay.h#define delay_2tcy() asm("goto $+1")#define delay_4tcy() asm("goto $+1");asm("goto $+1");#define delay_10tcy() delay_2tcy();delay_4tcy();delay_4tcy();#define delay_1us() delay_4tcy()in delay.cvoid delay_1ms(){unsigned char n = 248;do {asm("goto $+1"); asm("goto $+1"); asm("goto $+1");asm("goto $+1"); asm("goto $+1"); asm("goto $+1");asm("nop");// decrease if not zero - decfsz (1 cycle if !=0, 2 cycles otherwise)// goto loop start = 2 cycles} while(--n);// = 16 cycles x [n+1] + 1 (from last decfsz) - 2 (last goto is skipped)// 249x16+1-2 = 3983 cycles (1 = decfsz ), -2 for last goto// + 1 = 3984 cycles (assignment of 248 to n)// + 4 = 3988 cycles (function call and return)delay_2tcy(); // = 3990 cyclesdelay_10tcy(); // = 4000 cycles}// Delay up to 255 milliseconds - approximately (ms + 19 cycles).void delay_ms(unsigned char ms) {unsigned char n;while(ms--) { // time in cycles for the inner loop includingn = 249; // cycles = 19+16*ms*[1+n]// cycles = 4000*ms + 19 for 16MHz clock// cycles = 2000*ms + 19 for 8MHz clockdelay_4tcy();do {asm("goto $+1");asm("goto $+1");asm("goto $+1");asm("goto $+1");asm("goto $+1");asm("goto $+1");asm("nop");// decrease if not zero - decfsz (1 cycle if !=0, 2 cycles otherwise)// goto loop start - 2 cycles} while(--n);// = 16 cycles x [n+1] + 1 (from last decfsz)}}

author: krish2487
author_rank: Frequent Contributor
timestamp: August 05, 2012, 09:28:10 pm
content: QuoteDo you already have a delays.h file???If yes, then check if the function you call for delay i.e delay_ms() is already defined in it.If it is defined then the compiler should already throw multiple declarations/definitions errors during the compiling stage.In case the delays.h file does not have the definition of delay_ms(),You are not defining it in the main code eitherWhich means, the code is working as it is supposed to, not as you expect it to.The led is blinking too fast for you to distinguish between on and off.Hence it is staying lit.Define what you want the delay_ms() to do first..and yes, include the#define _XTAL_FREQ xxxxxxin your code too..Actually getting a book on C programming should be your priority first.PS: There is a quirk to getting the delay_ms() function running by default in Hitech C.Hitech C has the definition for _delay_us() and _delay_ms() in pic.h file, but the functions are not enabled for running in std mode.comment theQuoteencapsulating the definitions for _delay_ms() and _delay_us() and you can use the delay subroutines even in std mode.

author: mariush
author_rank: Super Contributor
timestamp: August 05, 2012, 09:46:44 pm
content: Yeah, I forgot about that...Here's the relevant quote from the hi-tech c manual :QuoteLike I said, I used my own code to get around that, by locking my functions to 16 Mhz (frequency of the internal osc in the pic16 chips I used)

author: baljemmett
author_rank: Unknown
timestamp: August 05, 2012, 11:57:08 pm
content: The #define looks massively suspicious; it's instructing the pre-processor to replace 'delay_ms' in the code with nothing, which leaves the meat of the program looking like this:Code:while(1)GPIO = 1;(500);GPIO = 0;(500);... so even after correcting the missing braces the loop will just toggle the LED at top whack (making it appear solidly lit), regardless of any delay routines that delays.h might be intended to support.

author: gooligumelec
author_rank: Contributor
timestamp: August 06, 2012, 11:36:31 am
content: Quote from: derekwdempsey on August 05, 2012, 08:29:08 pmThere are a few problems with your code, which people here have already pointed out. A big one is that "#define delay_ms".But instead of going through it in detail, I suggest looking at my tutorials athttp://www.gooligum.com.au/tutorials.htmland especially mid-range C lesson 1, which includes flashing an LED.That lesson is written for the 12F629, but the 12F683 is very similar. The only change you may need is in the __CONFIG line, but the '629 version is likely to be ok.

author: westfw
author_rank: Super Contributor
timestamp: August 06, 2012, 11:48:51 pm
content: Aside from the code you have being significantly wrong (see other posts), you are also missing the code that is usually necessary on such PICs to disable the analog functions (A-D converter, analog comparator) on pins, so that they can be used in digital mode...

author: gooligumelec
author_rank: Contributor
timestamp: August 07, 2012, 01:59:37 am
content: Actually, you can use the pins as digital outputs (to blink an LED), without disabling analog inputs.Of course, to use digital inputs, it's another story.

author: westfw
author_rank: Super Contributor
timestamp: August 07, 2012, 08:52:51 am
content: Not on a 675; I found out the hard way! :-) (I would expect the 683 to work similarly.)It's usually the comparator that's the problem, IIRC, and not on all the pins.