BasicMicroUK - Forums
It is currently Fri Oct 19, 2018 8:24 pm

All times are UTC [ DST ]

Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: Using registers to set up interrupts on nano
PostPosted: Tue Feb 26, 2013 10:38 am 

Joined: Mon Dec 21, 2009 6:52 am
Posts: 92

Has anyone already used registers to set up interrupts on nano chip?
I need to detect changes on a pin (0-1) to trigger an other pin.
Sample of code would be very useful :-)



 Post subject: Re: Using registers to set up interrupts on nano
PostPosted: Wed Jul 10, 2013 7:10 pm 
Site Admin
User avatar

Joined: Fri Apr 12, 2013 8:38 pm
Posts: 87
Location: Nottingham, England, UK
Hi Chris

This is an interesting subject and I notice its been on the forum for some time now but so far no one has posted a reply,

Using interrupts can be quite a hard task to master and there are many pitfalls you can come up against and traps that can cause a great deal of head scratching as things that can go wrong are normally invisible and hard to find BUT if you can master them it can be very rewarding as there are lots of tasks that can be done with interrupts that are almost impossible without,

There are many useful things that can be done using interrupts, below are just three examples that spring to mind,

One example is reading a rotary encoder, these units have continuous rotation and output two pulse trains, on any pulse you know the knob has been rotated and that would cause the interrupt then within the ISR you can check to see if pulse train A is leading or lagging pulse train B and dependent on the result you can then alter a variable value either UP or DOWN depending on the distance the knob is rotated, this value can then be used in the main program for what ever purpose you require but reading a device like this is something that's not at all easy normally without stopping all other program activity and concentrating on reading the device and that's not practical in many applications,

Another example for the use of interrupts is to program one of the timers on the Nano to cause the interrupt to occur on a regular basis such as every 10ms, then within the ISR you can update a multiplexed multi digit LED display for example and drive one digit on each interrupt setting each digits segments up according to the data sent from the main program, if the interrupt is set fast enough the LED's are updated so quick that it looks like a solid display because of the human persistence of vision, in your main program you just set up the variables with the value you want to display and the ISR does all the hard work, if this is done correctly you get a solid flicker free display that's not possible in most cases without interrupts, if you try and update the display within the main program you have to stop updating on many blocking commands such as software serial communications, pause commands etc and this often leads to significant flicker on the display,

Its also possible to set up interrupts from serial inputs and outputs so for example a Nano to Nano communications interface can run in the background so if for example one Nano was reading analogue and digital inputs if can then pass the results on to a second Nano for storage or display, the communications can be checked using a CRC check to ensure the data received is correct,

Basic Micro do support interrupts on the Atom PRO range and there are BASIC commands to allow an interrupt to be set up and serviced with relative ease however the more basic Nano range does not support them in BASIC however if you know what to do then it can still be set up to use interrupts without too much trouble,

Here at BasicMicro UK as well as selling the Basic Micro range of chips and hardware we actively use the Basic Micro products in a number of our products and applications in industrial control and for that we often use the Nano range and implement interrupts on a regular basis with great results,

I have shown an example below of a simple program you can use on the Nano-18 and above to detect a pulse on the P0 port using interrupts,

;     Nano Interrupts on P0 Example
P0Int_Count     var     BYTE         ; Set up a BYTE variable for the interrupt count and clear it
P0Int_Count = 0                       

INTEDG = 1                                ; Set the edge the interrupt will occur on, 1=Rising Edge and 0=Falling Edge   
INTE = 1                                    ; Enable the Port 0 interrupt

PEIE = 1                                     ; Enable the peripheral interrupts
GIE = 1                                      ; Global interrupt enable


     pause 1000                            ; pause for one second
     serout s_out, i9600, ["Interrupts on P0=",DEC P0Int_Count,13] ; Print to number of interrupts that have occurred
     pause 1000                            ; pause for one second
goto     main


israsm{                                                        ; This is where the program will come to when an interrupts occurs

      banksel P0Int_Count&0x1FF                      ; set up to point to the variable to store the interrupt count in
     incf P0Int_Count&0x7f,F                           ; increment the counter by one every interrupt 
      banksel 0                                                ; restore the bank pointers
      bcf  INTCON,INTF                                    ; Clear the P0 interrupt flag to allow it to be triggered again
}                                                                 ; leave the interrupt routine and continue where it was before the interrupt 

In the example an interrupt is enabled and in this case its set to cause an interrupt on the rising edge of the signal on P0

In the main program there is a loop that just pauses for a second and then prints out the number of rising edge interrupts on P0 and then pauses for a second again before looping round continuously, the pause command is a blocking command so while the micro controller is in that command waiting no other activity can occur EXCEPT the interrupts are still running so if while your in the pause 1000 command for that second 10 pulses have come in on P0 the P0Int_Count will be showing 10 higher than before,

You mention you want to trigger another port pin on the interrupt so you could for example add the following code to the bottom of the interrupt service routine before the banksel 0 line :-
            banksel    PORTB                 ; Select PortB(Nano P0 to P7 Pins)
            bsf   PORTB,1               ; Set PortB Bit 1(Nano pin P1 to a HIGH state
Now when you first enter the interrupt Nano P1 would be set HIGH, for this to work you need to add a LOW P0 in the main program before the main loop, if you wanted to make the port pin P1 LOW on interrupt then change the bsf command to a bcf,

If you were to test the state of the PortB pins at entry to the interrupt you could for example make it toggle the state of the Pin P1 every time the interrupt is called by a pulse input on P0,

As far as where to find more information on the PIC Micro Controllers that form the heart of the Nano Range you can download the manuals for the various devices right from the HELP section of the Basic Micro IDE, its heavy going but well worth a read if you want to get the most from your Nano devices,

We are able to offer hardware and software help and support to customers who have purchased hardware from BasicMicroUK,

I hope this helps you getting more out of your Nano Micro controllers,

Many Thanks

01623 720730

 Post subject: Re: Using registers to set up interrupts on nano
PostPosted: Fri Oct 25, 2013 3:37 pm 

Joined: Mon Dec 21, 2009 6:52 am
Posts: 92
Hi Dave,

I just saw your post now.
Your help will be very helpful.
I know now there is a BasicmicroUK.

Thanks a lot


 Post subject: Re: Using registers to set up interrupts on nano
PostPosted: Wed Jan 13, 2016 6:01 pm 

Joined: Fri Dec 07, 2012 3:48 pm
Posts: 67
Very old topic, but I have only recently "discovered" the need for interrupts. Thanks to Dave for the sample code which works fine on the Nano18, but has anybody managed to get this working on a Nano8? As far as I can figure out, the sample code should work as-is on a Nano8 (or am I wrong?), but ISRASM does not seem to be invoked at all - I get ""Interrupts on P0=0" all the time. I have scoured the Include Libraries and the Register Addresses seem to be correct. I have also compared the ASM output for a Nano8 and Nano18 and I can see nothing obviously wrong. Any ideas, anybody?

Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]

Who is online

Users browsing this forum: No registered users and 0 guests

You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group