![](templates/subSilver/images/CCSLogo.jpg) |
![CCS C Software and Maintenance Offers](templates/subSilver/images/forumAd6.jpg) |
View previous topic :: View next topic |
Author |
Message |
OJ Guest
|
2 UARTS sharing a function call |
Posted: Wed Feb 20, 2008 2:28 pm |
|
|
I don't know how to explain this but I am trying to implement the use of two UARTs. I want to be able to share the same function call when extracting data from either buffer but I don't know how to go about it. Is there anyone that can help? Thanks in advance!
NOTE: this section inside get_packet() is not being printed correctly on this post.
Quote: | for(i=1; i<9> 0){OK++;}
this should be within the for loop: if(! strncmp ( temp,CMD[i],3)){command =i;}} followed by: if( command > 0) {OK++;}
|
The following code below works perfect for one UART:
Code: |
#include <18F67J60.h>
#device adc=12
#FUSES NOWDT, WDT128, HS, NODEBUG, NOXINST, STVREN, NOPROTECT, FCMEN, IESO, ETHLED, PRIMARY, RESERVED
#use delay(clock=25000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=USB)
#use rs232(baud=9600,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8,stream=UART)
#include <string.h>
#define BUFFER_SIZE 32
char packet[BUFFER_SIZE];
char USB_buffer[BUFFER_SIZE];
char UART_buffer[BUFFER_SIZE];
int index=0,next = 0;
int command,data_reg,length;
char CMD[9][4]="ERR","RTC","ADC","DAC","TCP","MMC","DIO","SLP","FLA"; //Command
int REG[9]={0,4,4,4,4,9,8,1,1}; // # of registers for each command
// Read example: CMD00\0x0D
// Write example: CMD10123456\0x0D
char temp[2];
/******************** Initialize Ports *****************************************/
void Initialize(void)
{
enable_interrupts(INT_RDA);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
}
/******************** Serial Receive Interrupt *********************************/
#int_rda
void USB_Receive()
{
int t;
USB_buffer[index] = fgetc(USB);
t = index;
index = (index + 1) % BUFFER_SIZE;
if(index == next)
index = t;
}
/******************** Serial Receive Interrupt *********************************/
#int_ext
void UART_Receive()
{
int t;
UART_buffer[index] = fgetc(UART);
t = index;
index = (index + 1) % BUFFER_SIZE;
if(index == next)
index = t;
}
/******************** Get Character From Buffer ********************************/
#define DataAvailable (index!=next)
char get_char()
{
int t=0,timeout=false;
char c;
while(!DataAvailable)
{
t++;
delay_us(600);
if(t > 5){timeout = true; break;}
}
if(timeout){timeout = false; return (-1);}
else
{
c = USB_buffer[next];
next = (next + 1) % BUFFER_SIZE;
return(c);
}
}
/******************** Get Packet ***********************************************/
int get_packet()
{
int valid=true,i,OK=0;
command=0;
length=0;
packet[0] = get_char();
packet[1] = get_char(); //GET COMMAND
packet[2] = get_char();
for(i=0; i<=2; i++){temp[i] = packet[i];}
for(i=1; i<9> 0){OK++;}
packet[3] = (get_char()- 48);
if((packet[3] == 0)||(packet[3] == 1)){OK++;} //GET R/W BIT
packet[4] = (get_char()- 48);
if((packet[4] >= 0 )&&(packet[4] <= REG[command])) //GET REGISTER BIT
{
data_reg = packet[4];
OK++;
}
if(packet[3]== 1)
{
for(i=5;i<BUFFER_SIZE; i++)
{
packet[i] = get_char(); //GET LENGTH OF DATA & DATA
length++;
if((packet[i] == 0x0D)||(packet[i] == -1)){length--; break;}
}
if(packet[length + 5] == 0x0D){OK++;} //GET CARRIAGE RETURN
}
else
{
packet[5]= get_char();
if(packet[5] == 0x0D){OK++;}
}
if(OK == 4){valid = true;}
else{valid = false;}
return valid;
}
/******************** Main Loop ************************************************/
void main()
{
Initialize();
while (true)
{
if(DataAvailable)
{
if(get_packet())
{
fprintf(USB,"command=%d bit=%d register=%d length=%d\n",command,packet[3],packet[4],length);
index = 0;next = 0;
}
}
}
} |
I tried modifying the get_packet() & get_char() functions in the code below but it doesn't seem to work.
Code: |
******************** Get Character From Buffer ********************************/
#define DataAvailable (index!=next)
char get_char(char* buffer)
{
int t=0,timeout=false;
char c;
while(!DataAvailable)
{
t++;
delay_us(600);
if(t > 5){timeout = true; break;}
}
if(timeout){timeout = false; return (-1);}
else
{
c = buffer[next];
next = (next + 1) % BUFFER_SIZE;
return(c);
}
}
/******************** Get Packet ***********************************************/
int get_packet(char* buffer)
{
int valid=true,i,OK=0;
command=0;
length=0;
packet[0] = get_char(buffer);
packet[1] = get_char(buffer); //GET COMMAND
packet[2] = get_char(buffer);
for(i=0; i<=2; i++){temp[i] = packet[i];}
for(i=1; i<9> 0){OK++;}
packet[3] = (get_char(buffer)- 48);
if((packet[3] == 0)||(packet[3] == 1)){OK++;} //GET R/W BIT
packet[4] = (get_char(buffer)- 48);
if((packet[4] >= 0 )&&(packet[4] <= REG[command])) //GET REGISTER BIT
{
data_reg = packet[4];
OK++;
}
if(packet[3]== 1)
{
for(i=5;i<BUFFER_SIZE; i++)
{
packet[i] = get_char(buffer); //GET LENGTH OF DATA & DATA
length++;
if((packet[i] == 0x0D)||(packet[i] == -1)){length--; break;}
}
if(packet[length + 5] == 0x0D){OK++;} //GET CARRIAGE RETURN
}
else
{
packet[5]= get_char(buffer);
if(packet[5] == 0x0D){OK++;}
}
if(OK == 4){valid = true;}
else{valid = false;}
return valid;
}
/******************** Main Loop ************************************************/
void main()
{
Initialize();
while (true)
{
if(DataAvailable)
{
if(get_packet(USB_buffer))
{
fprintf(USB,"command=%d bit=%d register=%d length=%d\n",command,packet[3],packet[4],length);
index = 0;next = 0;
}
if(get_packet(UART_buffer))
{
fprintf(UART,"command=%d bit=%d register=%d length=%d\n",command,packet[3],packet[4],length);
index = 0;next = 0;
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
Ken Johnson
Joined: 23 Mar 2006 Posts: 197 Location: Lewisburg, WV
|
|
Posted: Wed Feb 20, 2008 3:10 pm |
|
|
1st - I don't know this part (sorry) - are these 2 hardware uarts? I'll assume yes; if not, I'm not sure you can pull this off.
2nd - assuming these are both active, you need separate indices for each buffer (you do have 2 separate buffers).
3rd - #int_ext is not a uart interrupt, do you want #int_rda2 ?
Well, that's my (maybe useless) comments, for starters. I didn't go into the meat of your code at all. Hope this helps a little.
Ken |
|
![](templates/subSilver/images/spacer.gif) |
OJ Guest
|
Re: 2 UARTS sharing a function call |
Posted: Wed Feb 20, 2008 4:39 pm |
|
|
Thank you for your input but I managed to get it working....just need to clean it up a little.
Here is working code for extracting data from a packet (using two UARTs) using a shared function.
Code: |
#include <18F67J60.h>
#device adc=8
#FUSES NOWDT, WDT128, HS, NODEBUG, NOXINST, STVREN, NOPROTECT, FCMEN, IESO, ETHLED, PRIMARY, RESERVED
#use delay(clock=25000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=USB)
#use rs232(baud=9600,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8,stream=UART)
#include <string.h>
#define BUFFER_SIZE 32
char packet[BUFFER_SIZE];
char USB_buffer[BUFFER_SIZE];
char UART_buffer[BUFFER_SIZE];
int USB_i=0,USB_o=0,UART_i=0,UART_o=0,select=1;
int command,data_reg,length;
char CMD[9][4]="ERR","RTC","ADC","DAC","TCP","MMC","DIO","SLP","FLA";
int REG[9]={0,4,4,4,4,9,8,1,1};
char temp[2];
/******************** Initialize Ports ***************************/
void Initialize(void)
{
enable_interrupts(INT_RDA);
ext_int_edge(H_TO_L);
enable_interrupts(INT_EXT);
enable_interrupts(GLOBAL);
}
/******************** Serial Receive Interrupt *******************/
#int_rda
void USB_Receive()
{
int t;
USB_buffer[USB_i] = fgetc(USB);
t = USB_i;
USB_i = (USB_i + 1) % BUFFER_SIZE;
if(USB_i == USB_o)
USB_i = t;
}
/******************** Serial Receive Interrupt *******************/
#int_ext
void UART_Receive()
{
int t;
UART_buffer[UART_i] = fgetc(UART);
t = UART_i;
UART_i = (UART_i + 1) % BUFFER_SIZE;
if(UART_i == UART_o)
UART_i = t;
}
/******************** Get Character From Buffer ****************/
#define USB_Data (USB_i != USB_o)
#define UART_Data (UART_i != UART_o)
char get_char()
{
int t=0,timeout=false;
char c;
if(select==1)
{
while(!USB_Data)
{
t++;
delay_us(600);
if(t > 5){timeout = true; break;}
}
}
if(select==2)
{
while(!UART_Data)
{
t++;
delay_us(600);
if(t > 5){timeout = true; break;}
}
}
if(timeout){timeout = false; return (-1);}
else
{
if(select==1)
{
c = USB_buffer[USB_o];
USB_o = (USB_o + 1) % BUFFER_SIZE;
}
if(select==2)
{
c = UART_buffer[UART_o];
UART_o = (UART_o + 1) % BUFFER_SIZE;
}
return(c);
}
}
/******************** Get Packet *****************************/
int get_packet()
{
int valid=true,i,OK=0;
command=0;
length=0;
packet[0] = get_char();
packet[1] = get_char(); //GET COMMAND
packet[2] = get_char();
for(i=0; i<=2; i++){temp[i] = packet[i];}
for(i=1; i<9;i++)
{
if (!strncmp(temp,CMD[i],3)){command = i;}
}
if(command > 0){OK++;}
packet[3] = (get_char()- 48);
if((packet[3] == 0)||(packet[3] == 1)){OK++;} //GET R/W BIT
packet[4] = (get_char()- 48);
if((packet[4] >= 0 )&&(packet[4] <= REG[command])) //GET REG BIT
{
data_reg = packet[4];
OK++;
}
if(packet[3]== 1) //CHECK IF WRITE SELECTED
{
for(i=5;i<BUFFER_SIZE; i++)
{
packet[i] = get_char(); //GET LENGTH & DATA
length++;
if((packet[i] == 0x0D)||(packet[i] == -1)){length--; break;}
}
if(packet[length + 5] == 0x0D){OK++;} //GET CARRIAGE RETURN
}
else //IF READ SELECTED END PACKET
{
packet[5]= get_char();
if(packet[5] == 0x0D){OK++;}
}
if(OK == 4){valid = true;}
else{valid = false;}
return valid;
}
/******************** Main Loop ******************************/
void main()
{
Initialize();
while (true)
{
if(USB_Data)
{
select=1;
if(get_packet())
{
//Place Execute packet Routine here
fprintf(USB,"command=%d bit=%d register=%d length=%d\n",command,packet[3],packet[4],length);
USB_i = 0;USB_o = 0;
}
}
if(UART_Data)
{
select=2;
if(get_packet())
{
//Place Execute packet Routine here
fprintf(UART,"command=%d bit=%d register=%d length=%d\n",command,packet[3],packet[4],length);
UART_i = 0;UART_o = 0;
}
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
|
|
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
|