Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

[SOLVED] Need Help: MikroBasic variable splitting

Status
Not open for further replies.

biboymusic

Junior Member level 1
Joined
Jan 7, 2011
Messages
16
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,430
Need Help: MikroBasic variable splitting / Interrupt

Hi,

Can anyone teach me how to split a variable?

Here's the scenario. I want to calculate something and the result ex. 135 is stored on a certain variable (myvar = 135). Now, how do I split each character onto single variables? Like, var1 = 1, var2 = 3, var3 = 5. The splitted variables should be as integer or anything in which I could calculate with them.

Splitted variables are best be "as byte" or "as integer". Please include also declarations so that I would know what type of variable I have to use (word, string, integer etc.)

Cheers!
 
Last edited:

Hi,
You do it like this:
Code:
dim a1 as byte
dim a2 as byte
dim a3 as byte
dim d as byte
d = 135
a1 = d div 100 'a1 = 1
a2 = (d div 10) mod 10 'a2 = 3
a3 = d mod 10 'a3 = 5
Hope this helps.
Tahmid.
 
Last edited:
Hey Tahmid,

Is this correct? seems like "d" is not declared. You just set the value of "x" and you stopped using it. Or a typo maybe?

Thanks.

---------- Post added at 08:51 ---------- Previous post was at 08:25 ----------

Can you also help me start with interrupts (using old PIC16F84A with 4MHz XTAL, plenty lying around, and MikroBasic)? I don't know what to do with OPTION_REG etc.

Just simple example that should get me started.

1. On start-up Timer starts and overflows every 1 sec and does "counter = counter +1".
2. On every RB0 interrupt does "freq_count = freq_count + 1".

I also don't know what to set for RB0. Should it be TRISB.0 as input or output? Is it affected if I use PORTB.0 = 0 or 1? Or do I lack any component on the circuit board for RB0?

I've tried every example I could find but still got no response on each RB0 interrupt (grounding this pin and releasing it, got a 10K pullup).
I did my own interrupt routines by using RA0, catching each logic changes like a button, it worked and pretty accurate too. But its very awkward doing a 1 sec overflow manually and I want to use the built-in interrupt.

The variable splitting is related to this. Each calculation result is split on different variable so that I could display them on 3 multiplexed 7-segment. Everything's fine except for the splitting of variables (which will be solved once I got your confirmation on your post) and trying to do this on RB0 interrupt.
 
Last edited:

Hi,
It seems fine now. I messed up x and d. Anyway try the code now.
I'll help with the code.
 

Hey man,

I've tried before reading your reply, assuming that you did a typo and it worked.

However will it also worked if the result is ex. 24 or just 7? It should display 007 or 024 (3 multiplexed 7-segments), never tried it yet.

Also in my calculation im having strange results, take a look:

Code:
dim counter as integer
dim timerx as integer
dim temp as integer
dim speed as byte
dim ox as byte                               'displays Ones
dim tx as byte                                'displays Tens
dim hx as byte                                'displays hundreds

if timerx = 3096 then
   temp = (counter * 158) / 100        'each rotation is 158cm = 1.58m
   temp = (temp * 60) * 60              'to get Km/h
   temp = temp div 1000                  '1000m = 1km
   speed = temp

   timerx = 0
   counter = 0
   temp = 0
end if

hx = speed div 100                    'hundreds
tx = (speed div 10) mod 10         'tens
ox = speed mod 10                    'ones

Strangely I get, 200+ on the display sometimes by just using a push button as an input. I don't think finger tapping can reach up to 200Km/h.

Here's how I catch each input:

Code:
dim counter as integer
dim bounce as byte

bounce = 1

While (TRUE)
   if PORTA.0 = 0 then
     if bounce = 1 then
        counter = counter + 1
        bounce = 0
     end if
   end if
   if PORTA.0 = 1 then
      bounce = 1
   end if
Wend

Im pretty sure my input tracking works because if I make the 7-segments display my tapping count, it is accurate. It doesn't count until I release the button. However if I pass it through my calculation above I get strange results.

Please take note that I'm not using internal interrupt routine on my current design. We'll get to it later once I fix this calculation thing.

