Author Topic: MPLAB help PIC12F508  (Read 360 times)

0 Members and 1 Guest are viewing this topic.

Offline Muffins

  • Contributor
  • Posts: 18
  • Country: za
MPLAB help PIC12F508
« on: March 18, 2018, 01:37:00 am »
Hi Guys,

I'm trying to get a blink program to work on MPLAB X with a XC8 compiler, I'm coding in C. My chip is a 12F508.

Coming from programming arduinos, this is quite a steep learning curve and I've found pretty much every tutorial I can find to be a little different. The two primary sources I am using are the closest I can find to the chip I have, the one on youtube:
 
And the other a Gooligum tutorial. I have had more progress trying to copy the code in the youtube video, it looks like this:

#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "header.h"


#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = OFF // Code Protection bit (Code protection off)
#pragma config MCLRE = ON // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)
#define _XTAL_FREQ 4000000

void main(void) {
    TRISGPIO = 1;
   
    while(1)
    {
        GPIObits.GP0 = 1;
        __delay_ms(500);
        GPIObits.GP0 = 0;
        __delay_ms(500);
    }
   
}

When using this code the LED, which is simply connected via a resistor from source to the pin, lights up but does not turn off.

Gooligum has code that is different, it goes more like this:

void main(void) {
    TRIS = 0b111101;
   
    for(;;)
    {
        GPIO = 0b000010;
        __delay_ms(500);
        GPIO = 0;
        __delay_ms(500);
    }
   
}
The LED does not light up with the Gooligum code.

After I created the project as a main project and added a C file as a source I also added a header file, which it created and I referenced without altering it. I was trying to copy the youtube video.

I seem to have not problem using a pickit 3 to interact with the chip, everything is plugged in the right place and I get no errors. I've been trying for hours to learn my way out of being stuck but I am still stuck. I think the problem with with my code or my IDE settings, I've attached images of my setup, I would greatly appreciate it if you could check it over.

Please help me, haha  :)
 

Offline Andy Watson

  • Super Contributor
  • ***
  • Posts: 1636
Re: MPLAB help PIC12F508
« Reply #1 on: March 18, 2018, 02:24:09 am »
 TRISGPIO = 1;   This sets TRISGPIO bit 0 to an input.   1=input, 0 = Output.

This code appears to be written to use GP1 - not GP0.
void main(void) {
    TRIS = 0b111101;
   
    for(;;)
    {
        GPIO = 0b000010;
        __delay_ms(500);
        GPIO = 0;
        __delay_ms(500);
    }
   
}


Yes, breaking into PIC programming can be a very steep curve :)

 

Offline hugo

  • Regular Contributor
  • *
  • Posts: 111
  • Country: ca
Re: MPLAB help PIC12F508
« Reply #2 on: March 18, 2018, 03:42:06 am »
Hi,

Code: [Select]
#include <xc.h>
#include <stdio.h>
#include <stdlib.h>
#include "header.h"

#pragma config OSC = IntRC // Oscillator Selection bits (internal RC oscillator)
#pragma config WDT = OFF // Watchdog Timer Enable bit (WDT disabled)
#pragma config CP = OFF // Code Protection bit (Code protection off)
#pragma config MCLRE = ON // GP3/MCLR Pin Function Select bit (GP3/MCLR pin function is MCLR)
//#define _XTAL_FREQ 4000000 

void main() {
   
TRISGPIO = 0;
   
    while(1)
    {
        GPIO = 0xff;
        __delay_ms(500);
        GPIO = 0;
        __delay_ms(500);
    }
   
}

GP3 pin should be connected to VCC with a 10K resistor (you could also turn it OFF #pragma config MCLRE = OFF) IIRC, you have to decide for the oscillator type: internal or external crystal oscillator, make sure you select the right PIC (PIC12F508) etc.
« Last Edit: March 18, 2018, 03:43:56 am by hugo »
 

Offline Muffins

  • Contributor
  • Posts: 18
  • Country: za
Re: MPLAB help PIC12F508
« Reply #3 on: March 18, 2018, 03:59:25 am »
Okay, it's working. I would cry right now if I wasn't emotionally defeated.

Tomorrow...more questions.

Thank you.
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf