First you will need to get the PICs USART enabled and properly configured. Check the USART chapter of the datasheet and everything is pretty well documented. This consists of selecting a baud rate, enabling receive and send, etc using configuration registers. Then you need to enable interrupts on the PIC (GIE bit) and then enable the serial receive interrupt (RCIE). This means that every time a byte is received on the PIC USART the PIC will stop what it is currently doing, store the current program pointer and begin executing code at 0x0004. If you aren't familiar with this, read the section in the datasheet on interrupts. To create the ISR at 0x0004 and not have it run when you start you can simply start your program like this: org 0x0 ; start address = 0000h goto Init org 0x4 goto ISR ;-------------------------- ; Function - Init ; Initialization runs only on hardware reset ;-------------------------- Init ; INITIALISE PORTS ; binary used to see individual pin level movlw b'00000000' movwf PORTA movlw b'00000000' movwf PORTB etc. In the ISR (Interrupt Service Routine - function at 0x0004 called whenever an enabled interrupt occurs) then you need to check the flag bit for the serial receive interrupt (RCIF) to see if it is a serial receieve interrupt that has occurred. If you test RCIF set in your ISR then you know you have a byte waiting in the serial receive register. By testing this byte you can receive commands from the PC. Before getting too far into that end of things though, always remember to return from your ISR with retfie (return from interrupt execution) instead of the usual return. This tells the PIC that it needs to reenable interrupts (they are automatically disabled when the interrupt executes) and return to the point in your code that was executing when the interrupt occurred. No other state saving occurs automatically so to avoid problems you either need to save registers that get modified in your ISR or ensure that interrupts are only enabled in parts of your code that do not assume that certain registers are unchanged. You can enable or disable interrupts by setting or clearing the GIE bit and when you set GIE any of the interrupts that are individually enabled and have occurred while GIE was clear will fire. So you can do something like this: bsf INTCON,GIE nop nop nop bcf INTCON,GIE ;do some small task here bsf INTCON,GIE nop nop nop bcf INTCON,GIE ; do something else here that doesn't care if the registers that have ; been used in your ISR have been modified ; etc. As long as you never wait so long that you encounter a serial buffer overrun. At 19200 baud and PIC running at 4MHz you have ~400 cycles between serial bytes. Now you have the basic framework for communication. What I have generally done from here is to send command characters from the PC to the PIC, to which the PIC respnds with data. So say you need a function that returns basic info about the PIC device. Pick a command letter (let's say "q" for query). In the PIC code you need to check all serial receive interrupts for a byte in the serial receive register equal to 'q'. When you find that you have received the 'q' command then you need to send data from the PIC to the PC. Your VB program knows how many bytes of what information are coming in what order and is programmed to handle the data properly. Here is a function I use that transmits the contents of W via the USART (which must already be properly configured and enabled. ;--------------------------- ; Function - TransmitW ; Transmit the contents of W via USART serial port - return when done ;--------------------------- TransmitW ; bsf STATUS,RP0 ; bank 1 ; bsf TXSTA,TXEN ; enables serial transmit bcf STATUS,RP0 ; bank 0 movwf TXREG TransmitWLoop2 btfss PIR1,TXIF goto TransmitWLoop2 bsf STATUS,RP0 ; bank 1 TransmitWLoop btfss TXSTA,TRMT goto TransmitWLoop ; bcf TXSTA,TXEN ; disables serial transmit bcf STATUS,RP0 ; bank 0 return ;---------------------- To send a command + data to the PIC (for example to set time in the PIC device, maybe send 't' followed by 4 bytes of time data) I create CurrentCommand and CommandBytesComing register in the PIC. If I get a command in the PIC that has data following I set the CurrentCommand register to the ascii code of the command and CommandBytesComing to the number of data bytes that are to follow. Then I set up indirect addressing to be ready to store the first byte of data. Whenever a serial byte is received then I check to see if CurrentCommand is zero. If it is then I know that a command byte is coming from the PC. Otherwise I know that data is coming and can check CurrentCommand to go to the correct data receiving code. I think that should get you started - good luck. Nick ----- Original Message ----- From: "jay chen" To: Sent: Wednesday, January 07, 2004 4:22 PM Subject: [PICLIST] [pic:] Help! VB Serial data Logger > Thanks for the help on previous topic about serial logger data to excel > directly. > > i just write and test a basic Visual Basic program for serial port > communication, i make a loop-back circuit on my computer COM 1 (9-pin) > serial port so i can type whatever from the keyboard and return to the same > serial port and display on the program window. the program is working, so i > am thinking next project: write a Visual Basic program to collect > information from a PIC and send the data to some kinda application > software(excel, access...) and automatic creat a plotting graphic in real > time(or maybe every 30 seconds). i don't know what is the simple and easy > way to work on this project. can someone direct me to finish this project, > any tip, code, website, or book? > thanks!! > > ps. it does not have to use excel. at the beginning i was thinking excel > could be the easy one to plot the data > > here i attach my simple VB code for the serial port communication: > ************************************************************* > ************************************************************* > > Private Sub Command1_Click() > End 'exit > End Sub > > Private Sub Form_Load() > MSComm1.CommPort = 1 'com port = 1 > MSComm1.Settings = "9600,N,8,1" > MSComm1.InputLen = 0 > MSComm1.PortOpen = True > MSComm1.RThreshold = 1 > 'MSComm1.InputMode = comInputModeText > End Sub > > Private Sub Text1_KeyPress(KeyAscii As Integer) 'text1 box show the key > input from key board > Dim TempAscii As String > TempAscii = KeyAscii > MSComm1.Output = TempAscii 'send it to serial port > End Sub > > > Private Sub MSComm1_OnComm() 'read the input and display > on text2 box > Dim Buffer As Long > If (MSComm1.CommEvent = comEvReceive) Then > Buffer = MSComm1.Input > > Debug.Print "Receive - " & StrConv(Buffer, vbUnicode) > Text2.Text = Text2.Text & Chr$(Buffer) > End If > End Sub > ************************************************** > ************************************************** > > _________________________________________________________________ > Enjoy a special introductory offer for dial-up Internet access limited > time only! http://join.msn.com/?page=dept/dialup > > -- > http://www.piclist.com hint: The list server can filter out subtopics > (like ads or off topics) for you. See http://www.piclist.com/#topics > -- http://www.piclist.com hint: The list server can filter out subtopics (like ads or off topics) for you. See http://www.piclist.com/#topics