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 CCS Technical Support

PIC18F2525 uart

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



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PIC18F2525 uart
PostPosted: Tue Jun 02, 2015 12:39 pm     Reply with quote

Hi,

I tried a new pic for me, the 18F2525 with a 20MHZ crystal.

Code:

#include <18F2525.h>

//fuses
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20000000)
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)


printf is ok, works. But I can't receive any char's!

This is not working
Code:

while(!kbhit())
{
    ch = getch();
    printf("ch");
}


and the #INT_RDA is also not working

Code:

#INT_RDA
void rs232_handler(void)
{
   chr=getc();
   printf("hallo");
}



Is it better to use a 10MHz crystal?
temtronic



Joined: 01 Jul 2010
Posts: 9221
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 1:20 pm     Reply with quote

you should post your entire code as 'snippets' don't tell the WHOLE story.

for instance we don't KNOW if you're enabled the interrupt for INT_RDA.

Jay
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 2:10 pm     Reply with quote

code below is working with a PIC18F2550 but not with the pic18f2525

Code:


/////////////////////////////////////////////////////////////////////////////
//
// Include libraries
//
/////////////////////////////////////////////////////////////////////////////
#include <modul192.h>

/////////////////////////////////////////////////////////////////////////////
//
// Global definitions
//
/////////////////////////////////////////////////////////////////////////////

char chr='A';
int16 bitcounter = 0;
char InputData[C_TOTAL_LED_HEX];
char OutputData[C_TOTAL_LED_HEX];
char SCTData[C_SCT_COUNT][C_LED_COUNT_ON_SCT];


/////////////////////////////////////////////////////////////////////////////
//
// Sub Functions
//
/////////////////////////////////////////////////////////////////////////////
void SetSCTDataBin(int8 val, int sctindex, int ledindex)
{
  int8 i;
  for (i = 0; i < 8; i++)
  {
    if (val & 0x80)  // test the MSB (Most significant bit (bit7))
      SCTData[sctindex][ledindex] = '1';
    else
      SCTData[sctindex][ledindex] = '0';
    ledindex++;
    val <<= 1;  // Shift left y by 1 bit
  }
}

void SplitInputData()
{
   int16 dataindex=0; //Input data index
   int sctindex=0;  //STC driver index
   int ledindex=0;  //LED index on serveral stc driver
   
   for (dataindex=0; dataindex<C_TOTAL_LED_HEX;dataindex++)
   {
      SetSCTDataBin(InputData[dataindex], sctindex, ledindex);
      ledindex+=8;
     
      if((dataindex>0) && ((dataindex+1) % (C_LED_COUNT_ON_SCT_HEX) == 0))
      {
         //next stc driver
         sctindex++;
         ledindex=0;
      }
   }
}

//Clock SCT driver
void SCT_Clock(void)
{
   output_high(SCT_CLK);
   delay_us(1);
   output_low(SCT_CLK);
}

//Move STCData to STC output register
void SCT_MoveDataToRegister(void)
{
   int ledindex=0;
   
   for (ledindex=0; ledindex<C_LED_COUNT_ON_SCT; ledindex++)
   {
      if(SCTData[0][ledindex]=='1') { output_high(SCT_SDI_1); }
      else { output_low(SCT_SDI_1); }
     
      if(SCTData[1][ledindex]=='1') { output_high(SCT_SDI_2); }
      else { output_low(SCT_SDI_2); }
     
      if(SCTData[2][ledindex]=='1') { output_high(SCT_SDI_3); }
      else { output_low(SCT_SDI_3); }
     
      if(SCTData[3][ledindex]=='1') { output_high(SCT_SDI_4); }
      else { output_low(SCT_SDI_4); }
     
      if(SCTData[4][ledindex]=='1') { output_high(SCT_SDI_5); }
      else { output_low(SCT_SDI_5); }
     
      if(SCTData[5][ledindex]=='1') { output_high(SCT_SDI_6); }
      else { output_low(SCT_SDI_6); }
     
      if(SCTData[6][ledindex]=='1') { output_high(SCT_SDI_7); }
      else { output_low(SCT_SDI_7); }
     
      if(SCTData[7][ledindex]=='1') { output_high(SCT_SDI_8); }
      else { output_low(SCT_SDI_8); }
     
      if(SCTData[8][ledindex]=='1') { output_high(SCT_SDI_9); }
      else { output_low(SCT_SDI_9); }
     
      if(SCTData[9][ledindex]=='1') { output_high(SCT_SDI_10); }
      else { output_low(SCT_SDI_10); }
     
      if(SCTData[10][ledindex]=='1') { output_high(SCT_SDI_11); }
      else { output_low(SCT_SDI_11); }
     
      if(SCTData[11][ledindex]=='1') { output_high(SCT_SDI_12); }
      else { output_low(SCT_SDI_12); }
     
      //Clock stc driver
      SCT_Clock();
 
   }
}

