{Newbie} need help in serial communication of pic16F877a

Thread Starter

sgkho

Joined Feb 2, 2012
11
As title, i'm doing a project tht require serial communicate btw pic and vb. I am new in serial communication require help and guide so that i manage to finished my GUI part within this week. My project hardware is turn on/off the sensors (indicate with leds)and buzzer . The remaining part is left with communicate the pic16f877a (by using hi-tech compiler c language) and vb2008. (COMM 7)

I have searched some sample code but majority are written in assembly :(
Below is my coding , i have try do my best :confused:. Hope some expert can help me correct it and sorry for the silly programming.

Rich (BB code):
#include<pic.h>
#include"delay.h"
#define PIC_CLK == 20000000  
// define the frequency of oscillator used;choose either one

__CONFIG(0x3F32); //in this case might be not important because pin assignment has been defined
#define BAUD 9600
#define SPBRG 129

#define SW1    RB0
#define SW2    RB1
#define LED1    RB6
#define LED2    RB7
#define buzzer     RB2
#define RX_PIN  RC7
#define TX_PIN  RC6

#define init_comms()
RX_PIN = 1;
TX_PIN = 1;

void putc(unsigned char);
unsigned char getc(void);

Is that correct ? If so do for "1" as well ?
void putc(unsigned char byte)
{
      while(!TXIF)
      TXIF = 0;
      SBUF = byte;
} 
      

unsigned char getc(void)
{
       while(!RCIF)
       RCIF = 0;
       return SBUF;
}




//------function prototype-------//
void delayS(int sec);
void delaymS(int msec);

//----global variable---------//



//------main program-------//
void main()
{
    int flag;
    TRISB=0x03;
    LED1=0;
    LED2=0;
    buzzer=0;
    
    while(1)
    {                //checking if the sw1 is pressed
        if(SW1==0)
            LED1=1;
        if(SW1==1) 
            LED1=0;    
        if(SW2==0)
            LED2=1;
        if(SW2==1)
            LED2=0;
        if(LED1==1 || LED2==1)
            buzzer=1;
        else
            buzzer=0;*/
    }
}
 

ErnieM

Joined Apr 24, 2011
8,377
I see several things wrong off hand, but the basic thing seems to be you don't set up the USART. In fact, you don't use it at all as the getc() and putc() routines never get called.

Spend some time with Chapter 10 of the data sheet and see how to work with the USART. There is also a sample program that uses the serial interface ("serial") in the samples folder for the HI-TECH compiler.

Asking a specific question is great. Asking "please fix this for me" will not get you anywhere.
 

Thread Starter

sgkho

Joined Feb 2, 2012
11
I have read through the chapter 10 and the sample but still quite dont understand . Could you give me hand by hand guide on the coding .Sorry for my poor programming knowledge.


#include<pic.h>
#include"delay.h"
#define PIC_CLK == 20000000 // define the frequency of oscillator used
__CONFIG(0x3F32); //in this case might be not important because pin assignment has been defined #define SW1 RB0

#define SW2 RB1
#define LED1 RB6
#define LED2 RB7
#define buzzer RB2
#define BAUD 9600
#define SPBRG 129
#define RX_PIN TRISC7
#define TX_PIN TRISC6

void serial_setup(void)
{
//Transmit status register TXSTA...
CSRC=0;// clock source select lbit.. neglect for asynchronous mode
TX9=0; // 8 bit mode
TXEN=1; //Transmit enable bit
SYNC=0;// asynchronous mode
SENDB=0;//1-> send break character bit ....
BRGH=1; //asynchronous mode 1-> high speed,
TRMT=0;// transmit shift register status bit
TX9D=0;// 1->nine bit mode ..

// Receive status and control register..RCSTA
SPEN=1;// serial port enable bit... // automatically configures RX n TX as input and output pins
RX9=0;// receive 9 bit ..0-> 8 bit 1->9bit
SREN=0;// asynchronous mode -> don't care ..
CREN=1; // continuous reveive enable bit
ADDEN=0;// address detect enable bit ..0-> 8 bit mode ..1-> nine bit mode
FERR=0; // Framing error bit // can be updated by reading RCREG
OERR=0;// over run error bit// can be cleared by clearing CREN RX9D=0;// nine bit..

// baudrate control register
BRG16=0; //8 bit baud rate generator
TXIE=1;// enable transmit interrupt
GIE=1;// enable global interrupt
PEIE=1;// enable peripheral interrupt
RCIE=1;// enable receive interrupt
}

IS that above 1 correct ?
N for the below part how i need to change since mine is not user key in the character
but the pic will send/receive signal on/off from vb config or sensors signal
void putc(unsigned char); unsigned char getc(void);
#deinfe clear_usart_errors_inline

if (OERR) \
{ \
TXEN=0; \
TXEN=1; \
CREN=0; \
CREN=1; \
} \
if (FERR) \
{ \
dummy=RCREG; \
TXEN=0; \
TXEN=1; \
}
void putc(unsigned char byte) { while(!TXIF) continue; TXREG = byte; } unsigned char getc(void) { while(!RCIF) RCIF = 0; return RCREG; }
Hope can get expert reply soon , thx.
 

Thread Starter

sgkho

Joined Feb 2, 2012
11
Does pic upper part assign correct ?
PIC16F877A
Rich (BB code):
#include<pic.h>
#include"usart.h"
//#define XTAL_FREQ    20Mhz    //one may choose this argument or the one below
#define PIC_CLK == 20000000  //be sure to include this argument;this argument is inside delay.h
// the above 2 statement define the frequency of oscillator used;choose either one
#include"delay.h"
__CONFIG(0x3F32); 
#define SW1    RB0
#define SW2    RB1
#define LED1    RB6
#define LED2    RB7
#define buzzer     RB2
#define LaserSwitch RB3

//------function prototype-------//
void delayS(int sec);
void delaymS(int msec);

//----global variable---------//



//------main program-------//
void main()
{
    int flag;
    TRISB=0b00000111;
    LED1=0;
    LED2=0;
    buzzer=0;
    
    while(1)
    {
        LaserSwitch=1;                
        if(SW1==0)                     //checking if the sw1 is pressed
            LED1=1;
        if(SW1==1) 
            LED1=0;    
        if(SW2==0)
            LED2=1;
        if(SW2==1)
            LED2=0;
    }
}
vb2008 - in this part :
1. how can i set the serailport1_DataReceived part
2. how/where to assign so that it knw wht the alphabet (c,d,e,f) means + display the status on/off
Rich (BB code):
Imports System
Imports System.ComponentModel
Imports System.Threading
Imports System.IO.Ports
Public Class frmMain
    Dim myPort As Array  'COM Ports detected on the system will be stored here
    Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
    Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
        myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available

        For i = 0 To cmbPort.Items.Add(myPort(i))
        Next
        cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected

        btnDisconnect.Enabled = False           'Initially Disconnect Button is Disabled
    End Sub

    Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
        SerialPort1.PortName = cmbPort.Text         'Set SerialPort1 to the selected COM port at startup
        SerialPort1.Open()

        btnConnect.Enabled = False          'Disable Connect button
        btnDisconnect.Enabled = True        'and Enable Disconnect button


    End Sub

    Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
        SerialPort1.Close()             'Close our Serial Port

        btnConnect.Enabled = True
        btnDisconnect.Enabled = False
    End Sub

    Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
        If SerialPort1.IsOpen = False Then
            SerialPort1.PortName = cmbPort.Text         'pop a message box to user if he is changing ports
        Else                                                                               'without disconnecting first.
            MsgBox("Valid only if port is Closed", vbCritical)
        End If
    End Sub

    Private Sub LaserbtnON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LaserbtnON.Click
        SerialPort1.Write("C")
    End Sub

    Private Sub LaserbtnOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LaserbtnOFF.Click
        SerialPort1.Write("D")
    End Sub

    Private Sub SmokebtnON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmokebtnON.Click
        SerialPort1.Write("E")
    End Sub

    Private Sub SmokebtnOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmokebtnOFF.Click
        SerialPort1.Write("F")
    End Sub

    Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived

    End Sub
End Class
Plss help me . THX
 
Top