• AVR Freaks

Helpful ReplyHot!How to send sensor data to PC using USART

Author
abhi143
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
2019/05/27 00:24:25 (permalink)
0

How to send sensor data to PC using USART

I have PIC16F877a and xc8 and assume one object sensor is connected to micro controller
 
If sensor detected object send 1 to PC else send 0 to PC
 
I am using hyper terminal to check incoming sensor data on PC so Problem is when sensor become high I don't receive logic 1 on the hyper terminal
#include <xc.h>
#include <stdio.h>
#include <conio.h>
 
#define _XTAL_FREQ 20000000

#define SENSOR  RC1

// CONFIG
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
//END CONFIG
 
void UART_Init(void)
{
 
    TXSTA = 0b00100100;    // Transmit enabled = 1, BRGH=1  High speed
    RCSTA = 0b10010000;    // Serial Port, Continuous Receive enabled
    SPBRG = 0x81;          // baud rate 9600
}
 
void putch(char data){                        
    while(!TRMT);
    TXREG = data;
}
 
unsigned char getch(){                
    while(!RCIF);
    return RCREG;
}
 
 
void main(void)
{

    TRISC1 = 1; // RC1 Sensor Pin
 
    UART_Init;
     
    while(1)
    {
      if (SENSOR == 1)
      {
           printf("\rThe Sensor is: %d", SENSOR);
      }
      else
     {
           printf("\rThe Sensor is: %d", SENSOR);
     }
    }
}

#1
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 00:50:33 (permalink) ☄ Helpfulby abhi143 2019/05/27 01:38:46
+3 (3)
What ARE you receiving on the PC?
Always “The Sensor is: 0”? Or nothing at all? Or garbled data?

If you are always receiving “The Sensor is: 0”, then how often, and for how long, is the sensor driving RC1 high?
If it is only occasionally high, and for a short interval, then by the time this line has detected it...
if (SENSOR == 1)

And then the PIC has had time to progress to executing this line...
printf("\rThe Sensor is: %d", SENSOR);

Where it must read the value of SENSOR again then it is probably back down to low again.
Try:
printf("\rThe Sensor is: 1");


Added to the fact that pumping text out at 9600 baud means you are only ever checking the sensor fairly infrequently. So the chances of catching a short high pulse are minimal in the first place.

Try a tighter loop checking the value of SENSOR for a change. Then, when you detect a change, send out a new message - just one. Then wait for it to change the other way, and send out the new message - etc. You will not get continuous messages, but at least you will stand a chance of getting one if something actually happens.

Alternatively, is your sensor even going high? Replace it with a switch (so you can keep held high for longer) and check that instead. Check it with meter of ideally scope.
#2
abhi143
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 01:42:56 (permalink)
0
pcbbc

What ARE you receiving on the PC?

Thanks pcbbc
I am not getting anything on the hyper terminal screen I just want  inform to PC if sensor detect object
I mean if sensor is high than send integer value 1 or else send 0 to PC
#3
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 02:53:38 (permalink)
+2 (2)
Then either your PIC isn’t running, or you haven’t configured your serial port correctly, or not connected it correctly.
Flash a led in the main loop so you can see it is running.
Scope the serial output so you can see it sending data, and measure the baud rate.
#4
abhi143
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 03:59:24 (permalink)
0
pcbbc
Then either your PIC isn’t running, or you haven’t configured your serial port correctly, or not connected it correctly.
Flash a led in the main loop so you can see it is running.
Scope the serial output so you can see it sending data, and measure the baud rate.

I have configured from the datasheet
 
TXSTA = 0b00100100;    //  9th bit of Transmit Data, can be Parity bit , TSR full , High speed, Asynchronous mode  Transmit enabled TXSTA



bit 7 CSRC: Clock Source Select bit
Asynchronous mode:
Don’t care.
Synchronous mode:
1 = Master mode (clock generated internally from BRG)
0 = Slave mode (clock from external source)
bit 6 TX9: 9-bit Transmit Enable bit
1 = Selects 9-bit transmission
0 = Selects 8-bit transmission
bit 5 TXEN: Transmit Enable bit
1 = Transmit enabled
0 = Transmit disabled
Note: SREN/CREN overrides TXEN in Sync mode.
bit 4 SYNC: USART Mode Select bit
1 = Synchronous mode
0 = Asynchronous mode
bit 3 Unimplemented: Read as ‘0’
bit 2 BRGH: High Baud Rate Select bit
Asynchronous mode:
1 = High speed
0 = Low speed
Synchronous mode:
Unused in this mode.
bit 1 TRMT: Transmit Shift Register Status bit
1 = TSR empty
0 = TSR full
bit 0 TX9D: 9th bit of Transmit Data, can be Parity bit


 
  RCSTA = 0b10010000;    // Serial Port, Continuous Receive enabled 

   