//Data latch STC
void SCT_LatchData(void)
{
   output_high(SCT_LA);
   delay_ms(1);
   output_low(SCT_LA);
}

//Initialize STC driver
void SCT_Init()
{
      //disable STC output
      output_high(SCT_OE);
      output_low(SCT_LA);
      output_low(SCT_CLK);
}

//Send data via rs232
void SendData()
{
   int i;
   for (i=0;i<C_TOTAL_LED_HEX;i++)
   {
      putc(OutputData[i]);
   }
   putc('#');
}

/////////////////////////////////////////////////////////////////////////////
//
// Interrupts
//
/////////////////////////////////////////////////////////////////////////////
#INT_RDA
void rs232_handler(void)
{
   chr=getc();
   printf("hallo"); //only for testing
   if(chr != '#')
   {
      //Catch Input Data
      //printf("%c", chr);
      InputData[bitcounter]=chr;
      bitcounter++;
    }
   else
   {
      disable_interrupts(GLOBAL);
      disable_interrupts(INT_RDA);
      bitcounter=0;
     
      //Send OutputData
      SendData();
     
      //Copy InputData to OutputData
      memcpy(OutputData, InputData, sizeof(OutputData));
   
      //Split InputData
      SplitInputData();
     
      //Send InputData to STC
      SCT_MoveDataToRegister();
     
      //Latch STC
      SCT_LatchData();
     
      enable_interrupts(GLOBAL);
      enable_interrupts(INT_RDA);
   }
}
/////////////////////////////////////////////////////////////////////////////
//
// Main
//
/////////////////////////////////////////////////////////////////////////////
void main()
{
   char ch;
   
   //Init STC driver
   SCT_Init();
     
   //Data init
   for (int i=0; i<C_TOTAL_LED_HEX; i++)
   {
      OutputData[i] = 0x00;
      InputData[i] = 0x00;
   }
   
   //for debug only
   //char c[]={0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0xff,0xff,0xff,0xff,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80,0x01,0x80};
   //memcpy(InputData, c, sizeof(c));
   
   //Split InputData
   SplitInputData();
   
   //Send InputData to STC
   SCT_MoveDataToRegister();

   //Latch STC
   SCT_LatchData();
   
   //Enable interrupts
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_RDA);
   output_low(SCT_OE);

/*   printf("Hallo");
   while(!kbhit())
      {
         ch = getch();
         printf("ch");
      }
*/
   while(TRUE)
   {
      //Get global OutputEnabled
      if(input(GLOBAL_OE)==1)
      {
         output_high(SCT_OE);
         //printf("1");
      }
      else
      {
         output_low(SCT_OE);
         //printf("0");
      }
   }
       
}


heeader file
Code:

#include <18F2525.h>

//fuses
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(clock=20000000)


