PROBLEM IN SENDING SIGNAL THROUGH USB to UART Converter

Discussion about projects that used PIC Microcontroller, Hardware Interface, Programming Algorithm and etc......

PROBLEM IN SENDING SIGNAL THROUGH USB to UART Converter

Postby kesheven » Mon Jul 04, 2011 3:36 pm

Hi, im using Enhanced 18 Pins PIC Start-up Kit with PIC 16F628. Actually my project is using thumbprint to open an em lock door. I facing a problem to send signal through USB to UART Converter to make the PIC function through Laptop. I using vb6 to do the programming where the thumbprint will connected to laptop. Once the thumbprint is verified, i must sen a signal to activate the PIC to generate signal to relay to open the door. Now the problem is, i can make theUSB to UART Converter function whereby when i click run in vb, the tx is blinking, but the PIC is not function to do what had programmed in PIC. Here i attached the code that programmed into PIC.

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 :
system.JPG
the vb6 interface

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.
kesheven
Fledgling
 
Posts: 1
Joined: Mon Jul 04, 2011 3:16 pm

Re: PROBLEM IN SENDING SIGNAL THROUGH USB to UART Converter

Postby shahrul » Mon Jul 04, 2011 3:59 pm

I think, you lack on PIC UART receive. I don't go through your code (don't have time to checking codes), but I go through your explanation only. See this example VB send command to PIC and also PIC send command to VB. PIC VB Lesson.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor

Re: PROBLEM IN SENDING SIGNAL THROUGH USB to UART Converter

Postby hyng » Tue Jul 05, 2011 10:28 am

IF i not mistaken you only send a character "l" to your PIC. There should be something wrong with the PIC coding. For if (a); why you put ";" after if (a)? and why you write a = uart_rec(); again after if(a) although it has bee written before if(a). Anyway, maybe you have other intention that i don't get to find out.

To check your code if it is working, you can try communicate with hypterterminal instead of GUI.
User avatar
hyng
Moderator
 
Posts: 292
Joined: Thu Apr 16, 2009 11:35 am

Re: PROBLEM IN SENDING SIGNAL THROUGH USB to UART Converter

Postby robosang » Tue Jul 05, 2011 10:09 pm

while(1)
{
a = uart_rec();

if(a);
{
a = uart_rec();

led1=0;
led2=0;
}
if (a == 'l')
{
led1=1;
led2=0;
}
}

Totally wrong at here.

1. When you do communication, you need some point/node that you confident it is working and you use it to verify one end first. In your case, you do both and connect both to test it. How can you know whether is your VB problem or PIC problem?
2. Connect the VB program to another computer and open HyperTerminal as hyng suggested, click the run button or any that will send the character 'l'. Ensure the HyperTerminal at another computer receive it. If that is fine then you are sure your VB is working.
3. Now connect your PIC with the code develop to computer, just open HyperTerminal, connect to correct COM port, as suggested by hyng, press the 'l' key, anything happen? If nothing happen, you program or the PIC have problem.

Looking at your code at PIC, I am confident the PIC code have problem. hyng has pointed out my comments, those are wrong coding, I think you simply modify it without understanding what is the receive function doing, and even the "if" also you don understand. What are you checking in if(a) ? and why is a being update from receive again after that if(a)? The semicolon ; is very important symbol in C, you cannot simply hamtam 8-)
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: PROBLEM IN SENDING SIGNAL THROUGH USB to UART Converter

Postby zhenning » Thu Jul 14, 2011 9:58 pm

You might want to check the converter itself by shorting pin2 and pin3 of the serial and do a loop back to hyperterminal :)
zhenning
Enthusiast
 
Posts: 351
Joined: Thu Dec 30, 2010 12:32 am


Return to PIC Microcontroller

Who is online

Users browsing this forum: No registered users and 7 guests