|
|
View previous topic :: View next topic |
Author |
Message |
micro_debugger
Joined: 08 Oct 2009 Posts: 73
|
24FJ..Buffered RS232 still not working - any help please |
Posted: Tue Aug 21, 2012 4:23 pm |
|
|
Hi,
I have started to write a simple program for buffered RS232 communication on the 24FJ64GB106. The non buffered procedure is working well, however the buffered routines are not working. Could you please make a short lookup and suggest me where I'm wrong. Thank you in advance,
Best Regards
Code: |
#include <24FJ64GB106.h>
//#device ICD=TRUE
#build(stack=512)
#fuses HS,NOWDT,PR
#use delay(clock=12000000,RESTART_WDT)
#pin_select U1TX = PIN_B6
#pin_select U1RX = PIN_B7
#use rs232(UART1,baud=9600,parity=N,bits=8, STREAM=RADIO)
#pin_select U2TX = PIN_G6
#pin_select U2RX = PIN_G7
#use rs232(UART2,baud=9600,parity=N,bits=8, STREAM=RS232)
// defined crystal is 12 Mhz fro the following calculated RTC timmer interrupt
#define RTCC_TIME_20_ms 35405 //20ms
#define RTCC_TIME_10_ms 50470 //10ms
#define RTCC_TIME_5_ms 58003 //5ms
#define RTCC_TIME_2_5ms 61769 //2.5ms
#define RTCC_TIME_2ms 62523 //2ms
#define TX_RADIO_BUFFER_SIZE 192
volatile unsigned int8 tx_radio_buffer[TX_RADIO_BUFFER_SIZE];
volatile unsigned int8 tx_radio_next_in;
volatile unsigned int8 tx_radio_next_out;
#define TX_RS232_BUFFER_SIZE 192
volatile unsigned int8 tx_rs232_buffer[TX_RS232_BUFFER_SIZE];
volatile unsigned int8 tx_rs232_next_in;
volatile unsigned int8 tx_rs232_next_out;
#define RX_RADIO_BUFFER_SIZE 192
volatile unsigned int8 rx_radio_buffer[RX_RADIO_BUFFER_SIZE];
volatile unsigned int8 rx_radio_next_in;
volatile unsigned int8 rx_radio_next_out;
#define bkbhit_radio (rx_radio_next_in != rx_radio_next_out)
#define RX_RS232_BUFFER_SIZE 192
volatile unsigned int8 rx_rs232_buffer[RX_RS232_BUFFER_SIZE];
volatile unsigned int8 rx_rs232_next_in;
volatile unsigned int8 rx_rs232_next_out;
#define bkbhit_rs232 (rx_rs232_next_in != rx_rs232_next_out)
void LED_SYNC_ON(void)
{
output_high(PIN_E0);
}
void LED_SYNC_OFF(void)
{
output_low(PIN_E0);
}
/////////////// RADIO SERIAL PORT HANDLER START ///////////////////////
#int_TBE
void TBE_isr(void)
{
if(tx_radio_next_in!=tx_radio_next_out)
{
putc(tx_radio_buffer[tx_radio_next_out], RADIO);
tx_radio_next_out=(tx_radio_next_out+1) % TX_RADIO_BUFFER_SIZE;
}
else
disable_interrupts(int_tbe);
}
void bputc_radio(unsigned char c)
{
short restart;
int ni;
restart=tx_radio_next_in==tx_radio_next_out;
tx_radio_buffer[tx_radio_next_in]=c;
ni=(tx_radio_next_in+1) % TX_RADIO_BUFFER_SIZE;
while(ni==tx_radio_next_out);
tx_radio_next_in=ni;
if(restart)
enable_interrupts(int_tbe);
}
#INT_RDA
void RDA_isr()
{
unsigned int8 t;
rx_radio_buffer[rx_radio_next_in] = fgetc(RADIO);
t = rx_radio_next_in;
rx_radio_next_in = (rx_radio_next_in+1) % RX_RADIO_BUFFER_SIZE;
if(rx_radio_next_in == rx_radio_next_out)
rx_radio_next_in=t; // Buffer full !!
}
int8 bgetc_radio()
{
unsigned int8 c;
unsigned int8 timeout;
while(!bkbhit_radio && ++timeout<=10)
delay_ms(10);
c = rx_radio_buffer[rx_radio_next_out];
rx_radio_next_out = (rx_radio_next_out+1) % RX_RADIO_BUFFER_SIZE;
return c;
}
/////////////// RADIO SERIAL PORT HANDLER STOP ///////////////////////
/////////////// RS232 SERIAL PORT HANDLER START ///////////////////////
#int_TBE2
void TBE2_isr(void)
{
if(tx_rs232_next_in!=tx_rs232_next_out)
{
putc(tx_rs232_buffer[tx_rs232_next_out], RS232);
tx_rs232_next_out=(tx_rs232_next_out+1) % TX_RS232_BUFFER_SIZE;
}
else
disable_interrupts(int_tbe2);
}
void bputc_rs232(unsigned char c)
{
short restart;
int ni;
restart=tx_rs232_next_in==tx_rs232_next_out;
tx_rs232_buffer[tx_rs232_next_in]=c;
ni=(tx_rs232_next_in+1) % TX_RS232_BUFFER_SIZE;
while(ni==tx_rs232_next_out);
tx_rs232_next_in=ni;
if(restart)
enable_interrupts(int_tbe2);
}
#INT_RDA2
void RDA2_isr()
{
unsigned int8 t;
rx_rs232_buffer[rx_rs232_next_in] = fgetc(RS232);
t = rx_rs232_next_in;
rx_rs232_next_in = (rx_rs232_next_in+1) % RX_RS232_BUFFER_SIZE;
if(rx_rs232_next_in == rx_rs232_next_out)
rx_rs232_next_in=t; // Buffer full !!
}
int8 bgetc_rs232()
{
unsigned int8 c;
unsigned int8 timeout;
while(!bkbhit_rs232 && ++timeout<=10)
delay_ms(10);
c = rx_rs232_buffer[rx_rs232_next_out];
rx_rs232_next_out = (rx_rs232_next_out+1) % RX_RS232_BUFFER_SIZE;
return c;
}
/////////////// RS232 SERIAL PORT HANDLER STOP ///////////////////////
#INT_TIMER1
void timer1_isr()
{
// set_timer1(TIMER_SET_01); //0.1 sec
// multipier10--;
// if(multipier10==0)
// {
// multipier10=10;
// num_seconds_LED++;
// num_seconds++;
//
// }
}
void main()
{
//setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
//set_timer1(TIMER_SET_01); //0.1 sec
//multipier10=10;
tx_radio_next_in = tx_radio_next_out = 0;
tx_rs232_next_in = tx_rs232_next_out = 0;
rx_radio_next_in = rx_radio_next_out = 0;
rx_rs232_next_in = rx_rs232_next_out = 0;
//setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
enable_interrupts(INTR_GLOBAL);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TBE);
enable_interrupts(INT_RDA2);
enable_interrupts(INT_TBE2);
while(1)
{
/*
LED_SYNC_OFF();
fprintf(radio,"%x", 0xAA);
fprintf(rs232, "%x", 0xAA);
delay_ms(50);
LED_SYNC_ON();
fprintf(radio,"%x", 0xAA);
fprintf(rs232, "%x", 0xAA);
delay_ms(50);
*/
LED_SYNC_OFF();
printf(bgetc_radio,"%x", 0xAA);
printf(bgetc_rs232, "%x", 0xAA);
delay_ms(50);
LED_SYNC_ON();
printf(bgetc_radio,"%x", 0xAA);
printf(bgetc_rs232, "%x", 0xAA);
delay_ms(50);
}
}
|
Last edited by micro_debugger on Wed Aug 22, 2012 4:36 pm; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19540
|
|
Posted: Wed Aug 22, 2012 1:26 am |
|
|
The obvious big mistake, is you are talking to the wrong function.
_putc_ (and bputc for the buffer), is the function to 'put' data. You are printing to 'bgetc', the function to get data.....
Best Wishes |
|
|
micro_debugger
Joined: 08 Oct 2009 Posts: 73
|
|
Posted: Wed Aug 22, 2012 11:38 am |
|
|
Hi Ttelmah,
Thank you so much for your indication. I corrected it as alo other small mistakes I have found. However it is still not working. I have used it 100 times in the 8 bit processors, but it is my first impression with 16bit. May be you can see it again, please, ansd have any idea. 4 eyes is much more than 2 eyes :-)
My best regards
here is the corrected code with your and some mine corrections
Code: |
#include <24FJ64GB106.h>
#device ICD=3
#build(stack=512)
#ZERO_RAM
#fuses HS,NOWDT,PR
//#fuses ICSP2
#use delay(clock=12000000,RESTART_WDT)
#pin_select U1TX = PIN_B6
#pin_select U1RX = PIN_B7
#use rs232(UART1,baud=9600,parity=N,bits=8, STREAM=RADIO)
#pin_select U2TX = PIN_G6
#pin_select U2RX = PIN_G7
#use rs232(UART2,baud=9600,parity=N,bits=8, STREAM=RS232)
// defined crystal is 12 Mhz fro the following calculated RTC timmer interrupt
#define RTCC_TIME_20_ms 35405 //20ms
#define RTCC_TIME_10_ms 50470 //10ms
#define RTCC_TIME_5_ms 58003 //5ms
#define RTCC_TIME_2_5ms 61769 //2.5ms
#define RTCC_TIME_2ms 62523 //2ms
#define TX_RADIO_BUFFER_SIZE 64
volatile unsigned int8 tx_radio_buffer[TX_RADIO_BUFFER_SIZE];
volatile unsigned int8 tx_radio_next_in;
volatile unsigned int8 tx_radio_next_out;
#define RX_RADIO_BUFFER_SIZE 64
volatile unsigned int8 rx_radio_buffer[RX_RADIO_BUFFER_SIZE];
volatile unsigned int8 rx_radio_next_in;
volatile unsigned int8 rx_radio_next_out;
#define bkbhit_radio (rx_radio_next_in != rx_radio_next_out)
#define TX_RS232_BUFFER_SIZE 64
volatile unsigned int8 tx_rs232_buffer[TX_RS232_BUFFER_SIZE];
volatile unsigned int8 tx_rs232_next_in;
volatile unsigned int8 tx_rs232_next_out;
#define RX_RS232_BUFFER_SIZE 64
volatile unsigned int8 rx_rs232_buffer[RX_RS232_BUFFER_SIZE];
volatile unsigned int8 rx_rs232_next_in;
volatile unsigned int8 rx_rs232_next_out;
#define bkbhit_rs232 (rx_rs232_next_in != rx_rs232_next_out)
void LED_SYNC_ON(void)
{
output_high(PIN_E0);
}
void LED_SYNC_OFF(void)
{
output_low(PIN_E0);
}
/////////////// RADIO SERIAL PORT HANDLER START ///////////////////////
#int_TBE
void TBE_isr(void)
{
if(tx_radio_next_in!=tx_radio_next_out)
{
fputc(tx_radio_buffer[tx_radio_next_out], RADIO);
tx_radio_next_out=(tx_radio_next_out+1) % TX_RADIO_BUFFER_SIZE;
}
else
disable_interrupts(int_tbe);
}
void bputc_radio(unsigned char c)
{
short restart;
unsigned int8 ni;
restart=tx_radio_next_in==tx_radio_next_out;
tx_radio_buffer[tx_radio_next_in]=c;
ni=(tx_radio_next_in+1) % TX_RADIO_BUFFER_SIZE;
while(ni==tx_radio_next_out);
tx_radio_next_in=ni;
if(restart)
enable_interrupts(int_tbe);
}
#INT_RDA
void RDA_isr()
{
unsigned int8 t;
rx_radio_buffer[rx_radio_next_in] = fgetc(RADIO);
t = rx_radio_next_in;
rx_radio_next_in = (rx_radio_next_in+1) % RX_RADIO_BUFFER_SIZE;
if(rx_radio_next_in == rx_radio_next_out)
rx_radio_next_in=t; // Buffer full !!
}
int8 bgetc_radio()
{
unsigned int8 c;
unsigned int8 timeout;
while(!bkbhit_radio && ++timeout<=10)
delay_ms(10);
c = rx_radio_buffer[rx_radio_next_out];
rx_radio_next_out = (rx_radio_next_out+1) % RX_RADIO_BUFFER_SIZE;
return c;
}
/////////////// RADIO SERIAL PORT HANDLER STOP ///////////////////////
/////////////// RS232 SERIAL PORT HANDLER START ///////////////////////
#int_TBE2
void TBE2_isr(void)
{
if(tx_rs232_next_in!=tx_rs232_next_out)
{
fputc(tx_rs232_buffer[tx_rs232_next_out], RS232);
tx_rs232_next_out=(tx_rs232_next_out+1) % TX_RS232_BUFFER_SIZE;
}
else
disable_interrupts(int_tbe2);
}
void bputc_rs232(unsigned char c)
{
short restart;
unsigned int8 ni;
restart=tx_rs232_next_in==tx_rs232_next_out;
tx_rs232_buffer[tx_rs232_next_in]=c;
ni=(tx_rs232_next_in+1) % TX_RS232_BUFFER_SIZE;
while(ni==tx_rs232_next_out);
tx_rs232_next_in=ni;
if(restart)
enable_interrupts(int_tbe2);
}
#INT_RDA2
void RDA2_isr()
{
unsigned int8 t;
rx_rs232_buffer[rx_rs232_next_in] = fgetc(RS232);
t = rx_rs232_next_in;
rx_rs232_next_in = (rx_rs232_next_in+1) % RX_RS232_BUFFER_SIZE;
if(rx_rs232_next_in == rx_rs232_next_out)
rx_rs232_next_in=t; // Buffer full !!
}
int8 bgetc_rs232()
{
unsigned int8 c;
unsigned int8 timeout;
while(!bkbhit_rs232 && ++timeout<=10)
delay_ms(10);
c = rx_rs232_buffer[rx_rs232_next_out];
rx_rs232_next_out = (rx_rs232_next_out+1) % RX_RS232_BUFFER_SIZE;
return c;
}
/////////////// RS232 SERIAL PORT HANDLER STOP ///////////////////////
#INT_TIMER1
void timer1_isr()
{
// set_timer1(TIMER_SET_01); //0.1 sec
// multipier10--;
// if(multipier10==0)
// {
// multipier10=10;
// num_seconds_LED++;
// num_seconds++;
//
// }
}
void main()
{
//setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
//set_timer1(TIMER_SET_01); //0.1 sec
//multipier10=10;
tx_radio_next_in = tx_radio_next_out = 0;
tx_rs232_next_in = tx_rs232_next_out = 0;
rx_radio_next_in = rx_radio_next_out = 0;
rx_rs232_next_in = rx_rs232_next_out = 0;
//setup_timer1(TMR_DISABLED|TMR_DIV_BY_1);
enable_interrupts(INTR_GLOBAL);
enable_interrupts(INT_RDA);
enable_interrupts(INT_TBE);
enable_interrupts(INT_RDA2);
enable_interrupts(INT_TBE2);
while(1)
{
/*
LED_SYNC_OFF();
fprintf(radio,"%x", 0xAA);
fprintf(rs232, "%x", 0xAA);
delay_ms(50);
LED_SYNC_ON();
fprintf(radio,"%x", 0xAA);
fprintf(rs232, "%x", 0xAA);
delay_ms(50);
*/
LED_SYNC_OFF();
printf(bputc_radio,"A");
printf(bputc_rs232, "A");
delay_ms(50);
LED_SYNC_ON();
printf(bputc_radio,"A");
printf(bputc_rs232, "A");
delay_ms(50);
}
}
|
|
|
|
|
|
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
|