%########################################################################################################### %###############################---MATLAB CODE BELOW---##################################################### %########################################################################################################### %this sends serial three digit numbers to the PIC %this works when the PIC is using interrupts to get the serial data too. SerPIC = serial('COM4'); set(SerPIC,'BaudRate', 9600, 'DataBits', 8, 'Parity', 'none','StopBits', 1, 'FlowControl', 'none'); %set up the camera com port fopen(SerPIC); '#############--start--###########' fprintf(SerPIC, '%s', '008'); fprintf(SerPIC, '%s', '016'); pause(1) fprintf(SerPIC, '%s', '004'); fprintf(SerPIC, '%s', '001'); '--done--' fclose(SerPIC) delete(SerPIC) clear SerPIC '########################################################################################################### '###############################---PIC CODE BELOW---######################################################## '########################################################################################################### ' PicBasic Pro program to demonstrate an interrupt-driven input buffer for Hserin using On Interrupt. ' Pin definitions compatible with PIC16F877 ' should be an LED and 470 ohm resistor in series on PortB.0 ' should be one LED per line of PortD (with 470 ohm resistors in series) ' NOTE: the hardware serial lines on the PIC16F877 are pins 25 and 26 (PortC.6 and PortC.7) ' you MUST USE these hardware serial lines if you want to have a serial interrupt!! (uses HSerin) ' serial input should be going to the pic on PortC.7 ' serial output from the pic is not used in this example but it would come from PortC.6 ' Defines for USART Include "bs2defs.bas" 'has some useful stuff in it DEFINE HSER_RCSTA 90h DEFINE HSER_TXSTA 24h DEFINE HSER_BAUD 9600 '9600 baud RCIF VAR PIR1.5 ' Alias RCIF (USART Receive Interrupt Flag) OERR VAR RCSTA.1 ' Alias OERR (USART Overrun Error Flag) CREN VAR RCSTA.4 ' Alias CREN (USART Continuous Receive Enable) i VAR BYTE ' loop counter hello var byte ' holds incomming serial data #1 hello2 var byte ' holds incomming serial data #2 INTCON = %11000000 ' Enable interrupts ON INTERRUPT GoTo serialin ' Declare interrupt handler routine PIE1.5 = 1 ' Enable interrupt on USART TRISD = %00000000 ' set PortD as an output port PortD = %00000000 ' set LED port to all zeros hello = 0 hello2 = 0 '-------------- Main program starts here - blink an LED at 1Hz loop: High PortB.0 ' Turn on LED connected to PORTB.1 For i = 0 to 50 ' Delay Pause 1 ' Use a short pause within a loop Next i ' instead of one long pause Low PortB.0 ' Turn off LED For i = 0 to 50 ' Delay Pause 1 ' Use a short pause within a loop Next i ' instead of one long pause display: IF hello=0 and hello2 = 0 Then loop ' loop if nothing in buffer PortD = hello hello = 0 For i = 0 to 250 ' Delay Pause 1 ' Use a short pause within a loop Next i ' instead of one long pause PortD = hello2 hello2 = 0 For i = 0 to 250 ' Delay Pause 1 ' Use a short pause within a loop Next i ' instead of one long pause PortD = 0 GoTo display '============================================================================ '=====================--interrupt handler--================================== '============================================================================ Disable ' Don't check for interrupts in this section serialin: ' Buffer the character received HSerin [DEC3 hello, DEC3 hello2] ' Read USART and store character to next empty location '<----LEE Resume ' Return to program End