Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[PIC] example code for dsPIC30f uart interface to transmit array data

Status
Not open for further replies.

Alahari Hemanth

Newbie level 2
Joined
May 23, 2015
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Activity points
15
how to access the data array[128] of 16 bit size..in dspic30f using uart??
if any one having example code kindly post it....thanx in advance....
 

how to access the data array[128] of 16 bit size..in dspic30f using uart??
if any one having example code kindly post it....thanx in advance....
do you wish to transmit the data in binary (each 16bit word as two bytes) or a text strings using sprintf() to convert the integer data to text?
do you have the code to write bytes to the UART?

- - - Updated - - -

assuming you have a function UART2PutChar(unsigned char ch) which transmits a byte to transmit your 16bit int data values as two bytes the code would look siomething along the lines of
Code:
	int i, data[128];
	for(i=0;i<128;i++)
		{
		UART2PutChar((unsigned int)(data[i]>>8));  // send most significant byte
		UART2PutChar((unsigned int) data[i]);         // send least significant byte
		}
 

do you wish to transmit the data in binary (each 16bit word as two bytes) or a text strings using sprintf() to convert the integer data to text?
do you have the code to write bytes to the UART?

- - - Updated - - -

assuming you have a function UART2PutChar(unsigned char ch) which transmits a byte to transmit your 16bit int data values as two bytes the code would look siomething along the lines of
Code:
	int i, data[128];
	for(i=0;i<128;i++)
		{
		UART2PutChar((unsigned int)(data[i]>>8));  // send most significant byte
		UART2PutChar((unsigned int) data[i]);         // send least significant byte
		}

thank you... but i have doubt ..
for dspic 30f we have to intialize all three register

Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
int i=0;
void __attribute__((__interrupt__, __auto_psv__)) _U1TXInterrupt(void);
 
int a[128];
void main()
 
{
 
       
 
       U1STA = 0x8400;
       U1BRG=0x0006;
 
        IFS0=0x0000;
        IECO=0x0400;
        U1MODE= 0x8000;
        for(;;)
        {
        }
 
}



can you please suggest something in detail please I am new to this area....
 
Last edited by a moderator:

I used this with a dsPIC30F3011

Code:
// initialise UART to 2400 baud, 8 data, 1 stop bit 
// U1BRG 2400 = 192, 4800 = 95, 9600 = 49, 19200=24, 38400=11 at 7.37MHz
//       2400 = 262, 56700 = 10 at 10MHz
// oscillator mode XT w/PLL 4*
// U1BRG counter = Fcy (16 * baudrate) - 1
//int U1BRGcounter = FCY/(16*2400L) - 1;
void initUART(long int baudRate)
  {
   //     TRISD=0;       
        TRISFbits.TRISF2 = 0;					
        TRISFbits.TRISF3 = 1;					
        U1MODE=0x8000;			// enable UART
        U1STA=0;				// clear status register
        // calcular baud rate counter  - add 0.5 to round to nearest integer
        U1BRG= (int) ((FCY/(16.0*baudRate) - 1)+0.5); //U1BRGcounter;	// Baudrate value 
        U1STA=U1STA | 0x400;	// enable TX 
  }
 

this code runs on a Microchip dsPICDEM 1.1 board with a dsPIC30F6014

main.c
Code:
#include <stdio.h>
#include "p30f6014.h"
#include "uart2.h"

 _FOSC(CSW_FSCM_OFF & XT_PLL4); 	// primary oscillator 4xPLL
 _FWDT(WDT_OFF); 					// Turn off Watchdog Timer

// main program code
int main(void)
{
    unsigned int i,  data[]={0x4845,0x4c4c,0x4f0a,0x0d00};
    UART2Init(19200);										// Setup the UART
    printf("\n\ndsPIC30 test program\n");
    // transmit array
    for(i=0;i<sizeof(data)/sizeof(unsigned int);i++)
		{
		UART2PutChar((unsigned char)(data[i]>>8));  // send most significant byte
		UART2PutChar((unsigned char) data[i]);         // send least significant byte
		}

uart2.c
Code:
// UART2.c  for dsPIC30

#include "p30f6014.h"

#define SYSCLK 8000000UL

#define Fosc  (SYSCLK*4ul)		// PLL output frequency

#include "uart2.h"
#include "libpic30.h"			// to change default printf output, see __C30_UART below
#include <stdio.h>

#define BRGH2           0


// Function:UART2Init -  sets up theUART2 module to baud rate
void UART2Init(long int baudRate)
{
    long int baudRateCheck;
     __C30_UART=2;			// set for printf output to this UART
    // calculate baud rate counter  - add 0.5 to round to nearest integer
    U2BRG= (int) (((SYSCLK/2)/(16.0*baudRate) - 1)+0.5); 	// set up baud rate register
    baudRateCheck=((SYSCLK/2)/(16*(U2BRG+1)));
    if(((baudRateCheck-baudRate)*100/baudRate) > 4)
      printf("UART2 baudrate required %lu was %lu, error %d\n", baudRate, baudRateCheck, (int) ((baudRateCheck-baudRate)*100/baudRate));
    U2MODE = 0;					// clear mode register
    U2STA = 0;					// clear status
    U2MODEbits.UARTEN = 1;		// enable UART
    U2STAbits.UTXEN = 1;		// enable reansmit
}


//Function:UART2IsPressed - return true if there is a new byte in UART reception buffer.
char UART2IsPressed()
{
    return  U2STAbits.URXDA;		// data in receive register?
}

//Function:UART2GetChar : waits for a byte to be received.  It then returns that byte.
char UART2GetChar()
{
    while(!U2STAbits.URXDA);	// wait for receive ready
    return U2RXREG;
}


// Function:UART2PutChar - This routine writes a character to the transmit FIFO
void UART2PutChar( unsigned char ch )
{
    while(U2STAbits.TRMT == 0);	// wait for transmit ready
    U2TXREG = ch;				// transmit character
}

main() transmits the elements of the unsigned int array data[] as pairs of bytes
Code:
    for(i=0;i<sizeof(data)/sizeof(unsigned int);i++)
		{
		UART2PutChar((unsigned char)(data[i]>>8));  // send most significant byte
		UART2PutChar((unsigned char) data[i]);         // send least significant byte
		}
note that the array data[]
Code:
    unsigned int i,  data[]={0x4845,0x4c4c,0x4f0a,0x0d00};
is initialised with the ASCII code for "HELLO" so when the proram is executed teraterm displays
teraterm5.jpg
 

Attachments

  • UART Send Array.rar
    44.7 KB · Views: 171
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top