Thanks and more power.
 

Hi,
The method should work with any value between 0 to 255. To make it work between 0 to 999, change a1,a2 and a3 data types to word.
eg. 24 displays 024
7 displays 007
129 displays 129

Try with this bit of code and let me know:
Code:
dim counter as integer
dim timerx as integer
dim temp as longword
dim speed as byte
dim ox as byte                               'displays Ones
dim tx as byte                                'displays Tens
dim hx as byte                                'displays hundreds

if timerx = 3096 then
   temp = counter * 158 * 60 * 60       'each rotation is 158cm = 1.58m            'to get Km/h
   temp = temp div 10000                  '1000m = 1km
   speed = temp

   timerx = 0
   counter = 0
   temp = 0
end if

hx = speed div 100                    'hundreds
tx = (speed div 10) mod 10         'tens
ox = speed mod 10

Hope this helps.
Tahmid.
 

Hi,

It worked. Now here comes reliability issues. It seems that my 1sec overflow is not constant, since I'm not using interrupt.

Can you help me convert this into interrupt?

How do I get started?

Here's my plan:

Code:
dim counter as integer
dim temp as longword
dim speed as byte
dim hx as byte
dim tx as byte
dim ox as byte

sub procedure interrupt
   if RB0 = interrupt then             'Don't know how to express the RB0 interrupt
      counter = counter + 1
   end if
   if Overflow = true then             'The overflow interrupt logic.
      temp = counter * 158 * 3600 
      temp = temp div 10000
      speed = temp
      hx = speed div 100                    'hundreds
      tx = (speed div 10) mod 10         'tens
      ox = speed mod 10      
      'reset counter
      counter = 0
   end if
   'It should reset TMR0 and Overflow timer here. I don't know how.
end sub

sub function Mask(dim num as byte) as byte    ' this function returns masks
  select case num                             ' for common cathode 7-seg. display
    case 0  result = 252
    case 1  result = 48
    case 2  result = 218
    case 3  result = 122
    case 4  result = 54                       '00110110
    case 5  result = 110                      '01101110
    case 6  result = 238                      '11101110
    case 7  result = 56                       '00111000
    case 8  result = 254                      '11111110
    case 9  result = 126                      '01111110
  end select                                  'case end
end sub

main:
      *on this part I don't know which OPTION_REG or TMR0 = 96 is correct. 
      *I cant even understand how they work. So please help me here.
      *It should start TMR0 here also
    TRISB = %00000000
    TRISA = %00001         'Im reserving PORTA.0 as input for additional function
    PORTB = 0
    'Start-up routine (displays 123 on 7-segment on startup)
    PORTA = %01101
    PORTB = Mask(1)
    Delay_ms(500)
    PORTA = %01011
    PORTB = Mask(2)
    Delay_ms(500)
    PORTA = %00111
    PORTB = Mask(3)
    Delay_ms(500)
    PORTA = %01111
   
    'Start of Loop
    While (TRUE)
        PORTA = %01101
        PORTB = Mask(hx)
        delay_ms(5)
        PORTA = %01011
        PORTB = Mask(tx)
        delay_ms(5)
        PORTA = %00111
        PORTB = Mask(ox)
        delay_ms(5)
    Wend
end

Its just a concept program. Will this be ok? Can you help me start on interrupts? how to enable it? how to get it to overflow every 1 sec? how do I enable RB0 interrupt?

Don't forget im on PIC16f84A on a 4MHz XTAL.

Thank you.
 

Hi,
I'll check the code and help you on the interrupts. I'll get back to you tomorrow.
 

Hey Tahmid,

Never mind the interrupt thing, I got it working flawlessly. Read up the datasheet and all that is in there LoL. Thanks.
However, the speed reading is very low resolution. Like you get a minimum of 5km/h then next would be 10, 15 on a 2 sec refresh. On a 1 sec refresh you get a resolution of by 10kmh which is awful.

How about measuring time in between pulses then calculate it to km/h? Is it possible?
I guess this would give a very resolution since display is update on every 2 pulses (interrupt handled).
I want to have an analog feel on the speed reading, like you see the speed rise as you go faster. Not that lagging refresh rate.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top