|
|
View previous topic :: View next topic |
Author |
Message |
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
sprintf resetting PIC24 |
Posted: Wed Dec 12, 2012 2:44 pm |
|
|
I am using sprintf to format a string, but the PIC keeps on resetting if I use the sprintf. If I remove the sprintf then no problem. I have replaced the sprintf with writing the data in one byte at a time and then everything works OK, tried to use sprintf to tidy the code up and more readable.
CCS 4.140
PIC24F16KA302
Osc.h
Code: |
#include <24F16KA302.h>
//#device ICD=TRUE
#DEVICE ADC=12
#include <stdlib.h>
#include <math.h>
//#FUSES WPRES32 //Watch Dog Timer PreScalar 1:32
//#FUSES WPOSTS3 //Watch Dog Timer PostScalar 1:4
#FUSES SOSC_DIGITAL
#FUSES NOWRT //Program Memory Write Protected
#FUSES NOPROTECT //Code protected from reads
#FUSES NOIESO //Internal External Switch Over mode disabled
#FUSES OSCIO //OSC2 is general purpose output
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
//#FUSES WDT_NOSL //Watch Dog Timer, disabled during SLEEP
//#FUSES DEBUG //Debug mode for use with ICD
#FUSES FRC_PLL
#FUSES NOWDT // No Watchdog timer
#CASE
//#use delay(clock=32000000,RESTART_WDT)
#use standard_IO(A)
#use standard_IO(B)
//--- IO assignment for LCD PG12864 --------------------------------------------
#define A0 PIN_B8 //A0
#define CS PIN_B12
#define SCL PIN_B5
#define SDA PIN_B6
#define RES PIN_B10
#define VBat 0
#define V1Ch 5
#define V2Ch 1
#define I1Ch 4
#define I2Ch 15
#byte SPI1STAT = 0x0240
#byte SPI1CON2 = 0x0244
#define REG_VOLT 338 //Voltage output of Reference
extern int8 Cnt;
extern char BatDisp[10];
extern char V1Disp[10];
extern char V2Disp[10];
extern char I1Disp[10];
extern char I2Disp[10];
extern int8 GainTest;
int1 Temp;
void delay_ms(int16 delay);
|
Osc1.c
Code: |
#include <Osc1.h>
//--- Add modules headers ------------------------------------------------------
#include "font_eb.h"
#include "PgSer.h"
#include "ADC.h"
#include "PGA.h"
//--- Add module code ----------------------------------------------------------
#include "PgSer.c"
#include "ADC.c"
#include "PGA.c"
char BVolt[12]= "Bat = ";
char V1Volt[12]="V1 = ";
char V2Volt[12]="V2 = ";
char I1Volt[12]="I1 = ";
char I2Volt[12]="I2 = ";
int8 Cnt;
char BatDisp[10];
char V1Disp[10];
char V2Disp[10];
char I1Disp[10];
char I2Disp[10];
int8 GainTest;
#int_TIMER2
void TIMER2_isr(void)
{
// Test Code flashing LED
Cnt = Cnt + 1;
if (Cnt >= 5) {
Temp = input(PIN_A2);
Temp = !Temp;
output_bit(PIN_A2,Temp);
Cnt = 0;
}
}
#int_SPI2
void SPI2_isr(void)
{
//Finished writing, disable CS
output_bit(CS,1);
}
void delay_ms(int16 delay) {
int16 i;
int16 j,a;
j= delay;
for (j=0;j<delay;j++) {
for (i=0;i<delay;i++) {
a=i*3;
a=a*4;
}
}
}
void main()
{
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4 | SPI_XMIT_L_TO_H | SPI_MODE_8B | SPI_SS_DISABLED);
setup_spi2(SPI_MASTER | SPI_L_TO_H | SPI_CLK_DIV_4);
setup_adc_ports(sAN0 | sAN1 | sAN4 | sAN5 | sAN15);
setup_adc(ADC_CLOCK_INTERNAL | ADC_TAD_MUL_31);
//setup_adc(ADC_CLOCK_DIV_32 | ADC_TAD_MUL_16);
setup_timer2(TMR_INTERNAL |TMR_DIV_BY_256 ,6250); //100ms interrupt
setup_timer4(TMR_DISABLED |TMR_DIV_BY_1 ,0);
// setup_timer3(TMR_INTERNAL |TMR_DIV_BY_64 ,250); //1ms interrupt
setup_timer5(TMR_DISABLED |TMR_DIV_BY_1 ,0);
setup_oscillator(OSC_INTERNAL,8000000);
setup_timer1(TMR_DISABLED);
//Define TRIS REG
enable_interrupts(INT_TIMER2);
glcd_Init(); //initialize LCD
while(TRUE){ //loop forever
ReadBatVolt();
ReadV1();
ReadV2();
delay_ms(1000);
}
} |
ADC.c
Code: |
void ReadBatVolt(void) {
int16 adcVal;
float BatVolt;
set_adc_channel(VBat);
delay_ms(1);
adcVal = read_adc();
BatVolt = ((float)adcVal*REG_VOLT/88200);
sprintf(BatDisp,"%4.2f V",BatVolt);
}
void ReadV1(void) {
int16 adcVal;
float Volt1;
set_adc_channel(V1Ch);
delay_ms(1);
adcVal = read_adc();
if (adcVal < 12) { //Overflow of positive signal
strcpy(V1Disp,"OFlow");
return;
}
adcVal = (GV1Off[pgaGV1E]-adcVal)/pgaGV1A;
Volt1 = ((float)adcVal*REG_VOLT*0.12/4096);
sprintf(V1Disp,"%4.2f V",Volt1);
}
void ReadV2(void) {
int16 adcVal;
float Volt;
set_adc_channel(V2Ch);
delay_ms(1);
adcVal = read_adc();
if (adcVal < 12) { //Overflow of positive signal
strcpy(V2Disp,"OFlow");
return;
}
adcVal = (GV2Off[pgaGV2E]-adcVal)/pgaGV2A;
Volt = ((float)adcVal*REG_VOLT*0.12/4096);
sprintf(V2Disp,"%4.2f V",Volt);
}
|
Thanks Alan |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9242 Location: Greensville,Ontario
|
|
Posted: Wed Dec 12, 2012 3:36 pm |
|
|
comment...
I'm curious as to how the compiler handled your 'delay_ms()' function when it already has one.
I'd have thought some sort of error message would have occoured... |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19538
|
|
Posted: Wed Dec 12, 2012 3:59 pm |
|
|
Commonest reason for printf/sprintf problems on PIC24. Stack not large enough.
Best Wishes |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Dec 12, 2012 11:13 pm |
|
|
Thanks for replies.
temtronic, I always battle with the CCS delay when simulating or debugging. On production code I use CCS. I found if I just define after #USE delay, compiler sees it as overload and uses that.
Ttelmah, any suggestions or work around. If I do it 'manually' it generates a lot of overhead code, eg. on 16k PIC ROM used go from 46% to 54%.
Thanks again |
|
|
alan
Joined: 12 Nov 2012 Posts: 357 Location: South Africa
|
|
Posted: Wed Dec 12, 2012 11:30 pm |
|
|
Sorry Tlelmah, I missed the solution last night, already on forum.
Need to include
|
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|