1. Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.
    Dismiss Notice

"mplab" + "pic assembly languag"e + "c++" = i need help !!!

Discussion in 'Microcontrollers' started by kira_kame, Apr 4, 2009.

  1. kira_kame

    kira_kame New Member

    Joined:
    Apr 4, 2009
    Messages:
    16
    Likes:
    0
    Location:
    Philippines, Davao
    "MPLAB" + "PIC ASSEMBLY LANGUAG"E + "C++" = I NEED HELP !!!

    im working on a project and i need help fast.....

    and i got only 72 hrs to do it!!!!!

    i have a c++ code then i need to use mplab compiler to inject the c++ code in the p.i.c. ...... ummm bit of a noob here so umm u know ^_^...

    ==============================================================================================
    so the question are:

    1> can u directly insert c++ code sa mplab?
    coz my predicament is that a PIC reqs. an asm or assembly code to operate, then some pipz told me u can use C++ (i understood c++ more than asm), and then i recently aquired a code fitting for my project and i need to inject that code somehow in my PIC....

    2> do i need converters to do this?
    some said, they make codes in c lang. then converters to asm then embed the code in mplab to recompile as asm ready for insertion for the pic through serial port or somthing......

    3> if 1> is yes :. how to do it???? (same with 2>)

    4> if u can do "1>", does it require certain conditions or special syntax? im just wild guessing...
    ==============================================================================================
    im in a dire pinch so any help will do.... ^_^
    (my code is fine thing is i need it "in" the microchip..... YORoSH KU!!!!)
     
  2. Nigel Goodwin

    Nigel Goodwin Super Moderator Most Helpful Member

    Joined:
    Nov 17, 2003
    Messages:
    39,199
    Likes:
    640
    Location:
    Derbyshire, UK
    ONLINE
    You can't use C++ that's a PC language, but depending on the PIC you use there are C compilers available, and a demo version of one is loaded along with MPLAB. Assuming you chose an 18F series PIC, then the C18 compiler is free from MicroChip.

    Essentially a C compiler produces PIC assembler, which MPLAB converts to a machine code HEX file, and is then transferred to a PIC via a PIC programmer, the PICKit2 been a popular choice as it has a debugger as well.
     
  3. tresca

    tresca Member

    Joined:
    Apr 11, 2004
    Messages:
    194
    Likes:
    2
    Location:
    Canada
    MPLAB is the ide. Not the compiler.
    1. I havent seen a c++ compiler and if there was one, then you would have to select that language tool within mplab to compile and then you can do all the c++ injections you want (though at that point, it wouldnt be called injections).

    So im going to say no.

    2. There might be converters. Not sure. Do a google search. However, C and C++ are not that far apart. If you have a C compiler such as PICC or C18, convert it yourself.

    Your code was written in C++ ?
    How do you know it works if its not in your uC ? Was it ever in any uC ?
    Keep in mind that code for a computer is typically only on a software layer. Code of a microcontroller is hardware + software. Just because it works on pc, doesnt mean it will work on your microcontroller depending on mempory requirements, clock speed etc.. etc...
     
  4. dave

    Dave New Member

    Joined:
    Jan 12, 1997
    Messages:
    -
    Likes:
    0


     
  5. Mike - K8LH

    Mike - K8LH Well-Known Member

    Joined:
    Jan 22, 2005
    Messages:
    3,637
    Likes:
    109
    Location:
    Michigan, USA

    The SourceBoost package includes BASIC, C, and C++ compilers and their IDE but the compilers can be installed to run in MPLAB IDE too.
     
  6. kira_kame

    kira_kame New Member

    Joined:
    Apr 4, 2009
    Messages:
    16
    Likes:
    0
    Location:
    Philippines, Davao
    hmmmm.... well according to my quick 12hour research, people always tend to say something about using the C18 or pic18cxxx or some pic microchips concerning the letter "c" and number "18",

    does that mean i need to use "pic18cxxx" series to conduct my project??, which also means i cant use my current chip "pic16f690"??

    i also have the mplab with that "c18" evaluation version, got it from microchip.com itself


    BTW, thanks for the help u guys!!!! n early thanks to further helps to come ^_^
     
  7. felis

    felis New Member

    Joined:
    May 30, 2006
    Messages:
    190
    Likes:
    2
    Location:
    Colorado
    MPLAB IDE also installs a free version of HiTech PICC C compiler for PIC16s. You can give it a try if you're committed to PIC16. In any case, converting from C++ to C won't be easy.
     
  8. kira_kame

    kira_kame New Member

    Joined:
    Apr 4, 2009
    Messages:
    16
    Likes:
    0
    Location:
    Philippines, Davao
    oh, my code is c not c++ now so its ok.... ummm how do i enable or render libraries in mplab to get the hi-tech picc to work???

    heres my my code.... its for an elevator.... though the code is 5 floors i only need to operate with 3.... and operate in pic16f690 ^_^

    ==================================================================

    // Elevator controller (5 floors) with PIC16F84 for Hades webdemos.
    // Also demonstrates how to implement state-machines as C programs.
    //
    // 22.12.05 - first version (picclite+hitide)
    //
    // (c) 2005 fnh hendrich@informatik.uni-hamburg.de

    #ifndef P16F690_H
    #define P16F690_H

    #include <pic16f690.h>


    // the push-buttons that request the elevator target floor
    // we map BUTTON_0=RA4 etc. to keep the schematics simple..
    //
    #define BUTTON_0 RA4
    #define BUTTON_1 RA3
    #define BUTTON_2 RA2
    #define BUTTON_3 RA1
    #define BUTTON_4 RA0

    // the sensors that indicate which floor the elevator car
    // has reached. Again we map 0-4 1-3 etc. to avoid signal
    // crossing in the schematics.
    //
    #define SENSOR_0 RB4
    #define SENSOR_1 RB3
    #define SENSOR_2 RB2
    #define SENSOR_3 RB1
    #define SENSOR_4 RB0

    // two output ports used to control the elevator motor
    // (on/off) and motor direction (up/ndown).
    //
    #define MOTOR_ON RB6
    #define UP_NDOWN RB7

    // the elevator states: parked means waiting (at floor < i >),
    // up_to and down_to imply moving to the corresponding floor.
    // We force an encoding that allows you to watch the state
    // during the simulation (upper nibble=1/2/4: parked/up/down),
    // lower nibble=floor index, 0xff=error
    //
    enum elevator_state {
    PARKED_0 = 0x10,
    PARKED_1 = 0x11,
    PARKED_2 = 0x12,
    PARKED_3 = 0x13,
    PARKED_4 = 0x14,

    UP_TO_1 = 0x21,
    UP_TO_2 = 0x22,
    UP_TO_3 = 0x23,
    UP_TO_4 = 0x24,

    DOWN_TO_3 = 0x43,
    DOWN_TO_2 = 0x42,
    DOWN_TO_1 = 0x41,
    DOWN_TO_0 = 0x40,

    UNKNOWN = 0xff,
    };

    enum motor_state {
    STOP = 0x00,
    MOVE_UP = 0xc0,
    MOVE_DOWN = 0x80,
    };


    // global variables for state-machine state and motor control
    //
    unsigned char state = PARKED_0;
    unsigned char motor = STOP;



    // inidicate an error by lighting the LED on port B.5
    // (note that RB5 is also used to reset the SR-flipflops,
    // but the short strobes should not be visible on a real LED.
    void
    error(void)
    {
    motor = STOP;
    for(;;) {
    RB5 = 1;
    }
    }


    void
    main(void)
    {

    GIE = 0; // disable interrupts
    TRISA = 0x1f; // all inputs
    TRISB = 0x1f; // RB7..RB5 outputs, RB4..RB0 inputs

    for( ;; ) {

    switch( state ) {
    case PARKED_0:
    if (BUTTON_1 | BUTTON_2 | BUTTON_3 | BUTTON_4) {
    state = UP_TO_1;
    motor = MOVE_UP;
    }
    else {
    motor = STOP;
    }
    break;

    case PARKED_1:
    if (BUTTON_2 | BUTTON_3 | BUTTON_4) {
    state = UP_TO_2;
    motor = MOVE_UP;
    }
    else if (BUTTON_0) {
    state = DOWN_TO_0;
    motor = MOVE_DOWN;
    }
    else {
    motor = STOP;
    }
    break;

    case PARKED_2:
    if (BUTTON_3 | BUTTON_4) {
    state = UP_TO_3;
    motor = MOVE_UP;
    }
    else if (BUTTON_0 | BUTTON_1) {
    state = DOWN_TO_1;
    motor = MOVE_DOWN;
    }
    else {
    motor = STOP;
    }
    break;

    case PARKED_3:
    if (BUTTON_4) {
    state = UP_TO_4;
    motor = MOVE_UP;
    }
    else if (BUTTON_0 | BUTTON_1 | BUTTON_2 ) {
    state = DOWN_TO_2;
    motor = MOVE_DOWN;
    }
    else {
    motor = STOP;
    }
    break;

    case PARKED_4:
    if (BUTTON_0 | BUTTON_1 | BUTTON_2 | BUTTON_3) {
    state = DOWN_TO_3;
    motor = MOVE_DOWN;
    }
    else {
    motor = STOP;
    }
    break;

    case UP_TO_1:
    if (SENSOR_2 | SENSOR_3 | SENSOR_4) {
    error();
    }
    if (BUTTON_2 | BUTTON_3 | BUTTON_4) {
    state = UP_TO_2;
    motor = MOVE_UP;
    }
    else if (SENSOR_1) {
    state = PARKED_1;
    motor = STOP;
    }
    else {
    motor = MOVE_UP;
    }
    break;

    case UP_TO_2:
    if (BUTTON_3 | BUTTON_4) {
    state = UP_TO_3;
    motor = MOVE_UP;
    }
    else if (SENSOR_2) {
    state = PARKED_2;
    motor = STOP;
    }
    else {
    motor = MOVE_UP;
    }
    break;

    case UP_TO_3:
    if (BUTTON_4) {
    state = UP_TO_4;
    motor = MOVE_UP;
    }
    else if (SENSOR_3) {
    state = PARKED_3;
    motor = STOP;
    }
    else {
    motor = MOVE_UP;
    }
    break;

    case UP_TO_4:
    if (SENSOR_4) {
    state = PARKED_4;
    motor = STOP;
    }
    else {
    motor = MOVE_UP;
    }
    break;

    case DOWN_TO_3:
    if (BUTTON_2 | BUTTON_1 | BUTTON_0) {
    state = DOWN_TO_2;
    motor = MOVE_DOWN;
    }
    else if (SENSOR_3) {
    state = PARKED_3;
    motor = STOP;
    }
    else {
    motor = MOVE_DOWN;
    }
    break;

    case DOWN_TO_2:
    if (BUTTON_1 | BUTTON_0) {
    state = DOWN_TO_1;
    motor = MOVE_DOWN;
    }
    else if (SENSOR_2) {
    state = PARKED_2;
    motor = STOP;
    }
    else {
    motor = MOVE_DOWN;
    }
    break;

    case DOWN_TO_1:
    if (BUTTON_0) {
    state = DOWN_TO_0;
    motor = MOVE_DOWN;
    }
    else if (SENSOR_1) {
    state = PARKED_1;
    motor = STOP;
    }
    else {
    motor = MOVE_DOWN;
    }
    break;

    case DOWN_TO_0:
    if (SENSOR_0) {
    state = PARKED_0;
    motor = STOP;
    }
    else {
    motor = MOVE_DOWN;
    }
    break;

    default:
    error();

    } // end switch(state)

    // a variant of the elevator circuit uses extra SR-flipflops
    // to make sure that the (short) pulses generated by the
    // floor-sensors are picked up by this program loop.
    // To reset the flipflops, we generate a short-pulse on RB5
    // now. We can do this here, because we have just handled
    // one iteration of the state-machine state update.
    // The single pulse is too short to make the error LED
    // light up visibly.
    RB5 = 1;
    RB5 = 0;

    // now activate the motor corresponding to the current
    // state. We just need RB7 and RB6, but the other bits
    // are inputs anyway.
    PORTB = motor;

    // finally, reset the watchdog timer.
    CLRWDT();
    }

    }

    // we don't use interrupts in this program.
    //
    static void interrupt
    isr(void)
    {
    if(T0IF) { // timer interrupt
    error();
    }
    else if(INTF) {
    error();
    }
    }

    ==================================================================

    please do tell if somethings wrong.... ^_^ i havent done anything like this before coz its usually the long assembly code that i used for my projects, which i suck at...
    though i suck im not afraid to get some asswhoopin!!! ha!!!! 24hours left!!!!
     
    Last edited: Apr 4, 2009
  9. kira_kame

    kira_kame New Member

    Joined:
    Apr 4, 2009
    Messages:
    16
    Likes:
    0
    Location:
    Philippines, Davao
    ohh, you can contact my Y.M. " f1r3_rav3n@yahoo.com " BTW, i got the HiTech software its not working it needs some activations or somethin.... cant seem to get it to work....

    thanx!! ^_^
     
  10. felis

    felis New Member

    Joined:
    May 30, 2006
    Messages:
    190
    Likes:
    2
    Location:
    Colorado
    The code looks like it was written for picc. Why don't you just compile it and see what happens?
     
  11. kira_kame

    kira_kame New Member

    Joined:
    Apr 4, 2009
    Messages:
    16
    Likes:
    0
    Location:
    Philippines, Davao
    yeah, didn't work though....
     
  12. 3v0

    3v0 Coop Build Coordinator Forum Supporter

    Joined:
    Jul 14, 2006
    Messages:
    9,404
    Likes:
    227
    Location:
    OKLAHOMA USA
    My crystal ball is out for repair. Can you list the output with the errors in it.

    The next time you post code please use the # icon from the toolbar to place [CODE][/CODE] tags around the code. It makes it easier to read.

    3v0
     
  13. felis

    felis New Member

    Joined:
    May 30, 2006
    Messages:
    190
    Likes:
    2
    Location:
    Colorado

Share This Page