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 support@ccsinfo.com

Interface VB with PIC
Goto page 1, 2  Next
 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

Interface VB with PIC
PostPosted: Thu Mar 17, 2011 6:54 am     Reply with quote

Can I know what is the coding to send and receive signal from Visual Basic ?
The Visual Basic using is 2010 edition.

I found this on web
UART1_Init
UART1_Data_Ready
UART1_Tx_Idle
UART1_Read
UART1_Read_Text
UART1_Write
UART1_Write_Text
UART_Set_Active

But these all are for mC Pro.
Do this apply to CCS compiler also?
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 7:17 am     Reply with quote

Visual Basic / PC and CCSC/PIC are completely different worlds. Check in the VB help to find the correct syntax for the RS232 communication. If you are using CCS-C and PICs then refer to the CCSC-compiler reference manual. Depending on which PIC you use you will find the right functions together with examples.

Make sure that you run on both machines (PIC and PC) the serial communication with the same parameters (baud-rate, start/stop-bits, etc) unless you want auto-detection.

Furthermore you need a level-converter to convert the PIC´s TTL-signals to the RS232-standard-signals. You may use a MAX232 - it´s simple to use and highly reliable.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 1:43 pm     Reply with quote

This post in the Code Library forum has the source code for a VB6
bootloader application that shows how to use serial communications
between VB and a PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=19217
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 5:50 pm     Reply with quote

Thanks for the reply.

I am using CCS IDE version 4.038 and VB 2010 express edition as window 7 do not support visual basic 6.

The PIC using is 16F877A and I am using UART for the communication between them.

I had tried the UART for the communication between hyperterminal and pic and it is successful but then I don't know the next. Which is the communication between VB and coding in PIC to send signal to PIC.

Quote:
UART1_Init
UART1_Data_Ready
UART1_Tx_Idle
UART1_Read
UART1_Read_Text
UART1_Write
UART1_Write_Text
UART_Set_Active


Mean the above coding is not the signal for CCS send signal to UART and receive back?
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Thu Mar 17, 2011 6:07 pm     Reply with quote

I had go thru the CCS compiler Reference Manual, is that

Quote:
UART_RX : Returns the receive pin for the first UART on this PIC (see PIN_XX)
UART_TX : Returns the transmit pin for the first UART on this PIC


do i need setup_uart(9600) if I already include it in the RS232 statement like below:
Quote:

#use rs232 (UART1,baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


This two is correct? Mean when i put code UART_RX in my pic program, the pic will read the pin RX in UART which is the data transmitted from VB to UART?
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Fri Mar 18, 2011 1:44 am     Reply with quote

No, you do not need to set up the USART again. You may use an ISR to let your PIC-program know when a character comes in from the PC:
Code:

#INT_RA      // example: interrupt on RA1 - reads characters coming from the RS232
void serial_isr()
{
      buffer[counter]=getc();

   if(++counter>255) counter=0;   // memory-wrap-around for buffer
 
        RS232_FLAG=1;                        // set flag, so main() knows something came in

        // place your own code here to process the incoming character

}


Then decide in your main function what to do with the received characters.
Code:

void main(void)
{
     ....

     while(1)
     {
          ....
          if (RS232_FLAG)
          {
               // place your code here
               RS232_FLAG=0;
          }
     }
}
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Fri Mar 18, 2011 7:34 am     Reply with quote

I still cant get what you mean but then
Quote:
#use rs232 (UART1,baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)


is needed right??

I want send a signal to Visual Basic when specific condition, and receive the signal back from the Visual Basic.

I need to use below 2 statements right??

UART_RX
UART_TX

I am using the USB to UART
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Fri Mar 18, 2011 8:14 am     Reply with quote

Hi,

The PIC16F877A has only 1 USART. You may use following simple program structure to accomplish your tasks:
Code:

#include "16f677a.h"

