View previous topic :: View next topic |
Author |
Message |
johnl
Joined: 30 Sep 2003 Posts: 120
|
12f629 adding a long variable causes program to hang |
Posted: Tue Sep 01, 2015 9:25 am |
|
|
This code runs correctly. If a long variable is introduced, it hangs. Compiler is pcm 4.121
Code: |
#include <12f629.h>
#fuses MCLR, PROTECT, NOWDT,INTRC_IO,PUT ,NOBROWNOUT
#use delay(clock=4000000)
#define SENSOR Pin_A5
#define LED Pin_A4
#define BEACON Pin_A2
#define LEAKMODEENABLE Pin_A0
#define LED_ON output_low(LED)
#define LED_OFF output_high(LED)
#define BEACON_ON output_high(BEACON)
#define BEACON_OFF output_low(BEACON)
#define THRESHOLD 240
#define set_options(value) {#ASM \
MOVLW value \
OPTION \
#ENDASM}
#byte PORTA = 5
#byte WPU = 0x95
/////////////////////////////////////////////
long junk; //Comment this out and the last line and all works fine;
// otherwise the program hangs.
////////////////////////////////////////////
unsigned char Hit;
unsigned char j;
unsigned char i;
unsigned char Mode = 1; //1 = normal operation, 0 = light leak detect mode.
void Flash_Beacon(void)
{ BEACON_ON;
if (Mode)
delay_ms(20); // longer flash when not in light leak mode
else
delay_us(500); // shorter flash when in light leak mode
BEACON_OFF;
}
void Leak_detect()
{
Hit=0;
for (i=0;i<255;i++)
{
LED_ON;
delay_cycles(5);
LED_OFF;
if ( !input(PIN_A5) ) { Hit++;} //
delay_cycles (2);
}
}
void Send_and_receive()
{
Hit=0;
for (i=0;i<255;i++)
{
LED_ON;
delay_cycles(5);
LED_OFF;
if ( !input(PIN_A5) ) { Hit++;} //
else Hit=0;
delay_cycles(2);
}
}
void main(void)
{
WPU = 1; //A0 weak pullup on
set_tris_a(0b00100001); //A0 input to detect Mode choice
LED_OFF; BEACON_OFF;// everything off
set_options(0x80);
setup_comparator(NC_NC_NC_NC);
Mode = input(PIN_A0) ; // 1 = normal, 0 = leak detect mode
set_tris_a(0b00100000); //turn off input on A0
for (j=0; j<(2+Mode); j++) // 3 flashes for normal mode , 2 for light leak mode
{BEACON_ON; delay_ms(2);BEACON_OFF; delay_ms(400);
}
delay_ms(3000);
while(1){
BEACON_OFF;
if (Mode)
Send_and_receive();
else
Leak_detect();
delay_ms(300);
if (Hit>THRESHOLD*Mode)
Flash_Beacon();
delay_ms(300);
junk = junk^1;
}
}
|
|
|
|
johnl
Joined: 30 Sep 2003 Posts: 120
|
|
Posted: Tue Sep 01, 2015 9:29 am |
|
|
Just figured it out. Need to use "int16" instead of "long"
Spoke too soon. I changed the variable "i" to int16 and the program hung.
Update: I had the programming range set to a fixed number in MPLAB. Changing to "Auto" fixed the issue.
Why does MPLAB report Programming/Verify complete when it is not true? |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19552
|
|
Posted: Tue Sep 01, 2015 11:06 am |
|
|
Because it has successfully programmed the range you have told it to program..... |
|
|
asmboy
Joined: 20 Nov 2007 Posts: 2128 Location: albany ny
|
|
Posted: Tue Sep 01, 2015 11:44 am |
|
|
one other observation:
you use set_tris to declare the I/O pin state for port A
S O . . . .
it is a REALLY good practice to have executed
a
#USE FAST_IO()
declaration for porta before doing the first SET_TRIS operation |
|
|
|