Welcome! Log In Create A New Profile

Advanced

UART2 + BV101

Posted by nemo888 
UART2 + BV101
September 10, 2012 01:26PM
Hello everyone,

So I had a different post where I ask about sending data to the pc, I'm doing this post to make my new question more visible, since I am a bit desperate for an answer.

I am trying to send data from the bv513 to the computer using UART2 since it's the one used to download programs into the board anyway. I have tried using an example that comes with the mplabc32, since I was not sure about the configurations I had to make, and I only changed the baud rate to 115200 since it's the one used for downloading programs to the board. From what I understand, I expect receiving something on the window of the BVCOM2, but after I download the program, nothing at all happens. The code I use is the following:


#include <GenericTypeDefs.h>
#include <plib.h>

// *****************************************************************************
// *****************************************************************************
// Section: Configuration bits
// *****************************************************************************
// *****************************************************************************
#pragma config FPLLODIV = DIV_1, FPLLMUL = MUL_20, FPLLIDIV = DIV_2, FWDTEN = OFF, FCKSM = CSECME, FPBDIV = DIV_1
#pragma config OSCIOFNC = ON, POSCMOD = XT, FSOSCEN = ON, FNOSC = PRIPLL
#pragma config CP = OFF, BWP = OFF, PWP = OFF

// *****************************************************************************
// *****************************************************************************
// Section: System Macros
// *****************************************************************************
// *****************************************************************************
#define GetSystemClock() (80000000ul)
#define GetPeripheralClock() (GetSystemClock()/(1 << OSCCONbits.PBDIV))
#define GetInstructionClock() (GetSystemClock())

// *****************************************************************************
// *****************************************************************************
// Section: Function Prototypes
// *****************************************************************************
// *****************************************************************************
void SendDataBuffer(const char *buffer, UINT32 size);
UINT32 GetMenuChoice(void);
UINT32 GetDataBuffer(char *buffer, UINT32 max_size);
// *****************************************************************************
// *****************************************************************************
// Section: Constant Data
// *****************************************************************************
// *****************************************************************************

const UINT32 lineControl[] =
{
(UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1),
(UART_DATA_SIZE_8_BITS | UART_PARITY_EVEN | UART_STOP_BITS_1),
(UART_DATA_SIZE_8_BITS | UART_PARITY_ODD | UART_STOP_BITS_1),
(UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_2),
(UART_DATA_SIZE_8_BITS | UART_PARITY_EVEN | UART_STOP_BITS_2),
(UART_DATA_SIZE_8_BITS | UART_PARITY_ODD | UART_STOP_BITS_2)
};
// *****************************************************************************
// Section: Code
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
// int main(void)
// *****************************************************************************
int main(void)
{
UINT32 menu_choice;
UINT8 buf[1024];

UARTConfigure(UART2, UART_ENABLE_PINS_TX_RX_ONLY);
UARTSetFifoMode(UART2, UART_INTERRUPT_ON_TX_NOT_FULL | UART_INTERRUPT_ON_RX_NOT_EMPTY);
UARTSetLineControl(UART2, UART_DATA_SIZE_8_BITS | UART_PARITY_NONE | UART_STOP_BITS_1);
UARTSetDataRate(UART2, GetPeripheralClock(), 115200);
UARTEnable(UART2, UART_ENABLE_FLAGS(UART_PERIPHERAL | UART_RX | UART_TX));


while(1){ if (UARTTransmitterIsReady(UART2))
{
UARTSendDataByte(UART2, 3);
}}


}

So..is it supposed to do anything? Should something come up on the BV_com screen? I would really be grateful for some guidance.

I have also tried using an example from a book about programming pic32, "Programming pic32 in C", using the following code:


#include "p32xxxx.h"
#define CTS _RB8 // Clear To Send, input
#define RTS _RB14 // Request To Send, output
#define TRTS TRISBbits.TRISB14 // Tris control for RTS pin

#define BRATE 77 // 115,200 Bd (BREGH=1)
#define U_TX 0x0400 // enable tx, clear all flags
#define U_ENABLE 0x8008 // enable,BREGH=1, 1 stop, no parity

void initU2( void)
{
U2BRG = BRATE; // initialize the baud rate generator
U2MODE = U_ENABLE; // initialize the UART module
U2STA = U_TX; // enable the Transmitter
TRTS = 0; // make RTS an output pin
RTS = 1; // set RTS default status (not ready)
} // initU2

//function to send a char to the UART2
int putU2( int c)
{
//while ( CTS); // wait for !CTS, clear to send
while ( U2STAbits.UTXBF); // wait while Tx buffer full
U2TXREG = c;
return c;
} // putU2

//function to read a char to the UART2
char getU2( void)
{
RTS=0; // assert Request To Send !RTS
while ( !U2STAbits.URXDA); // wait for a new char to arrive
RTS=1;
return U2RXREG; // read char from receive buffer
} // getU2

