PIC16F628A HI-TECH C Compiler while loop issue

Discussion in 'Embedded Systems and Microcontrollers' started by odm4286, Sep 27, 2015.

  1. odm4286

    Thread Starter Active Member

    Sep 20, 2009
    155
    5
    Well, this might sound like a crazy problem but hopefully someone can help me out. So I'm writting code to interface with an XBEE chip and I couldn't get it to work. So as a bit of a sanity check I decided to turn on a LED(RA1) just to make sure the chip was actually running. To my complete surprise the LED will NOT turn on during the while loop, it seems the only code thats ever run is what is outside the while loop, is this a special thing with the HI-TECH compiler? First time I've used it so im not sure, heres my source.

    Code (C):
    1.  
    2. /*
    3.  * File:   main.c
    4.  * Author: user
    5.  *
    6.  * Created on September 27, 2015, 10:05 AM
    7.  */
    8.  
    9. #include <stdio.h>
    10. #include <stdlib.h>
    11. #include <pic16f628a.h>
    12. #include <htc.h>
    13.  
    14. __CONFIG(FOSC_INTOSCIO & WDTE_OFF & PWRTE_OFF & MCLRE_OFF & BOREN_ON & LVP_ON & CPD_OFF & CP_OFF);
    15.  
    16. #define ONESEC 125000
    17.  
    18.  
    19. /*
    20.  *
    21.  */
    22. int main(int argc, char** argv) {
    23.  
    24.      char id[] = {'a','b','c','d'};
    25.      // Pin and interrupt configuration
    26.      TRISB = 0x7;                         // all outputs except, RB0 - RB2
    27.      TRISA = 0x0;                         // all outputs
    28.      RA1 = 1;
    29.      INTCON = 0x10;                       // enabling INT(RB0) external interrupt, clearing flag
    30.      INTEDG = 1;                          // Configures RBO to interrupt on rising edge
    31.  
    32.      RCIE = 1;                            // Rx interrupt enabled, interrupt when receiving data
    33.      RCIF = 0;                            // clearing flag
    34.      
    35.      // USART configuration - hopefully this works correctly with xbee
    36.      BRGH = 1;                            // highspeed selected, desired 9600 actual 9615
    37.      SPBRG = 25;                          // baud rate = Fosc/16(SPBRG+1)
    38.      CSRC = 0;                            // Aynchronous mode, internal BRG
    39.      TXEN = 1;                            // Transmit enable
    40.      SYNC = 0;                            // Asynchronous mode.
    41.      SPEN = 1;                            // Enable serial comm
    42.      CREN = 1;                            // Continuous receive
    43.  
    44.      // timer1 setup
    45.      T1CONbits.T1CKPS = 0b11;             // 1:8 prescaler 125k = one second
    46.      TMR1ON = 1;                          // turn the timer on
    47.      while(1)
    48.      {
    49.            
    50.              // transmit test...
    51.              //TXREG = id[0];
    52.             // anything in here doesn't work for some reason
    53.  
    54.            
    55.      }
    56.     return (EXIT_SUCCESS);
    57. }
    58.  
     
  2. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    Read the datasheet carefully, particularly the portion about the gpio pins. Pay extreme attention to the registers that you will need to take care of, and compare your code to the examples.

    This is where mos PIC programmers are taken initially.

    You can simulate your code in mplab to see what's going on.
     
  3. odm4286

    Thread Starter Active Member

    Sep 20, 2009
    155
    5
    Thanks for the tip, but I've done that. The code works fine if I do stuff OUTSIDE the while loop, which is so strange to me
     
  4. odm4286

    Thread Starter Active Member

    Sep 20, 2009
    155
    5
    Seems to work now, I must have had something wired in correctly or a bad connection o_O good thing is it works now
     
  5. dannyf

    Well-Known Member

    Sep 13, 2015
    1,835
    367
    That would be a strong indication that the code isn't working.
     
Loading...