Complete beginner - pulling hair out - 18f13k22

Discussion in 'Embedded Systems and Microcontrollers' started by ashumu, Nov 25, 2010.

  1. ashumu

    Thread Starter New Member

    Nov 25, 2010
    Hello all,

    I've programmed in C18 before briefly using tutorials and managed to do some basics. However, I've now got an 18F13K22 and I've found I'm not even capable of getting it to do the basics. I'm using mplab IDE v8.60 and microchips c18 compiler. I've read through the compiler, pic and mplab manuals as well as going through the entire 18f13k22.h file to try and get an idea as to what is going on.

    So far I have this:

    Code ( (Unknown Language)):
    1. #include <p18f13k22.h>
    5. void main(){
    7. TRISA = 0;
    8. TRISB = 0;
    11. while(1){
    13. PORTBbits.RB7 = 1;
    14. PORTAbits.RA0 = 1;
    16. }
    17. }
    Now I would assume that this, being such a basic and trivial task, would be simple enough to get first try. Alas, a whole day of reading, programming and stressing out has led me to believe that I'm missing some important details as there is zero output on either of those pins when it is hooked up. I can't find a document or source of info on this PIC that doesn't seem to either be missing out all the basics that I need or simply in another language.

    Can anyone please shed some light on this issue?
  2. Markd77

    Senior Member

    Sep 7, 2009
    Does anything change if you replace "PORT" with "LAT"?
    Worth a shot.
  3. ashumu

    Thread Starter New Member

    Nov 25, 2010
    Just gives me an error :( thanks for the quick response though
  4. Markd77

    Senior Member

    Sep 7, 2009
    I don't use C (as you can probably tell)
    Maybe if LATbits isn't defined something like this will work:
    The output LATches are a safer way of outputting.
    LATB.7 = 1;
    LATA.0 = 1;
  5. t06afre

    AAC Fanatic!

    May 11, 2009
    Have you read and understood section 22.1 Configuration Bits in the data sheet ;). If you load your chip with code without setting these bits. It will probably do nothing. However if you simulate your code in MPLAB SIM it do not matter I think. See my post here about MPLAB SIM tutorial
  6. spinnaker

    AAC Fanatic!

    Oct 29, 2009

    Mark is right. You read ports and write to latches. Plus port A and B have analog outputs. You should make sure they are binary outputs by setting their ANS bits. See your datasheeet for ANSEL.
  7. ashumu

    Thread Starter New Member

    Nov 25, 2010
    Hmmmm okay fantastic. Thank you so much for the responses guys! I'll get back on it tomorrow.
  8. jdraughn

    Active Member

    Jan 30, 2009
    I have always found it useful to pause the debugger, then change the values to a 1 manually (Hit enter after changing the value), and then step forward, if it changes right back to a 0, then you know that it's most likely a configuration setting. Look at your TRIS values in the watch window too, see if they are still set to outputs.