CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

Problem with software rs232

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
cyclo.sl



Joined: 07 Oct 2009
Posts: 11

View user's profile Send private message

Problem with software rs232
PostPosted: Mon Oct 26, 2009 11:35 am     Reply with quote

I'm using a software rs232 to communicate with another pic. When I'm testing the application with the pc rs232, the transmit of the sw rs232 works fine but the receive pin doesn't seem to work. Following is a part of my coding.. (pic18f4431)

Code:
#include pic18f4431.h
#use rs232(stream=2,baud=9600,parity=N,xmit=PIN_E1,rcv=PIN_E0,bits=8,errors,force_sw,disable_ints)


set_tris_e(0x01);

fprintf(2,"Hello");   // working

if (kbhit(2))
  int8 char = getch(2);  //not working..
//but when I disconnect the recev wire to pic, getch(2) returns a zero.

Any idea about this?
Thanks..
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 11:54 am     Reply with quote

1. Post a short compilable test program, with #fuses, #use delay, main(), etc.

2. Post if you are using a MAX232-type chip to interface the serial port
connections on the PIC to the PC.

3. Post your compiler version.
cyclo.sl



Joined: 07 Oct 2009
Posts: 11

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 1:00 pm     Reply with quote

Thanks PCM Programmer..
my bad.. I should have posted a complete program.
I'm using ccs PCWHD 4.057 and yes I'm using a MAX232 level converter.

Code:

#include <18F4431.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV27                   //Brownout reset at 2.7V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES NOSTVREN                 //Stack full/underflow will not 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 FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOWINEN                  //WDT Timer Window Disabled
#FUSES T1LOWPOWER               //Timer1 low power operation when in sleep
#FUSES HPOL_HIGH                //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7)
   //PWM module high side output pins have active high output polarity
#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 LPOL_HIGH                //Low-Side Transistors Polarity is Active-High (PWM 0,2,4 and 6)
   //PWM module low side output pins have active high output polar
#FUSES PWMPIN                   //PWM outputs disabled upon Reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES FLTAC1                   //FLTA input is multiplexed with RC1
#FUSES SSP_RC                   //SCK/SCL=RC5, SDA/SDI=RC4, SDO=RC7
#FUSES PWM4D5                   //PWM4 output is multiplexed on RD5
#FUSES EXCLKC3                  //TMR0/T5CKI external clock input is muliplexed with RC3

#use delay(clock=8000000)
#use rs232(stream=1,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
#use rs232(stream=2,baud=9600,parity=N,xmit=PIN_E1,rcv=PIN_E0,bits=8,errors,force_sw,disable_ints)

void main(){
  fprintf(1,"\n\rInitializing...");
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0|ADC_WHEN_INT0|ADC_INT_EVERY_OTHER);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);

   set_tris_a(0x00);   
   set_tris_b(0x00);
   set_tris_c(0x80);
   set_tris_d(0xFF);
   set_tris_e(0x01);

  fprintf(1,"Done");    //working (Hyper terminal)
  fprintf(2,"Soft UART");    //working

  int8 char = 'A';
  while(true){
    char = 'A';
    if (kbhit(2))
      char = getch(2);
   fprintf(1,"char %u",char);  // printing A
   }
}
 


As I said before, once I disconnect the receive wire from the sw rs232 recv pin, getch() always returns a zero(char=0).
I tried changing the sw uart pins in #use rs232 directive. But same thing happens with others pins too. Transmit is working but not the receive.
Tried using the Invert param in use rs232,but didn't work either.

Hope I made my self clear. If not, please tell me what else I have to post.. thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 3:34 pm     Reply with quote

Quote:
int8 char = 'A';

*** Error 51 "PCH_Test.c" Line 64(3,7): A numeric expression must appear here

Your code doesn't compile. I installed PCH vs. 4.057 and I got this error.

Quote:
char = 'A';
*** Error 28 "PCH_Test.c" Line 66(12,13): Expecting an identifier

And I got this error, and more.

Quote:
if (kbhit(2))
char = getch(2);
fprintf(1,"char %u",char); // printing A

*** Error 51 "PCH_Test.c" Line 68(7,11): A numeric expression must appear here
*** Error 51 "PCH_Test.c" Line 69(24,28): A numeric expression must appear here
*** Error 43 "PCH_Test.c" Line 71(1,2): Expecting a declaration

It doesn't like any of this either, and for the same reasons.



You can't use a keyword as a variable name. This means you're not
posting your real program.

Quote:
#use rs232(stream=1,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors) #use rs232(stream=2,baud=9600,parity=N,xmit=PIN_E1,rcv=PIN_E0,bits=8,errors,force_sw,disable_ints)

*** Error 100 "PCH_Test.c" Line 39(5,78): USE parameter value is out of range "STREAM"
*** Error 100 "PCH_Test.c" Line 40(5,100): USE parameter value is out of range "STREAM"

If I compile it with vs. 4.099, it gives me these additional errors.
It doesn't like numbers as streams. I pretty much knew that, but
apparently 4.057 doesn't report the error.

You need to fix all this. Post real code that compiles with no errors
and has been tested.
cyclo.sl



Joined: 07 Oct 2009
Posts: 11

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 5:48 pm     Reply with quote

PCM programmer wrote:

Your code doesn't compile. I installed PCH vs. 4.057 and I got this error.


omg thanks a lot PCM programmer. and Im really sorry for not posting the real code. The reason is that it is very lengthy. Since I don't want to make you angry any more, I'll post it. I've omitted few parts which will not affect this code. No compile errors generated.

Code:

#include "pic18f4431core.h"   //which holdes all thoes fuses and #use directives mentioned in my prev post
#define STREAM_SENSORS 2
#define STREAM_IR 1
#define INIT_DUTY 400L
#define MOTOR1_START_DUTY 564L
#define MOTOR2_START_DUTY 572L
#define MOTOR3_START_DUTY 596L
#define MOTOR4_START_DUTY 564L

int8 command=0;
int1 hasNewCommand=0;

void initialize(void);
void readCommand(void);
void processCommand(void);

void initialize()
{
   fprintf(STREAM_IR,"\n\rInitializing...");      
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_OFF|ADC_TAD_MUL_0|ADC_WHEN_INT0|ADC_INT_EVERY_OTHER);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);

   set_tris_a(0x00);   
   set_tris_b(0x00);
   set_tris_c(0x80);
   set_tris_d(0xFF);
   set_tris_e(0x01);
   
   setup_power_pwm(PWM_CLOCK_DIV_64 | PWM_FREE_RUN,1,0,2499L,0,1,0);
   setup_power_pwm_pins(PWM_BOTH_ON,PWM_BOTH_ON,PWM_BOTH_ON,PWM_BOTH_ON);
   
   set_power_pwm0_duty(INIT_DUTY);
   set_power_pwm2_duty(INIT_DUTY);
   set_power_pwm4_duty(INIT_DUTY);
   set_power_pwm6_duty(INIT_DUTY);
   
   fprintf(STREAM_IR,"  Done.");      
}

void readCommand()
{
   if (kbhit(STREAM_SENSORS)){      
      command = getch(STREAM_SENSORS);
      hasNewCommand = 1;
   }
}

void processCommand()
{
   fprintf(STREAM_IR,"Command %u received.",command);   
   //processing
   hasNewCommand = 0;
   
}

void main()
{
   initialize();
   delay_ms(500);
   while(true)
   {
      fprintf(STREAM_IR,".");
      delay_ms(500);
      readCommand();
      if (hasNewCommand)
         processCommand();   
   }
}


Stream_IR, which is the hardware UART, is working fine. And fprintfs with Stream_Sensors are also working fine. Yet the kbhit() in readCommand() always returns false even though I send values from the pc. Once I remove the recv wire, it returns true and the command variable will have a zero(may be because of pin floating). Is that indicate the receiving is working?

I thought the stream id should be a number and thats why I used a #define for that. yes the Compiler isn't giving any warnings for that. But why only receive is not working..?
Thank you
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 5:51 pm     Reply with quote

Quote:
#include "pic18f4431core.h"

It won't compile without this file.
cyclo.sl



Joined: 07 Oct 2009
Posts: 11

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 6:27 pm     Reply with quote

Thanks.
Yes that file only contains all thoes fuse settings and #use directives which I posted earlier. It is the normal header file automatically generated by CCS upon creating a new project.

pic18f4431core.h
Code:

#include <18F4431.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES NOPROTECT                //Code not protected from reading
#FUSES IESO                     //Internal External Switch Over mode enabled
#FUSES NOBROWNOUT               //No brownout reset
#FUSES BORV27                   //Brownout reset at 2.7V
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOCPD                    //No EE protection
#FUSES NOSTVREN                 //Stack full/underflow will not 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 FCMEN                    //Fail-safe clock monitor enabled
#FUSES NOWINEN                  //WDT Timer Window Disabled
#FUSES T1LOWPOWER               //Timer1 low power operation when in sleep
#FUSES HPOL_HIGH                //High-Side Transistors Polarity is Active-High (PWM 1,3,5 and 7)
   //PWM module high side output pins have active high output polarity
#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 LPOL_HIGH                //Low-Side Transistors Polarity is Active-High (PWM 0,2,4 and 6)
   //PWM module low side output pins have active high output polar
#FUSES PWMPIN                   //PWM outputs disabled upon Reset
#FUSES MCLR                     //Master Clear pin enabled
#FUSES FLTAC1                   //FLTA input is multiplexed with RC1
#FUSES SSP_RC                   //SCK/SCL=RC5, SDA/SDI=RC4, SDO=RC7
#FUSES PWM4D5                   //PWM4 output is multiplexed on RD5
#FUSES EXCLKC3                  //TMR0/T5CKI external clock input is muliplexed with RC3

#use delay(clock=8000000)
#use rs232(stream=1,baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)
#use rs232(stream=2,baud=9600,parity=N,xmit=PIN_E1,rcv=PIN_E0,bits=8,errors,force_sw,disable_ints)
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 7:05 pm     Reply with quote

I told you that modern versions of the compiler will not accept numbers
as the stream identifiers.

I suggest that you give the streams proper names, instead of using '1'
and '2'. Give them names such as "PC" and "GPS", or whatever name
is suitable for your application. Change all functions that use streams
to use the new names.

See if that fixes the problem.
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Mon Oct 26, 2009 7:46 pm     Reply with quote

A minor error, but could bite you later:
Code:
setup_spi(SPI_SS_DISABLED);
This is an error in the CCS wizard. Disable SPI with:
Code:
setup_spi(FALSE);
cyclo.sl



Joined: 07 Oct 2009
Posts: 11

View user's profile Send private message

PostPosted: Tue Oct 27, 2009 8:31 am     Reply with quote

Hey PCM programmer, I tried changing the stream IDs but still not working. Sad Please tell me if you have any suggestions.
Thank you

Thanks Ckielstra.. I made that change starting away.. Cool
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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