Author Topic: pic 12f683  (Read 9117 times)

0 Members and 1 Guest are viewing this topic.

Offline derekwdempseyTopic starter

  • Newbie
  • Posts: 3
pic 12f683
« on: August 05, 2012, 08:29:08 pm »
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 appreciated

code:

#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);
}
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5136
  • Country: ro
  • .
Re: pic 12f683
« Reply #1 on: August 05, 2012, 08:40:54 pm »
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 actually

   while(1)  {
        GPIO = 1;
 
  }
        delay_ms(500);
        GPIO = 0;
        delay_ms(500);

... meaning keep the led on forever.

change to

   while(1)  {
        GPIO = 1;
        delay_ms(500);
        GPIO = 0;
        delay_ms(500);
  }
 

Offline derekwdempseyTopic starter

  • Newbie
  • Posts: 3
Re: pic 12f683
« Reply #2 on: August 05, 2012, 08:50:38 pm »
thanks, that makes sense, but it still isnt working
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5136
  • Country: ro
  • .
Re: pic 12f683
« Reply #3 on: August 05, 2012, 08:59:32 pm »
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)

 

Offline derekwdempseyTopic starter

  • Newbie
  • Posts: 3
Re: pic 12f683
« Reply #4 on: August 05, 2012, 09:03:25 pm »
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.
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5136
  • Country: ro
  • .
Re: pic 12f683
« Reply #5 on: August 05, 2012, 09:25:51 pm »
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_.pdf




So if you say GPIO =1 , you're setting GP0 to 1 ... GP0 is the pin below Vss

Alternatively 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: [Select]

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.c

void 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 cycles
    delay_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 including
        n = 249;     // cycles = 19+16*ms*[1+n]
                     // cycles = 4000*ms + 19 for 16MHz clock
                     // cycles = 2000*ms + 19 for 8MHz clock
        delay_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)
    }
}

 

Offline krish2487

  • Frequent Contributor
  • **
  • Posts: 523
  • Country: dk
Re: pic 12f683
« Reply #6 on: August 05, 2012, 09:28:10 pm »
Quote
#include <delays.h>

#define delay_ms

Do 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 either

Which 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 xxxxxx

 in 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 the
Quote
#ifdef __PICCPRO__

#endif

encapsulating the definitions for _delay_ms() and _delay_us() and you can use the delay subroutines even in std mode.
 ;)
« Last Edit: August 05, 2012, 09:34:52 pm by krish2487 »
If god made us in his image,
and we are this stupid
then....
 

Offline mariush

  • Super Contributor
  • ***
  • Posts: 5136
  • Country: ro
  • .
Re: pic 12f683
« Reply #7 on: August 05, 2012, 09:46:44 pm »
Yeah, I forgot about that...

Here's the relevant quote from the hi-tech c manual :

Quote

__DELAY_MS, __DELAY_US
Synopsis
__delay_ms(x)  // request a delay in milliseconds
__delay_us(x)  // request a delay in microseconds
Description
As it is often more convenient request a delay in time-based terms rather than in cycle
counts, the macros __delay_ms(x) and __delay_us(x) are provided. These mac-
ros simply wrap around _delay(n) and convert the time based request into instruction
cycles based on the system frequency. In order to achieve this, these macros require
the prior definition of preprocessor symbol _XTAL_FREQ. This symbol should be
defined as the oscillator frequency (in Hertz) used by the system.
An error will result if these macros are used without defining oscillator frequency
symbol or if the delay period requested is too large.
See also
_delay()

_DELAY()
Synopsis
#include <htc.h>
 
void _delay(unsigned long cycles);
Description
This is an inline function that is expanded by the code generator. When called, this rou-
tine expands to an inline assembly delay sequence. The sequence will consist of code
that delays for the number of cycles that is specified as argument. The argument must
be a literal constant.
An error will result if the delay period requested is too large. For very large delays, call
this function multiple times.
Example
#include <htc.h>
void
main (void)
{
  control |= 0x80;
  _delay(10);    // delay for 10 cycles
  control &= 0x7F;
}


Like 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)
 

Offline baljemmett

  • Supporter
  • ****
  • Posts: 665
  • Country: gb
Re: pic 12f683
« Reply #8 on: August 05, 2012, 11:57:08 pm »
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: [Select]
        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.
 

Offline gooligumelec

  • Contributor
  • Posts: 34
  • Country: au
    • Gooligum Electronics
Re: pic 12f683
« Reply #9 on: August 06, 2012, 11:36:31 am »
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 appreciated


There 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 at http://www.gooligum.com.au/tutorials.html and 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.
David Meiklejohn
www.gooligum.com.au
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4307
  • Country: us
Re: pic 12f683
« Reply #10 on: August 06, 2012, 11:48:51 pm »
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...
 

Offline gooligumelec

  • Contributor
  • Posts: 34
  • Country: au
    • Gooligum Electronics
Re: pic 12f683
« Reply #11 on: August 07, 2012, 01:59:37 am »
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.
David Meiklejohn
www.gooligum.com.au
 

Offline westfw

  • Super Contributor
  • ***
  • Posts: 4307
  • Country: us
Re: pic 12f683
« Reply #12 on: August 07, 2012, 08:52:51 am »
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.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf