Beginner having trouble with interrupt..


Closed Thread
Results 1 to 11 of 11
  1. #1
    Join Date
    Oct 2008
    Posts
    65

    Smile Beginner having trouble with interrupt..

    Hello everyone,
    I'm trying to make a simple program that will blink a LED on PortB.2. While waits for an incoming character to PortC.7(with max232). My trouble I think is with the command serin PortC.7, T2400, RX and my PortB.2 doesnt blink. I appreciate any help/guide to understand how I can make this work.
    Code:
    CLEAR 
    include "modedefs.bas" 'include serout defines
    DEFINE OSC 20
    OPTION_REG=%10000101	
    				;Bit 7=1 disable pull ups on PORTB 
    				;Bit 5=0 selects timer mode 
    				;Bit 2=1  }
    				;Bit 1=0  } sets Timer0 pre-scaler to 64
    				;Bit 0=1  }
    				
    INTCON=%10100000	;Bit 7=1 Enables all unmasked interrupts
    				;Bit 5=1 Enables Timer0 overflow interrupt 
    				;Bit 2 flag will be set on interrupt and has to be cleared 
    				;in the interrupt routine.  It is set clear to start with.
    
    RX   var byte    ; holds incoming character 
    symbol led1=PORTB.0
    symbol led2=PORTB.1
    i    var byte    ;counter
    Cnt  var byte
    TRISB   = %11110000	;sets the 3 output pins in the B port
    PORTB = %00000000	;sets all pins low in the B port
    TRISC = %10000000   ;Set portC.7 for  serial input				
    ON INTERRUPT GOTO INTERUPTROUTINE	
    			
    				
    MAINLOOP:		
            High PORTB.2       
            For i = 0 to 200	
    	    	Pause 1   	
    		Next i		
            Low PORTB.2     
            For i = 0 to 200	
    	        Pause 1   
    		Next i 
            
       ''''''''''''''''''''''''''''''''''''''''''''''''
        IF Cnt=49 then
                led1=1-led1
                pause 100
                Cnt=0
          endif   
    
          if Cnt=50 then
                led2=1-led2
                pause 100
                Cnt=0
          endif
    GOTO MAINLOOP	;end of loop
    				
    DISABLE	
    INTERUPTROUTINE:	
       serin PortC.7, T2400, RX 'Receive menu  
       Cnt = Rx
    ENDINTERRUPT:	;
    INTCON.2 = 0		;clears the interrupt flag.
    RESUME		;resume the main program
    ENABLE		;DISABLE and ENABLE must bracket the int routine
    END
    mbox

  2. #2
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Default

    Hello mbox,
    are you sure your PIC has portC interrupts? Which PIC are you using? I guess my question is what is the source of the interrupt? The timer ? I do not see anything to trigger the interrupt.
    Last edited by Archangel; - 28th April 2009 at 08:38.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

  3. #3
    Join Date
    Oct 2008
    Posts
    65

    Default

    Hi joe,
    I'm using Pic16F877A, to be honest I'm trying to learn how to use Interrupt command and got stuck...

    regards,
    mbox

  4. #4
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    With a prescaler of 64 at 20MHz, Timer0 will overflow in about 3.2mS. With your loop from 0 to 200, you're going to have to send a LOT of serial data before it can make it through your loop and toggle PORTB.2 off.

    Every 3.2mS it jumps to your SERIN interrupt routine. It's going to sit there until it receives a byte of data. So it's going to take a good while before making it through your first loop.

    And, you really don't know what the count in Timer0 is when you exit your interrupt routine, so you might want to clear Timer0 before exiting just in case.

    You can check to make sure your Timer0 interrupt is happening by commenting out the SERIN line, and toggling another LED in your interrupt routine.

    Something like this;
    Code:
    DISABLE	
    INTERUPTROUTINE:	
       'serin PortB.3, T2400, RX 'Receive menu  
       'Cnt = Rx
       TOGGLE PORTB.3
       TMR0=0           ;clear Timer0 before exit
       INTCON.2 = 0   ;clears the interrupt flag.
       RESUME            ;resume the main program
       ENABLE            ;DISABLE and ENABLE must bracket the int routine
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  5. #5
    Join Date
    Oct 2008
    Posts
    65

    Question

    Hi Bruce,
    Thanks for the reply, I realized that when I used the code PortB.3, T2400, RX PortB.2 starts to blink. Is this mean that Portc.7 can not be used as my rx?


    thanks in advance,
    mbox

  6. #6
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    Is this mean that Portc.7 can not be used as my rx?
    No. I was just pointing out why your original code didn't work as you expected.

    It doesn't matter which pin you use for receiving serial data. You just need to understand why your original code isn't working as expected.

    Every 3.2mS or so your program jumps to the SERIN interrupt routine. And it just sits there until it receives a byte of data.

    This means it will take a very long time before your first loop can finish because it gets interrupted too often for your first loop to complete.
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  7. #7
    Join Date
    Oct 2008
    Posts
    65

    Default

    Bruce I adjust the code, but it doesnt blink the led on PortB.1(only high output) When I use the serial communicator and entering 1 (5x) it turns off the led PortB.1. I was expecting that it will trigger PortB.0.
    Code:
    CLEAR 
    include "modedefs.bas" 'include serout defines
    DEFINE OSC 20	;using a 4 MHz oscillator here
    TRISC = %10000000
    OPTION_REG=%10000101	;Page 48 or data sheet
    				;Bit 7=1 disable pull ups on PORTB 
    				;Bit 5=0 selects timer mode 
    				;Bit 2=1  }
    				;Bit 1=0  } sets Timer0 pre-scaler to 64
    				;Bit 0=1  }
    				
    INTCON=%10100000	;Bit 7=1 Enables all unmasked interrupts
    				;Bit 5=1 Enables Timer0 overflow interrupt 
    				;Bit 2 flag will be set on interrupt and has to be cleared 
    				;in the interrupt routine.  It is set clear to start with.
    RX var byte ' Receive byte
    X    VAR    BYTE 	;Declare x as a variable
    i var byte
    TEMP      var BYTE 52
    Cnt       VAR BYTE 49
    symbol led1=PORTB.0
    x=20
    ALPHA		VAR  WORD	;this variable counts in the PauseUS loop
    BETA		VAR  BYTE	;this variable counts the 61 interrupt ticks
    TRISB   = %11110000	;sets the 3 output pins in the D port
    PORTB = %00000000	;sets all pins low in the D port
    				
    ON INTERRUPT GOTO INTERUPTROUTINE	;This line needs to be early in the program, before
    				;the routine is called in any case.
    				
    MAINLOOP:		
            High PORTB.1       
            For i = 0 to 10	
    	    	Pause 1   
    		Next i		
            Low PORTB.1     
            For i = 0 to 10	
    	        Pause 1   	
    		Next i 
            
       ''''''''''''''''''''''''''''''''''''''''''''''''
        IF Cnt=49 then
                led1=1-led1
                pause 100
                cnt=0
         endif   
                       
       ''''''''''''''''''''''''''''''''''''''''''''''''         	
    GOTO MAINLOOP	;end of loop
    				
    DISABLE	
    INTERUPTROUTINE:	
       serin PortC.7, T2400, RX 'Receive menu
       Cnt = Rx
       'TOGGLE PORTB.2
       'TMR0=0           ;clear Timer0 before exit
       INTCON.2 = 0   ;clears the interrupt flag.
       RESUME		;resume the main program
    ENABLE		;DISABLE and ENABLE must bracket the int routine
    END
    Do I need to change some settings to have my target output?
    _____________________
    mbox
    Last edited by mbox; - 28th April 2009 at 23:43.

  8. #8
    Join Date
    Jul 2003
    Posts
    2,405

    Default

    Try this with Cnt VAR BYTE;

    Code:
    MAINLOOP:		
        High PORTB.1       
        Pauseus 3000   
        Low PORTB.1     
        Pauseus 3000
            
       ''''''''''''''''''''''''''''''''''''''''''''''''
        PORTB.0 = Cnt.0 ' send a 1 or 0 by serial to toggle portb.0
        GOTO MAINLOP
    Regards,

    -Bruce
    tech at rentron.com
    http://www.rentron.com

  9. #9
    Join Date
    Oct 2008
    Posts
    65

    Default

    ok thanks, i will try it later at home...

    __________________
    mbox

  10. #10
    Join Date
    Oct 2008
    Posts
    65

    Default

    Hi everyone, I found a program which is the one I'm interested to learn, the code is from mister_e. As I understand I should use PortC.7 as RX. One line to RX and one line to GRND. Using the serial communicator. But it seems I can not trigger led on PortB1. anyone has an idea what I missed? Pls check my connection PC to PIC.
    this is the code from mister_e
    Code:
      '
        '   Simple program to handle USART interrupt using PIC16F877 @20MHZ
        '   ===============================================================
        '
        '   This program will Blink a LED on PORTB.0 to show
        '   that the PIC is running.  
        '
        '   This program also allow the user to activate or deactivate 
        '   a LED connected on PORTB.1 pin by sending command from a PC
        '   terminal software using 9600,n,8,1 setting
        '
        '   User command:
        '   =============
        '                1. If character "1" is received => enable the LED
        '                2. if character "2" is received => disable the LED
        '
        '
        'DEFINE LOADER_USED 1        ' using bootloader
        DEFINE OSC 20               ' running at 20 MHZ
        
        '    PORT setting
        '    ============
             '
             TRISC=%10000000        ' RC.7 => USART RX pin set to input
                                    ' all other pin set to output
                                    '
             TRISB=0                ' RB<7:0> set to output
        
        '    USART setting
        '    =============
             ' Since we will not use HSERIN/HSEROUT, we must
             ' write directly to internal PIC register
             ' 
             TXSTA=$24              ' enable transmit and SPBRGH=1
             RCSTA=$90              ' Enable USART and continuous receive
             SPBRG=129              ' BAUD RATE = 9600 BAUDS
       
        '    Interrupt definition
        '    ====================
             '
             PIE1.5   =1            ' enable USART receive interrupt
             INTCON.6 =1            ' enable peripheral interrupt
        
        '    Alias definition
        '    ================
             '
             RCIF      var PIR1.5   ' receiver interrupt
             StatusLED var PORTB.0  ' the LED who show a 'running process'
             UserLED   var PORTB.1  ' the LED that user want to control
             
        '    Variable definition
        '    ===================
             '
             Delay     var word     ' 
             DataIn    var byte     ' use to store RCREG content
             Discard   var byte     ' use to erase RCREG register
            
        '    Hardware/software initialisation
        '    ================================
             '
             PORTB=0                ' clear PORTB         
             on interrupt goto USARTInterrupt
            
    Start:
        '    Main program loop that make the user happy to
        '    see a LED flashing to mean that something is 
        '    running 
        '
        toggle statusled
        for delay = 1 to 5000       ' use a loop delay to ensure
          pauseus 5                 ' getting interrupt as fast as
          next                      ' possible
        goto start
        
    
    disable interrupt
    USARTInterrupt:
        '    Here's the interrupt routine who will make the user
        '    much happy by giving him the feeling to have the 
        '    control on the machine when the status of the user
        '    LED will change
        '
        RCSTA=0                     ' Disable serial port AND
                                    ' clear possible error (FERR,OERR)
                                    
        datain=RCREG                ' Get data
                                    
        while RCif                  ' wait untill the RCREG is empty
              discard=RCREG         ' by reading it and store result in a
        wend                        ' don't care variable
                                    
        select case datain          ' What to do with that data???
                                    
               case "1"             ' User selection = 1
                    userled=1       '      => Enable the LED
                                    
               case "2"             ' User selection =2
                    userled=0       '      => disable the LED
                                    
               end select           
        RCSTA=$90                   ' Re-enable the serial PORT AND
        resume                      ' get out of here
    enable interrupt

    thanks,
    mbox
    Attached Images Attached Images  

  11. #11
    Join Date
    Aug 2006
    Location
    Look, behind you.
    Posts
    2,818

    Default

    Here, READ THIS, and UNDERSTAND IT.( the link that is)
    You are using code from mister_e. mister_e writes good code, so what are you doing or not doing correctly? Right off the top I see there are no config statements, and the OSC is defined at 20 mhz, guess what? The DEFAULT config statement in the 16F87x.inc file (actual number may vary) is set for XT_OSC which means it will provide only enough power to swing 4 mhz, you need HS_OSC to run at 20 mhz.
    http://www.picbasic.co.uk/forum/show...3375#post73375
    If no OSC then no blinky.
    Last edited by Archangel; - 2nd May 2009 at 07:46.
    If you do not believe in MAGIC, Consider how currency has value simply by printing it, and is then traded for real assets.
    .
    Gold is the money of kings, silver is the money of gentlemen, barter is the money of peasants - but debt is the money of slaves
    .
    There simply is no "Happy Spam" If you do it you will disappear from this forum.

Similar Threads

  1. Can't ID interrupt source with this IntHandler??
    By jellis00 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 3rd June 2009, 02:35
  2. Very Trouble with 8722 Interrupt
    By smarino in forum mel PIC BASIC
    Replies: 4
    Last Post: - 5th March 2009, 09:48
  3. Help with Analog Interrupt
    By brid0030 in forum mel PIC BASIC Pro
    Replies: 7
    Last Post: - 13th February 2008, 18:14
  4. NEWBIE: Some basic questions using interrupts
    By JackPollack in forum mel PIC BASIC Pro
    Replies: 5
    Last Post: - 8th March 2006, 02:59
  5. USART interrupt not interrupting right
    By Morpheus in forum mel PIC BASIC Pro
    Replies: 12
    Last Post: - 6th March 2005, 01:07

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts