Sonsivri
 
*
Welcome, Guest. Please login or register.
Did you miss your activation email?
May 18, 2024, 05:47:58 17:47


Login with username, password and session length


Pages: [1]
Print
Author Topic: MikroBasic coding question  (Read 6680 times)
0 Members and 1 Guest are viewing this topic.
suresh13
Guest
« on: May 09, 2006, 11:19:44 11:19 »

Please check this (mikrobasic) code and see if there is any mistakes:


sub procedure led_blink
PORTD.0 = 1
delay_ms(1000)
PORTD.0 = 0
delay_ms(500)
end sub

main:                      
ADCON1 = %00000111 (makes porta digital)
PORTA = 0                     (clears port a)
TRISA = $FF                   (makes porta as input)
PORTD = 0                     (clear port d)
TRISD = 0                      (port D is output)


dim counter as byte
counter = 0

while 1=1
if porta.0 = 1 then
  counter = counter + 1
end if
if porta.1 = 1 then
  goto next
end if
wend

next:
 'in this part of the program, i need it to loop the sub procedure led_blink with the value of counter. Ex: if counter is 3, then led_blink will execute 3 times. Any ideas guys?


Also, the code:

if porta.0 = 1 then
  counter = counter + 1
end if

is too simple i think, cuz i may press the switch once, but maybe there is noise and the pic reads it as 100 presses??? how do i overcome that too???

the switches are pull-down when not pressed and pull-up when pressed.


thanks in advance guys!
Logged
hbruno
Junior Member
**
Offline Offline

Posts: 41

Thank You
-Given: 112
-Receive: 75


« Reply #1 on: May 09, 2006, 09:29:34 21:29 »

sub procedure led_blink(dim count as byte)
dim loop as byte
    for loop = 1 to count
       PORTD.0 = 1 delay_ms(1000)
       PORTD.0 = 0 delay_ms(500)
    next loop
end sub

if porta.1 = 1 then led_blink(counter) end if

 
if porta.0 = 1 then
counter = counter + 1
end if
=> BAD
 
if Button(PORTA, 0, 10, 1) then inc(counter) end if
=> GOOD
 
Works with MikroBasic 4.003
 
Salut.
Logged
suresh13
Guest
« Reply #2 on: May 10, 2006, 06:37:39 06:37 »

thanks dude!

one more thing though

what if i wanted to display the value of counter in a lcd, how would it be done? my lcd is 16x2, using the 4-bit lcd library.
Logged
hbruno
Junior Member
**
Offline Offline

Posts: 41

Thank You
-Given: 112
-Receive: 75


« Reply #3 on: May 10, 2006, 09:49:12 21:49 »

'LCD 4-bit on PORTB
'D7 -> port.7
'D6 -> port.6
'D5 -> port.5
'D4 -> port.4
'E  -> port.3
'RS -> port.2
'R/W -> port.0

  Lcd_Init(PORTB)                        ' Initialize LCD connected to PORTB
  Lcd_Cmd(LCD_CLEAR)               ' Send command to LCD "clear display"
  ByteToStr(Counter, Counter_str) ' Byte -> String[3]
  Lcd_Out(1,4,"Counter")                ' Print txt to LCD, 1st row, 4th column
  Lcd_Out(2,1,Counter_str)            ' Print txt to LCD, 2nd row, 1st column

Salut
Logged
suresh13
Guest
« Reply #4 on: May 15, 2006, 06:23:13 06:23 »

hi hbruno. the code is working except for one part.

if porta.1 = 1 then led_blink(counter) end if

i'm getting an error here.

i put dim counter as byte in the beginning.

LCD display is ok.
counter incrementing is ok also.
just this part.
Logged
Pages: [1]
Print
Jump to:  


DISCLAIMER
WE DONT HOST ANY ILLEGAL FILES ON THE SERVER
USE CONTACT US TO REPORT ILLEGAL FILES
ADMINISTRATORS CANNOT BE HELD RESPONSIBLE FOR USERS POSTS AND LINKS

... Copyright © 2003-2999 Sonsivri.to ...
Powered by SMF 1.1.18 | SMF © 2006-2009, Simple Machines LLC | HarzeM Dilber MC