bit 7 SPEN: Serial Port Enable bit
1 = Serial port enabled (configures RC7/RX/DT and RC6/TX/CK pins as serial port pins)
0 = Serial port disabled
bit 6 RX9: 9-bit Receive Enable bit
1 = Selects 9-bit reception
0 = Selects 8-bit reception
bit 5 SREN: Single Receive Enable bit
Asynchronous mode:
Don’t care.
Synchronous mode – Master:
1 = Enables single receive
0 = Disables single receive
This bit is cleared after reception is complete.
Synchronous mode – Slave:
Don’t care.
bit 4 CREN: Continuous Receive Enable bit
Asynchronous mode:
1 = Enables continuous receive
0 = Disables continuous receive
Synchronous mode:
1 = Enables continuous receive until enable bit CREN is cleared (CREN overrides SREN)
0 = Disables continuous receive
bit 3 ADDEN: Address Detect Enable bit
Asynchronous mode 9-bit (RX9 = 1):
1 = Enables address detection, enables interrupt and load of the receive buffer when RSR<8>
is set
0 = Disables address detection, all bytes are received and ninth bit can be used as parity bit
bit 2 FERR: Framing Error bit
1 = Framing error (can be updated by reading RCREG register and receive next valid byte)
0 = No framing error
bit 1 OERR: Overrun Error bit
1 = Overrun error (can be cleared by clearing bit CREN)
0 = No overrun error
bit 0 RX9D: 9th bit of Received Data (can be parity bit but must be calculated by user firmware)



#5
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 07:12:29 (permalink)
+2 (2)
None of that matters not one jot if your PIC isn’t running. Check that first.
Then scope the serial output and see if it is transmitting, and at the correct baud.
Then if still not receiving, something at the PC end is not configured correctly or not connected.
#6
abhi143
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 09:02:56 (permalink)
0
pcbbc
None of that matters not one jot if your PIC isn’t running. Check that first.
Then scope the serial output and see if it is transmitting, and at the correct baud.
Then if still not receiving, something at the PC end is not configured correctly or not connected.

Hello pcbbc
 
I don't have scope so I can't check
 
I would  try to send / receive via  USART
#7
pcbbc
Super Member
  • Total Posts : 1188
  • Reward points : 0
  • Joined: 2014/03/27 07:04:41
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/27 10:11:03 (permalink)
+2 (2)
Start with checking the PIC is actually running. You haven’t confirmed that yet.
Toggle a pin at startup, or indeed after you send each and every character. You can view that with a multimeter.

At the moment we have no idea if your external crystal is connected correctly. So maybe your code isn’t even running.

If you don’t have a scope you’re going to be stumbling around in the dark trying to work out what is wrong.
#8
abhi143
Starting Member
  • Total Posts : 66
  • Reward points : 0
  • Joined: 2018/05/20 09:06:52
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/29 05:12:25 (permalink)
0
pcbbc
Start with checking the PIC is actually running. You haven’t confirmed that yet.
Toggle a pin at startup, or indeed after you send each and every character. You can view that with a multimeter.

At the moment we have no idea if your external crystal is connected correctly. So maybe your code isn’t even running.

If you don’t have a scope you’re going to be stumbling around in the dark trying to work out what is wrong.

PIC is working
#define _XTAL_FREQ 2000000
#include <xc.h>
#include <stdio.h>

// BEGIN CONFIG
#pragma config FOSC = HS
#pragma config WDTE = OFF
#pragma config PWRTE = OFF
#pragma config BOREN = ON
#pragma config LVP = OFF
#pragma config CPD = OFF
#pragma config WRT = OFF
#pragma config CP = OFF
//END CONFIG
 
void putch(char data){                     
    while(!TRMT);
    TXREG = data;
}
unsigned char getch(){                          //used to get each character
    while(!RCIF);
    return RCREG;
}
 
void getString(char *input, unsigned int length){
    for(int i=0;i<length;i++){                       
        input[i] = getch();                           //acquire each character until 10 chars are received
        if(input[i]==13)                              //or if newline is received
            break;
    }
    printf("\rHello ");
    printf("%.10s",input);                        //print input string
}
 
void main(void) {
    
    TXSTA = 0b00100100;    // TX enabled, BRGH=1
    RCSTA = 0b10010000;    // Serial Port, Continuous Receive enabled
    SPBRG = 0x81;          // baud rate 9600
    
    unsigned char *buffer = (char *)0x20;   //assign to start of gen.purpose registers
    printf("Enter your name: ");
    getString(&buffer, 10);                          //pass pointer address to function
 }

 
 
post edited by abhi143 - 2019/05/29 05:15:11

Attached Image(s)

#9
BHUSHAN
Starting Member
  • Total Posts : 62
  • Reward points : 0
  • Joined: 2019/02/04 23:17:18
  • Location: 0
  • Status: offline
Re: How to send sensor data to PC using USART 2019/05/29 05:25:16 (permalink)
0
Try giving some delay after UART initialization function in your problem code. Also in every if-else section use some delay function(try with 1ms delay). If still not received then it is better to use a switch in place of sensor at the concerned pin to drive the pin to logic1 or vice-versa.
Keep on posting about your progress.
post edited by BHUSHAN - 2019/05/29 05:29:42
#10
Jump to:
© 2019 APG vNext Commercial Version 4.5