void main(void){
char c;
// 1. init the UART2 serial port
initU2();
// 2. prompt
putU2( 0 );
// 3. main loop

while ( 1)
{
// 3.1 wait for a character
c = getU2();
// 3.2 echo the character
putU2( 0x12);
} // mainloop
}

After sending the program to the board I get a continuous "a4" writing on the window ov BVCOM, which I think is some kind of error message. Any advice as to what is wrong?

I'm sorry if I'm asking stupid or basic stuff, but I'm just getting started with PIC programming and the electronics world and I'm having a bit of a hard time here heh..

Thanks O_o
Re: UART2 + BV101
September 10, 2012 02:51PM
Hello,
you may get more response asking this question in a PIC32 programming forum. It looks like you have a PIC programmer and using C. Most of the users in this forum use BASIC and or the built in boot loader of the BV513. This link may also help: [doc.byvac.com]
Re: UART2 + BV101
September 13, 2012 01:20AM
Hello jimeer, and thanks for the reply.

Yes I am programming in C but I download the programs using the boot loader that is provided with the board. Since there's a way to load programs into the pic32 of the bv513mini using the bv101, I supposed there is a way to receive data from the pic using uart2, since that's the way the bv101 uses to load the .hex files mplab produces into the board.

Any hint on that?
Re: UART2 + BV101
September 13, 2012 07:18AM
I don't understand why you are using C if you are just staring out when in mBASIC you can achieve what you need in 3 lines of code, yet using C will at least a couple of functions and for buffered input will need considerably more?
Re: UART2 + BV101
September 13, 2012 09:00AM
Well, the reason why I am using C programming is, perhaps, because I have no idea of the mBasic. What I am trying to do is a Bachelor thesis, and the only programming experiences I had were in C or assembler, not counting Matlab which is simplified C, I'd say. The project consists in building and controlling a quadcopter, and I have a lot of work done already, I can't just switch to another programming language so easily.
So, currently I was trying to understand how I could send data to the computer for debugging.
Re: UART2 + BV101
September 13, 2012 09:10AM
Okay then this is the code you are looking for but as I said before there are lots of good PIC32, C programming sites not to mention the code provided by Microchip itself that has examples of UART code.
// =================== S E R I A L ==================================
// simple serial communication on com port 2, no handshaking or
// buffering
// ******************************************************************
// Open com port 2
// NOTE when using printf, the output is not sent until \n so:
// printf("\nHello"); DOES NOT WORK but
// printf("Hello\n"); does work
// ******************************************************************
void openIO(int baudRate, unsigned int mode)
{ 	
	// Open UART2 with Receive and Transmitter enable.
	U2BRG=(PBCLK/(4*baudRate))-1;
	U2MODE=0X8808+mode;
	U2STA=0X1400;
}

// ******************************************************************
// Low level char needed for xmodem, putchar() c function not 
// working quite rigt yet
// ******************************************************************
void b_putc(unsigned char c)
{
	while( U2STAbits.UTXBF); // wait
		U2TXREG=c;
}
//-------------------------------------------------------------
unsigned char b_getc()
{
	if(U2STAbits.OERR) { // overun error must be cleared
		U2STAbits.OERR=0;
			return 0;
	}
	while(!U2STAbits.URXDA); // wait for char
      	return U2RXREG;
}

Re: UART2 + BV101
September 25, 2012 09:43AM
Hello again,

Thanks for the code, but I couldn't make it work. There's some configuration parameter I don't do right since I only receive DDDDDDDDDDDs on BV_com, and not at the frequency I'm sending data (the baud rate seems fine, but i send data with at least some hundred chars per second, I get 4DDD's/second). I've tried other forums, I've tried Microchip's forum, I can't get it to work. You provided the configuration of the UART2 in openIO, and someone told me that I also have to disable JTAG before initializing UART. The configuration for the pic32 are the ones you provide in the demo projects. Is there perhaps something else that has to be configured/enabled/disabled to obtain data transfer?

I wish I could make this work..

Best regards
Re: UART2 + BV101
September 25, 2012 04:26PM
It is reasonably straightforward to program the BV513,BV514 and BV523 in C instead of BASIC, no programmer is needed as there is a boot loader. The DDD.. you are seeing is part of the boot loader, it is waiting for you to send a binary or hex file using the ‘Load Application’ icon of BV_COMM. Full instruction for this are here [doc.byvac.com] Can I suggest that you try and get an LED flashing (Hardware equivalent to Hello World) so you can establish a base point before trying the more difficult stuff. You will find an example of this in the above link.
Re: UART2 + BV101
September 25, 2012 05:42PM
Hello jimeer,

You don't know how useful your last post was to me!

After so much trouble with all of this, going through all kind of crazy assumptions and alternative attempts of sending data from the pic32, I finally came to realize by the blinking led checker you proposed (which I had implemented ages ago and wanted to use to debug the UART program, but I somehow forgot to do that... ) that the bloody problem was that I wasn't including the linker files you provided! God I feel like punching myself right now! Thats what you get for mass copy-paste, you forget the basics...

Many, many thanks! This really saved me..
Sorry, only registered users may post in this forum.

Click here to login