CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

#define and port bit value testing

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Jim McBride



Joined: 24 Mar 2004
Posts: 21

View user's profile Send private message

#define and port bit value testing
PostPosted: Thu May 27, 2004 4:22 pm     Reply with quote

I have written some code and tested it with MPLAB simulator. However I am getting unexpected results when running a circuit with the debugger. I think I am doing something grossly wrong while just trying to make my code readable. Here is the basic pertinent code snippet:

#define Trig_source PIN_D0
int internal = 1, external = 0,on = 1;
while(on)
{
if(input(Trig_source) == internal)//internal
run_internal();
else//external
run_external();
}

This always tests false no matter what is on PIN_D0.
The simulator seemed to work fine using stimulous.
A mouse over shows Trig_source to have a value of 31768, as defined is the 18F452.h file.
Any ideas?
Haplo



Joined: 06 Sep 2003
Posts: 659
Location: Sydney, Australia

View user's profile Send private message

PostPosted: Thu May 27, 2004 5:11 pm     Reply with quote

Hard to say...post the full code.
Guest








PostPosted: Thu May 27, 2004 5:20 pm     Reply with quote

Ok, but be warned, not too short.

#include "C:\Documents and Settings\mcbride7\Desktop\PCS DAC simulator\pcs_code\PCS_DAS_simulator.h"
//#include "A:\pcs_code\PCS_DAS_simulator.h"
#ZERO_RAM

#define T1 PIN_B3
#define T3LED PIN_B4
#define trig_55 PIN_B5

#define cables_1_1 PIN_C0
#define cables_55 PIN_C1
#define cables__46 PIN_C2
#define cables__55 PIN_C3
#define igref_1 PIN_C4
#define igref__05 PIN_C5
#define pilc_55 PIN_C6
#define pilc__46 PIN_C7

#define Trig_source PIN_D0
#define Trig_mode PIN_D1
#define Man_trigger PIN_D2
#define Int_trig_select PIN_D3
#define Shot_mode_full_pilc PIN_D4
#define Shot_mode_mesm PIN_D5
#define T2LED PIN_D6
#define T1LED PIN_D7

int internal = 1, external = 0,
manual = 1, automatic = 0,
mesm = 1, pilc = 0, full = 1,
on = 1, off = 0, dummyVar, delay_remainder;
int16 shot_delay, delay_loops;


#int_EXT//T1
EXT_isr()
{
output_high(T1LED);
output_high(T1);
output_low(T1);
delay_us(50);
output_low(T1LED);
}

#int_EXT1//T2
EXT1_isr()
{
output_c(0b01000010);
//cables +.55 on
//PILC +.55 on
output_high(T2LED);//T2LED on
output_high(trig_55);//Triggers +.55 on
delay_us(20);
output_low(trig_55);//Triggers off
delay_us(30);
output_c(0b10000100);
//cables -.46 on
//PILC -.46 on
//cables +.55 off
//PILC +.55 off
output_low(trig_55);//triggers +.55 off
output_low(T2LED);//T2LED off
delay_us(60);
output_c(0b00000000);
//cables -.46 off
//PILC -.46 off
}

#int_EXT2//T3
EXT2_isr()
{
output_b(0b00110000);
output_c(0b00010001);
//T3LED on
//cables +1.1 on
//ignd,iref +.1 on
//Triggers +.55 on
delay_us(20);
output_low(trig_55);//Triggers +.55 off
delay_us(30);
output_low(T3LED);//T3LED off
delay_us(150);
output_c(00101000);
//cables -.55 on
//ignd,iref -.05 on
//cables +1.1 off
//ignd,iref +.1 off
delay_us(400);
output_c(0b00000000);
//cables -.55 off
//ignd,iref -.05 off
}

init_lamp_check()
{
output_high(T1LED);
output_high(T2LED);
output_high(T3LED);
delay_ms(2000);
output_low(T1LED);
output_low(T2LED);
output_low(T3LED);
delay_ms(2000);
output_high(T1LED);
output_high(T2LED);
output_high(T3LED);
delay_ms(2000);
output_low(T1LED);
output_low(T2LED);
output_low(T3LED);
}

