|
|
View previous topic :: View next topic |
Author |
Message |
jworam Guest
|
PCB compiler for 16F54 does compile but code won't run |
Posted: Fri May 08, 2009 11:18 pm |
|
|
Sorry to bother you but I appreciate your help on getting the #device *=8
straightened out.
But this code is actually old code that runs and is in production but compiled on old MPLABC compiler. I changed the syntax over to work with the PCB compiler.
Problem is that it looks like the oscillator is not running. (specified HS)
If I plug in the old chip it works just fine.
I ran the code on MPLAB simulator and it seems to be working in that mode.
The program is not that big.
Could I send it to you to get your 'global eye' view to see if I did something wrong?
Thank You Very Kindly,
JWORAM (Jim) |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Fri May 08, 2009 11:26 pm |
|
|
Quote: | Problem is that it looks like the oscillator is not running. (specified HS) |
CCS puts a hidden SLEEP instruction at the end of main(). If you let the
program run off the end of main(), the code will execute that instruction
and the PIC will go to sleep. The oscillator will shut off. Put a while(1);
statement at the end of main() to prevent this.
If that is not what's happening, then consider the following:
Start with a small program that just blinks an LED (using the HS fuse).
Get that working. This will prove that the hardware is correct and that
your code and the compiler version all all working.
Then use the same fuses in your large program. If it doesn't run, then
comment out the calls to your functions in main(), one at a time, until the
program does something. Then you will know the routine that is causing
the problem.
Quote: | but compiled on old MPLABC compiler. |
I have never heard of this compiler. Post a link to the webpage for it. |
|
|
jworam Guest
|
Thank you for the great tip,,, now just in case I have wrong |
Posted: Sat May 09, 2009 8:16 am |
|
|
This is what I had for code:
Code: | #include <16F54.h> //
#fuses NOWDT,NOPROTECT //
#use delay(clock=3686400)
#define BEEP 0 // pin 6 PORTB.0
#define RALARM 1 // pin 7 PORTB.1
#define TIME_OUT_LED 2 // pin 8 PORTB.2
#define OP_LED 3 // pin 9 PORTB.3
#define LOW_9V_LED 4 // pin 10 PORTB.4
#define AC_OUT_LED 5 // pin 11 PORTB.5
#define PUMP 6 // pin 12 PORTB.6
#define TEST_9V 7 // pin 13 PORTB.7
#define AC_STAT 0 // pin 17
#define STAT_9V 1 // pin 18
#define NU 2 // Pin 1
#define FLOAT_SW 3 // Pin 2
#define DOWN 1
#define FLTUP 0
#define OFF 1
#define ON 0
#define BEEP_OFF 0
#define BEEP_ON 1
#define ACTIVE 1
#define AC_OK 1
#define PUMP_ACCRU_TIME 10 // accrued pump time in minutes
#define TIME_TO_TEST_9V 8 // 8 sec, 1 min for testing, 15 minutes norm
#define BEEP_9V_EVERY_X_MINUTES 30 // 30 sec for testing
#define AC_alarm 0 // bit_test(flag_reg, AC_alarm)
#define excess_alarm 1 // bit_test(flag_reg, excess_alarm)
#define Alarm_9V 2 // bit_test(flag_reg, Alarm_9V)
#define OP_toggle 3 // bit_test(flag_reg, OP_toggle)
#define chirp_flag_9V 4 // bit_test(flag_reg, chirp_flag_9V)
#define extra_flag 5 // bit_test(flag_reg, )
#byte RTCC = 0x1 // Timer0 Module Register
//#byte PCL = 0x2 // Program Counter’s (PC) Least Significant Byte
#byte STATUS = 0x3 // IRP RP1 RP0 TO PD Z DC C
//#byte FSR = 0x4 // Indirect Data Memory Address Pointer
#byte PORTA = 0x5 // PORTA Data Latch when written; PORTA pins when read
#byte PORTB = 0x6 // PORTB Data Latch when written; PORTB pins when read
#byte WREG = 0x7 //
#byte OPTION_REG = 0x8
// 0x9 //
#byte PCLATH = 0xA // Write Buffer for the upper 5 bits of the Program Counter
#byte INTCON = 0xB // GIE PEIE TMR0IE INTE RBIE TMR0IF INTF RBIF
#byte PIR1 = 0xC // -- ADIF — — SSPIF CCP1IF TMR2IF TMR1IF
#byte PIR2 = 0xD //
#byte TMR1L = 0xE // Holding Register for the Least Significant Byte of the 16-bit TMR1 Register
#byte TMR1H = 0xF // Holding Register for the Most Significant Byte of the 16-bit TMR1 Register
#byte T1CON = 0x10 //
#byte TMR2 = 0x11 //
#byte T2CON = 0x12 //
#byte SSPBUF = 0x13 //
#byte SSPCON = 0x14 //
#byte CCPR1L = 0x15 //
#byte CCPR1H = 0x16 //
#byte CCP1CON = 0x17 //
#byte ADRESH = 0x1E // A/D Result Register High Byte
#byte ADCON0 = 0x1F //
// 0x20 //
// to //
// 0x7F General Pourpose RAM 96 bytes
//#byte OPTION_REG = 0x81 //
//#byte PCL = 0x82 //
//#byte STATUS = 0x83 //
//#byte FSR = 0x84 //
//#byte TRISA = 0x85 //
//#byte TRISB = 0x86 //
// --- = 0x87, 0x88, 0x89
//#byte PCLATH = 0x8A //
//#byte INTCON = 0x8B //
//#byte PIE1 = 0x8C // PERIPHERAL INTERRUPT ENABLE REGISTER 1 (ADDRESS 8Ch)
//#byte PIE2 = 0x8D // PERIPHERAL INTERRUPT ENABLE REGISTER 2 (ADDRESS 8Dh)
//#byte PCON = 0x8E // POWER CONTROL REGISTER (ADDRESS 8Eh)
//#byte OSCCON = 0x8F // OSCCON = 0110 0X00 = 4 MHz X = IOFS (frequency stable bit)
//#byte OSCTUNE = 0x90 //
// = 0x91 //
//#byte PR2 = 0x92 //
//#byte SSPADD = 0x93 //
//#byte SSPSTAT = 0x94 //
// function prototypes
void main(void);
void general_timer_functions(void);
void hours_timer(void);
void clrvar(void);
void check_float(void);
void check_antilock(void);
void check_9V_battery(void);
void beep_9V_low(void);
void check_ac_power(void);
void beep_alarms(void);
void check_charging_battery(void);
// C static global variables
unsigned char ms_cntr;
unsigned char seconds;
unsigned char minutes;
unsigned char hours;
unsigned char delay_timer; //
unsigned char timer_9V_read;
unsigned char timer_9V_beep;
unsigned char Chirp_timer_9V;
unsigned char RTCC_trip_value;
unsigned char beep_toggle_timer;
unsigned char pump_antilock_state;
unsigned char beep_state;
unsigned char alarm_state_9V;
// Flags register
unsigned char flag_reg;
// 7 6 5 4 3 2 1 0
// | | | | | | \- AC_alarm;
// | | | | | \ --- excess_alarm;
// | | | | \------- Alarm_9V;
// | | | \---------- OP_toggle;
// | | \------------- chirp_flag_9V;
// | \---------------- extra flag
// \------------------- extra flag
//
unsigned char pump_timer;
unsigned char antilock_timer;
unsigned char pump_accru_timer;
//void __STARTUP()
//{
// init();
//}
void main(void)
{
clrvar();
while(delay_timer > 0){ // startup delay for voltages to stabilize
//CLRWDT(); // clear watchdog timer
general_timer_functions();
}
while(1){
//CLRWDT(); // clear watchdog timer
general_timer_functions(); // check RTCC
check_ac_power();
check_float();
check_antilock();
check_9V_battery();
beep_9V_low();
beep_alarms();
}
}
void check_float(void)
{
if(bit_test(PORTA,FLOAT_SW) == 0){ // FLOAT_SW FLTUP Float is up (0) PORTA.3 // Pin 2
pump_timer = 7; // timer will extend pumping
if(pump_accru_timer > PUMP_ACCRU_TIME){ // if pumping for 10 min
pump_accru_timer = PUMP_ACCRU_TIME+1; // fix to prevent recurrence
bit_set(flag_reg, excess_alarm); // = 1; set the excess alarm flag.
bit_clear(PORTB,TIME_OUT_LED); // TIME_OUT_LED = ON;// LED on
bit_clear(PORTB,RALARM); // RALARM = 0; Remote alarm active, relay off, was 1
}
}
else{ // FLOAT_SW == DOWN (1) 4/28/2009
//
bit_clear(flag_reg, excess_alarm); // = 0;
if(bit_test(flag_reg, AC_alarm) == 0){ // No AC alarm, Relay on, remote alarm off.
bit_set(PORTB,RALARM); // If AC is OK, no alarm, relay on. was 0
}
bit_set(PORTB,TIME_OUT_LED); // TIME_OUT_LED = OFF
// check for the antilock condition
if(pump_timer > 0){ // pumping even though the float is down (timing out)
// Here the pump is still pumping but the float is down.
// if the pump_accru_timer is greater than 1 minute,
//
if(pump_accru_timer > 1){
if(bit_test(PORTA,FLOAT_SW) == DOWN){ // (float is down) (1) FLOAT_SW
pump_antilock_state = 1;
}
}
}
if(pump_antilock_state == 0){
pump_accru_timer = 0;
}
}
}
/* ************************************************************ */
/* After pumping for 1 minute: */
/* Put in a 3 second 'off time' when the float switch is */
/* seen to go down while the pump is still pumping. */
/* When the 10 sec timer times out, the state will */
/* be set and the pump will shut off for 2 sec to relieve */
/* the air lock. */
/* ************************************************************ */
void check_antilock(void)
{
switch(pump_antilock_state)
{
case 0: // 0 holding state
break; //
case 1:
antilock_timer = 2; // reset the up timer
pump_antilock_state = 2;
break;
case 2:
if(antilock_timer == 0){
bit_clear(PORTB,PUMP); // = 0; pump off for 2 sec
antilock_timer = 2; // set delay for 2 sec.
pump_antilock_state = 3;
}
break;
case 3:
if(antilock_timer == 0){ // 2 seconds of pump off
bit_set(PORTB,PUMP); // pump on
pump_antilock_state = 0;
}
break;
default:
pump_antilock_state = 0;
}
}
void check_9V_battery(void)
{
switch(alarm_state_9V){
case 0:
if(timer_9V_read >= TIME_TO_TEST_9V){
bit_set(PORTB,7); // TEST_9V = 1; // turn on the 9V test circuit.
alarm_state_9V = 1;
}
break;
case 1:
if(timer_9V_read >= TIME_TO_TEST_9V + 2){ // + 2 sec
bit_set(PORTB,7); // = 1; turn on the 9V test circuit.
alarm_state_9V = 2;
}
break;
case 2:
if(bit_test(PORTA.1) == 1){ // STAT_9V Read the state of the 9V battery
bit_set(flag_reg, Alarm_9V); // = 0x1; // if high, the battery is low. chirp will start
bit_clear(PORTB,LOW_9V_LED); // LOW_9V_LED = ON 0;
}
else{
bit_clear(flag_reg, Alarm_9V); // = 0x0; // if the pin is low, battery is good.
bit_set(PORTB,LOW_9V_LED); // LOW_9V_LED = OFF 1
}
bit_clear(PORTB,7); // = 0; turn off the 9V test circuit.
timer_9V_read = 0x0;
alarm_state_9V = 0;
break;
default:
alarm_state_9V = 0;
}
}
void beep_9V_low(void)
{
if(timer_9V_beep >= BEEP_9V_EVERY_X_MINUTES){
if(bit_test(flag_reg, Alarm_9V) == 1){
if(beep_toggle_timer >= 6){ // Window of no beeping, ok to chirp
bit_set(flag_reg, chirp_flag_9V); // = 1;
timer_9V_beep = 0;
}
}
}
}
void beep_alarms(void)
{
if(beep_toggle_timer > 10){
beep_toggle_timer = 0;
}
switch(beep_state){ // Regular alarm beeps
case 0:
if((bit_test(flag_reg, excess_alarm) == 0x1) ||
(bit_test(flag_reg, AC_alarm) == 0x1)){
if(beep_toggle_timer == 1){ // align on 1
bit_set(PORTB,BEEP); // = BEEP_ON;
beep_state = 1;
}
}
break;
case 1:
if(beep_toggle_timer == 2){ // pick up on 2
bit_clear(PORTB,BEEP); // = BEEP_OFF;
beep_state = 0;
}
else{
bit_set(PORTB,BEEP); // = BEEP_ON;
}
break;
default:
beep_state = 0;
}
}
void clrvar(void)
{
WREG = 0x7; // 0x7 = /256, 0x0 = /2, 0x1 = /4 is for accelerated tests
// 3.6864 Mhz / 4 / 2 = 460,800 =
// 0 would make the time 128 times faster
// so 12 hr would be 275.15 sec = 4.58 min
OPTION_REG = 7; // OPTION(); set the prescaler, use osc/4
set_tris_a(0x0F); // __TRIS(0x0F, PORTA); Set up RA0,1,2,3 for input
PORTA = 0xF;
set_tris_b(0x00); // __TRIS(0x00, PORTB); Set up port B
PORTB = 0x0; //
bit_set(PORTB,2); // LED off
ms_cntr = 0;
seconds = 0;
bit_clear(flag_reg, excess_alarm); // = 0;
beep_toggle_timer = 0;
flag_reg = 0;
pump_accru_timer = 0;
pump_timer = 0;
antilock_timer = 0;
pump_antilock_state = 0; // 12/29/2008
//pump_antilock_minute_timer = 0;
delay_timer = 10;
RTCC_trip_value = 240;
timer_9V_read = TIME_TO_TEST_9V-4;
timer_9V_beep = BEEP_9V_EVERY_X_MINUTES;
bit_clear(flag_reg, chirp_flag_9V); // = 0;
Chirp_timer_9V = 0;
alarm_state_9V = 0;
beep_state = 0;
bit_clear(flag_reg, Alarm_9V); // = 0;
bit_set(PORTB,AC_OUT_LED); // AC_OUT_LED = OFF;
bit_set(PORTB,2); // = OFF; LED off
bit_set(PORTB,4); // = OFF;
bit_clear(PORTB,BEEP); // = BEEP_OFF;
bit_clear(PORTB,7); // turn off the 9V test circuit.
bit_set(PORTB,RALARM); // = 1; // No alarm, Relay on
}
/* ********************************************************************** */
/* Track the ms_cntr via RTCC */
/* JWO */
/* Formula: 921600/256/240/15 = 1 Sec */
/* Formula: 921600/4/240/15 = 1 Sec (fast mode 64x) */
/* ********************************************************************** */
void general_timer_functions(void)
{
// 3.6864 Mhz software
// Osc/4 = 921600 Hz
// Osc/4 = 3.6864 Mhz/4 = 921600 Hz
// prescaler (WREG = 7)= /256
// prescaler (WREG = 6)= /128
// prescaler (WREG = 5)= /64
// prescaler (WREG = 4)= /32
// prescaler (WREG = 3)= /16
// prescaler (WREG = 2)= /8
// prescaler (WREG = 1)= /4
// prescaler (WREG = 0)= /2
// Osc/4 /Prescaler / RTCC = 921600/256/240 = 15 Hz = 66.666 ms
if (RTCC == RTCC_trip_value){ // 921600/256/240 = 15 Hz = 66.666 ms
//
RTCC_trip_value = 240 + RTCC; //
ms_cntr++;
if(bit_test(flag_reg, chirp_flag_9V) == 1){ // 9V Batt is low, flag is set, look for window.
if(ms_cntr == 7){ // start the 9V chirp when 9V is low
bit_set(PORTB,BEEP); // = BEEP_ON;
}
if(ms_cntr == 9){ // End the 9V chirp here.
bit_clear(PORTB,BEEP); // = BEEP_OFF; ok to turn off beeper
bit_clear(flag_reg, chirp_flag_9V); // = 0 clear the chirp flag til next chirp time.
}
}
if(ms_cntr >= 15){ // 921600/256/240/15 = 1 Sec
ms_cntr = 0;
seconds++;
timer_9V_read++; // read 9V Batt, use seconds for 9V testing
timer_9V_beep++; // beep when 9V is low
beep_toggle_timer++;
if(antilock_timer > 0){
antilock_timer--; // count down
}
if(pump_timer > 0){ // if float switch is up, timer is set, keep pumping
pump_timer--;
if(pump_antilock_state == 0){ // special case when we want to
bit_set(PORTB,PUMP); // shut the pump off
}
}
else{
//
if(pump_antilock_state == 0){
bit_clear(PORTB,PUMP); // = 0; PUMP OFF
}
}
if(delay_timer > 0){ // used on startup delay
delay_timer--;
}
if(bit_test(flag_reg, OP_toggle) == 1){ // Flash the 'operating' LED
bit_set(PORTB,OP_LED); // OP_LED = 1;
bit_clear(flag_reg, OP_toggle); // = 0;
}
else{
bit_clear(PORTB,OP_LED); // = 0;
bit_set(flag_reg, OP_toggle); // = 1;
}
if(seconds > 59){ // 0 to 59 = 60 sec
seconds = 0;
minutes++;
pump_accru_timer++; // increment every minute
//
if(minutes >= 60){ // 0 to 59 = 60 min
minutes = 0;
hours++;
if(hours >= 24){ // 24 hours
hours = 0;
}
}
}
} // end if ms_cntr
} // If RTCC
}
void check_ac_power(void)
{
if(bit_test(PORTA.0) != AC_OK){ // confirmed power failure AC_STAT
bit_clear(PORTB,AC_OUT_LED); // = ON;
bit_set(flag_reg, AC_alarm);
bit_clear(PORTB,RALARM); // = 0; Remote alarm active, relay off, was 1
}
else{ // AC is ok
bit_clear(flag_reg, AC_alarm); // clear the alarm, but still flashes
bit_set(PORTB,AC_OUT_LED); // = OFF;
if(bit_test(flag_reg, excess_alarm) == 0){
bit_set(PORTB,RALARM); // RALARM = 1; No remote alarm, (relay on), if no excess pumping, was 0
}
}
} |
|
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sat May 09, 2009 10:43 am |
|
|
Quote: | #include <16F54.h> //
#fuses NOWDT,NOPROTECT //
#use delay(clock=3686400)
Problem is that it looks like the oscillator is not running. (specified HS)
|
You don't have an oscillator fuse. It defaults to RC mode. Of course
it won't run.
Quote: | if(bit_test(PORTA.1) == 1){
if(bit_test(PORTA.0) != AC_OK){
PCB compiler for 16F54 does compile
|
It doesn't compile. That's not the proper syntax for the bit_test()
function. It should be a comma, not a period. |
|
|
jworam Guest
|
MPLABC compiler |
Posted: Sun May 10, 2009 1:57 pm |
|
|
Dear CCS Guru,
Thanks for your help getting my code fixed, it seems that I am getting old and the screen looks fuzzy even though I wear reading glasses.
On my screen a period looks like a comma.
The code is starting to run, leds flashing, but still some problem with the old code ported into the new program using CCS PCB compiler where the rtcc does not respond like it did using MPLABC.
Does not work: (but used to and is in production now)
Code: | if(RTCC == RTCC_trip_value){
RTCC_trip_value = 240 + RTCC; // (never catches the equality) |
Does work... but too fast...
Code: | if(RTCC >= RTCC_trip_value){
RTCC_trip_value = 240 + RTCC; // |
MPLABC Ver 1.10 released in 1993, is a very old compiler that I got back in 1996. Runs on Win 98 but may run on XP.
Designed by Microchip and Byte Craft Limited, works well and is very efficient generating compact code.
Thank You Very Kindly,
Jim jworam |
|
|
jworam Guest
|
Mistake found it using rtcc |
Posted: Sun May 10, 2009 2:18 pm |
|
|
Dear CCS Guru,
I found an error that my be causing the problem with rtcc
code should be:
if(get_timer0() == RTCC_trip_value){
not
if(RTCC == RTCC_trip_value){
Thank You Very Kindly,
jworam,
CCS dummy |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 10, 2009 2:25 pm |
|
|
Quote: | #byte WREG = 0x7
#byte OPTION_REG = 0x8
void clrvar(void)
{
WREG = 0x7;
OPTION_REG = 7; |
Your method of setting the Option register won't work. The Option register
does not have an address of 8. You can't set it by writing to an address.
It has to be done with the OPTION instruction. CCS has provided a
macro to do this. It's in the Ex_Macro.c file. Here it is:
Insert this code above main() in your program.
Code: |
#define set_options(value) {#ASM \
MOVLW value \
OPTION \
#ENDASM}
|
Then call it with the parameter that you want to set the Option register to.
Example:
Also, get rid of WREG and OPTION_REG (both the #byte statements and
the code that references them). Use the code above instead.
Then the Timer0 pre-scaler will be setup correctly. |
|
|
jworam Guest
|
OPTION register code from list file, works |
Posted: Sun May 10, 2009 8:00 pm |
|
|
Dear CCS Guru,
Thank you for the tip. I did it and it works !
I think the problem was porting old MPLABC code to CCS PCB code, typo errors, the need to get new glasses, not to mention a brain overhaul.
There were a lot of things left over from the old C code that were wrong.
and I am too dumb to spot them.
just to make sure I include the LST code:
Code: |
....................
.................... set_options(0x07); // as per CCS
0145: MOVLW 07
0146: OPTION
....................
.................... set_rtcc(240);
0147: MOVLW F0
0148: MOVWF 01 |
Does this look ok?
Thank You Very Kindly,
Jim jworam |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Sun May 10, 2009 8:23 pm |
|
|
The list file code looks OK.
Also, you should call me by my screen name (PCM), and not "guru". |
|
|
jworam Guest
|
Correct |
Posted: Mon May 11, 2009 1:17 pm |
|
|
Dear PCM,
How can I thank you?
A box of donuts? :-) or just thanks and buy more CCS products, of course!
Thanks Very Kindly,
jworam |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Mon May 11, 2009 1:37 pm |
|
|
I don't work for CCS. |
|
|
drh
Joined: 12 Jul 2004 Posts: 192 Location: Hemet, California USA
|
|
Posted: Tue May 12, 2009 8:09 am |
|
|
PCM programmer wrote: | I don't work for CCS. |
Too bad for them, they and us could benefit from the help. _________________ David |
|
|
|
|
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
|