View previous topic :: View next topic |
Author |
Message |
ADN
Joined: 23 Dec 2005 Posts: 9
|
software RS232 problem! help! |
Posted: Thu Feb 16, 2006 11:11 am |
|
|
Hello,
I am using a PIC18F2550 which has a software R232-connection to the PC. I want that the RS232 waits till it gets 8 characters from the PC.
This is my code:
Code: |
#use rs232(baud=57600, bits=8,parity=n,xmit=PIN_A1, rcv=PIN_A2, stream=commu)
#byte TRISA = 0xF92
bit_clear(TRISA,1);
bit_set(TRISA,2);
RB_counter=0;
while (RB_counter<8)
{
while(!kbhit(commu));
if (kbhit(commu))
{
alps_in_data[RB_counter]=fgetc(commu);
delay_us(1);
RB_counter=RB_counter+1;
}
}
|
I thought the pic would wait at the line while(!kbhit(commu)); till it gets a new character but I habe seen the pic does not stop at that positon and read 8 NULL values, without that the PC has sent any thing!
I am really at loss and need urgently your help!
Thx in advance!! |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Feb 16, 2006 11:56 am |
|
|
If the loop doesn't stop, it seems something wrong with the Rcv input.
At first glance I see that the pins used are Digital I/O or Analog Inputs.
After power up, they wake up as Analog Inputs unless other specification.
Add this in the beggining:
setup_adc_ports(NO_ANALOGS);
Made some changes in the polling..
Code: |
RB_counter=0;
while(1)
{
do
{
// wait here...
}while(!kbhit(commu));
if (kbhit(commu))
{
alps_in_data[RB_counter]=fgetc(commu);
RB_counter=RB_counter+1;
}
if (RB_counter>8)
{
RB_counter=0;
......
// read the received packet
......
}
}
|
Humberto
Last edited by Humberto on Thu Feb 16, 2006 12:10 pm; edited 2 times in total |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 12:03 pm |
|
|
I have already added this command
setup_adc_ports(NO_ANALOGS|VSS_VDD);
so it is obviously not the cause of the problem
ADN |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Feb 16, 2006 12:14 pm |
|
|
PIN_A2 should be high when idle, did you check this ?
Humberto |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 12:19 pm |
|
|
Quote: |
Code:
RB_counter=0;
while(1)
{
do
{
// wait here...
}while(!kbhit(commu));
if (kbhit(commu))
{
alps_in_data[RB_counter]=fgetc(commu);
RB_counter=RB_counter+1;
}
if (RB_counter>8)
{
RB_counter=0;
......
// read the received packet
......
}
}
|
but I think this does not change any thing, in the fact that the "while(!kbhit(commu)); " loop does not wait for an incoming character. |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 12:21 pm |
|
|
Humberto wrote: | PIN_A2 should be high when idle, did you check this ?
Humberto |
yes I have checked this and both pins A1 and A2 are high |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 12:26 pm |
|
|
What does the rest of your RS-232 interface look like? Do you use an interface chip like the MAX-232? |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 12:27 pm |
|
|
Quote: | but I think this does not change any thing, in the fact that the "while(!kbhit(commu)); " loop does not wait for an incoming character. | The software UART is level sensitive and sees a logic 0 as the start bit, this as opposed to a hardware UART which is edge sensitive and would wait for the high-to-low edge. |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 12:33 pm |
|
|
ckielstra wrote: | What does the rest of your RS-232 interface look like? Do you use an interface chip like the MAX-232? |
exactly, I am using MAX232 |
|
|
ckielstra
Joined: 18 Mar 2004 Posts: 3680 Location: The Netherlands
|
|
Posted: Thu Feb 16, 2006 12:43 pm |
|
|
Which compiler version are you using? It might be a compiler bug.
Does your program work when you move the receive PIN_A2 to a pin on another port, for example port B? |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 12:51 pm |
|
|
ckielstra wrote: | Which compiler version are you using? It might be a compiler bug.
Does your program work when you move the receive PIN_A2 to a pin on another port, for example port B? |
I am using the PCWH version of CCSC.
Actually I did not try to choose an other pin on another port. But this is a good idea. I will try it out.
ADN |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Feb 16, 2006 12:51 pm |
|
|
ADN wrote:
Quote: |
but I think this does not change any thing, in the fact that the "while(!kbhit(commu)); " loop does not wait for an incoming character.
|
Yes it does. kbhit() wait for the start bit of an incoming character.
Is the only way to wait for a start bit using software UART. At @57600 Baud -as you are using- the start bit is 17us long.
When asking something, pls post full info. We doesn't know how did you set the
18 configuration bits that determine the main behaviour of this processor.
Humberto
Last edited by Humberto on Thu Feb 16, 2006 12:57 pm; edited 1 time in total |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 12:57 pm |
|
|
sorry Humbrto, you r right!
here are the fuses I am using and the different setup commands:
Code: |
#FUSES NOWDT //No Watch Dog Timer
//#FUSES WDT128 //Watch Dog Timer uses 1:128 Postscale
#FUSES XTPLL //Crystal/Resonator with PLL enabled
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOBROWNOUT //No brownout reset
//#FUSES BORV20 //Brownout reset at 2.0V
#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 Programming on B3(PIC16) or B5(PIC18)
#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 MCLR //Master Clear pin enabled
#FUSES LPT1OSC //Timer1 configured for low-power operation
#FUSES NOXINST //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLL1 //No PLL PreScaler
#FUSES USBDIV
#FUSES VREGEN
#use delay(clock=48000000)
setup_adc_ports(NO_ANALOGS|VSS_VDD);
setup_adc(ADC_OFF);
setup_spi(FALSE);
setup_wdt(WDT_OFF);
setup_timer_0(RTCC_INTERNAL);
setup_timer_1(T1_DISABLED);
setup_timer_2(T2_DISABLED,0,1);
setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
setup_comparator(NC_NC_NC_NC);
setup_vref(VREF_LOW|-2);
setup_low_volt_detect(FALSE);
setup_oscillator(False);
setup_spi(spi_master |spi_clk_div_4 |SPI_L_TO_H | SPI_XMIT_L_TO_H);
|
hope it helps find out the mistake!
ADN |
|
|
Humberto
Joined: 08 Sep 2003 Posts: 1215 Location: Buenos Aires, La Reina del Plata
|
|
Posted: Thu Feb 16, 2006 1:28 pm |
|
|
#use delay(clock=48000000)
Quote: |
#FUSES XTPLL //Crystal/Resonator with PLL enabled
|
What kind of crystal are you using ? I mean serial / paralel cut.
Mmmh.. did you make a sofware delay test to make sense of how fast the PIC is running?
Humberto |
|
|
ADN
Joined: 23 Dec 2005 Posts: 9
|
|
Posted: Thu Feb 16, 2006 1:41 pm |
|
|
I am using a 4 Mhz crystal. And it works smoothly. |
|
|
|