pilc_shot()//T1,T2
{
//T1
output_high(T1);
output_low(T1);
output_high(T1LED);
delay_us(50);
output_low(T1LED);
delay_ms(50);
//T2 @ T1 + 100 ms
output_c(0b01000010);
//cables +.55 on
//PILC +.55 on
output_high(T2LED);//T2LED on
output_high(trig_55);//Triggers +.55 on
delay_us(20);
output_low(trig_55);//Triggers +.55 off
delay_us(30);
output_c(0b10000100);
//cables -.46 on
//PILC -.46 on
//cables +.55 off
//PILC +.55 off
output_low(T2LED);//T2LED off
delay_us(60);
output_c(0b00000000);
//output_low(cables__46);//cables -.46 off
//output_low(pilc__46);//PILC -.46 off
}

mesm_shot()//T1,T3
{
//T1
output_high(T1);
output_low(T1);
output_high(T1LED);
delay_us(50);
output_low(T1LED);
delay_ms(350);
//T3 @ T1 + 400 ms
output_b(0b00110000);
output_c(0b00010001);
//T3LED on
//cables +1.1 on
//ignd,iref +.1 on
//Triggers +.55 on
delay_us(20);
output_low(trig_55);//Triggers +.55 off
delay_us(30);
output_low(T3LED);//T3LED off
delay_us(150);
output_c(00101000);
//cables -.55 on
//ignd,iref -.05 on
//cables +1.1 off
//ignd,iref +.1 off
delay_us(400);
output_c(0b00000000);
//cables -.55 off
//ignd,iref -.05 off
}

full_shot()//T1,T2,T3
{
//T1
output_high(T1);
output_low(T1);//~200 ns on time
output_high(T1LED);
delay_us(50);
output_low(T1LED);
delay_ms(50);
//T2 @ T1 + 100 ms
output_c(0b01000010);
//cables +.55 on
//PILC +.55 on
output_high(T2LED);//LED on
output_high(trig_55);//Triggers +.55 on
delay_us(20);
output_low(trig_55);//Triggers +.55 off
delay_us(30);
output_c(0b10000100);
//cables -.46 on
//PILC -.46 on
//cables +.55 off
//PILC +.55 off
output_low(T2LED);//LED off
delay_us(60);
output_c(0b00000000);
//output_low(cables__46);//cables -.46 off
//output_low(pilc__46);//PILC -.46 off
delay_us(190);
//T3 @ T1 + 400 ms
output_b(0b00110000);
output_c(0b00010001);
//LED on
//cables +1.1 on
//ignd,iref +.1 on
//Triggers +.55 on
delay_us(20);
output_low(trig_55);//Triggers +.55 off
delay_us(30);
output_low(T3LED);//LED off
delay_us(150);
output_c(00101000);
//cables -.55 on
//ignd,iref -.05 on
//cables +1.1 off
///ignd,iref +.1 off
delay_us(400);
output_c(0b00000000);
//cables -.55 off
//ignd,iref -.05 off
}


run_internal()
{
while (input(Trig_source) == internal)//internal
{
if (input(Int_trig_select) == manual)//manual
{
if (input(Man_trigger) == on)//on
{
if (input(Shot_mode_mesm) == mesm)//mesm
mesm_shot();
else if(input(Shot_mode_full_pilc) == full)//full
full_shot();
else //(input(Shot_mode_full_pilc) == pilc)//pilc
pilc_shot();
}
}
while (input(Int_trig_select) == automatic)//automatic
{
set_adc_channel(0);
delay_us(10);
shot_delay = 2000 + (63 * read_adc());//shot_delay;// = 2,000 - 10,001,adc=0-127
delay_loops = shot_delay / 255;
shot_delay = shot_delay % 255;
delay_remainder = shot_delay;
while (delay_loops)
{
delay_ms(255);//.5Hz - .1Hz, 2000 - 10,001 us
--delay_loops;
}

delay_ms(delay_remainder);
if (input(Shot_mode_mesm) == mesm)//mesm
mesm_shot();
else if(input(Shot_mode_full_pilc) == full)//full
full_shot();
else //(input(Shot_mode_full_pilc) == pilc)//pilc
pilc_shot();
}
}

}

run_external()
{
enable_interrupts(global);
while (input(Trig_source) == external)//external
{
//wait for interrupts and service
}
disable_interrupts(global);
}


