CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to CCS Technical Support

Communication LabView-PIC

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
rfjhh



Joined: 31 Mar 2011
Posts: 51
Location: Mexico

View user's profile Send private message

Communication LabView-PIC
PostPosted: Fri Mar 22, 2019 12:30 pm     Reply with quote

I'm trying to communicate between a 16f887 and LabView, but I can not read strings. My code is as follows:
Code:

#include <16F887.h>
#device adc=10
#fuses INTRC_IO,NOWDT,NOPROTECT,PUT,NOLVP,NOBROWNOUT
#use delay(clock=8M)
#use rs232(baud=9600,xmit=PIN_A2,rcv=PIN_A3)

#include<LCD420.c>
#include<input.c>

#use fast_io(A)
#use fast_io(B)

void configura()
{
   set_tris_a(0xff);
   set_tris_b(0xf0);
   setup_adc(sAN0);
   lcd_init();
   setup_oscillator(osc_8MHz);
   setup_adc(ADC_CLOCK_INTERNAL);
    SETUP_ADC_PORTS(sAN0);
    set_adc_channel(0);
}

void main()
{
   char mensaje[12];
   configura();
   for(;;)
   {
      char mensaje[31];
      lcd_gotoxy(1,1);
      printf(lcd_putc,"Pba get_string");
      output_bit(PIN_B0,0);
      if(kbhit())
     {
         output_bit(PIN_B0,1);
         get_string(mensaje,30);
         delay_ms(5);
         output_bit(PIN_B0,0);
         lcd_gotoxy(1,2);
         printf(lcd_putc,"%30s", mensaje);
      }
           
   }
}

I do not know how to insert the code image in labview.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 22, 2019 2:25 pm     Reply with quote

quick general comments
1) you need to add 'ERRORS' as an option in #USE RS232(.....ERRORS)

this will keep the HW UART from overflowing and 'locking up'.

edit.. OOPS ! you're NOT using the hardware UART. You actually should. Now you're using a software or 'bitbang' UART, so there could be a timing problem....

get_string() will wait forever for 30 characters or a CR ( carriage return)..

Now I'm assuming you've got a USB<>TTL module between PC and PIC. Cut LABVIEW code to be a 'terminal' and have the KBD send to USB<>TTL module, loopback TXD to RXD, and see KBD data on the PC SCREEN. This will confirm you ARE sending data to the PIC.

I don't know how well it works with a SW UART though others will.

2) The ADC clock is wrong, you should not use 'internal '. Check with the datasheet, adc section, there's a chart of proper ADC clocks vs PIC clock speed.

Jay
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Fri Mar 22, 2019 2:34 pm     Reply with quote

ADC_CLOCK_INTERNAL is not recommended for your clock. At 8MHz you
need ADC_CLOCK_DIV_32

Honestly, use the hardware UART, pins C6, and C7. Software serial
should only be used as a 'last resort'.

You talk about communicating with LabView. Describe your hardware to
make this connection. Have you got RS232 transceivers?
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Fri Mar 22, 2019 3:18 pm     Reply with quote

I do not like SW uarts one bit unless you are only transmitting. According to the manual, if you are using SW uart, then you must poll kbhit() at least 10 times the rate of your baud rate. Otherwise you risk missing incoming uart data.

When I used SW uart, I even had ADC reading in the main loop screw up my SW uart.
temtronic



Joined: 01 Jul 2010
Posts: 9243
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Mar 22, 2019 6:17 pm     Reply with quote

gads I feel OLD now.. I used to MAKE HW UARTs from a handfull of 7400 series chips all wirewrapped. Used a 4702 timebase chip for the clock. Sad thing is I SAW one of them the other day in a bin, along with a 6402 which was one of the first CMOS UARTS on the market.
I've used SW UARTs for years, run at 600 baud, no problems. One trick is 'interleaving' the tasks within the 'bit' widths. Mice were done that way, using 16C71s(?), I do recall the Microchip application note. probably on their website though I prefer hardcopy books to thumb through.

Jay
rfjhh



Joined: 31 Mar 2011
Posts: 51
Location: Mexico

View user's profile Send private message

SW vs HW
PostPosted: Mon Mar 25, 2019 8:58 am     Reply with quote

Jay

Yes I'm using an USB<>TTL module between PC and PIC. I think you're ok, I don't know if LabView inserts a CR by default in each string sent, I'll check it. By the way, I'm using pins C6, and C7.

May you explain me the difference between SW and HW please.

Regards
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Mon Mar 25, 2019 9:09 am     Reply with quote

If you don't know if labview inserts a CR, then you can run labview's output to your TTL USB module and check it on a terminal program like realterm. That will show you the characters
Ttelmah



Joined: 11 Mar 2010
Posts: 19539

View user's profile Send private message

PostPosted: Mon Mar 25, 2019 9:12 am     Reply with quote

Software and hardware.

Most PIC's have a hardware UART, on specific pins.
CCS can use this hardware or provide a UART 'emulation' in code. A
'software' UART.
Your post at the start was using pins A2 & A3, which are not the
UART hardware pins for this chip.
Using a software UART, there is no buffering at all. The code must be sitting
'polling' the kbhit function at least 10* per bit, and start reception
immediately. Any pause and data will be missed/lost.
Transmitting with the software UART, providing you are not using anything
interrupt driven, can be quite reliable. Reception though is much more
likely to have issues.
Use hardware if you can.
dluu13



Joined: 28 Sep 2018
Posts: 395
Location: Toronto, ON

View user's profile Send private message Visit poster's website

PostPosted: Mon Mar 25, 2019 11:00 am     Reply with quote

Regarding transmitting using a software UART:

If allowed, you can use something like
Code:
disable_interrupts(INTR_GLOBAL);
printf("...");
enable_interrupts(INTR_GLOBAL);


Just be careful that you can afford to miss an interrupt of whatever you will be disabling.
rfjhh



Joined: 31 Mar 2011
Posts: 51
Location: Mexico

View user's profile Send private message

Fixed
PostPosted: Mon Mar 25, 2019 12:37 pm     Reply with quote

Thanks all of you guys.

Just a CR concatenated to the string and get_string() is working.
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2005 phpBB Group