would like someone to review my c pic program

Discussion in 'Embedded Systems and Microcontrollers' started by ibigpapa, Jun 1, 2009.

  1. ibigpapa

    Thread Starter New Member

    May 9, 2009
    7
    0
    I'm using hi-tech c compiler.
    I was able to use ic prog to load the hex file that was created don't know if that is the correct way of using it but that's how i did it.
    Code ( (Unknown Language)):
    1. #include <htc.h>    // Required to interface with delay routines
    2.  
    3. #ifndef _XTAL_FREQ
    4.  // Unless already defined assume 4MHz system frequency
    5.  // This definition is required to calibrate __delay_us() and __delay_ms()
    6.  #define _XTAL_FREQ 4000000
    7. #endif
    8.  
    9. void pause_1(void);
    10.  
    11. unsigned int i;
    12.  
    13. main(void)
    14. {
    15.     while(1)
    16.     {
    17.         PORTB = (0b00000001);   //turn blue on
    18.         pause_1();              //wait 5 secs 5000 ms
    19.         PORTB = (0b00000010);   //turn red on - blue off
    20.         pause_1();              //wait 5 secs 5000 ms
    21.         PORTB = (0b00000100);   //turn white on - red off
    22.         pause_1();              //wait 5 secs 5000 ms
    23.         PORTB = (0b00000000);   //turn white off
    24.         pause_1();              //wait 5 secs 5000 ms
    25.         PORTB = (0b00000111);   //turn red, white, blue on
    26.         pause_1();              //wait 5 secs 5000 ms
    27.         PORTB = (0b00000000);   //turn red, white, blue off
    28.         pause_1();              //wait 5 secs 5000 ms
    29.     }; 
    30. }
    31.  
    32. void pause_1()
    33. {
    34.     for(i=0;i<40;i++)
    35.     {
    36.     __delay_ms(125);            //delay 1000 ms (1 sec)
    37.     };
    38. }
     
  2. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    What device are you using? Is there a header file for that device or family that needs to be included? (#include <p18f542.h>)

    Does the part have a watch dog timer that needs to be considered? (#pragma config WDT = OFF)

    You have not defined PORTB as an output, is that the default, or should it be defined?

    What, if anything, is the device doing after you program it?
     
  3. ibigpapa

    Thread Starter New Member

    May 9, 2009
    7
    0
    I updated my code to include the missing items.
    I did not find a header file for th pic16f628a .
    This just turns on ports b 0 1 2 in an order.

    Those ports are connected to a npn transistor (2n3904)
    Code ( (Unknown Language)):
    1. #include <htc.h>    // Required to interface with delay routines
    2. #ifndef _XTAL_FREQ
    3.  // Unless already defined assume 4MHz system frequency
    4.  // This definition is required to calibrate __delay_us() and __delay_ms()
    5.  #define _XTAL_FREQ 4000000
    6. #endif
    7.  
    8. /* Program device configuration word
    9.  * Oscillator = RC: CLKOUT on RA6/OSC2/CLKOUT, RC on RA7/OSC1/CLKIN
    10.  * Watchdog Timer = On
    11.  * Power Up Timer = Disabled
    12.  * Brown Out Detect = Enabled
    13.  * Master Clear Enable = Enabled
    14.  * Low Voltage Program = Enabled
    15.  * Data EE Read Protect = Disabled
    16.  * Code Protect = Off
    17.  */
    18. __CONFIG(RCCLK & WDTEN & PWRTDIS & BOREN & MCLREN & LVPEN & UNPROTECT & UNPROTECT);
    19.  
    20. // Peripheral initialization function
    21. void init(void);   
    22. void pause_1(void);
    23.  
    24. unsigned int i;
    25.  
    26. main(void)
    27. {
    28.         init();
    29.     while(1)
    30.     {
    31.         PORTB = (0b00000001);   //turn blue on
    32.         pause_1();              //wait 5 secs 5000 ms
    33.         PORTB = (0b00000010);   //turn red on - blue off
    34.         pause_1();              //wait 5 secs 5000 ms
    35.         PORTB = (0b00000100);   //turn white on - red off
    36.         pause_1();              //wait 5 secs 5000 ms
    37.         PORTB = (0b00000000);   //turn white off
    38.         pause_1();              //wait 5 secs 5000 ms
    39.         PORTB = (0b00000111);   //turn red, white, blue on
    40.         pause_1();              //wait 5 secs 5000 ms
    41.         PORTB = (0b00000000);   //turn red, white, blue off
    42.         pause_1();              //wait 5 secs 5000 ms
    43.     }; 
    44. }
    45.  
    46. void init(void)
    47. {
    48.     /***** Common Code ****
    49.      *  Portbit7:4 interrupt-on-change disabled
    50.      *  Peripheral interrupts not enabled
    51.      *  Global interrupt disabled during initialization
    52.      */
    53.     INTCON  = 0b00000000;
    54.     /*
    55.      *  Weak pullup on PORT disabled
    56.      */
    57.     OPTION  = 0b10000000;
    58.    
    59.     /***** PortA Code ****
    60.      *  Port directions: 1=input, 0=output
    61.      */
    62.     TRISA   = 0b00000000;
    63.    
    64.     /***** PortB Code ****
    65.      *  Port directions: 1=input, 0=output
    66.      */
    67.     TRISB   = 0b00000000;
    68.    
    69.     ei();   // Global interrupts enabled
    70. }
    71. void pause_1()
    72. {
    73.     for(i=0;i<40;i++)
    74.     {
    75.     __delay_ms(125);            //delay 1000 ms (1 sec)
    76.     };
    77. }
     
  4. StayatHomeElectronics

    Well-Known Member

    Sep 25, 2008
    864
    40
    How does the compiler know what chip you are using?

    You may want to disable the watch dog timer until you get up and running.

    What, if anything, is the device doing after you program it?
     
  5. AlexR

    Well-Known Member

    Jan 16, 2008
    735
    54
    The standard way to do it is to select the chip type in MPLAB IDE "Configure>SelectDevice..." menu.
    Then when you invoke the C compiler from MPLAB, MPLAB passes the chip type to the compiler and the htc.h header file automatically includes the appropriate chip header file.
    Manual inclusion of device specific header files is not recommended in Hi-Tech C.
     
Loading...