void main() {

setup_adc_ports(RA0_ANALOG);
setup_adc(ADC_CLOCK_DIV_2);
setup_psp(PSP_DISABLED);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
ext_int_edge(H_TO_L);
ext_int_edge( 1, H_TO_L);
ext_int_edge( 2, H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
//set_tris_a (value)
set_tris_b (0b11000111);
set_tris_c (0b00000000);
set_tris_d (0b00111111);
//set_tris_e (value)
//enable_interrupts(global);
delay_ms(2000);

init_lamp_check();
internal = input(Trig_source);
while(on)
{
if(input(Trig_source) == internal)//internal
run_internal();
else//external
run_external();
}

}//end main
ninjanick



Joined: 25 May 2004
Posts: 25

View user's profile Send private message

Re: #define and port bit value testing
PostPosted: Sat May 29, 2004 12:04 am     Reply with quote

Jim McBride wrote:


#define Trig_source PIN_D0
int internal = 1, external = 0,on = 1;
while(on)
{
if(input(Trig_source) == internal)//internal
run_internal();
else//external
run_external();
}


I didn't look at the entire code, but have you tried if(input(Trig_source) only? Could be a mismatch between int and the value returned from input(). Also, to save some space you can use an int1 identifier for internal, external, on since they are only 1 bit.
Ttelmah
Guest







Re: #define and port bit value testing
PostPosted: Sat May 29, 2004 4:38 am     Reply with quote

Jim McBride wrote:
I have written some code and tested it with MPLAB simulator. However I am getting unexpected results when running a circuit with the debugger. I think I am doing something grossly wrong while just trying to make my code readable. Here is the basic pertinent code snippet:

#define Trig_source PIN_D0
int internal = 1, external = 0,on = 1;
while(on)
{
if(input(Trig_source) == internal)//internal
run_internal();
else//external
run_external();
}

This always tests false no matter what is on PIN_D0.
The simulator seemed to work fine using stimulous.
A mouse over shows Trig_source to have a value of 31768, as defined is the 18F452.h file.
Any ideas?


As a purely 'personal' comment here, you are making the code bulkier than it needs to be. You are reading a port bit, potentially converting this to an integer, and then performing a comparison. It is quicker, and probably less likely to have errors, to take advantage of the #bit format instead. Hence:

#byte PORTD=0x??? //set this to suit your processor
#bit Trig_source=PORTD.0

Then code as:

if (Trig_source) {
//Here internal

}
else {
//Here external

}

The 'if' test here, becomes a single bit_test operation on the processor (assuming fast_io is being used), and gives less likely errors, and faster code...

Best Wishes
Jim McBride



Joined: 24 Mar 2004
Posts: 21

View user's profile Send private message

PostPosted: Tue Jun 01, 2004 10:06 am     Reply with quote

Gentlemen,
Thank you for your replies. I do agree that the code is bulky. But it was written so that the next person should be able to read it easily and see what I was trying to do. I did try to use #bit. And it did not work for me either. The code reads right, simulates right. So the problem must be physical right? YUP. It turned out that my solderless breadboard needs its power busses jumpered half way down the board. I was plugging into the high and low buss but there was not voltage or ground present. Everything works great now. I will take note of your comments.
Thanks!!
Ttelmah
Guest







PostPosted: Tue Jun 01, 2004 10:22 am     Reply with quote

Jim McBride wrote:
Gentlemen,
Thank you for your replies. I do agree that the code is bulky. But it was written so that the next person should be able to read it easily and see what I was trying to do. I did try to use #bit. And it did not work for me either. The code reads right, simulates right. So the problem must be physical right? YUP. It turned out that my solderless breadboard needs its power busses jumpered half way down the board. I was plugging into the high and low buss but there was not voltage or ground present. Everything works great now. I will take note of your comments.
Thanks!!

Glad you found the problem. However there is no reason to make it bulkier to make it plain.
#byte PORTD=0x??? //set this to suit your processor
#bit Trig_source_internal=PORTD.0

Then code as:

if (Trig_source_internal) {

}
else {
//Here external

}

Makes it about as plain as possible, and results in small code too...

Best Wishes
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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