Jump to content
Sign in to follow this  
Beaker

Newbie Problems With Boostc

Recommended Posts

Hi,

 

I'm returning to PIC programming after a break of many years :lol: I've just installed (free) SourceBoost 7.01. I've had some minor successes but hope someone will be kind enough to answer the following, probably dumb, questions.

 

  • Using the hardware uart code from the manual, I have serial transmission working OK from PIC to PC via MAX232 but can't understand what I have to do to receive characters from PC. [h/w works fine - I use a serial bootloader on my 16F877]. Do I need to write some interrupt code for reception? The only time it seems to work is if I have a receive buffer of a single character.
     
  • I can't find putc(), getc(), etc. I am using uart.h code on 16F877. Examples on the web all make use of these for serial I/O?

 

My program is below. It was generate using the ProjectWizard, with uart code copied from the manual. Any help greatly appreciated.

#include <system.h>

#define SINGLE_PORT_MODE
#include "uart_driver.h"

//Target PIC16F877 configuration word
#pragma DATA _CONFIG, _PWRTE_OFF & _BODEN_OFF & _WDT_OFF & _LVP_ON & _CPD_OFF & _DEBUG_OFF & _XT_OSC & _CP_OFF

//Set clock frequency
#pragma CLOCK_FREQ	4000000

#define uartInit \
rs232Init<PIE1,TXIE,PIE1,RCIE,RCSTA,CREN,RCSTA,SPEN>
#define uartTxInterruptHandler \
rs232TxInterruptHandler<PIR1,TXIF,TXREG,sizeof(txBuffer), \
TXSTA,TXEN,TXSTA,TRMT>
#define uartRxInterruptHandler \
rs232RxInterruptHandler<PIR1,RCIF,RCREG,sizeof(rxBuffer), \
RCSTA,CREN,RCSTA,OERR,RCSTA,FERR>
#define uartRx \
rs232Rx<sizeof(rxBuffer)>
#define uartTx \
rs232Tx<sizeof(txBuffer),TXSTA,TXEN>

// Defines needed for UART code
// See page 86 of BoostC manual
#define uartInit \
rs232Init<PIE1,TXIE,PIE1,RCIE,RCSTA,CREN,RCSTA,SPEN>
#define uartTxInterruptHandler \
rs232TxInterruptHandler<PIR1,TXIF,TXREG,sizeof(txBuffer), \
TXSTA,TXEN,TXSTA,TRMT>
#define uartRxInterruptHandler \
rs232RxInterruptHandler<PIR1,RCIF,RCREG,sizeof(rxBuffer), \
RCSTA,CREN,RCSTA,OERR,RCSTA,FERR>
#define uartRx \
rs232Rx<sizeof(rxBuffer)>
#define uartTx \
rs232Tx<sizeof(txBuffer),TXSTA,TXEN>

// Need to define these vars for UART code
// See page 85 of BoostC manual
#define TX_BUFFER_SIZE	32
#define RX_BUFFER_SIZE	1

unsigned char txBuffer[TX_BUFFER_SIZE];
unsigned char txHead;
unsigned char txTail;
unsigned char rxBuffer[RX_BUFFER_SIZE];
unsigned char rxHead;
unsigned char rxTail;
unsigned char rxCnt;

void interrupt( void )
{

uartTxInterruptHandler();
if( uartRxInterruptHandler() )
{
//… new data just arrived ...  <--- DO I NEED SOMETHING HERE?
}


//Handle port change interrupt
if( intcon & (1<<RBIF) )
{
	clear_bit( intcon, RBIF );
}

//Handle timer0 interrupt
if( intcon & (1<<T0IF) )
{
	clear_bit( intcon, T0IF ); //clear timer 0 interrupt bit
}

//Handle timer1 interrupt
if( pir1 & (1<<TMR1IF) )
{
	clear_bit( pir1, TMR1IF ); //clear timer 1 interrupt bit
}

//Handle timer2 interrupt
if( pir1 & (1<<TMR2IF) )
{
	clear_bit( pir1, TMR2IF ); //clear timer 2 interrupt bit
}

}


void main( void )
{

char msg[]="Hello World!";
int i;
char data_in[RX_BUFFER_SIZE];
char ch;

// Need to delay 500ms at power up for LCD to settle
delay_ms(1000);

//Configure port A
trisa = 0x00;
//Configure port B
trisb = 0xF0;
//Configure port C
trisc = 0x80;
//Configure port D
trisd = 0x00;
//Configure port E
trise = 0x00;

//Configure A/D pins
adcon1 = 0x0E;


//Initialize port A
porta = 0x00;
//Initialize port B
portb = 0x00;
//Initialize port C
portc = 0x00;
//Initialize port D
portd = 0x00;
//Initialize port E
porte = 0x00;
//Set Timer0 mode
clear_bit( option_reg, T0CS ); //configure timer0 as a timer
//Set prescaler assignment
clear_bit( option_reg, PSA ); //prescaler is assigned to timer0
//Set prescaler rate
clear_bit( option_reg, PS2 ); //prescaler rate 1:2
clear_bit( option_reg, PS1 );
clear_bit( option_reg, PS0 );
//Set timer0 source edge selection
set_bit( option_reg, T0SE ); //increment on high-to-low transition on RA4/T0CKI pin

//Set timer 1 prescaler rate
clear_bit( t1con, T1CKPS1 ); //prescaler rate 1:1
clear_bit( t1con, T1CKPS0 );
//Set timer 1 mode
clear_bit( t1con, TMR1ON ); //disable timer 1

//Set timer 2 prescaler rate
clear_bit( t2con, T2CKPS1 ); //prescaler rate 1:1
clear_bit( t2con, T2CKPS0 );
//Set timer 2 postscaler rate
clear_bit( t2con, TOUTPS3 ); //postscaler rate 1:1
clear_bit( t2con, TOUTPS2 );
clear_bit( t2con, TOUTPS1 );
clear_bit( t2con, TOUTPS0 );
//Set timer 2 mode (enable or disable)
clear_bit( t2con, TMR2ON ); //enable timer 2


//Enable interrupts (Port change, Timer0)
intcon = 0xA8;

//Configure serial port speed and interrupt
// See page 95 of PIC 16F877 datasheet
txsta.BRGH = 1; //high speed
txsta.SYNC = 0;
txsta.TX9 = 0;
txsta.TXEN = 1;

spbrg = 25; // 9600baud at 4MHz

// Set Receive port
rcsta.SPEN = 1;
rcsta.RX9 = 0;
rcsta.CREN = 1;
rcsta.ADDEN = 0;

//Configure UART pins
trisc.7 = 1;
trisc.6 = 0;

//Init uart driver
uartInit();

	// Serial LCD display is connected to serial port too
uartTx(254); uartTx(1); delay_ms(10); // Clear LCD screen
for (i=0; i<sizeof(msg); i++)
	uartTx(msg[i]);	// Send the "Hello World!" message

//Endless loop
while( 1 ) 
{
	portb++;	// obligatory flashing LEDs
	delay_ms(5000);

		while( rxCnt )	// just trying basic echo of input
		{
			unsigned char data = uartRx();
			uartTx(data);
		}		
}
}

 

Thanks,

 

Mark

Share this post


Link to post
Share on other sites

Mark,

 

For what you are trying to do you only need:

uartTxInterruptHandler();
uartRxInterruptHandler();

in your interrupt function to handle the UART.

 

These two functions handle the transmitting and receiving of characters to and from the PC via the UART entirely under interrupt control.

 

uartRx() and uartTx() are effectively getc() and putc() respectively and access the rxBuffer[] and txBuffer[] buffers used by uartRxInterruptHandler() and uartTxInterruptHandler().

 

In your main() to send data simply place it in the txBuffer[] using uartTx().

Any data received will be in the rxBuffer[] so before reading it using uartRx() see if there is any data in it by checking if rxCnt > 0.

 

How you handle the data after that is up to you!

 

I haven't checked the rest of your code regarding UART setup etc.

 

Regards

 

davidb

Edited by davidb

Share this post


Link to post
Share on other sites
Mark,

 

For what you are trying to do you only need:

uartTxInterruptHandler();
uartRxInterruptHandler();

in your interrupt function to handle the UART.

 

These two functions handle the transmitting and receiving of characters to and from the PC via the UART entirely under interrupt control.

 

uartRx() and uartTx() are effectively getc() and putc() respectively and access the rxBuffer[] and txBuffer[] buffers used by uartRxInterruptHandler() and uartTxInterruptHandler().

 

In your main() to send data simply place it in the txBuffer[] using uartTx().

Any data received will be in the rxBuffer[] so before reading it using uartRx() see if there is any data in it by checking if rxCnt > 0.

 

How you handle the data after that is up to you!

 

I haven't checked the rest of your code regarding UART setup etc.

 

Regards

 

davidb

 

Thanks for the clarification. I've cleaned up the code. The Tx side still works fine but the Rx side is still not working. In fact, if I press more than one key the program pauses for a while. I'm not actually consuming the data that's received, but since it's a cyclical buffer that shouldn't matter.

 

My txsta & rcsta settings are as follows. Do they look right?

	//Configure serial port speed and interrupt
// See page 95 of PIC 16F877 datasheet
txsta.BRGH = 1; // high speed
txsta.SYNC = 0; // asynch mode
txsta.TX9 = 0;	// 8 bit
txsta.TXEN = 1;	// enable

spbrg = 12; 	// 19200 baud at 4MHz, BRGH=1

// Set Receive port
rcsta.SPEN = 1;	// enable
rcsta.RX9 = 0;	// 8bit
rcsta.CREN = 1;	//

 

Thanks,

 

Mark

Share this post


Link to post
Share on other sites

Join the conversation

You are posting as a guest. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...