Exact match. Not showing close matches.
PICList
Thread
'[PIC] PIC24H program resets'
2008\12\28@045819
by
Mer13
|
Hi,
I'm using the PIC24HJ12GP202 with MPLAB C30 compiler. My code works great
when I'm debugging and stepping through it. However when I program it to the
PIC (flashing to it) the program seems to restart itself. What I have is
that my code receives inputs by the use of a keypad, and these inputs are
stored into an array of characters. These characters are stored well in the
array and everything works good, but after some time, the array seems to
loose those values and it just replenishes it with NULLs, as if nothing had
been written to it.
The arrays used to store these inputs are the cellnum[],tmax[], and tmin[].
The code never calls for re-initialiazations =/ What could be wrong? I've
got my code below:
#include <p24hj12gp202.h>
#include <adc.h>
#include <stdlib.h>
#include <stdio.h>
#include <uart.h>
#include <string.h>
#include <math.h>
#include <ports.h>
#include <float.h>
#include <i2c.h>
#include "I2C_LCD.h"
#include "LCD_Uart.h"
#define FCY 25000000
#define BAUDRATE 9600
#define BRGVAL ((FCY/BAUDRATE)/16)-1
//#define Sleep() {__asm__ volatile ("pwrsav #0");}
//_FOSC( POSCMD_XT )
// Configure Oscillator to operate the device at 40Mhz
// Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
// Fosc= 8M*40(2*2)=80Mhz for 8M input clock
_FPOR(ALTI2C_OFF)
#define offset .274 //offset voltage
#define range 1024
_FBS( BSS_NO_FLASH & BWRP_WRPROTECT_OFF ); //From Table 18-2
_FGS( GSS_OFF & GCP_OFF & GWRP_OFF );
_FOSCSEL( FNOSC_PRIPLL & IESO_ON );
_FOSC( FCKSM_CSECMD & OSCIOFNC_OFF & POSCMD_XT );
_FWDT( FWDTEN_OFF & WINDIS_OFF & WDTPOST_PS32768 );
//_FPOR( FPWRT_PWR128 & ALTI2C_OFF );
_FICD( COE_ON & JTAGEN_OFF & ICS_PGD1 );
////////////////////////////////////////////////
///**GLOBAL VARIABLES**/////////////////////////
////////////////////////////////////////////////
char *str;
char power_loss[]="Warning, Pet Protect has detected a power loss!";
char temp_detect[]="Warning, Pet Protect has detected the temperature
outside the range!";
char smoke_detect[]="Warning, Pet Protect detected smoke!";
char CO_detect[]="Warning, Pet Protect detected CO!";
char status_ok[]="Pet Protect status is ok.";
char keyboard[] = "ACBD2850396#174*";
char y; //input number
char cellnum[10],tmax[3],tmin[2]; //phone and temperature buffers
char volatile *cell,*tmx,*tmn,value; //pointers for buffers
int s=0,c=0,p=0,max,min,t=0,flag = 0,_temper; //flags sensors
////////////////////////////////////////////////
void Telit_onoff(void) //
{
PORTBbits.RB7 = 1;
DELAY_MS(1950);
PORTBbits.RB7 = 0;
}
char input(void) //function that exits until
key is pressed
{
if(PORTBbits.RB10 == 0) //strobe
{
return keyboard[(PORTB >> 11) & 0x0f]; //shift 11 spaces to get
LSB and "AND" it with 0x0F
}
else
{
DELAY_MS(500); //delay some time before
recursive function
input(); //recursive function
}
}
char inputx(void) //function for receiving
input
{
return keyboard[(PORTB >> 11) & 0x0f]; //shift 11 spaces to get
LSB and "AND" it with 0x0F
}
void initA2D(void)
{
TRISBbits.TRISB15 = 1; //SETS PIN26 (RB15/AN6) as input for Temp
AD1PCFGL = 0x03bf; // sets PORTB AN6 (pin 26) as analog input (pg 163)
AD1CON2 = 0x0000; // sets voltage reference (pg 158)
AD1CON3 = 0x1F02; // Sample time bits = 24 (pg 159)
AD1CON1 = 0x20E0; // SAMP bit = 0 ends sampling and auto-convert, Idle
ON (pg 156)
AD1CSSL = 0; // skip input scan (pg 163)
AD1CON1bits.ADON = 1; // turn ADC ON (pg 156)
}
int readADC(void)
{
AD1CHS0= 0x0006; // Connect AN6 as CH0 input ..
AD1CON1bits.SAMP = 1; // Sets SAMP bit ON and start sampling
while (!AD1CON1bits.DONE); // conversion done?
return ADC1BUF0; // if yes, then get ADC value
}
int convert() //converts voltage into measured temperature value
{
int i = 0;
int avg = 0, temp;
double volt,v4,v3,v2,v1;
while(i!=20) //creates a running total up to 20
{
avg = (readADC() + avg);
i++;
}
avg = (avg/(i-1)); //averages total
volt = (2.7/range)*avg; //finds analog rep value
// y = 21.755x4 - 122.89x3 + 241.67x2 - 156.73x + 65.382
v4=21.755*volt*volt; //equation to get temperature reading
v4=v4*volt*volt;
v3=122.89*volt;
v3=v3*volt*volt;
v2=241.67*volt*volt;
v1=156.73*volt;
temp = v4-v3+v2-v1+65.382;
return temp;
}
void temperature()
{
int temperature,sign;
unsigned int p1,p2,p3;
sign = (char)'F';
temperature = convert(); //calls convert returns float
_temper = temperature;
if(flag == 0)
{
p1 = (temperature % 10); //splits temp into 3 chars
p2 = (temperature / 10);
p3 = (temperature / 100);
p1 = (p1 + 48);
p2 = (p2 + 48);
p3 = (p3 + 48);
if (p3!= '0')
{
i2c_lcd_char(p3);
}
else i2c_lcd_char(' ');
i2c_lcd_char(p2);
delay();
i2c_lcd_char(p1);
delay();
i2c_lcd_char(0xB2);
i2c_lcd_char(sign);
}//end if(flag) //only prints when flag is down
}
void init(void)
{
RCONbits.WDTO=0;
RCONbits.SWDTEN=0;
TRISAbits.TRISA0 = 1; /*sets smoke input to pin 2*/
TRISAbits.TRISA1 = 1; /*sets CO input to pin 3*/
TRISBbits.TRISB2 = 1; /*sets battery backup input pin 6*/
TRISBbits.TRISB3 = 0; /*sets ALARM pin 7*/
TRISBbits.TRISB4 = 0; /*sets LED output for reset pin 11*/
TRISBbits.TRISB14 = 1; /*SETS PIN25 as input for keypad input (MSB)
pin 25*/
TRISBbits.TRISB13 = 1; /*SETS PIN24 as input for keypad input in 24*/
TRISBbits.TRISB12 = 1; /*SETS output on pin 23*/
TRISBbits.TRISB11 = 1; /*SETS PIN22 as input for keypad
input (LSB) pin 22*/
TRISBbits.TRISB10 = 1; /*SETS PIN21 as input for keypad
strobe pin 16*/
TRISAbits.TRISA4 = 0; /*SETS PIN12 to LED for output
(ALARM)*/
TRISBbits.TRISB5 = 1; /*SETS PIN14 to ASDA for I2C data*/
TRISBbits.TRISB6 = 1; /*SETS PIN15 to ASCL for I2C clock*/
TRISBbits.TRISB7 = 0; //turn ON/OFF the wireless module
//***************************/
// Assign U1Rx To Pin RP0 (pin 4)
//***************************/
RPINR18bits.U1RXR = 0;
//***************************/
// Assign U1CTS To Pin RP1 (pin 5)
//***************************/
RPINR18bits.U1CTSR = 1;
//***************************/
// Assign U1Tx To Pin 18
//***************************/
RPOR4bits.RP9R = 3;
//***************************/
// Assign U1RTS To Pin 17
//***************************/
RPOR4bits.RP8R = 4;
//UART Initialization//
// Configure Oscillator to operate the device at 40Mhz
//Fosc= Fin*M/(N1*N2), Fcy=Fosc/2
//Fosc= 8M*40(2*2)=80Mhz for 8M input clock
PLLFBD=38; // M=40
CLKDIVbits.PLLPOST=0; // N1=2
CLKDIVbits.PLLPRE=0; // N2=2
RCONbits.SWDTEN=0; /* Disable Watch Dog Timer */
while (OSCCONbits.LOCK!=1) {}; /* Wait for PLL to lock */
U1MODEbits.STSEL = 0; // 1-stop bit
U1MODEbits.PDSEL = 0; // No Parity, 8-data bits
U1MODEbits.ABAUD = 0; // Autobaud Disabled
U1MODEbits.BRGH = 0; // Low Speed mode
U1BRG = BRGVAL; // BAUD Rate Setting for 9600
U1STAbits.UTXISEL1 = 0;
U1MODEbits.UARTEN = 1; // Enable UART
U1STAbits.UTXEN = 1; // Enable UART Tx
U1MODEbits.RTSMD = 0; // Simplex Mode
}
void phone(void)
{
int j = 0,x;
char n;
cell = cellnum;
i2c_lcd_clear();
i2c_lcd_string("Pet Protect");
i2c_lcd_cursorposition(1,2);
i2c_lcd_string("Enter Phone Number");
i2c_lcd_cursorposition(1,4);
i2c_lcd_underlinecursor(1);
DELAY_MS(1000);
for(x=0; x<9; x++) //loop that resets the input character buffer
{
cellnum[x]= NULL;
}
do
{
DELAY_MS(1000);
n = input();
if(n == 'B')
{
if(j == 0)
{
i2c_lcd_cursorposition(1,4);
}
else
{
i2c_lcd_cursorleft();
i2c_lcd_char(' ');
i2c_lcd_cursorleft();
cellnum[j] = NULL;
j--;
}
}
if(n == 'C') //Clear buffer
{
for(x=0; x<9; x++) //loop that resets the input character
buffer
{
cellnum[x]= NULL;
}
j = 0;
cell=cellnum[0];
i2c_lcd_cursorposition(1,4);
i2c_lcd_string(" ");
i2c_lcd_cursorposition(1,4);
}
if((n!='B')&&(n!='A')&&(n!='C')&&(n!='D')&&(n!='*')&&(n!='#')&&(n!=' '))
{
cell = &n;
cellnum[j] = *cell;
i2c_lcd_char(n);
j++;
if(j == 11)
{
for(x=0; x<9; x++) //loop that resets the input character
buffer
{
cellnum[x]= NULL;
}
j = 0;
cell=cellnum[0];
i2c_lcd_cursorposition(1,4);
i2c_lcd_string(" ");
i2c_lcd_cursorposition(1,4);
}
}
}while(*cell != 'A'); //while
if(n == 'A')
{
i2c_lcd_clear();
Menu();
}
}//END PHONE
void SMS_send(char l) //FUNCTION to send Text Messages
{
int x=0;
char number;
str=cmgf; //Sends "at+cmgf=1"
OutStr(str);
DELAY_MS(300);
WriteUART1(0x0D); //Sends ENTER
WriteUART1(0x0A);
DELAY_MS(500);
str=service_command; //Sends "at+csca="
OutStr(str);
DELAY_MS(300);
str=service_address; //Sends +13123149810
OutStr(str); //Sends Service Address Number
DELAY_MS(300);
WriteUART1(0x0D); //Sends ENTER
WriteUART1(0x0A);
DELAY_MS(500);
str=band;
OutStr(str);
WriteUART1(0x0D); //Sends ENTER
WriteUART1(0x0A);
DELAY_MS(500);
str=phone_command; //Sends at+cmgs=
OutStr(str);
DELAY_MS(300);
WriteUART1(0x22); //Sends "
DELAY_MS(300);
while(x<10) //Sends User's phone number
{
number=cellnum[x];
WriteUART1(number);
DELAY_MS(100);
x++;
}
DELAY_MS(200);
WriteUART1(0x22); //Sends "
DELAY_MS(300);
WriteUART1(0x0D);
WriteUART1(0x0A);
DELAY_MS(700);
switch(l)
{
case 'P':
DELAY_MS(500);
str = power_loss; //string to send power loss
OutStr(str);
break;
case 'T':
DELAY_MS(500);
str = temp_detect; //string to send temperature detected
OutStr(str);
break;
case 'S':
DELAY_MS(500);
str = smoke_detect; //string to send temperature detected
OutStr(str);
break;
case 'C':
DELAY_MS(500);
str = CO_detect; //string to send temperature detected
OutStr(str);
break;
case 'D':
DELAY_MS(5000); //5 second wait between each sms
transmission
str = status_ok; //string that things back to normal
OutStr(str);
break;
default:
DELAY_MS(500);
break;
}
DELAY_MS(300);
WriteUART1(0x1A);
DELAY_MS(300);
WriteUART1(0x0D);
WriteUART1(0x0A);
}//END FUNCTION SMS
void temp(void)
{
int j=0,x;
char n;
tmx = tmax;
tmn = tmin;
i2c_lcd_clear();
i2c_lcd_string("PET PROTECT");
i2c_lcd_cursorposition(16,1);
temperature();
i2c_lcd_cursorposition(1,3);
i2c_lcd_string("ENTER NEW MAX:");
i2c_lcd_cursorposition(1,4);
i2c_lcd_string("ENTER NEW MIN:");
i2c_lcd_cursorposition(17,3);
for(x=0; x<3; x++) //loop that resets the input character buffer
{
tmax[x]= NULL;
tmin[x]= NULL;
}
DELAY_MS(1000);
do
{
DELAY_MS(1000);
n = input();
DELAY_US(100);
if(n == 'B')
{
if(j==0)
{
i2c_lcd_cursorposition(17,3);
}
if(j==1)
{
i2c_lcd_cursorposition(18,3);
tmax[j] = NULL;
i2c_lcd_cursorleft();
i2c_lcd_char(' ');
i2c_lcd_cursorleft();
j--;
}
if(j==2)
{
i2c_lcd_cursorposition(19,3);
tmax[j] = NULL;
i2c_lcd_cursorleft();
i2c_lcd_char(' ');
i2c_lcd_cursorleft();
j--;
}
if(j==3)
{
i2c_lcd_cursorposition(20,3);
tmax[j] = NULL;
i2c_lcd_cursorleft();
i2c_lcd_char(' ');
i2c_lcd_cursorleft();
j--;
}
}
if(n == 'C') //Clear buffer
{
for(x=0; x<2; x++) //loop that resets the input character
buffer
{
tmax[x]= NULL;
}
j = 0; //reset index of array to beginning
tmx=tmax[0];
i2c_lcd_cursorposition(17,3);
i2c_lcd_string(" ");
i2c_lcd_cursorposition(17,3);
}
if((n!='B')&&(n!='A')&&(n!='C')&&(n!='D')&&(n!='*')&&(n!='#')&&(n!=' '))
{
tmx = &n;
tmax[j] = n;
i2c_lcd_char(n);
j++;
if(j == 4)
{
j = 0;
tmx=tmax;
i2c_lcd_cursorposition(17,3);
i2c_lcd_string(" ");
i2c_lcd_cursorposition(17,3);
for(x=0; x<3; x++) //loop that resets the input character buffer
{
tmax[x]= NULL;
}
}
}
}while(*tmx != 'A'); //while "enter" is not pressed keep
putting numbers into array
max = atoi(tmax); //convert character string into decimal
integer
if(n == 'A')
{
i2c_lcd_cursorposition(17,4); //To enter minimum
temperature
j = 0;
DELAY_MS(1000);
do
{
DELAY_MS(1000);
n = input();
DELAY_US(100);
if(n == 'B')
{
if(j==0)
{
i2c_lcd_cursorposition(17,4);
}
if(j==1)
{
i2c_lcd_cursorposition(18,4);
tmin[j] = NULL;
i2c_lcd_cursorleft();
i2c_lcd_char(' ');
i2c_lcd_cursorleft();
j--;
}
if(j==2)
{
i2c_lcd_cursorposition(19,4);
tmin[j] = NULL;
i2c_lcd_cursorleft();
i2c_lcd_char(' ');
i2c_lcd_cursorleft();
j--;
}
}
if(n == 'C') //Clear buffer
{
for(x=0; x<2; x++) //loop that resets the input character
buffer
{
tmin[x]= NULL;
}
j = 0;
tmn=tmin[0];
i2c_lcd_cursorposition(17,4);
i2c_lcd_string(" ");
i2c_lcd_cursorposition(17,4);
}
if((n!='B')&&(n!='A')&&(n!='C')&&(n!='D')&&(n!='*')&&(n!='#')&&(n!=' '))
{
tmn = &n;
tmin[j] = *tmn;
i2c_lcd_char(n);
j++;
if(j == 3)
{
j = 0;
tmn=tmin[0];
i2c_lcd_cursorposition(17,4);
i2c_lcd_string(" ");
i2c_lcd_cursorposition(17,4);
for(x=0; x<3; x++) //loop that resets the input character buffer
{
tmin[x]= NULL;
}
}
}
}while(*tmn != 'A'); //while
if(n == 'A')
{
i2c_lcd_clear();
min = atoi(tmin);
Menu();
}
}//end if
}//end temp function
void phonebook(void)
{
int c=0,t=0,r=0;
cell = cellnum;
i2c_lcd_clear();
i2c_lcd_string("Current Phone #:");
i2c_lcd_cursorposition(6,2);
DELAY_MS(750);
cell = cellnum;
while(c < 10)
{
i2c_lcd_char(*cell);
cell++;
c++;
}
i2c_lcd_cursorposition(16,2);
i2c_lcd_string(" ");
DELAY_MS(500);
i2c_lcd_cursorposition(1,3);
i2c_lcd_string("Temperature Range:");
DELAY_MS(500);
i2c_lcd_cursorposition(1,4);
DELAY_MS(1000);
i2c_lcd_string("MAX: MIN: ");
DELAY_MS(500);
i2c_lcd_cursorposition(5,4);
DELAY_MS(500);
tmx=tmax;
while(t<3)
{
i2c_lcd_char(*tmx);
tmx++;
t++;
if(tmax[t]==NULL)
i2c_lcd_char(' ');
}
i2c_lcd_cursorposition(8,4);
i2c_lcd_string(" ");
DELAY_MS(500);
i2c_lcd_cursorposition(15,4);
tmn=tmin;
while(r<2)
{
i2c_lcd_char(*tmn);
tmn++;
r++;
if(tmin[r]==NULL)
i2c_lcd_char(' ');
}
i2c_lcd_cursorposition(17,4);
i2c_lcd_string(" ");
DELAY_MS(7000);
i2c_lcd_clear();
Menu();
}//END OF PHONEBOOK
void Menu(void)
{
int counter = 0;
int ctf=0;
DELAY_MS(1000);
i2c_lcd_string("PET PROTECT ");
i2c_lcd_cursorposition(1,2);
i2c_lcd_string("1.Enter Phone Number");
i2c_lcd_cursorposition(1,3);
i2c_lcd_string("2.Enter Max/Min Temp");
i2c_lcd_cursorposition(1,4);
i2c_lcd_string("3.View Settings");
i2c_lcd_cursorposition(16,1);
temperature();
if(cellnum[0] == NULL)
{
s=1;
c=1;
t=1;
p=1;
LATBbits.LATB4=1; //turns reset LED on
}
while(PORTBbits.RB2) //If there is power loss then send message to
owner
{
DELAY_MS(300);
if(ctf==0)
{
SMS_send('P'); //Send text message
ctf=1;
}
LATBbits.LATB3=1; //sound alarm
DELAY_MS(500);
LATAbits.LATA4=1; //turn on LED detection
DELAY_MS(10000); //do a 10 second loop for audible alarm
counter++;
if(counter == 12) //120 seconds (2 minutes) each text message
sent
{
SMS_send('P'); //SEND P to Telit (every 2 minutes)
counter = 0; //resets counter
}
if(PORTBbits.RB2 == 0) //if power came back
{
LATBbits.LATB4=0; //turn off alarm
DELAY_MS(500);
LATBbits.LATB4=0; //turn off reset
DELAY_MS(500);
LATAbits.LATA4=0; //turn off indicator
DELAY_MS(500);
SMS_send('D'); //send to display normal status
counter = 0;
Menu(); //exits loop ;refresh screen to show current
temperature
}
y = inputx();
if(y == 'D')
{
LATBbits.LATB3=0; //turns off audible speech alarm (locks to off)
LATAbits.LATA4=0; //turn off red LED for detection indicator
counter = 0;
ctf=0;
Menu(); //Go to Menu function to exit loop; refresh
screen
}
LATBbits.LATB4=1; //turn ON green LED for reset
DELAY_MS(500); //needs delay to turn off the relay
LATBbits.LATB3=0; //turn off alarm relay
DELAY_MS(500);
}//close while(portBbits.rb2)
if(s == 0)
{
while((PORTAbits.RA0) &&(y!='D')) //if smoke is detected
{
SMS_send('S');
LATAbits.LATA4=1; //turn ON red LED for detection
LATBbits.LATB3=1; //send alarm
DELAY_MS(10000); //counter every 10 seconds the alarm will
sound
counter ++;
if(counter == 12)
{ //Send 'S' to module (every 2 minutes)
SMS_send('S');
counter = 0;
}
LATBbits.LATB3=0; //turn off relay switch
y = inputx();
if(y == 'D')
{
LATBbits.LATB3=0; //turns off audible speech alarm (locks to off)
LATAbits.LATA4=0; //turn off red LED for detection indicator
s = 1; //flag for smoke detection
Menu();
}
LATBbits.LATB4=1; //turn ON green LED for reset
DELAY_MS(500); //needs delay to turn off the relay
LATBbits.LATB3=0; //turn off alarm relay
}//end while
}//end if(s=0)
if(c == 0)
{
while((PORTAbits.RA1)&&(y!='D')) //if CO is detected
{
SMS_send('C');
LATAbits.LATA4=1; //turn ON red LED for detection
LATBbits.LATB3=1; //send alarm
DELAY_MS(10000); //counter every 10 seconds the alarm will
sound
counter++;
if(counter==12)
{
SMS_send('C'); //Send 'C' to module
counter = 0;
}
y = inputx();
if(y == 'D')
{
LATBbits.LATB3=0; //turns off audible speech alarm (locks to off)
LATAbits.LATA4=0; //turn off red LED for detection indicator
c = 1;
Menu();
}
LATBbits.LATB4=1; //turn ON green LED for reset
DELAY_MS(500); //needs delay to turn off the relay
LATBbits.LATB3=0; //turn off alarm relay
}//end while
}//end if(c == 0)
if(t == 0)
{
while(((_temper > max) || (_temper < min)) && (y!= 'D') && (PORTBbits.RB2
== 0))
{
LATAbits.LATA4=1; //sets detection light on
DELAY_US(300);
LATBbits.LATB3=1; //send alarm (send signal through relay to
activate speech alarm
DELAY_MS(500);
LATBbits.LATB3=0; //turn off to set the relay back to normal
position
DELAY_US(300);
LATBbits.LATB4=1; //sets reset light on
//Send 'T' to module
SMS_send('T');
DELAY_MS(10000); //loops every 10 seconds the speech alarm
will sound until it is disarmed
counter++;
if(counter==12)
{
SMS_send('T'); //send text message every 2 minutes
counter = 0;
}
y = inputx();
if(y == 'D')
{
LATAbits.LATA4=0; //turn off detection indicator
LATBbits.LATB3=0; //turn off alarm
t = 1;
c = 1;
s = 1;
p = 1;
Menu();
}
flag = 1; //sets flag as to
not print temperature on LCD
temperature(); //checks
temperature in case it gets lowered than threshold
if((_temper < max) || (_temper > min)) //compares
temperature to see if returned to normal..if yes, then automatic shut off
{
LATBbits.LATB4=0; //turns of reset
LED
LATBbits.LATB3=0; //if temperature
goes back to normal then turn off alarm
LATAbits.LATA4=0; //turns off disarm
LED
flag = 0; //resets flag to
be able to print temperatures on LCD again
t = 0;
}
if((PORTBbits.RB2)&&(p == 0))
{
SMS_send('P');
p = 1;
}
if((PORTAbits.RA0)&&(s == 0))
{
SMS_send('S'); //Send 'S' to module indicating smoke
s = 1;
}
if((PORTAbits.RA1)&&(c == 0))
{
SMS_send('C'); //Send 'C' to module indicating CO
c = 1;
}
}//end while
}//end if (t = 0)
DELAY_US(500);
y = inputx();
switch(y)
{
case '1': phone(); //set phone number
break;
case '2': temp(); //set temperature
break;
case '3': phonebook(); //view settings
break;
case '*': //RESET
{
LATBbits.LATB4=0; //turns off green LED for reset
s = 0; //sets smoke detect on
c = 0; //sets CO detect on
t = 0; //sets temperature detect on
p = 0; //sets power loss detect on
}
default:
{
DELAY_MS(800);
Menu(); //recursive function, return to menu and loops
}
}
} //END MENU FUNCTION
///////////////////////////////////////////////////////////////////
int main ()
{
init(); // initalize register pins and the UART
initA2D(); // initialize A2D converter
U1STAbits.UTXBF = 1; // sets the TX ON
i2cInit(); // sets the I2C
Telit_onoff(); // turns on/~off Telit
while(1)
{
Menu();
}
return 0;
}//Main
Thanks, I really appreciate the help on this matter. I don't know what else
to turn to :(
--
View this message in context: www.nabble.com/-PIC--PIC24H-program-resets-tp21190756p21190756.html
Sent from the PIC - [PIC] mailing list archive at Nabble.com.
2008\12\28@055141
by
Jan-Erik Soderholm
Mer13 wrote:
> Hi,
>
> I'm using the PIC24HJ12GP202 with MPLAB C30 compiler...
> ...
> ...
> Thanks,... I don't know what else to turn to :(
You could cut down your code to the smallest possible test-case
that still shows the "problem". You can't honestly think
that anyone would read throught *that* code that you posted,
do you ? There must be a lot that can be cut away and still
having the same problem.
And it might very well be something else then the *code*...
2008\12\28@150909
by
Vitaliy
|
"Mer13" wrote:
> I'm using the PIC24HJ12GP202 with MPLAB C30 compiler. My code works great
> when I'm debugging and stepping through it. However when I program it to
> the
> PIC (flashing to it) the program seems to restart itself. What I have is
> that my code receives inputs by the use of a keypad, and these inputs are
> stored into an array of characters. These characters are stored well in
> the
> array and everything works good, but after some time, the array seems to
> loose those values and it just replenishes it with NULLs, as if nothing
> had
> been written to it.
>
> The arrays used to store these inputs are the cellnum[],tmax[], and
> tmin[].
> The code never calls for re-initialiazations =/ What could be wrong? I've
> got my code below:
Jan-Erik is right, whenever you can't figure out what's going on, you should
try to isolate the problem to the smallest section of code. Which brings me
to the subject of refactoring, and other unsolicited advice. :-)
You have a lot of code written, and it looks interesting, but there is
plenty of room for improvement. The code is difficult to follow because:
- The project consists of a one long file
- Functions and variable names are not descriptive enough
- Most of the functions are doing too much
- Some of the functions do things that are none of their business, like
the phonebook() displaying temperature range on the LCD.
Here are a few suggestions to consider:
1. Split the project into several modules. For example, you could have
adc.c, lcd.c, phone.c, in addition to main.c.
2. Give your functions and variables more meaningful names. Function names
should be verbs, and variable names should be nouns. It's also good practice
to prepend global function names with the module name, for example
Phone_SendSms().
Some people omit the underscore, and write
AdcGetTemperature()
3. Split your long functions into several smaller functions. My rule of
thumb is that if a function is longer than 10 lines, it is a candidate for
splitting up.
4. Make sure each function does exactly what its name says it's supposed to
do.
These practices reinforce each other, and it is therefore best to use them
simultaneously. For example, #2 would help you practice #3 and #4. If your
function name is GetPhoneNumberValidateItAndSaveItToFlash() you know that it
needs to be split into three smaller functions:
GetPhoneNumber()
ValidatePhoneNumber()
SavePhoneNumber()
And if you find that inside ValidatePhoneNumber() you have code that
requests battery voltage, you know that it needs to be factored out into its
own GetBatteryVoltage() function.
Software development is about managing complexity. If you can't manage it,
you will drown in it.
Sincerely,
Vitaliy
2008\12\28@151413
by
Vitaliy
By the way, in order to successfully refactor your code, you need to use a
file versioning system. Subversion/TortoiseSVN are the de facto standard,
and are relatively easy to install and use on any PC.
2008\12\28@151817
by
Carl Denk
|
Thanks, sounds good to me.
Vitaliy wrote:
{Quote hidden}> Jan-Erik is right, whenever you can't figure out what's going on, you should
> try to isolate the problem to the smallest section of code. Which brings me
> to the subject of refactoring, and other unsolicited advice. :-)
>
> You have a lot of code written, and it looks interesting, but there is
> plenty of room for improvement. The code is difficult to follow because:
>
> - The project consists of a one long file
> - Functions and variable names are not descriptive enough
> - Most of the functions are doing too much
> - Some of the functions do things that are none of their business, like
> the phonebook() displaying temperature range on the LCD.
>
>
> Here are a few suggestions to consider:
>
> 1. Split the project into several modules. For example, you could have
> adc.c, lcd.c, phone.c, in addition to main.c.
>
>
> 2. Give your functions and variables more meaningful names. Function names
> should be verbs, and variable names should be nouns. It's also good practice
> to prepend global function names with the module name, for example
>
> Phone_SendSms().
>
> Some people omit the underscore, and write
>
> AdcGetTemperature()
>
>
> 3. Split your long functions into several smaller functions. My rule of
> thumb is that if a function is longer than 10 lines, it is a candidate for
> splitting up.
>
>
> 4. Make sure each function does exactly what its name says it's supposed to
> do.
>
>
> These practices reinforce each other, and it is therefore best to use them
> simultaneously. For example, #2 would help you practice #3 and #4. If your
> function name is GetPhoneNumberValidateItAndSaveItToFlash() you know that it
> needs to be split into three smaller functions:
>
> GetPhoneNumber()
> ValidatePhoneNumber()
> SavePhoneNumber()
>
> And if you find that inside ValidatePhoneNumber() you have code that
> requests battery voltage, you know that it needs to be factored out into its
> own GetBatteryVoltage() function.
>
> Software development is about managing complexity. If you can't manage it,
> you will drown in it.
>
> Sincerely,
>
> Vitaliy
>
>
>
2008\12\28@184801
by
Vitaliy
Carl Denk wrote:
> Thanks, sounds good to me.
Carl, I'm confused. Are you Mer13?
Vitaliy
2008\12\28@190100
by
Carl Denk
No, I'm just cdenk, no one else, not a scitzo (sp). I was monitoring the
thread. I do just a small amount programming, mostly "C", or a little
"Basic", but the guidelines you expressed sounded good, and I hoped to
add to things to try and do to keep my programming usable, well
documented, and easy to troubleshoot. Just thanking for the input. Sorry
if I caused confusion. Just giving credit for good input. :)
Vitaliy wrote:
> Carl Denk wrote:
>
>> Thanks, sounds good to me.
>>
>
> Carl, I'm confused. Are you Mer13?
>
> Vitaliy
>
2008\12\29@022854
by
Vitaliy
"Carl Denk" wrote:
> No, I'm just cdenk, no one else, not a scitzo (sp).
I didn't think you were, it's just that sometimes people have two or more
accounts. :)
Vitaliy
2008\12\29@100647
by
Tamas Rudnai
> I didn't think you were, it's just that sometimes people have two or more
> accounts. :)
Current account and savings? :-)
Tamas
On Mon, Dec 29, 2008 at 7:21 AM, Vitaliy <spam_OUTspamTakeThisOuT
maksimov.org> wrote:
> "Carl Denk" wrote:
> > No, I'm just cdenk, no one else, not a scitzo (sp).
>
> I didn't think you were, it's just that sometimes people have two or more
> accounts. :)
>
> Vitaliy
>
> -
2008\12\29@102825
by
Carl Denk
Modest, not rich and not a pauper :)
Tamas Rudnai wrote:
{Quote hidden}>> I didn't think you were, it's just that sometimes people have two or more
>> accounts. :)
>>
>
> Current account and savings? :-)
>
> Tamas
>
>
>
> On Mon, Dec 29, 2008 at 7:21 AM, Vitaliy <
.....spamKILLspam
@spam@maksimov.org> wrote:
>
>
>> "Carl Denk" wrote:
>>
>>> No, I'm just cdenk, no one else, not a scitzo (sp).
>>>
>> I didn't think you were, it's just that sometimes people have two or more
>> accounts. :)
>>
>> Vitaliy
>>
>> --
More... (looser matching)
- Last day of these posts
- In 2008
, 2009 only
- Today
- New search...