Simple stupid questions

Discuss the Firewing language

Simple stupid questions

Postby Timbo » Wed Feb 25, 2015 3:19 pm

Hi,

Sorry I have some simple stupid questions

1 Testing a bit
I need to test a bit, looking around I think you can just use the alias eg x = (D12)

But if use
dim PulseState as boolean
PulseState = (D12)

I get an error message "Incompatible types"

2 I have a lot of my background operations running in an interrupt, eg in this instance I want to check an input every 100hz to test for a state change
I have the interrupt code, but as its a module Its not how I would normally write my code.
Is there a "proper" way to manage interrupt code?

Sorry its muddled question. I will have to come back with more on it once I get to grips with the basic stuff
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Simple stupid questions

Postby Timbo » Wed Feb 25, 2015 3:30 pm

Ok I see that I cannot use Boolean in that way Bit is better
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Simple stupid questions

Postby Jerry Messina » Wed Feb 25, 2015 3:51 pm

As you noticed, a 'bit' and a 'boolean' are different types.

D12 is a port bit, while your variable PulseState is declared as a boolean.

You could say:

if (D12 = 1) then
....

or using a typecast

PulseState = CBool(D12)
if (PulseState) then

or declare PulseState using

dim PulseState as boolean = CBool(D12)
if (PulseState) then


For your other question, it's hard to answer w/out a better idea of what you're after, but either:
1 - declare your own module w/the ISR and include all your code
or
2 - use an Event to link the ISR w/ your code. The ISR invokes the Event, and the Event handler contains your code
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am

Re: Simple stupid questions

Postby David John Barker » Wed Feb 25, 2015 4:04 pm

> Ok I see that I cannot use Boolean in that way Bit is better

Yes, a boolean can only be "true" or "false". A bit can only be "1" or "0". Either declare as a bit, or convert to a boolean (typecast)

Code: Select all
PulseState = cbool(D12)


> I have a lot of my background operations running in an interrupt..

OK, write your interrupt as normal. Let's say you want to modify the Tick() module to count milliseconds and trigger every 10ms (100 times a second) - you would have something like this:

Code: Select all
// interrupt handler...
Public Event OnTick()
private Interrupt OnTimer(ipLow)
   _time += 1   
   
   ' testing against nothing will remove all code inside the
   ' if statement if no event handler is assigned in the main code
   ' block - so is optional, but recommended...
   if OnTick <> nothing then       
      if _time mod 10 = 0 then       ' trigger every 10 ms
         RaiseEvent OnTick()         ' raise event
      end if
   end if     

   // reload _timer...
   dim timer as ushort
   timer.Byte0 = TMR3L
   timer.Byte1 = TMR3H
   timer = timer + _reloadTimerValue
   TMR3H = timer.Byte1
   TMR3L = timer.Byte0
   
   // clear interrupt flag
   PIR2.1 = 0
End Interrupt

Note the public declaration "Public Event OnTick()". This is a place holder that will be assigned a subroutine in the main program. Then you have the actual code that will call the external event "RaiseEvent OnTick() ". Now, in your main program :

Code: Select all
imports myTick

sub OnTickEvent() handles Tick.OnTick
   toggle(D13)
end sub

sub Main()
   'loop forever...
   while true
   end while
End Sub

Pretty much as the code says - OnTickEvent() will handle Tick.OnTick(). Put your code inside the event as though it's inside your interrupt (don't forget though, context save rules still apply - i.e. you need to make sure you do not damage registers used by the main program!). Using this technique separates module code, so you can make them generic. Specific code can be handled by the main program. You could add a variable to the module so the event triggers at different frequencies, making it more adaptable. I've attached the resultant waveform. Note that frequency is 50Hz, as the event is triggered 100 times a second. Change the ISR code to

Code: Select all
if _time mod 10 = 0 then


to display a 100Hz waveform (event triggered 200 times a second)
Attachments
output.jpg
output.jpg (92.1 KiB) Viewed 10942 times
User avatar
David John Barker
 
Posts: 491
Joined: Thu Nov 08, 2012 12:21 pm

Re: Simple stupid questions

Postby Timbo » Wed Feb 25, 2015 11:04 pm

Thanks

I think its the "on event" that I will need to use.

I modified the tick module. Renamed it to something that better reflects the task then realised I could no longer use the tick.xx Notation. I had to change it timercounterInt.timer etc. Made it look a right mess!

Good thing is it all worked for the task and I can read up on events at my leasure

One note, I used ISIS to do a VSM and looked the timing of the tick routines. The timing is a little out so used an offset of 12 to add to the reload value I was able to bring back to 1 us out for on a 1hz tick. I could not figure out where the error was coming in. It was without the addition ~340us out. Not a big deal but just thought I would mention it.

Thanks again everyone for the help, I will need to go over the examples and force myself to fully understand the way it works.
Timbo
 
Posts: 93
Joined: Fri May 03, 2013 7:51 pm

Re: Simple stupid questions

Postby Jerry Messina » Thu Feb 26, 2015 11:34 am

>> The timing is a little out so used an offset of 12 to add to the reload value

That would make sense since it takes 12 instruction cycles to update the timer count...
Code: Select all
// reload _timer...
   dim timer as ushort
   timer.Byte0 = TMR3L
   timer.Byte1 = TMR3H
   timer = timer + _reloadTimerValue
   TMR3H = timer.Byte1
   TMR3L = timer.Byte0


At 16MHz, that's ~3us error for the 1KHz TMR3 interrupt, so if you don't correct for the time it takes to reload the timer then
the errors will just keep adding up.

Thanks for pointing that out.
Jerry Messina
 
Posts: 280
Joined: Thu Feb 14, 2013 10:16 am


Return to Language

Who is online

Users browsing this forum: No registered users and 3 guests

x