would like someone to review my c pic program

Thread Starter

ibigpapa

Joined May 9, 2009
7
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.
Rich (BB code):
#include <htc.h>	// Required to interface with delay routines

#ifndef _XTAL_FREQ
 // Unless already defined assume 4MHz system frequency
 // This definition is required to calibrate __delay_us() and __delay_ms()
 #define _XTAL_FREQ 4000000
#endif

void pause_1(void);

unsigned int i;

main(void)
{
	while(1)
	{
		PORTB = (0b00000001);	//turn blue on
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000010);   //turn red on - blue off
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000100);   //turn white on - red off
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000000);   //turn white off
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000111);   //turn red, white, blue on
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000000);   //turn red, white, blue off
		pause_1();				//wait 5 secs 5000 ms
	};	
}

void pause_1()
{
	for(i=0;i<40;i++)
	{
	__delay_ms(125);			//delay 1000 ms (1 sec)
	};
}
 
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?
 

Thread Starter

ibigpapa

Joined May 9, 2009
7
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)
Rich (BB code):
#include <htc.h>	// Required to interface with delay routines
#ifndef _XTAL_FREQ
 // Unless already defined assume 4MHz system frequency
 // This definition is required to calibrate __delay_us() and __delay_ms()
 #define _XTAL_FREQ 4000000
#endif

/* Program device configuration word
 * Oscillator = RC: CLKOUT on RA6/OSC2/CLKOUT, RC on RA7/OSC1/CLKIN
 * Watchdog Timer = On
 * Power Up Timer = Disabled
 * Brown Out Detect = Enabled
 * Master Clear Enable = Enabled
 * Low Voltage Program = Enabled
 * Data EE Read Protect = Disabled
 * Code Protect = Off
 */
__CONFIG(RCCLK & WDTEN & PWRTDIS & BOREN & MCLREN & LVPEN & UNPROTECT & UNPROTECT);

// Peripheral initialization function
void init(void);	
void pause_1(void);

unsigned int i;

main(void)
{
        init();
	while(1)
	{
		PORTB = (0b00000001);	//turn blue on
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000010);   //turn red on - blue off
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000100);   //turn white on - red off
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000000);   //turn white off
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000111);   //turn red, white, blue on
		pause_1();				//wait 5 secs 5000 ms
		PORTB = (0b00000000);   //turn red, white, blue off
		pause_1();				//wait 5 secs 5000 ms
	};	
}

void init(void)
{
	/***** Common Code ****
	 *  Portbit7:4 interrupt-on-change disabled
	 *  Peripheral interrupts not enabled
	 *  Global interrupt disabled during initialization
	 */
	INTCON	= 0b00000000;
	/*
	 *  Weak pullup on PORT disabled
	 */
	OPTION	= 0b10000000;
	
	/***** PortA Code ****
	 *  Port directions: 1=input, 0=output
	 */
	TRISA	= 0b00000000;
	
	/***** PortB Code ****
	 *  Port directions: 1=input, 0=output
	 */
	TRISB	= 0b00000000;
	
	ei();	// Global interrupts enabled
}
void pause_1()
{
	for(i=0;i<40;i++)
	{
	__delay_ms(125);			//delay 1000 ms (1 sec)
	};
}
 
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?
 

AlexR

Joined Jan 16, 2008
732
How does the compiler know what chip you are using?
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.
 
Top