#DEVICE ADC=10
#fuses XT,NOWDT,NOCPD,NOPROTECT
#use delay(crystal=20Mhz)            
#use standard_io(A)
#use rs232(baud=9600, xmit=PIN_A0, rcv=PIN_A1, PARITY=N, BITS=8, STOP=1)

#Byte PortA=0x05

unsigned char Zaehler=0, RS232_FLAG=0;

char rom buffer[256];         

#INT_RA      
void serial_isr()
{
      buffer[Zaehler]=getc();

   if(++Zaehler>255) Zaehler=0;   
 
        RS232_FLAG=1;

}


void main()

   SET_TRIS_A( 0b00000010 );        // check if OK with your application
   enable_interrupts(int_ra1);
   enable_interrupts(global);
   output_high(PIN_A0);

   while(1)
   {
         if(RS232_FLAG)
         {
              RS232_FLAG=0;
             
              puts("OK\n\r");
         }
   }
   
}

This code in your PIC sends every time it receives a character from the PC an acknowledge-message (OK) back to the PC.
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Fri Mar 18, 2011 9:32 pm     Reply with quote

you are using the USB to UART connector also?
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Sat Mar 19, 2011 6:30 am     Reply with quote

Sometimes I use the FTDI DLP-USB232M-G USB-RS232-converter (for notebooks without RS232). This converter acts like a real RS232 on the PC (virtual COM drive)
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Tue Mar 22, 2011 5:34 am     Reply with quote

I test the UART with Visual studio 2010 with one button. If the button is press, it will send "A" to PIC. If the PIC receive "A", it will turn on portC0 then the LED will turn ON.

I can see the UART tx pin is lighting when i press the button but then the PIC seem like cant receive it. Can anyone correct the coding for me?

My PIC coding
Quote:
#include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

#byte portC=7 // port C map to memory address 7


char rx;


#int_rda

void serial_isr()
{
rx=getch();

if (rx=="A")
{
output_high(pin_C0);
delay_ms(2000);
}

}


void main()
{

set_tris_c(0b10000000);


}


My Visual Studio using C#
Quote:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;

namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

//configuring the serial port
serialPort1.PortName = "COM5";
serialPort1.BaudRate = 9600;
serialPort1.DataBits = 8;
serialPort1.Parity = Parity.None;
serialPort1.StopBits = StopBits.One;

//opening the serial port
serialPort1.Open();

//write data to serial port
serialPort1.Write("A");

//close the port
serialPort1.Close();


}

private void Form1_Load(object sender, EventArgs e)
{

}
}
}
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Tue Mar 22, 2011 9:24 am     Reply with quote

looking over quite fast it it should be this way:

if(rx=='A') // just 1 character, not a string!
...
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Tue Mar 22, 2011 9:25 am     Reply with quote

Can you help me correct it?

I really have no idea on using serial port.
ALPL



Joined: 29 Mar 2009
Posts: 30

View user's profile Send private message

PostPosted: Tue Mar 22, 2011 9:51 am     Reply with quote

Try following (when you press the capital A the LED will turn on - press any other character to turn it off:


#include <16F877A.h>
#fuses HS, NOWDT, NOPROTECT, BROWNOUT, PUT, NOLVP
#use delay(clock=20000000)
#use rs232 (baud=9600, xmit=PIN_C6, rcv=PIN_C7, ERRORS)

char rx;

#int_rda
void serial_isr()
{
rx=getch();

if (rx=='A')
output_high(PIN_C0);
else
output_low(PIN_C0);
}


void main()
{

set_tris_c(0b10000000); // I do not know how you drive the LED on C0: output/input?

enable_interrupts(int_rda);
enable_interrupts(global);

while(1);

}


Last edited by ALPL on Tue Mar 22, 2011 9:58 am; edited 1 time in total
stacey



Joined: 14 Jan 2011
Posts: 22
Location: MY

View user's profile Send private message

PostPosted: Tue Mar 22, 2011 9:55 am     Reply with quote

Thanks. I go try now..
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Goto page 1, 2  Next
Page 1 of 2

 
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