// i/o pins
#define SCT_SDI_1    PIN_A0
#define SCT_SDI_2    PIN_A1
#define SCT_SDI_3    PIN_A2
#define SCT_SDI_4    PIN_A3
#define SCT_SDI_5    PIN_A4
#define SCT_SDI_6    PIN_A5
#define SCT_SDI_7    PIN_B0
#define SCT_SDI_8    PIN_B1
#define SCT_SDI_9    PIN_B2
#define SCT_SDI_10   PIN_B3
#define SCT_SDI_11   PIN_B4
#define SCT_SDI_12   PIN_B5

#define SCT_OE       PIN_C1
#define SCT_CLK      PIN_C2
#define SCT_LA       PIN_C0
#define GLOBAL_OE    PIN_C4

//serial interface
#use rs232(baud=115200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,errors)

//constants
#define C_LED_COUNT_ON_SCT 16
#define C_LED_COUNT_ON_SCT_HEX 2
#define C_SCT_COUNT 12
#define C_TOTAL_LED_HEX 24
asmboy



Joined: 20 Nov 2007
Posts: 2128
Location: albany ny

View user's profile Send private message AIM Address

PostPosted: Tue Jun 02, 2015 3:27 pm     Reply with quote

Quote:

while(!kbhit())
{
ch = getch();
printf("ch");
}

will NEVER work if you have an INT_RDA set up........
AND could very well contend with it too,
as you are standing on the receive UART function WAITING in foreground!
what a mess of bad serial I/O Sad

also get the PRINTF() out of the INT_RDA handler
as well as all the junk code for manipulation.

everything from here
Quote:

if(chr != '#')


on down should be moved to MAIN() and polled .....

AND
whatever might be in VAR ch
all you willever print is CH..CH..CH etc Very Happy Very Happy Very Happy
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Tue Jun 02, 2015 11:58 pm     Reply with quote

This thinks are only for testing the serial interface.
My problem is that I cant' read any data from serial interface with PIC18F2525. With PIC18F2550 it works.

I try the ccs sample and the pic doesen't receive characters. There are special configuration settings for the PIC18F2525?
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 1:21 am     Reply with quote

1) A test program should be simple. What you want to show involves a dozen lines at most.... Currently you have so many things 'not involved' in the actual problem being used, that finding what is wrong is difficult...

2) Always with a problem like this, _post the compiler version_. This matters.

2) The actual logic you post for your 'read test' is wrong. Think about it. You want to wait 'while' there is not a key hit, and then when there is a key hit try to read the character. Currently you try to read the character if there is not one there, but if there is a character, never try to read it. If a character is seen on boot, the display will never be called....

Code:

#include <18F2525.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP,NODEBUG
#use delay(crystal=20000000)
#use rs232(baud=9600,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PORT1,errors)
int1 have_char=FALSE;
int8 ch;

#INT_RDA
void rs_rx(void)
{
   ch=getch();
   have_char=TRUE;
}

//interrupt version
void main(void)
{
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   while (TRUE)
   {
      if (have_char)
      {
         have_char=false;
         printf("ch"); //and print a message
      }
   }
}

//non interrupt version
void main2()
{
   int8 ch;
   while(TRUE)
   {
      while(!kbhit())
        ; //wait if there is no key
      ch = getch(); //now get the key
      printf("ch"); //and print a message
   }
}


Two complete versions here. 'main' using interrupts, and 'main2' not using interrupts. Compile whichever you want to test.

Your existing test, could only print a character, if the chip doesn't see one when it boots. Now I'd suspect the 2525, is waking faster than the 2550 (the PLL in the clock on this takes a long time to wake), so the 2525, wakes up before the RS232 inverter has stabilised, so always sees a character, so never responds to the faulty test code.....

The compiler version matters a lot on these chips, because there was a change after the revision B silicon to the meaning of two bits in the baudcon register. An old compiler might well not set these correctly, which could result in the serial receive line actually being inverted.
Prefekt



Joined: 21 Oct 2010
Posts: 85

View user's profile Send private message

PostPosted: Wed Jun 03, 2015 4:43 am     Reply with quote

thanks, i will test it.

compiler version is: 5.032
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