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

PIC16F882 UART issue

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



Joined: 17 Sep 2008
Posts: 10

View user's profile Send private message

PIC16F882 UART issue
PostPosted: Tue Oct 07, 2008 7:28 am     Reply with quote

Hi

I'm using CCS 4.076 version with PIC16F882 and following is my code.
The putc() function inside the for loop does not work properly.

Please could you let me know how can I correct it?


Main.C

Code:
/**************************************************************************************************
System Includes
**************************************************************************************************/
#include   <16F882.H>

/**************************************************************************************************
 compiler directives using __PCM__ compiler
***************************************************************************************************/
// Preprocessor directive that defines the chip fuses
#fuses INTRC,NOWDT,NOPROTECT,NOPUT,BORV21
// Preprocessor directive that specifies clock speed
#use delay(clock=4M)
// Preprocessor directive that includes RS232 libraries
#use RS232(BAUD=19200,  BITS=8, PARITY=N, STOP=1, XMIT=PIN_C6, RCV=PIN_C7, ERRORS)   

/**************************************************************************************************
User Includes header files
***************************************************************************************************/
#include "ControlUnit.h"
#include "ERROR_CODES.h"

/**************************************************************************************************
User source code files
***************************************************************************************************/
#include "RS232.C"

/**************************************************************************************************
Global variables
***************************************************************************************************/
//Push buttons press status flag
volatile BOOLEAN flg_B0_Pressed = FALSE;
volatile BOOLEAN flg_B1_Pressed = FALSE;

BOOLEAN g_bCameraON_OFF = TRUE;


/**************************************************************************************************
Function Name   : isr_timer2
Description      : Interrupt service routine called every milli-second. 
Parameters      : None
Returns         : None
****************************************************************************************************/
#int_timer2
void isr_timer2(void)
{
   //Initialize g_LastB0B1 to set port B0 & B1 to high by default (connected to pull-ups).
   static unsigned char g_LastB0B1 = 3;
   volatile signed long iDelay = DEBOUNCE_DELAY;
     unsigned char bChanges;
   
   //Check  B0 or B1 changed.
   bChanges = (g_LastB0B1 ^ input_b());
   g_LastB0B1 = input_b();

   //Checking SW0/B0 and SW1/B1 if it goes High to Low
   if( bit_test(bChanges,0) && (!bit_test(g_LastB0B1,0)) )
   {  //B0 went to low
      flg_B0_Pressed  = TRUE;
      //Debouncing wait.
      while(idelay --);
   }   
   else if( bit_test(bChanges,1) && (!bit_test(g_LastB0B1,1)) )
   {   //B1 went to low
      flg_B1_Pressed  = TRUE;
      //Debouncing wait.
      while(idelay --);
   }
   
   //output_toggle(PIN_B7); // Usful for testing timer2 as a waveform on analyser 1mS wave.
}

/**************************************************************************************************
Function Name   : main
Description      : Main - Start of Program
Parameters      : None
Returns         : None
****************************************************************************************************/
void main(void)
{
   unsigned char byError = 65;
   unsigned char ch;

      //Initialise RS232 communication
   InitRS232();

   //Setup up timer2 to interrupt every 1ms if using 20Mhz clock
   setup_timer_2(T2_DIV_BY_4,79,16);   

   //Enable timer2 interrupt
      enable_interrupts(INT_TIMER2);   

   //Enable all interrupts (else timer2 wont happen)
      enable_interrupts(GLOBAL);       

   //Initialise, switch on & config the camera.
   printf("Setting LED ON!!\r\n Variable value =  %d\r\n", byError);
   output_high(PIN_C2);

   putc('.');         
   printf("%d\r\n", 49);
   putc('N');         

   while(TRUE)
   {
      putc('.');         

         //Check if Camera ON/OFF button pressed.
      if(flg_B0_Pressed  == TRUE)         
        {
         flg_B0_Pressed  = FALSE;
         g_bCameraON_OFF = !g_bCameraON_OFF;      
      }
      
      //Check if Spare button B1 / Key1 pressed.   
         if(flg_B1_Pressed  == TRUE)         
        {
         flg_B1_Pressed  = FALSE;
      }
      
      if(IsDataAvailable() == TRUE)
      {   
         ch = g_RxBuffer[g_Next_out];
             g_Next_out =(( g_Next_out + 1 ) % BUFFERSIZE);
          //printf("%c" ch);   
         putc(ch );         


      }
      delay_ms(300);
   }
}


