djc
Advanced Member level 1
Hi all,
I am working on a project using PIC18f4520 and mikroc. Here there are two displays which are connected to a computer. One of two displays is a master and another one is slave. i am working on a slave display. So whenever master display gets character 'A' sent by a computer via serial communication, it sends all the data which it is displaying, to the computer and the slave display. Slave display has to display the data as it is. Data will be in the format '0000;0000;0000000;'. Means 'Upto 4 digit semicolon Upto 4 digit semicolon Upto 7 digit semicolon'.
Now what i have done is first of all i receive charater 'A' and incremented the index variable. Then waited for 3 semicolons to arrive. If 3 semicolons are there and first character is 'A' then two flags 'Finish ' and 'Flag' are set. Then one function is called regularly from main and in that function 'Finish' and 'Flag' are checked. if they are set further processing is done.
Now here what happening is, if i send data manually through hyper terminal, data being displayed is correct. However when i receive data serially via master, and being displayed on slave FND,
data keep getting shifted on each FND. Moreover unwanted data may also get displayed. Whether data received by the computer sent by the master is correct.
My question is where i am getting wrong. Why array is behaving strange like this. Here is the code. Posting only necessary part regarding the query.I applied two different logic.
Logic 1
Logic2
Help i finding the bug.
I am working on a project using PIC18f4520 and mikroc. Here there are two displays which are connected to a computer. One of two displays is a master and another one is slave. i am working on a slave display. So whenever master display gets character 'A' sent by a computer via serial communication, it sends all the data which it is displaying, to the computer and the slave display. Slave display has to display the data as it is. Data will be in the format '0000;0000;0000000;'. Means 'Upto 4 digit semicolon Upto 4 digit semicolon Upto 7 digit semicolon'.
Now what i have done is first of all i receive charater 'A' and incremented the index variable. Then waited for 3 semicolons to arrive. If 3 semicolons are there and first character is 'A' then two flags 'Finish ' and 'Flag' are set. Then one function is called regularly from main and in that function 'Finish' and 'Flag' are checked. if they are set further processing is done.
Now here what happening is, if i send data manually through hyper terminal, data being displayed is correct. However when i receive data serially via master, and being displayed on slave FND,
data keep getting shifted on each FND. Moreover unwanted data may also get displayed. Whether data received by the computer sent by the master is correct.
My question is where i am getting wrong. Why array is behaving strange like this. Here is the code. Posting only necessary part regarding the query.I applied two different logic.
Logic 1
Code:
void Validity(){
if((Finish==1) && (Flag==1)){
start1=count;
while(input_string[count]!=59){
count++;
}
start11=count;
count--;
while(count >=start1){
if((input_string[count]>47) && (input_string[count]<58)){
VFND[count] = input_string[count]-48;
}
count--;
}
while(count<start11){
count++;
}
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[4] = VFND[count];
}
else{
FND[4]=0;FND[3]=0;FND[2]=0;FND[1]=0;
//goto Next;
}
if(count>start1) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[3] = VFND[count];
}
}
else {
FND[3]=0;FND[2]=0;FND[1]=0;
//goto Next;
}
if(count>start1) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[2] = VFND[count];
}
}
else {
FND[2]=0;FND[1]=0;
//goto Next;
}
if(count>start1) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[1] = VFND[count];
}
}
else {
FND[1]=0;
//goto Next;
}
//Next:
start11++;
count = start11;
start2=count;
while(input_string[count]!=59){
count++;
}
start22=count;
count--;
while(count >=start2){
if((input_string[count]>47) &&(input_string[count]<58)){
VFND[count] = input_string[count]-48;
}
count--;
}
while(count<start22){
count++;
}
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[8] = VFND[count];
}
else{
FND[8]=0;FND[7]=0;FND[6]=0;FND[5]=0;
//goto Down;
}
if(count>start2) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[7] = VFND[count];
}
}
else {
FND[7]=0;FND[6]=0;FND[5]=0;
//goto Down;
}
if(count>start2) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[6] = VFND[count];
}
}
else{
FND[6]=0;FND[5]=0;
//goto Down;
}
if(count>start2) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[5] = VFND[count];
}
}
else{
FND[5]=0;
//goto Down;
}
//Down:
start22++;
count=start22;
start3=count;
while(input_string[count]!=59){
count++;
}
start33=count;
count--;
while(count>=start3){
if((input_string[count]>47) &&(input_string[count]<58)){
VFND[count] = input_string[count]-48;
}
count--;
}
while(count<start33){
count++;
}
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[15] = VFND[count];
}
else{
FND[15]=0;FND[14]=0;FND[13]=0;FND[12]=0;FND[11]=0;FND[10]=0;FND[9]=0;
//goto Last;
}
if(count>start3) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[14] = VFND[count];
}
}
else {
FND[14]=0;FND[13]=0;FND[12]=0;FND[11]=0;FND[10]=0;FND[9]=0;
//goto Last;
}
if(count>start3) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[13] = VFND[count];
}
}
else{
FND[13]=0;FND[12]=0;FND[11]=0;FND[10]=0;FND[9]=0;
//goto Last;
}
if(count>start3) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[12] = VFND[count];
}
}
else {
FND[12]=0;FND[11]=0;FND[10]=0;FND[9]=0;
//goto Last;
}
if(count>start3) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[11] = VFND[count];
}
}
else{
FND[11]=0;FND[10]=0;FND[9]=0;
//goto Last;
}
if(count>start3) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[10] = VFND[count];
}
}
else {
FND[10]=0;FND[9]=0;
//goto Last;
}
if(count>start3) {
count--;
if((VFND[count]>=0) && (VFND[count]<=9)){
FND[9] = VFND[count];
}
}
else{
FND[9]=0;
//goto Last;
}
//Last:
for(sent=0;sent<20;sent++){
input_string[sent]=0;
VFND[sent]=0;
}
count=15;Finish=0;End=0;index=0;Flag=0;
}
void interrupt() iv 0x0008 ics ICS_AUTO
{
Max_Control=0;
if(RCIF_bit==1){
if ((RCSTA.OERR )/*&& (RCIE_bit==1)*/){
RCSTA.CREN = 0;
Nop();
Nop();
RCSTA.CREN = 1;
Garbage=RCREG;
Garbage=RCREG;
RCIF_bit=0;
}
else{
sdata=RCREG;
input_string[index]=sdata;
if(input_string[index]==59){
End=End+1;
if(End==3) {
Finish=1;
if(input_string[0]==65){
Flag=1;
index=0;
End=0;
input_string[0]=0;
}
else{
Flag=0;
Finish=0;
End=0;
index=0;
input_string[0]=0;
}
}
}
if(input_string[0]==65){
index=index+1;
}
}
}
}
void main() {
Port_Setting();
Timer_Setting();
Serial_Setting();
index=0;
Max_Control=0;
TMR0ON_bit = 1;
while(1) {
Validity();
}
}
Code:
void Validity(){
if((Finish==1) && (Flag==1)){
count=15;
index--;
while(input_string[index]!=59){
if((input_string[index]>47) &&(input_string[index]<58)){
VFND[index] = input_string[index]-48;
if((VFND[index]>=0) && (VFND[index]<=9)){
FND[count] = VFND[index];
count--;
}
}
index--;
}
for(count=count;count>8;count--){
FND[count]=0;
}
count=8;
index--;
while(input_string[index]!=59){
if((input_string[index]>47) &&(input_string[index]<58)){
VFND[index] = input_string[index]-48;
if((VFND[index]>=0) && (VFND[index]<=9)){
FND[count] = VFND[index];
count--;
}
}
index--;
}
for(count=count;count>4;count--){
FND[count]=0;
}
count=4;
index--;
while(input_string[index]!=59){
if((input_string[index]>47) &&(input_string[index]<58)){
VFND[index] = input_string[index]-48;
if((VFND[index]>=0) && (VFND[index]<=9)){
FND[count] = VFND[index];
count--;
}
}
index--;
}
for(count=count;count>0;count--){
FND[count]=0;
}
for(sent=0;sent<20;sent++){
input_string[sent]=0;
VFND[sent]=0;
}
count=15;Finish=0;End=0;index=0;Flag=0;
}
void interrupt() iv 0x0008 ics ICS_AUTO
{
Max_Control=0;
if(RCIF_bit==1){
if ((RCSTA.OERR )/*&& (RCIE_bit==1)*/){
RCSTA.CREN = 0;
Nop();
Nop();
RCSTA.CREN = 1;
Garbage=RCREG;
Garbage=RCREG;
RCIF_bit=0;
}
else{
sdata=RCREG;
input_string[index]=sdata;
if(input_string[index]==59){
End=End+1;
if(End==3) {
Finish=1;
if(input_string[0]==65){
Flag=1;
//index=0;
End=0;
input_string[0]=0;
}
else{
Flag=0;
Finish=0;
End=0;
index=0;
input_string[0]=0;
}
}
}
if(input_string[0]==65){
index=index+1;
}
}
}
}
void main() {
Port_Setting();
Timer_Setting();
Serial_Setting();
index=0;
Max_Control=0;
TMR0ON_bit = 1;
while(1) {
Validity();
}
}