Plz if there anyone could help with this or anyone has similar code .. Thanx in advance
here are my codes
PIC16F887 code:
- CODE: SELECT_ALL_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
}
The VB code :
- CODE: SELECT_ALL_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