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

18f27j13 usart problem

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



Joined: 10 Oct 2004
Posts: 21
Location: UK

View user's profile Send private message Yahoo Messenger MSN Messenger

18f27j13 usart problem
PostPosted: Wed Aug 05, 2015 3:10 am     Reply with quote

Hi,

I can't seem to get either usart to work on this device and some help would be greatly appreciated.

Uart1 does not work at all and uart2 locks the processor up.

In the code there is dataCounter and dataCounter2 both of which do not change.

I have scoped both rx pins and they have valid signals on them.

Compiler version is 5.044

Setup code is
Code:

#include "18F27J13.h"

#Device  PASS_STRINGS=IN_RAM 
#ZERO_RAM

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES WDT128                   //Watch Dog Timer uses 1:128 Postscale
#FUSES NOSTVREN                 //Stack full/underflow will not cause reset
#FUSES NOXINST                  //Extended set extension and Indexed Addressing mode disabled (Legacy mode)
#FUSES PLLEN                    //4X HW PLL enabled
#FUSES INTRC_PLL_IO             //Internal RC Osc with 4X PLL, no CLKOUT
#FUSES NOCLOCKOUT           
#FUSES SOSC_DIG                 //Digital mode, I/O port functionality of RC0 and RC1
#FUSES NOFCMEN                  //Fail-safe clock monitor disabled
#FUSES NOIESO                   //Internal External Switch Over mode disabled
#FUSES NODSBOR                  //BOR disabled in Deep Sleep
#FUSES NODSWDT                  //Deep Sleep Watchdog Timer disabled
#FUSES NOIOL1WAY                //Allows multiple reconfigurations of peripheral pins

//#OPT 9
#use delay(crystal=16M,clock=16M)

#pin_select U2RX=PIN_B7
#pin_select U2TX=PIN_B6

#use rs232(baud=9600,parity=N,bits=8,ERRORS,UART1)
#use rs232(baud=9600,parity=N,bits=8,ERRORS,UART2)

#use fast_io(A)
#use fast_io(B)
#use fast_io(C)



and

Code:

void init(void)
{
   port_B_pullups(0xFF);
    
   TRISA = 0b11010000;// a0 a1 a2 a3 relays
   TRISB = 0b10000111;// b0 b1 b2 buttons b4 tx_en
   TRISC = 0b10000000;// c0 lcd reset c1 lcd_cs c3 sck c5 sdi c6 txd1 c7 rxd1
   
   ANSELB = 0x00;                  // All digital
     ANSELC = 0x00;                  // All digital
   
#define SPI_MODE_0  (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1  (SPI_L_TO_H)
#define SPI_MODE_2  (SPI_H_TO_L)
#define SPI_MODE_3  (SPI_H_TO_L | SPI_XMIT_L_TO_H)

   delay_ms(1000);
   
   ///////////////////////////////////
   //Start with everything disabled
   ///////////////////////////////////   
   setup_oscillator(OSC_16MHZ|OSC_NORMAL|OSC_PLL_OFF);
   setup_timer_0(T0_OFF);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16, 157, 16);
   setup_timer_3(T3_DISABLED|T3_DIV_BY_1);
   setup_timer_4(T4_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_ccp1(CCP_OFF);
   setup_ccp2(CCP_OFF);
   setup_ccp3(CCP_OFF);
   setup_ccp4(CCP_OFF);
   setup_ccp5(CCP_OFF);
//   setup_dac(DAC_OFF);
   setup_adc(ADC_OFF | NO_ANALOGS);
   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   ///////////////////////////////////
   
   // was mode 0
   setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16 );
   
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_RDA2);
   enable_interrupts(INT_TBE);
   enable_interrupts(INT_TBE2);
   enable_interrupts(periph);
   enable_interrupts(GLOBAL);           

     // for now force txen to on
   DE1 = 1;// Transmit pin enabled usart1 high is enabled
   RE1 = 0;// Receive pin enabled usart1 low ie enabled
   
   DE1 = 1;// Transmit pin enabled usart2 high is enabled
   RE2 = 0;// Receive pin enabled usart2 low ie enabled
   
   // register enables
   TXEN = 1;
   TXEN2 = 1;   
}


