|
|
View previous topic :: View next topic |
Author |
Message |
soulweed
Joined: 20 Oct 2006 Posts: 23
|
Please tell me where code wrong |
Posted: Thu Mar 22, 2007 2:38 pm |
|
|
Code: |
#include<18f452.h>
#include <stdlib.h>
#include <CTYPE.h>
#include <STRING.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600,xmit=PIN_C6,rcv=PIN_C7)//Hw UART-->INT RDA
void CHECK_Valve(float);
int convHex(char);
int i=0,a,b,c;
float cTemp,mask=0.1;
float Ans,Inact_valve,act_valve;
BOOLEAN RX=0;
char Data,command,array[12];
byte Temp[16],recv1[8],temp2;
float MASK_value;
#INT_RDA //Rx PIN Connect to D.O. Meter
void hw_uart_isr (){
Temp2=getc();
temp[i]=Temp2;
i++;
if(temp[i]==0x0D){
command= Temp[0];
switch (command) {
case 'A':
recv1[0]=Temp[1];
recv1[1]=Temp[2];
recv1[2]=Temp[3];
recv1[3]=Temp[4];
for (i=0;i<15;i++){
Temp[i]=0; //Clear value in Temp[]
}
MASK_value=atof(recv1);
act_valve=MASK_value;
return;
case 'I':
recv1[0]=Temp[1];
recv1[1]=Temp[2];
recv1[2]=Temp[3];
recv1[3]=Temp[4];
for (i=0;i<15;i++){
Temp[i]=0; //Clear value in Temp[]
}
MASK_value=atof(recv1);
Inact_valve=MASK_value;
// printf("INACTIVE Value is : %f\n\r",Inact_valve);
return;
default :
a =convHex(Temp[12]); //Convert Ten Digit
b =convHex(Temp[13]); //Convert Unit Digit
c =convHex(Temp[14]); //Convert .X Digit
a =a*10; //multiply 10 for TEN Digit
cTemp =c*(mask); //Assigned floating point Ctemp value
Ans=a+b+cTemp;
sprintf(array,"%3.1f",Ans );//convert float variable to string
CHECK_Valve(Ans);
// RX=1; //Set RX for INT Service finish
output_low(PIN_D4);// D4 use for select data Line (low-->A,High-->B)
return;
}
}
}
void main (){
enable_interrupts(GLOBAL);
enable_interrupts(INT_RDA);
set_tris_d(0xEE);
for(;;){
printf("Test my Application \n\r");
delay_ms(3000);//Read onece in 3 sec
output_high(PIN_D4);//Enable for read oxygen(Select Line B Rx)
delay_ms(2000);
output_low(PIN_D4);// D4 use for select data Line (low-->A,High-->B)
printf("%s\n\r",array);
}
}
/******************* convert Routine ************************/
int convHex (char Data) {
switch (Data) { //case ASCII Type and MAP them to integer
case '0' :
return 0x00;
break;
case '1' :
return 0x01;
break;
case '2' :
return 0x02;
break;
case '3' :
return 0x03;
break;
case '4' :
return 0x04;
break;
case '5' :
return 0x05;
break;
case '6' :
return 0x06;
break;
case '7' :
return 0x07;
break;
case '8' :
return 0x08;
break;
case '9' :
return 0x09;
break;
default :break;
}
}
/******************* CHECK_Valve Routine ************************/
void CHECK_Valve(float Ans)
{
if (Ans <=act_valve){
output_high(PIN_D0);//Open Valve
return;
}else if ((Ans >act_valve) & (Ans >= Inact_valve)){
output_low(PIN_D0);//Close Valve
}
}
|
It doesn't work when I test with my dev board. So sad
Thank alot
Last edited by soulweed on Thu Mar 22, 2007 2:49 pm; edited 2 times in total |
|
|
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
Posted: Thu Mar 22, 2007 2:44 pm |
|
|
Quote: | void CHECK_Valve(float Ans)
{
if (Ans <act_valve>act_valve) & (Ans >= Inact_valve)){
output_low(PIN_D0);//Close Valve
}
} |
You need to disable HTML when you post. Notice the line in bold
is garbled ? That's because HTML was enabled.
Here's how to fix it permanently, so you never have to worry about it
again.
1. Go to the main forum page, here:
http://www.ccsinfo.com/forum/viewforum.php?f=1
Make sure you are "logged in".
2. In the upper left corner of the screen, there is a "Profile" button.
Click it.
3. You will now be on your member Profile page. Scroll down near
the bottom. You will see this entry:
Code: | Always allow HTML: Yes No |
Click on the selection for "No".
4. Then down at the bottom, click on the "Submit" button.
Now you're set. No more HTML problems. (This only works if you
always log in. If you post as a 'guest', you still need to manually
disable HTML with the tickbox below the posting window). |
|
|
rwyoung
Joined: 12 Nov 2003 Posts: 563 Location: Lawrence, KS USA
|
|
Posted: Thu Mar 22, 2007 4:25 pm |
|
|
"break" statement in the switch statement in your interrupt is missing? _________________ Rob Young
The Screw-Up Fairy may just visit you but he has crashed on my couch for the last month! |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Mar 22, 2007 6:35 pm |
|
|
rwyoung wrote: | "break" statement in the switch statement in your interrupt is missing? | It is better coding practice to have the break statement present but not required, he is doing a 'dirty' return from each case. It might however be that the CCS compiler is having problems with the break statements missing.
Quote: | It doesn't work when I test with my dev board. So sad | What is not working? Now we do have to guess... Guessing takes a lot of time and that's why we skip your question and go to the next person looking for help.
Code: | Temp2=getc();
temp[i]=Temp2;
i++;
if(temp[i]==0x0D){ | 1) Temp2 is only used locally, so don't make it a global variable. Global variables make your code much more difficult to read as we have to look all over the code if it is not used somewhere else. Also for global variables the compiler can not optimize memory usage by reusing the same memory location in another function.
2) Why did you introduce Temp2 anyway? It doesn't seem to do anything.
3) Bug: you are testing temp[i] after i was incremented, this means you are checking data in the temp-buffer which is not filled yet. My guess is this is why you introduced the Temp2 variable?
Code: | case 'A':
recv1[0]=Temp[1];
recv1[1]=Temp[2];
recv1[2]=Temp[3];
recv1[3]=Temp[4];
for (i=0;i<15;i++){
Temp[i]=0; //Clear value in Temp[]
}
MASK_value=atof(recv1);
| 1) Change 'i<15' to 'i<16', or even better, study the memset() function.
2)bug: atof() expects a zero terminated string, so make sure recv1[4] = 0. |
|
|
|
|
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
|