接口PIC16F887与PC(VB.net) [英] Interface PIC16F887 with PC (VB.net)
问题描述
你好..我正在做我的FYP,我在将数据从PC(vb.net)传送到将在LCD 2x16中显示的微控制器(PIC16f887)时出现问题。例如,我想发送的问题(你好)我得到(hlo)代替(你好)或有时我得到从VB.net的文本框发送的第3位或字符..用于PIC和PC之间的通信使用USB到UART转换器和我正在使用微控制器的启动工具包。
如果有人可以提供帮助,或者任何人都有类似的代码.. Thanx提前:)
这是我的代码
PIC16F887代码:
Hello ..I'm doing my FYP and am having problems with getting data from the PC (vb.net) to the microcontroller (PIC16f887) that will be displayed in LCD 2x16 .. The problem for example I wanna send (hello) to the microcontroller so I get ( hlo) instead of ( hello ) or sometimes i get the 3rd digit or char that am sending from the textbox in VB.net.. for the communication between the PIC and PC am using USB to UART converter and am using startup kit for the microcontroller .
Plz if there anyone could help with this or anyone has similar code .. Thanx in advance :)
here are my codes
PIC16F887 code:
//========================================================================
// include
//========================================================================
#include <pic.h>
//========================================================================
// Configuration
//========================================================================
__CONFIG(FOSC_HS & //External Crystal at High Speed
WDTE_OFF & //Disable Watchdog Timer
PWRTE_ON & //Enable Power Up Timer
BOREN_OFF & //Disable Brown Out Reset
MCLRE_ON & //MCLR function is enabled
LVP_OFF); //Disable Low Voltage Programming
//=========================================================================
// Define
//=========================================================================
#define rs RB4 //RS pin of the LCD display
#define e RB5 //E pin of the LCD display
#define b_light RC1 //backlight of the LCD display (1 to on backlight)
#define SW1 RB0 //button (active low)
#define SW2 RB1 //button (active low)
#define lcd_data PORTD //LCD 8-bit data PORT
#define LED1 RB6 //led 1 (active high)
#define LED2 RB7 //led 2 (active high)
//===========================================================================
// Function prototype
//===========================================================================
void delay(unsigned long data);
void send_config(unsigned char data);
void send_char(unsigned char data);
void lcd_goto(unsigned char data);
void lcd_clr(void);
void send_string(const char *s);
unsigned char uart_rec(void);
void beep(void);
void uart_send(unsigned char data);
void uart_str(const char *s);
void send_cmd(unsigned char num, unsigned int data, unsigned int ramp);
//===========================================================================
// Main function
//===========================================================================
void main(void)
{
unsigned char a;
PORTA = 0; // Clear Port
PORTB = 0;
PORTC = 0;
PORTD = 0;
TRISA = 0b00000000; // set PortA as OUTPUT
TRISB = 0b00000011; // set PORTB<1:0> as INPUT
TRISC = 0b00000000;
TRISD = 0b00000000; // set PORTD as OUTPUT
ANSEL = 0; // Set PORTA as Digital I/O for PIC16F887
ANSELH = 0; // SET PORTB as DIGITAL I/O for PIC16F887
//setup USART
SPBRG = 0x81; //set baud rate to 9600 for 20Mhz
BRGH = 1; //baud rate high speed option
TXEN = 1; //enable transmission
CREN = 1; //enable reception
SPEN = 1; //enable serial port
TX9 =0; //8-bit transmission
RX9 =0; //8-bit reception
//setup ADC
ADCON1 = 0b00000110; //set ADx pin digital I/O
//configure lcd
send_config(0b00000001); //clear display at lcd
send_config(0b00000010); //lcd return to home
send_config(0b00000110); //entry mode-cursor increase 1
send_config(0b00001100); //display on, cursor off and cursor blink off
send_config(0b00111000); //function set
//infinity loop
//uart_init();
while(1)
{
CREN=1;
a = uart_rec();
lcd_clr();
lcd_goto(0);
send_string("The value is:");
lcd_goto(20);
send_char(a);
CREN=0;
}
}
//===========================================================================
// Functions
//===========================================================================
void delay(unsigned long data) //delay function, the delay time
{ //depend on the given value
for( ;data>0;data-=1);
}
void send_config(unsigned char data) //send lcd configuration
{
rs=0; //set lcd to configuration mode
lcd_data=data; //lcd data port = data
e=1; //pulse e to confirm the data
delay(50);
e=0;
delay(50);
}
void send_char(unsigned char data) //send lcd character
{
rs=1; //set lcd to display mode
lcd_data=data; //lcd data port = data
e=1; //pulse e to confirm the data
delay(10);
e=0;
delay(10);
}
void lcd_goto(unsigned char data) //set the location of the lcd cursor
{ //if the given value is (0-15) the
if(data<16) //cursor will be at the upper line
{ //if the given value is (20-35) the
send_config(0x80+data); //cursor will be at the lower line
} //location of the lcd cursor(2X16):
else // -----------------------------------------------------
{ // | |00|01|02|03|04|05|06|07|08|09|10|11|12|13|14|15| |
data=data-20; // | |20|21|22|23|24|25|26|27|28|29|30|31|32|33|34|35| |
send_config(0xc0+data); // -----------------------------------------------------
}
}
void lcd_clr(void) //clear the lcd
{
send_config(0x01);
delay(600);
}
void send_string(const char *s) //send a string to display in the lcd
{
unsigned char i=0;
while (s && *s)send_char (*s++);
}
unsigned char uart_rec(void) //receive uart value
{
unsigned char rec_data;
while(RCIF==0); //wait for data
rec_data = RCREG;
return rec_data; //return the data received
}
void uart_send(unsigned char data)
{
while(TXIF==0); //only send the new data after
TXREG=data; //the previous data finish sent
}
void uart_str(const char *s)
{
while(*s)uart_send(*s++); // UART sending string
}
VB代码:
The VB code :
Imports System.IO.Ports
Public Class Form2
Delegate Sub AddText(ByVal Text As String)
Dim myEnc As System.Text.Encoding = System.Text.Encoding.GetEncoding(28591)
Private Sub SerialPort1_DataReceived(ByVal sender As Object, _
ByVal e As SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
Dim br As Integer = SerialPort1.BytesToRead
If br > 0 Then
Dim b(br - 1) As Byte
br = SerialPort1.Read(b, 0, br)
Dim s As String = myEnc.GetChars(b)
RichTextBox1.Invoke(New AddText(AddressOf RichTextBox1.AppendText), s)
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
If SerialPort1.IsOpen Then
SerialPort1.Close()
Else
Try
SerialPort1.PortName = "COM8"
SerialPort1.BaudRate = 9600
SerialPort1.Encoding = myEnc
SerialPort1.Open()
MsgBox("The port is open", MsgBoxStyle.Information)
Catch ex As Exception
MessageBox.Show(ex.ToString)
End Try
End If
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SerialPort1.Write(TextBox1.Text)
End Sub
推荐答案
在 PIC
方面,您可能需要使用中断(或避免使用更好的民意调查技巧d松散字符。
On PIC
side you probably have to use interrupts (or a better polling technique) to avoid loosing characters.
这篇关于接口PIC16F887与PC(VB.net)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!