{Newbie} need help in serial communication of pic16F877a

Discussion in 'Embedded Systems and Microcontrollers' started by sgkho, Feb 6, 2012.

  1. sgkho

    Thread Starter New Member

    Feb 2, 2012
    11
    0
    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.

    Code ( (Unknown Language)):
    1. #include<pic.h>
    2. #include"delay.h"
    3. #define PIC_CLK == 20000000  
    4. // define the frequency of oscillator used;choose either one
    5.  
    6. __CONFIG(0x3F32); //in this case might be not important because pin assignment has been defined
    7. #define BAUD 9600
    8. #define SPBRG 129
    9.  
    10. #define SW1    RB0
    11. #define SW2    RB1
    12. #define LED1    RB6
    13. #define LED2    RB7
    14. #define buzzer     RB2
    15. #define RX_PIN  RC7
    16. #define TX_PIN  RC6
    17.  
    18. #define init_comms()
    19. RX_PIN = 1;
    20. TX_PIN = 1;
    21.  
    22. void putc(unsigned char);
    23. unsigned char getc(void);
    24.  
    25. [I][COLOR=SlateGray]Is that correct ? If so do for "1" as well ?[/COLOR][/I]
    26. void putc(unsigned char byte)
    27. {
    28.       while(!TXIF)
    29.       TXIF = 0;
    30.       SBUF = byte;
    31. }
    32.      
    33.  
    34. unsigned char getc(void)
    35. {
    36.        while(!RCIF)
    37.        RCIF = 0;
    38.        return SBUF;
    39. }
    40.  
    41. [COLOR=#666666][I]
    42. [/I][/COLOR][COLOR=#666666][I]
    43. [/I][/COLOR][COLOR=#666666][I][/I][/COLOR]
    44. //------function prototype-------//
    45. void delayS(int sec);
    46. void delaymS(int msec);
    47.  
    48. //----global variable---------//
    49.  
    50.  
    51.  
    52. //------main program-------//
    53. void main()
    54. {
    55.     int flag;
    56.     TRISB=0x03;
    57.     LED1=0;
    58.     LED2=0;
    59.     buzzer=0;
    60.    
    61.     while(1)
    62.     {                //checking if the sw1 is pressed
    63.         if(SW1==0)
    64.             LED1=1;
    65.         if(SW1==1)
    66.             LED1=0;    
    67.         if(SW2==0)
    68.             LED2=1;
    69.         if(SW2==1)
    70.             LED2=0;
    71.         if(LED1==1 || LED2==1)
    72.             buzzer=1;
    73.         else
    74.             buzzer=0;*/
    75.     }
    76. }
    77.  
     
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    7,386
    1,605
    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.
     
  3. sgkho

    Thread Starter New Member

    Feb 2, 2012
    11
    0
    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.
     
  4. sgkho

    Thread Starter New Member

    Feb 2, 2012
    11
    0
    plss,any1 can help me with the coding .
     
  5. sgkho

    Thread Starter New Member

    Feb 2, 2012
    11
    0
    Does pic upper part assign correct ?
    PIC16F877A
    Code ( (Unknown Language)):
    1. #include<pic.h>
    2. #include"usart.h"
    3. //#define XTAL_FREQ    20Mhz    //one may choose this argument or the one below
    4. #define PIC_CLK == 20000000  //be sure to include this argument;this argument is inside delay.h
    5. // the above 2 statement define the frequency of oscillator used;choose either one
    6. #include"delay.h"
    7. __CONFIG(0x3F32);
    8. #define SW1    RB0
    9. #define SW2    RB1
    10. #define LED1    RB6
    11. #define LED2    RB7
    12. #define buzzer     RB2
    13. #define LaserSwitch RB3
    14.  
    15. //------function prototype-------//
    16. void delayS(int sec);
    17. void delaymS(int msec);
    18.  
    19. //----global variable---------//
    20.  
    21.  
    22.  
    23. //------main program-------//
    24. void main()
    25. {
    26.     int flag;
    27.     TRISB=0b00000111;
    28.     LED1=0;
    29.     LED2=0;
    30.     buzzer=0;
    31.    
    32.     while(1)
    33.     {
    34.       [COLOR=Black]  [/COLOR][COLOR=Black]LaserSwitch=1;                
    35.         if(SW1==0)                     //checking if the sw1 is pressed
    36.             LED1=1;
    37.         if(SW1==1)
    38.             LED1=0;    
    39.         if(SW2==0)
    40.             LED2=1;
    41.         if(SW2==1)
    42.             LED2=0;[/COLOR]
    43.     }
    44. }
    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
    Code ( (Unknown Language)):
    1. Imports System
    2. Imports System.ComponentModel
    3. Imports System.Threading
    4. Imports System.IO.Ports
    5. Public Class frmMain
    6.     Dim myPort As Array  'COM Ports detected on the system will be stored here
    7.     Delegate Sub SetTextCallback(ByVal [text] As String) 'Added to prevent threading errors during receiveing of data
    8.     Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    9.         'When our form loads, auto detect all serial ports in the system and populate the cmbPort Combo box.
    10.         myPort = IO.Ports.SerialPort.GetPortNames() 'Get all com ports available
    11.  
    12.         For i = 0 To cmbPort.Items.Add(myPort(i))
    13.         Next
    14.         cmbPort.Text = cmbPort.Items.Item(0)    'Set cmbPort text to the first COM port detected
    15.  
    16.         btnDisconnect.Enabled = False           'Initially Disconnect Button is Disabled
    17.     End Sub
    18.  
    19.     Private Sub btnConnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnConnect.Click
    20.         SerialPort1.PortName = cmbPort.Text         'Set SerialPort1 to the selected COM port at startup
    21.         SerialPort1.Open()
    22.  
    23.         btnConnect.Enabled = False          'Disable Connect button
    24.         btnDisconnect.Enabled = True        'and Enable Disconnect button
    25.  
    26.  
    27.     End Sub
    28.  
    29.     Private Sub btnDisconnect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDisconnect.Click
    30.         SerialPort1.Close()             'Close our Serial Port
    31.  
    32.         btnConnect.Enabled = True
    33.         btnDisconnect.Enabled = False
    34.     End Sub
    35.  
    36.     Private Sub cmbPort_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmbPort.SelectedIndexChanged
    37.         If SerialPort1.IsOpen = False Then
    38.             SerialPort1.PortName = cmbPort.Text         'pop a message box to user if he is changing ports
    39.         Else                                                                               'without disconnecting first.
    40.             MsgBox("Valid only if port is Closed", vbCritical)
    41.         End If
    42.     End Sub
    43.  
    44.     Private Sub LaserbtnON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LaserbtnON.Click
    45.         SerialPort1.Write("C")
    46.     End Sub
    47.  
    48.     Private Sub LaserbtnOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LaserbtnOFF.Click
    49.         SerialPort1.Write("D")
    50.     End Sub
    51.  
    52.     Private Sub SmokebtnON_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmokebtnON.Click
    53.         SerialPort1.Write("E")
    54.     End Sub
    55.  
    56.     Private Sub SmokebtnOFF_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SmokebtnOFF.Click
    57.         SerialPort1.Write("F")
    58.     End Sub
    59.  
    60.     Private Sub SerialPort1_DataReceived(ByVal sender As Object, ByVal e As System.IO.Ports.SerialDataReceivedEventArgs) Handles SerialPort1.DataReceived
    61.  
    62.     End Sub
    63. End Class
    64.  
    Plss help me . THX
     
  6. sgkho

    Thread Starter New Member

    Feb 2, 2012
    11
    0
    This is my usart file and vb interface . Hope some1 check my coding.:(
     
Loading...