- CODE: SELECT_ALL_CODE
//====================================================================================
// Author : Cytron Technologies
// Project : UART of SK18B
// Description : Sample Soure Code for UART Using PIC16F628
//====================================================================================
//====================================================================================
#include<pic.h>
//configuration
//====================================================================================
__CONFIG (0x3F22);
// define IO port
//====================================================================================
#define SW RB0
#define led1 RB7
#define led2 RB6
// FUNCTION PTOTOTYPE
//====================================================================================
unsigned char uart_rec(void); //receive uart value
void uart_send(unsigned char data);
void uart_str(const char *s);
void delay(unsigned long data); //delay function, the delay time
// Main Program
//====================================================================================
void main ()
{
unsigned char a,b;
//set I/O input output
TRISA = 0b00000000;
TRISB = 0b00000000;
//Initial Condition
led1 = 1;
led2 = 1;
//Configure UART
SPBRG=129; //set baud rate as 9600 baud
BRGH=1; //baud rate high speed option
TXEN=1; //enable transmission
TX9 =0; //8-bit transmission
RX9 =0; //8-bit reception
CREN=1; //enable reception
SPEN=1; //enable serial port
while(1)
{
a = uart_rec();
if(a);
{
a = uart_rec();
led1=0;
led2=0;
}
if (a == 'l')
{
led1=1;
led2=0;
}
}
}
// Subroutine UART
//================================================================================
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++);
}
void delay(unsigned long data) //delay function, the delay time
{ //depend on the given value
for( ;data>0;data--);
}
So, it suppose to make 1 light ON n 1 light to OFF. The vb6 code is as below :
The coding for run button and connect button as in attachment is as below :
- CODE: SELECT_ALL_CODE
connect button :
Private Sub Button1_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Application.DoEvents()
If Button1.Text = "Connect" Then
'Check whether serial port is initially open or not
If serialport.IsOpen Then
serialport.Close()
End If
If ComboBox1.Text = Nothing Then
MsgBox("Please Choose your Comm Port", MsgBoxStyle.Critical)
Else
Try
With serialport
.PortName = ComboBox1.SelectedItem
.BaudRate = 9600
.Parity = IO.Ports.Parity.None
.DataBits = 8
.StopBits = IO.Ports.StopBits.One
End With
' Set the read/write timeouts
serialport.ReadTimeout = 1000
serialport.WriteTimeout = 1000
serialport.Open()
Button1.Text = "Connected"
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
ElseIf Button1.Text = "Connected" Then
Button1.Text = "Connect"
serialport.Close()
End If
End Sub
run button :
Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
If serialport.IsOpen = True Then
' Transmit data
Timer3.Enabled = True
Timer3.Interval = 100
Timer3.Start()
time = True
Try
serialport.Write(CChar("l"))
Catch ex As Exception
MsgBox(ex.ToString)
End Try
End If
End Sub
Plz, can anyone let me what is the problem and what i need to change in the coding, plz do let me know about it..i need it very urgently. Thank you. Do ask me if anythg need in further.