|
|
View previous topic :: View next topic |
Author |
Message |
nmeyer
Joined: 09 Jul 2004 Posts: 70
|
Using fgetc() not working? |
Posted: Mon Apr 28, 2008 11:38 am |
|
|
I have a design that uses a PIC18F8722 and i use both uarts on this part. The first one i have as serial out to a PC. The second i have driving a RS485 converter chip to talk on a RS485 bus. I am able to write to the bus with my chip just fine. I can send data and get the devices i am talking to working. However, i also need to read data back from the devices and this is where i am having issues. I have tried to use the fgetc() and kbhit() to read in the data values. However, what i read in is garbage, i get numbers but they are not what i expect. I have attached a rs485 monitor to the rs485 bus and i can see the data i am writing and i can see the data that the devices is writing back. so , know the devices is sending back valid data, i am just not reading it in correctly. I have also tried to use #int_rda2 with no better results. Here is some code that i am testing with. This was my full program, but i have hacked it back and there fore there are some things that are not used. Version 4.062
Code: |
#include <18F8722.h>
#device adc=10
#include <stdio.h>
#include <math.h>
#include <bootloader.h>
#include <stdlib.h>
//#include <input.c>
#FUSES WDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
//#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
//#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled
#use delay(clock=40000000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,RESTART_WDT,ERRORS,stream=SP1)
#use rs232(baud=19200,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8,RESTART_WDT,ERRORS,stream=rs485)
#use rs232(baud=9600,parity=N,xmit=PIN_H4,rcv=PIN_H5,bits=8,RESTART_WDT,ERRORS,stream=SP2)
#use rs232(baud=9600,parity=N,xmit=PIN_H6,rcv=PIN_H7,bits=8,RESTART_WDT,ERRORS,stream=SP3)
#use spi(DO=PIN_C5,CLK=PIN_C3,bits=7)
#priority CCP1, TIMER1 //added for frequency measurement
#define BytePtr(var, offset) (char *)(&(char *)var + offset)
#byte PIR1 = 0xF9E
#bit TMR1IF = PIR1.0
#define LCD_POWER PIN_D7 //enable pin for LCD
#define MODE PIN_C4 //used on LED driver chip
#define XLAT PIN_G4 //used to latch state of LED driver chip
#define BLANK PIN_D2 //Blank pin on led drive chip
#define INTS_PER_SECOND 76 //timer set up variable
#define start_addr getenv("PROGRAM_MEMORY")-0x0200
#org start_addr,start_addr+32{}
#include <Flex_LCD_8722.C> //handles LCD set up
#include <internal_eeprom.c>
**Variables set up here, deleted code for clarity**
////////////////////////////////////////////////////////////////////////////////
#int_rda2
void rs484_isr(void)
{
output_high(PIN_H2);
/*if(kbhit(rs485)&& rda_num==0)
{
rbyte1=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==1)
{
rbyte2=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==2)
{
rbyte3=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==3)
{
rbyte4=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==4)
{
rbyte5=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==5)
{
rbyte6=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==6)
{
rbyte7=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==7)
{
rbyte8=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==8)
{
rbyte9=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==9)
{
rbyte10=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==10)
{
rbyte11=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==11)
{
rbyte12=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==12)
{
rbyte13=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==13)
{
rbyte14=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==14)
{
rbyte15=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==15)
{
rbyte16=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==16)
{
rbyte17=fgetc(rs485);
//rda_num=rda_num+1;
}
if(kbhit(rs485)&& rda_num==17)
{
rbyte18=fgetc(rs485);
output_low(PIN_H2);
}*/
rda_num=rda_num+1;
if(kbhit(rs485))
{
rbyte1=fgetc(rs485);
rbyte2=fgetc(rs485);
rbyte3=fgetc(rs485);
rbyte4=fgetc(rs485);
rbyte5=fgetc(rs485);
rbyte6=fgetc(rs485);
rbyte7=fgetc(rs485);
rbyte8=fgetc(rs485);
rbyte9=fgetc(rs485);
rbyte10=fgetc(rs485);
rbyte11=fgetc(rs485);
rbyte12=fgetc(rs485);
rbyte13=fgetc(rs485);
rbyte14=fgetc(rs485);
rbyte15=fgetc(rs485);
rbyte16=fgetc(rs485);
rbyte17=fgetc(rs485);
rbyte18=fgetc(rs485);
}
output_low(PIN_H2);
}
////////////////////////////////////////////////////////////////////////////////
int16 crc_1021(int16 old_crc, int8 data)
{
//int16 crc;
int16 x;
x=make8(old_crc,1)^data;
x^=x>>4;
crc=(old_crc<<8) ^ (x<<12) ^ (x<<5) ^ x;
crc&=0xffff;
return crc;
}
////////////////////////////////////////////////////////////////////////////////
void write_rs485(void)
{
crc=0;
crc=crc_1021(crc,byte5);
crc=crc_1021(crc,byte6);
crc=crc_1021(crc,byte7);
crc=crc_1021(crc,byte8);
crc=crc_1021(crc,byte9);
crc=crc_1021(crc,byte10);
crc=crc_1021(crc,byte11);
crc=crc_1021(crc,byte12);
crc=crc_1021(crc,byte13);
crc=crc_1021(crc,byte14);
crc=crc_1021(crc,byte15);
crc=crc_1021(crc,byte16);
byte17=make8(crc,1);
byte18=make8(crc,0);
crc_send=crc;
output_high(PIN_H2);
fprintf(rs485,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",byte1,byte2,byte3,
byte4,byte5,byte6,byte7,byte8,byte9,byte10,byte11,byte12,byte13,
byte14,byte15,byte16,byte17,byte18,byte19,byte20);
output_low(PIN_H2);
}
////////////////////////////////////////////////////////////////////////////////
void read_rs485(void)
{
rda_num=0;
output_high(PIN_H2);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte1=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte2=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte3=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte4=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte5=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte6=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte7=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte8=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte9=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte10=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte11=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte12=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte13=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte14=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte15=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte16=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte17=fgetc(rs485);
//while(!kbhit(rs485))
//delay_us(5);
if(kbhit(rs485))
rbyte18=fgetc(rs485);
output_low(PIN_H2);
crc=0;
crc=crc_1021(crc,rbyte5);
crc=crc_1021(crc,rbyte6);
crc=crc_1021(crc,rbyte7);
crc=crc_1021(crc,rbyte8);
crc=crc_1021(crc,rbyte9);
crc=crc_1021(crc,rbyte10);
crc=crc_1021(crc,rbyte11);
crc=crc_1021(crc,rbyte12);
crc=crc_1021(crc,rbyte13);
crc=crc_1021(crc,rbyte14);
crc=crc_1021(crc,rbyte15);
crc=crc_1021(crc,rbyte16);
r_crc=make16(rbyte17,rbyte18);
nbyte17=make8(crc,1);
nbyte18=make8(crc,0);
//mult=read_adc();
//tempbyte=make32(0x00,byte14,byte15,byte16);
//disbyte=tempbyte;
//tempbyte=pmtrrpm_A*1200;//*1200
count=0;
/*if(A_state==1)
{
byte14=make8(tempbyte,2);
byte15=make8(tempbyte,1);
byte16=make8(tempbyte,0);
}*/
rda_num=0;
}
////////////////////////////////////////////////////////////////////////////////
void address_poll(void)
{
device_cnt=2;
devices[0]=0x00100008;
devices[1]=0x00100005;
//devices[0]=0x00100007;
//devices[1]=0x00100009;
}
////////////////////////////////////////////////////////////////////////////////
void main()
{
//int16 frequency; //variable used in frequency calculation
int32 current_ccp_delta; //varibal used frequency calculation
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
output_low(PIN_C5);
output_low(PIN_C3);
output_high(XLAT);
output_low(MODE);
output_low(BLANK);
setup_wdt(WDT_ON);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
set_rtcc(0);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_CCP2(CCP_CAPTURE_RE);
set_timer1(0);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_CCP1(CCP_CAPTURE_FE);
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
enable_interrupts(INT_EXT3);
ext_int_edge(0,H_TO_L);
ext_int_edge(1,H_TO_L);
ext_int_edge(2,H_TO_L);
ext_int_edge(3,H_TO_L);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);///int_ccp2
//enable_interrupts(INT_RDA2);
enable_interrupts(global);
output_high(LCD_POWER);
output_low(LCD_RW);
output_low(PIN_H2);
output_low(XLAT);
setup_spi(spi_master);
lcd_init();
set_tris_a(0b11111111);
set_tris_b(0b00111111);
set_tris_c(0b00000111);
set_tris_d(0b00000000);
set_tris_g(0b00000000);
set_tris_h(0b00000000);
output_low(PIN_C5);
output_low(PIN_C3);
output_low(XLAT);
output_low(MODE);
output_high(BLANK);
spi_write(0xFF);
output_high(XLAT);
delay_ms(10);
output_low(XLAT);
output_low(BLANK);
byte1=0x55;
byte2=0x55;
byte3=0x55;
byte4=0x54;
byte5=0xF1;
byte6=0x44;
byte7=0x10; //0x10
byte8=0x00;
byte9=0x05; //0x05 was other box
byte10=0x00;
byte11=0x00;
byte12=0x04;
byte13=0xC0;
byte14=0x01;
byte15=0x86;
byte16=0xA0;
byte19=0xFF;
byte20=0xFF;
rda_num=0;
////////////////////////////////////////////////////////////////////////////////
delay_ms(100);
print_now=10;
while (true)
{
byte9=0x05;
//if(rda_num==0)
write_rs485();
//if(rda_num==17)
read_rs485();
delay_ms(100);
if(print_now==10)
{
fprintf(SP1,"\fS%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\r",byte1,byte2,byte3,
byte4,byte5,byte6,byte7,byte8,byte9,byte10,byte11,byte12,byte13,
byte14,byte15,byte16,byte17,byte18);
fprintf(SP1,"\fR%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\r",rbyte1,rbyte2,rbyte3,
rbyte4,rbyte5,rbyte6,rbyte7,rbyte8,rbyte9,rbyte10,rbyte11,rbyte12,rbyte13,
rbyte14,rbyte15,rbyte16,rbyte17,rbyte18,nbyte17,nbyte18);
//fprintf(SP1,"\f%Lu %Lu %Lu\n\r",frequency,hz,variable_save);
//fprintf(SP1,"\f%u %u %u %lu\n\r",encoder,new_encoder,sub_val,Ppop_A);
fprintf(SP2,"\fSerial Port 2\n\r");
fprintf(SP3,"\Serial Port 3\n\r");
print_now=0;
}
print_now=print_now+1;
}
}
|
|
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Mon Apr 28, 2008 12:02 pm |
|
|
Just some basical comments after see your code at first glance:
1) To transmit in RS485 mode, you need to toggle the transceiver control lines. In your code
I do not see anthing doing this. See the ENABLE option.
2) Inside the INT_RDA interrupt handler, it is not necesary to use the function kbhit()
because you already are in the interrupt vector. If the interrupt was triggered is
because the UART detected the Start bit, hence kbhit() is redundant.
3) Your code inside the interrupt handler is quite twisted. You can get the same using a
storing buffer and then doing a parser inside the main().
4) It is not a good practice to use several getc() inside the same function.
Humberto |
|
|
nmeyer
Joined: 09 Jul 2004 Posts: 70
|
|
Posted: Mon Apr 28, 2008 12:20 pm |
|
|
Thanks. I am toggling an enable pin on the rs485 manually. I saw the enable in the #use set up and i am going to implement that. Yes, the int is rather poorly written, but i was trying that vs just reading with the kbhit() function. I am going to work on cleaning this all up and reducing it to just a read and write program to make sure i do not have anything else that is conflicting. |
|
|
nmeyer
Joined: 09 Jul 2004 Posts: 70
|
|
Posted: Mon Apr 28, 2008 9:05 pm |
|
|
I have been able to get it to work better, but now it is not consistant. Some times when i power up my unit it runs for minutes and writes and reads the correct data with no glitches. then some times it will read errounous values all the time. I have attached my updated code, this is the best i have seen so far.
Code: |
#include <18F8722.h>
#device adc=10
#include <stdio.h>
#include <math.h>
#include <bootloader.h>
#include <stdlib.h>
#FUSES WDT //No Watch Dog Timer
#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES HS //High speed Osc (> 4mhz)
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
#FUSES BORV25 //Brownout reset at 2.5V
#FUSES NOPUT //No Power Up Timer
#FUSES NOCPD //No EE protection
#FUSES STVREN //Stack full/underflow will cause reset
#FUSES NODEBUG //No Debug mode for ICD
#FUSES NOLVP //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOWRT //Program memory not write protected
#FUSES NOWRTD //Data EEPROM not write protected
#FUSES IESO //Internal External Switch Over mode enabled
#FUSES FCMEN //Fail-safe clock monitor enabled
//#FUSES PBADEN //PORTB pins are configured as analog input channels on RESET
#FUSES NOWRTC //configuration not registers write protected
#FUSES NOWRTB //Boot block not write protected
#FUSES NOEBTR //Memory not protected from table reads
#FUSES NOEBTRB //Boot block not protected from table reads
#FUSES NOCPB //No Boot Block code protection
//#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES MCLR //Master Clear pin enabled
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled
#use delay(clock=40000000,RESTART_WDT)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,RESTART_WDT,ERRORS,stream=SP1)
#use rs232(baud=19200,parity=N,xmit=PIN_G1,rcv=PIN_G2,bits=8,RESTART_WDT,ERRORS,ENABLE=PIN_H2,stream=rs485)
#use rs232(baud=9600,parity=N,xmit=PIN_H4,rcv=PIN_H5,bits=8,RESTART_WDT,ERRORS,stream=SP2)
#use rs232(baud=9600,parity=N,xmit=PIN_H6,rcv=PIN_H7,bits=8,RESTART_WDT,ERRORS,stream=SP3)
#use spi(DO=PIN_C5,CLK=PIN_C3,bits=7)
#priority CCP1, TIMER1 //added for frequency measurement
#define BytePtr(var, offset) (char *)(&(char *)var + offset)
#byte PIR1 = 0xF9E
#bit TMR1IF = PIR1.0
#define LCD_POWER PIN_D7 //enable pin for LCD
#define MODE PIN_C4 //used on LED driver chip
#define XLAT PIN_G4 //used to latch state of LED driver chip
#define BLANK PIN_D2 //Blank pin on led drive chip
#define INTS_PER_SECOND 76 //timer set up variable
#define start_addr getenv("PROGRAM_MEMORY")-0x0200
#org start_addr,start_addr+32{}
#include <Flex_LCD_8722.C> //handles LCD set up
#include <internal_eeprom.c>
**Variable set up removed to shorten displayed program
////////////////////////////////////////////////////////////////////////////////
#int_timer1
void timer1_isr(void) //used in calculating frequency
{
gc_timer1_extension++;
}
////////////////////////////////////////////////////////////////////////////////
#int_ccp1
void ccp1_isr(void) // used to calculate frequency
{
char timer_ext_copy;
int32 current_ccp;
static int32 old_ccp=0;
gc_capture_flag=TRUE;
current_ccp=(int32)CCP_1;
timer_ext_copy=gc_timer1_extension;
if(TMR1IF)
{
if(*BytePtr(current_ccp,1)<2)
timer_ext_copy++;
gc_timer1_extension++;
TMR1IF=0;
}
*BytePtr(current_ccp,2)=timer_ext_copy;
g32_ccp_delta=(current_ccp>old_ccp)?current_ccp-old_ccp:current_ccp+(0x1000000-old_ccp);
old_ccp=current_ccp;
}
////////////////////////////////////////////////////////////////////////////////
#int_ccp2 //Radar A input
void ccp2_isr()
{
fall=ccp_1;
rise=ccp_2;
rate=rise - fall;
if(A_state==1)
{
dist=dist+1;
irqxdistcnt=irqxdistcnt+1;
}
if(feet>=9999)
irqxdistcnt=0;
}
////////////////////////////////////////////////////////////////////////////////
#int_rtcc
clock_isr()
{
count=count+1;
if(--int_count==0) //decrement counter until 0
{
++seconds; //count up seconds
int_count=INTS_PER_SECOND; //reset counter
if (seconds>=65000)
seconds=65000;
}
}
////////////////////////////////////////////////////////////////////////////////
#int_rda2
void rs484_isr(void)
{
rbyte[i]=fgetc(rs485);
i=i+1;
if(i>18)
{
i=1;
disable_interrupts(INT_RDA2);
}
}
////////////////////////////////////////////////////////////////////////////////
int16 crc_1021(int16 old_crc, int8 data)
{
//int16 crc;
int16 x;
x=make8(old_crc,1)^data;
x^=x>>4;
crc=(old_crc<<8) ^ (x<<12) ^ (x<<5) ^ x;
crc&=0xffff;
return crc;
}
////////////////////////////////////////////////////////////////////////////////
void write_rs485(void)
{
crc=0;
crc=crc_1021(crc,byte5);
crc=crc_1021(crc,byte6);
crc=crc_1021(crc,byte7);
crc=crc_1021(crc,byte8);
crc=crc_1021(crc,byte9);
crc=crc_1021(crc,byte10);
crc=crc_1021(crc,byte11);
crc=crc_1021(crc,byte12);
crc=crc_1021(crc,byte13);
crc=crc_1021(crc,byte14);
crc=crc_1021(crc,byte15);
crc=crc_1021(crc,byte16);
byte17=make8(crc,1);
byte18=make8(crc,0);
crc_send=crc;
enable_interrupts(INT_RDA2);
fprintf(rs485,"%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c",byte1,byte2,byte3,
byte4,byte5,byte6,byte7,byte8,byte9,byte10,byte11,byte12,byte13,
byte14,byte15,byte16,byte17,byte18,byte19,byte20);
}
////////////////////////////////////////////////////////////////////////////////
void read_rs485(void)
{
crc=0;
crc=crc_1021(crc,rbyte[5]);
crc=crc_1021(crc,rbyte[6]);
crc=crc_1021(crc,rbyte[7]);
crc=crc_1021(crc,rbyte[8]);
crc=crc_1021(crc,rbyte[9]);
crc=crc_1021(crc,rbyte[10]);
crc=crc_1021(crc,rbyte[11]);
crc=crc_1021(crc,rbyte[12]);
crc=crc_1021(crc,rbyte[13]);
crc=crc_1021(crc,rbyte[14]);
crc=crc_1021(crc,rbyte[15]);
crc=crc_1021(crc,rbyte[16]);
r_crc=make16(rbyte[17],rbyte[18]);
nbyte17=make8(crc,1);
nbyte18=make8(crc,0);
//mult=read_adc();
//tempbyte=make32(0x00,byte14,byte15,byte16);
//disbyte=tempbyte;
//tempbyte=pmtrrpm_A*1200;//*1200
count=0;
/*if(A_state==1)
{
byte14=make8(tempbyte,2);
byte15=make8(tempbyte,1);
byte16=make8(tempbyte,0);
}*/
rda_num=0;
}
////////////////////////////////////////////////////////////////////////////////
void address_poll(void)
{
device_cnt=2;
devices[0]=0x00100008;
devices[1]=0x00100005;
//devices[0]=0x00100007;
//devices[1]=0x00100009;
}
////////////////////////////////////////////////////////////////////////////////
void main()
{
//int16 frequency; //variable used in frequency calculation
int32 current_ccp_delta; //varibal used frequency calculation
setup_adc(ADC_OFF);
setup_psp(PSP_DISABLED);
output_low(PIN_C5);
output_low(PIN_C3);
output_high(XLAT);
output_low(MODE);
output_low(BLANK);
setup_wdt(WDT_ON);
setup_counters(RTCC_INTERNAL,RTCC_DIV_2);
set_rtcc(0);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(FALSE);
setup_CCP2(CCP_CAPTURE_RE);
set_timer1(0);
setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
setup_CCP1(CCP_CAPTURE_FE);
clear_interrupt(INT_TIMER1);
enable_interrupts(INT_TIMER1);
clear_interrupt(INT_CCP1);
enable_interrupts(INT_CCP1);
enable_interrupts(INT_EXT);
enable_interrupts(INT_EXT1);
enable_interrupts(INT_EXT2);
enable_interrupts(INT_EXT3);
ext_int_edge(0,H_TO_L);
ext_int_edge(1,H_TO_L);
ext_int_edge(2,H_TO_L);
ext_int_edge(3,H_TO_L);
enable_interrupts(INT_RTCC);
enable_interrupts(INT_CCP2);///int_ccp2
//enable_interrupts(INT_RDA2);
enable_interrupts(global);
output_high(LCD_POWER);
output_low(LCD_RW);
output_low(PIN_H2);
output_low(XLAT);
setup_spi(spi_master);
lcd_init();
set_tris_a(0b11111111);
set_tris_b(0b00111111);
set_tris_c(0b00000111);
set_tris_d(0b00000000);
set_tris_g(0b00000000);
set_tris_h(0b00000000);
output_low(PIN_C5);
output_low(PIN_C3);
output_low(XLAT);
output_low(MODE);
output_high(BLANK);
spi_write(0xFF);
output_high(XLAT);
delay_ms(10);
output_low(XLAT);
output_low(BLANK);
byte1=0x55;
byte2=0x55;
byte3=0x55;
byte4=0x54;
byte5=0xF1;
byte6=0x44;
byte7=0x10; //0x10
byte8=0x00;
byte9=0x05; //0x05 was other box
byte10=0x00;
byte11=0x00;
byte12=0x04;
byte13=0xC0;
byte14=0x01;
byte15=0x86;
byte16=0xA0;
byte19=0xFF;
byte20=0xFF;
step=0;
i=1;
th=0;
int_count=INTS_PER_SECOND; //used for timer
seconds=0;
loop_int=true;
field_op=0;
calib=0;
device0=devices[0];
device1=devices[1];
productA=FALSE;
productB=FALSE;
switch_val=1;
switch_val=binary;
pl_mode=0;
percent_2=0.02;
percent_4=0.04;
percent_6=0.066;
new_encoder=35;
dev=0;
rate_mode=1;
overspeed=0;
sub_val=40;
enc_fp=0;
rda_num=0;
////////////////////////////////////////////////////////////////////////////////
delay_ms(100);
print_now=10;
while (true)
{
byte9=0x05;
//if(rda_num==0)
write_rs485();
//if(rda_num==17)
read_rs485();
delay_ms(100);
if(print_now==10)
{
fprintf(SP1,"\fS%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\r",byte1,byte2,byte3,
byte4,byte5,byte6,byte7,byte8,byte9,byte10,byte11,byte12,byte13,
byte14,byte15,byte16,byte17,byte18);
/*fprintf(SP1,"\fR%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\r",rbyte1,rbyte2,rbyte3,
rbyte4,rbyte5,rbyte6,rbyte7,rbyte8,rbyte9,rbyte10,rbyte11,rbyte12,rbyte13,
rbyte14,rbyte15,rbyte16,rbyte17,rbyte18,nbyte17,nbyte18);*/
fprintf(SP1,"\fR%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c%c\n\r",rbyte[1],rbyte[2],rbyte[3],
rbyte[4],rbyte[5],rbyte[6],rbyte[7],rbyte[8],rbyte[9],rbyte[10],rbyte[11],rbyte[12],rbyte[13],
rbyte[14],rbyte[15],rbyte[16],rbyte[17],rbyte[18],nbyte17,nbyte18);
fprintf(SP2,"\fSerial Port 2\n\r");
fprintf(SP3,"\Serial Port 3\n\r");
print_now=0;
}
print_now=print_now+1;
}
}
|
|
|
|
Ttelmah Guest
|
|
Posted: Tue Apr 29, 2008 2:39 am |
|
|
As a comment, without looking at the code, how is the RS485 bus terminated?.
On RS485, you have to either use receivers, that 'see' an undriven bus as 'inactive, and return a logic high to chips reading the bus, or you have to ensure that the lines on the bus have a small 'bias', so that an idle bus is read as the inactive state. The normal way, is to terminate the bus, with a resistor to +5v, then a resistor between the lines, and then another resistor to ground, whose total parallel impedance, gives the required termination impedance, and also applies the slight bias needed. Typically 1.2KR to +5v, from the 'A' line, 120R between the lines, and then 1..2K to ground, from the 'B' line, gives a good match to 100R cable, and provides the required bias. Some manufacturers provide terminator chips, that give this bias, and some do line receivers guaranteed not to need this. Unless you have this, you _will_ be seeing huge amounts of pretty random data when the bus is not driven, and this could easily be making the code become erratic...
Best Wishes |
|
|
nmeyer
Joined: 09 Jul 2004 Posts: 70
|
|
Posted: Tue Apr 29, 2008 7:04 am |
|
|
I do have a 120 ohm between A and B terminating the line, but i do not have any pull up or pull down resistors. |
|
|
|
|
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
|