View previous topic :: View next topic |
Author |
Message |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
Interface VB with PIC |
Posted: Thu Mar 17, 2011 6:54 am |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Thu Mar 17, 2011 7:17 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
PCM programmer
Joined: 06 Sep 2003 Posts: 21708
|
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Thu Mar 17, 2011 5:50 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Thu Mar 17, 2011 6:07 pm |
|
|
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? |
|
![](templates/subSilver/images/spacer.gif) |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Fri Mar 18, 2011 1:44 am |
|
|
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;
}
}
} |
|
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Fri Mar 18, 2011 7:34 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Fri Mar 18, 2011 8:14 am |
|
|
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. |
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Fri Mar 18, 2011 9:32 pm |
|
|
you are using the USB to UART connector also? |
|
![](templates/subSilver/images/spacer.gif) |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Sat Mar 19, 2011 6:30 am |
|
|
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) |
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Tue Mar 22, 2011 5:34 am |
|
|
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)
{
}
}
}
|
|
|
![](templates/subSilver/images/spacer.gif) |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Tue Mar 22, 2011 9:24 am |
|
|
looking over quite fast it it should be this way:
if(rx=='A') // just 1 character, not a string!
... |
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Tue Mar 22, 2011 9:25 am |
|
|
Can you help me correct it?
I really have no idea on using serial port. |
|
![](templates/subSilver/images/spacer.gif) |
ALPL
Joined: 29 Mar 2009 Posts: 30
|
|
Posted: Tue Mar 22, 2011 9:51 am |
|
|
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 |
|
![](templates/subSilver/images/spacer.gif) |
stacey
Joined: 14 Jan 2011 Posts: 22 Location: MY
|
|
Posted: Tue Mar 22, 2011 9:55 am |
|
|
Thanks. I go try now.. |
|
![](templates/subSilver/images/spacer.gif) |
|