/***************************************************************************************************************************
End of the file
***************************************************************************************************************************/




RS232.C:


Code:
/**************************************************************************************************
Revision History
DD.MM.YYYY UID Description
24.09.2008 NPS First draft
***************************************************************************************************/

/**************************************************************************************************
System Includes
**************************************************************************************************/

/**************************************************************************************************
User Includes
***************************************************************************************************/
#include "RS232.h"

/**************************************************************************************************
Global variables
***************************************************************************************************/
//Global Rx buffer
unsigned char g_RxBuffer[BUFFERSIZE];
//Circular buffer positions initialisation
unsigned int  g_Next_in  = 0;
unsigned int  g_Next_out = 0;


/**************************************************************************************************
Static variables
***************************************************************************************************/

/**************************************************************************************************
Function Name   : InitRS232
Description      : Initialise RS232 buffer and enable the Rx interrupt.
Parameters      : None
Returns         : None
****************************************************************************************************/
void InitRS232(void)
{
   //Initialise the message structure.
   memset(g_RxBuffer, 0, BUFFERSIZE);

   //Set up MAX232
   output_high(RS232_FORCEON);
   output_high(RS232_FORCEOFF_BAR);
   output_low(RS232_ENABLE);

   //Enbale Rx interrupt.
    enable_interrupts(INT_RDA);
}


/**************************************************************************************************
Function Name   : isr_rda
Description      : Interrupt service routine called wenever a byte received in RS232 Rx buffer.
Parameters      : None
Returns         : None
****************************************************************************************************/
#int_rda
void isr_rda(void)
{
     unsigned int  update_next_in;
   unsigned char charIn;

   //Read character temporary.  If the buffer is full then drop the character.
      charIn = getc();

   if(FALSE ==  BUFFER_FULL(g_Next_in, g_Next_out ) )
   {
      //Store the character
      g_RxBuffer[g_Next_in]  = charIn;

      //Calculate next in postion.
      update_next_in = ((g_Next_in + 1) % BUFFERSIZE);

      //Following is to avoid false buffer empty situation if both
         if( update_next_in != g_Next_out )
         {
         g_Next_in = update_next_in;
      }
    }
}


/**************************************************************************************************
Function Name   : IsDataAvailable
Description      : This function is to provide status if there is any data avialable in RS232 Rx buffer.
Parameters      : None
Returns         : None
****************************************************************************************************/
BOOLEAN IsDataAvailable(void)
{
   BOOLEAN bReturn;

   bReturn = (g_Next_in == g_Next_out )? 0 :1;

 return(bReturn);
}


/**************************************************************************************************
Function Name   : GetBufChar
Description      : This function retrieves a byte from circular Rx buffer.
Parameters      : unsigned char* pChar - This will contain byte retried from circular Rx buffer.
Returns         : BOOLEAN  1 - Success
                     0 - Error / buffer empty.
****************************************************************************************************/
BOOLEAN GetBufChar(unsigned char* pChar)
{
   BOOLEAN bReturn = FALSE;
   volatile unsigned long ulGetChDelay = DELAY_GET_CHAR;

   while( (bReturn == FALSE) && (ulGetChDelay !=0) )
   {
      if( FALSE == BUFFER_EMPTY(g_Next_in, g_Next_out) )
         {
         *pChar = g_RxBuffer[g_Next_out];
             g_Next_out =(( g_Next_out + 1 ) % BUFFERSIZE);
         bReturn = TRUE;
      }
      else
      {
         ulGetChDelay--;
         //TODO: Reset WDT
      }
   }
 return(bReturn);
}
/***************************************************************************************************************************
End of the file
***************************************************************************************************************************/




O/p is:
Code:

Setting LED ON!!
                                                             
 Variable value =  65
                                                         
.49
                                                                           
N..                                                                             
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Oct 07, 2008 10:49 am     Reply with quote

You've posted a lot of code, probably more than we want to look at.
But, you need to post the output that you want to see. You have posted
the "bad" output. Post your desired output and post the bad output just
below it, so we can compare them.
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