interrupt code

Code:

#INT_RDA                  
void rx_handler(void)
{
   unsigned char temp;
   
   dataCounter++;   
     
   if(RCSTA & 0x06){       // If an error has occured, old code produces an error
      temp = RCREG;      // dummy read to clear usart rx buffer
      temp = RCREG;      // dummy read to clear usart rx buffer
      temp = RCREG;      // dummy read to clear usart rx buffer - done 3 times to clear FIFO
      CREN = 0;         // Clear the error
      CREN = 1;         // Enable the port to recieve
   }
   else{      
      rxBuf[rxRecPtr] = RCREG;
      rxRecPtr++;
      rxRecPtr &= RX_BUFF_SIZE;   // Enforce buffer circularity
      
      //dataCounter++;   
   }
}

#INT_RDA2                  
void rx_handler2(void)
{
   unsigned char temp;
   
   dataCounter2++;

   if(RCSTA2 & 0x06){       // If an error has occured,  this produces an error
      temp = RCREG2;      // dummy read to clear usart rx buffer
      temp = RCREG2;      // dummy read to clear usart rx buffer
      temp = RCREG2;      // dummy read to clear usart rx buffer - done 3 times to clear FIFO
      CREN2 = 0;         // Clear the error
      CREN2 = 1;         // Enable the port to recieve
   }
   else{
      rxBuf2[rxRecPtr2] = RCREG2;   // Read the recieve regester
      rxRecPtr2++;
      rxRecPtr2 &= RX_BUFF_SIZE;   // Enforce buffer circularity
      
      packetCount++;
   }
}

mjdale2003



Joined: 10 Oct 2004
Posts: 21
Location: UK

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Aug 05, 2015 3:24 am     Reply with quote

Updated the compiler to v5.048 but the problem is still there
mjdale2003



Joined: 10 Oct 2004
Posts: 21
Location: UK

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Aug 05, 2015 3:43 am     Reply with quote

Changed fuses to

Code:

#fuses HS,NODEBUG,NOXINST,NOPROTECT,NOWDT,NOCLOCKOUT,NOIOL1WAY,NOWPCFG,NOWPFP


but still no change. The processor is running as i have an oled printing the values of other variables one of which is being xored once a second in the main loop
mjdale2003



Joined: 10 Oct 2004
Posts: 21
Location: UK

View user's profile Send private message Yahoo Messenger MSN Messenger

PostPosted: Wed Aug 05, 2015 3:54 am     Reply with quote

i have looked in the list file and there is no reference RPOR10, RPOR9 or address ECA, EC9 which is where i would expect the #pin_select to be doing something
Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Wed Aug 05, 2015 4:04 am     Reply with quote

First rid of your enable_interrupts on the two TBE interrupts.

The point is that TBE, is _always_ true, until you actually start sending data. It says 'my buffer is empty I want to send something'. This will always be true when the chip starts. You should never enable INT_TBE, until you have data to actually send.

Look at how ex_stisr.c does it. INT_TBE is _not_ enabled, till the routine to send data is called. It then saved the data to the buffer, and enables the interrupt. In the interrupt handler if data is available, it loads it. If not, it disables the interrupt.

INT_TBE (and 2), needs to always be disabled, unless you are sending data. Depending on how your handler is written it might recover, but otherwise it'll hang the chip.

Then simplify. We can't have even a remote hope of knowing what your code is doing, because it is incomplete, but we are not going to wade through loads of code. If you have a UART problem, write a small (<50line) program that just sets up the two UART's, and attempts to use both. It needs to be something complete so we can compile it.
The odds are that when you write this you find what the problem actually is.

Honestly you code is 'half CCS', You enable CCS to handle UART errors for you, but then don't use the CCS code to do this. You have CCS code to set the analogs to all off, but also set the registers yourself. 'Belt and braces' is not good in code. It is likely to lead to things not being really set how you expect.
onuruygur



Joined: 17 Jul 2009
Posts: 7

View user's profile Send private message

PostPosted: Wed Aug 05, 2015 9:55 am     Reply with quote

it seems not be related with uarts. you may check the timings and the interrupts. locking of the cpu generally occurs by the timing or looping interrupts or deadly resets.
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