Help Converting code from Arduino to PIC

Thread Starter

Kkyy

Joined Dec 25, 2021
10
I've been working on an Arduino project.but I have to convert this code to pic microcontroller 12F683 in mplab ide.
CODE FUNCTIONALITY:
if input GP0 low (negative edge)output GP1 on for 2 sec and Off.
if input GP0 high (positive Edge)output GP1 is off .

My concerted PIC code toggle high to low for input is low .but i want only exceute once and exit loop.
 

Attachments

hexreader

Joined Apr 16, 2011
581
I agree with twohats about posting code as text within code tags.

In this case - I have no intention of copying your code. It looks way to complicated to me.

I gave up on porting Arduino code long ago. Much preferred writing from scratch, else I always seems to end up with a hideous mess that was hard to debug.

Here is my attempt at a solution to the described problem, whilst ignoring the Arduino code:

Code:
// simple LED blink in response to button press

// I've been working on an Arduino project.but I have to convert this code to pic microcontroller 12F683 in mplab ide.
// CODE FUNCTIONALITY:
// if input GP0 low (negative edge)output GP1 on for 2 sec and Off.
// if input GP0 high (positive Edge)output GP1 is off .
// My concerted PIC code toggle high to low for input is low .but i want only exceute once and exit loop.

// PIC12F683 Configuration Bit Settings
// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

#include <xc.h>

#define _XTAL_FREQ 8000000

#define BUTTON GPIObits.GP0
#define LED GPIObits.GP1

void main(void) {

    OSCCON = 0x70;                                                              // change default 4MHz INTOSC to 8MHz INTOSC
    TRISIO = 0x3d;                                                              // GP1 output for LED, rest input
    GPIO = 0;                                                                   // all outputs low to start with
    ANSEL = 0;                                                                  // all digital
    WPU = 0x3f;                                                                 // pull-up on all pins
    OPTION_REGbits.nGPPU = 0;                                                   // pull-ups enabled
    CMCON0 = 0x07;                                                              // comparator disabled

    while(1){
        if(!BUTTON){                                                            // low when button pressed
            LED = 1;
            __delay_ms(2000);                                                   // LED on for 2 seconds (also serves as button de-bounce)
            LED = 0;
            while(!BUTTON);                                                     // wait for button release
            __delay_ms(20);                                                     // switch de-bounce time
        }
    } 
}
 
Last edited:

Thread Starter

Kkyy

Joined Dec 25, 2021
10
I agree with twohats about posting code as text within code tags.

In this case - I have no intention of copying your code. It looks way to complicated to me.

I gave up on porting Arduino code long ago. Much preferred writing from scratch, else I always seems to end up with a hideous mess that was hard to debug.

Here is my attempt at a solution to the described problem, whilst ignoring the Arduino code:

Code:
// simple LED blink in response to button press

// I've been working on an Arduino project.but I have to convert this code to pic microcontroller 12F683 in mplab ide.
// CODE FUNCTIONALITY:
// if input GP0 low (negative edge)output GP1 on for 2 sec and Off.
// if input GP0 high (positive Edge)output GP1 is off .
// My concerted PIC code toggle high to low for input is low .but i want only exceute once and exit loop.

// PIC12F683 Configuration Bit Settings
// CONFIG
#pragma config FOSC = INTOSCIO  // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF         // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF        // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = OFF      // Brown Out Detect (BOR disabled)
#pragma config IESO = OFF       // Internal External Switchover bit (Internal External Switchover mode is disabled)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)

#include <xc.h>

#define _XTAL_FREQ 8000000

#define BUTTON GPIObits.GP0
#define LED GPIObits.GP1

void main(void) {

    OSCCON = 0x70;                                                              // change default 4MHz INTOSC to 8MHz INTOSC
    TRISIO = 0x3d;                                                              // GP1 output for LED, rest input
    GPIO = 0;                                                                   // all outputs low to start with
    ANSEL = 0;                                                                  // all digital
    WPU = 0x3f;                                                                 // pull-up on all pins
    OPTION_REGbits.nGPPU = 0;                                                   // pull-ups enabled
    CMCON0 = 0x07;                                                              // comparator disabled

    while(1){
        if(!BUTTON){                                                            // low when button pressed
            LED = 1;
            __delay_ms(2000);                                                   // LED on for 2 seconds (also serves as button de-bounce)
            LED = 0;
            while(!BUTTON);                                                     // wait for button release
            __delay_ms(20);                                                     // switch de-bounce time
        }
    }
}
Thank you so much for your support ☺ HEXREADER I'm beginner in PIC controller . your code work fine.
 
Top