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] how to send int array via UART on PIC16F887

Status
Not open for further replies.

betuse

Junior Member level 3
Junior Member level 3
Joined
Oct 17, 2013
Messages
26
Helped
0
Reputation
0
Reaction score
0
Trophy points
1
Visit site
Activity points
173
Hi for all,
I need to send int array via UART to PC,
I use PIC16F887,
the code for creating array is:
(exactly I count impulses every 1ms from encoder with Timer1...)

Code:
unsigned int TMR1_value;
int vector[48];
char i = 0;

void main() {
     ANSEL  = 0;                        
     ANSELH = 0;
     OSCCON = 0b01110001;
     T1CON = 0b00000111;

     TRISC = 1;                         
     TRISA = 0;                         

     PORTA = 0b00000001;                

     TMR1H = TMR1L = 0;

  while(1) {
         TMR1_value = (TMR1H << 8) + TMR1L;
         vector[i] = TMR1_value;
         i++;
         delay_ms(1);                   
         if (i == 48) break;
  }
}
can sombody advice how to do that?
best regards
 

can sombody advice how to do that?
best regards[/QUOTE]

you can convert the int to a string and transmit that, e.g. assuming you have sprintf()
https://www.cplusplus.com/reference/cstdio/sprintf/

so send element vector
Code:
char text[50];
sprintf(text,"%d\n",vector[i]);
UART2printString(text);

or using itoa()
https://www.cplusplus.com/reference/cstdlib/itoa/
Code:
char text[50];
itoa(vector[i],text,10);
UART2printString(text);

or you can send it as two binary bytes
Code:
UART2putChar(vector[i]);
UART2putChar(vector[i]>>8);
you then some code at the received to put it back together again
 
  • Like
Reactions: betuse

    betuse

    Points: 2
    Helpful Answer Positive Rating
Thanks Horace1.
I use mikroc for pic, and 887 dont have sprintf, also itoa is not allowed,
only third option is ok, but I can not use it, not work,
regards
 

putChar is not supported in mikroc
I don't use MikroC but here is some code for a Microchip mechatronics trainer using PIC16F917 with the Hi-Tech C compiler which may help
Code:
//  RS232 serial IO functions
#include <htc.h>


//**********************************************************
//*	Define serial communications control functons


void UARTInit(void)
{
    OSCCON=0x71;				// Fosc use internal oscillator 8MHz
	TXSTA=0;					// set up transmitter
	TXEN=1;						// Asychronous, 8 bit, BRGH=1
	BRGH=1;
	TRMT=1;
    TRISC6=1;

	RCSTA=0;					// set up receiver
	SPEN=1;						// Asychronous, 8 bit, continuous receive, serial enable
	CREN=1;    
	TRISC7=0;

// calculate Baud Rate = FOSC/(16 (X + 1))
//#define BaudRate 	51 	//SPBRG value for 9600 Baud when Fosc=8MHz and BRGH=1
#define BaudRate    8 	//SPBRG value for 57600 Baud when Fosc=8MHz and BRGH=1
	SPBRG=BaudRate;  			// setup baud rate
}

// put a char to RS232 serial line
int putchar(const int ch)
{
	while(!TRMT);
    TXREG=ch;
    //delay(1);
}

// put a string to RS232 serial line
void putString(const char *ch)
{
	while(*ch>0) { putchar(*ch); ch++; }
}

// check if a character has ben received - set true if so
int uartReceived(void)
{
  return RCIF;	// return character received
}

// get a character from serial line
int getchar(void)
{
   while(!uartReceived());	// wait for a character received
   return RCREG;						// return the character
}
 
  • Like
Reactions: betuse

    betuse

    Points: 2
    Helpful Answer Positive Rating
In mikroC there is IntToString() and LTrim(). Use them and convert each int value to string and then use UART_Write_Text() function to send string through UART.
 
  • Like
Reactions: betuse

    betuse

    Points: 2
    Helpful Answer Positive Rating
Milan, can you help me with concrete advice on my code
Code:
while(1) {
         TMR1_value = (TMR1H << 8) + TMR1L;
         vector[i] = TMR1_value;
         i++;
         delay_ms(1);                   
         if (i == 48) break;
  }

how can I send vector(i) to PC via UART using IntToStr
 

Please zip and post your mikroC project files. It will be easy to modify the code then.



Code C - [expand]
1
2
3
4
5
6
7
8
9
char strTemp[17];
 
while(1) {
         vector[i] = (TMR1H << 8) + TMR1L;
     IntToStr(vector[i++], strTemp);
     UART1_Write_Text(strcat(Ltrim(strTemp), "\r\n"));
         delay_ms(1);                   
         if (i == 48) break;
}

 
  • Like
Reactions: betuse

    betuse

    Points: 2
    Helpful Answer Positive Rating
Horace1, Milan,
I have working code for this example, but show count on LCD, and it is good only for some capture time >200ms,
if I use faster capture time, LCD can not show clear number...
that is the reason why I need other way to save count's.
code with LCD is:
Code:
// LCD
sbit LCD_RS at RB4_bit;
sbit LCD_EN at RB5_bit;
sbit LCD_D4 at RB0_bit;
sbit LCD_D5 at RB1_bit;
sbit LCD_D6 at RB2_bit;
sbit LCD_D7 at RB3_bit;

sbit LCD_RS_Direction at TRISB4_bit;
sbit LCD_EN_Direction at TRISB5_bit;
sbit LCD_D4_Direction at TRISB0_bit;
sbit LCD_D5_Direction at TRISB1_bit;
sbit LCD_D6_Direction at TRISB2_bit;
sbit LCD_D7_Direction at TRISB3_bit;


unsigned int TMR1_value;
char to_LCD[6];

void main() {
     ANSEL  = 0;                        
     ANSELH = 0;
     OSCCON = 0b01110001;
     T1CON = 0b00000111;

     TRISC = 1;                         
     TRISA = 0;                         
     
     PORTA = 0b00000001;                //motor pin's

     LCD_Init();
     LCD_Out(1, 1, "Count_imp");

  TMR1H = TMR1L = 0;

  while(1) {
         TMR1_value = (TMR1H << 8) + TMR1L;
         WordToStr(TMR1_value, to_LCD);
         LCD_Out(2, 1, to_LCD);
         delay_ms(200);  
  }
}

mcu is 887 and I use mikroC pro for Pic,

I send you that code because maybe you will advice some better way to do that...
I write count's on vector (887 support only 48 int vector array) but can not send to PC for analyse...
thanks again
and best regards
 

zip file in attach
 

Attachments

  • encoder vo array.rar
    22.5 KB · Views: 105

You get IRP bit has to be manually set warning in Compiler. It is better if you use PIC18F device.

Do you want to send TMR1H and TMR1L values separately ?


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
unsigned int vector[48];
char i = 0;
char strTemp[17];
 
void main() {
 
     OSCCON = 0b01110001;
     
     ANSEL  = 0;
     ANSELH = 0;
     UART1_Init(9615);
     Delay_ms(100);
     UART1_Write_Text("Start\r\n");
 
     T1CON = 0b00000111;
     
     TRISC = 1;
     TRISA = 0;
     
     PORTA = 0b00000001;
 
     TMR1H = TMR1L = 0;
     
 
 
     while(1) {
          IRP_bit = 1;
          vector[i] = (TMR1H << 8) + TMR1L;
          IntToStr(vector[i], strTemp);
          IRP_bit = 0;
          UART1_Write_Text(strcat(LTrim(strTemp), "\r\n"));
          i++;
          delay_ms(1);
     }
}

 
Last edited:
  • Like
Reactions: betuse

    betuse

    Points: 2
    Helpful Answer Positive Rating
Thanks Milan,
you say: Do you want to send TMR1H and TMR1L values separately ?
is it possible to send (merge) TMR1H and TMR1L like whole number?

p.s code have UART1_Write_Text("Start\r\n");
but on terminal I dont see Start...
only numbers...
 
Last edited:

Try this


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
unsigned int vector[48];
char i = 0;
char strTemp[17];
 
void main() {
 
     OSCCON = 0b01110001;
     
     ANSEL  = 0;
     ANSELH = 0;
     
     T1CON = 0b00000111;
     
     TRISC = 0x81;
     TRISA = 0x00;
     
     PORTA = 0x01;
 
     TMR1H = TMR1L = 0;
     UART1_Init(9615);
     Delay_ms(100);
     UART1_Write_Text("Start\r\n");
 
     while(1) {
          IRP_bit = 1;
          vector[i] = (TMR1H << 8) + TMR1L;
          IntToStr(vector[i], strTemp);
          IRP_bit = 0;
          UART1_Write_Text(strcat(LTrim(strTemp), "\r\n"));
          i++;
          delay_ms(1);
     }
}

 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top