Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

MikroBasic problem? whats wrong please

Status
Not open for further replies.

bigal_scorpio

Active Member
Hi Guys,

I have been trying for days to write a program to read a pin pin going ground and flashing a corresponding LED on another port.

The program below is about the tenth attempt and still refuses to work.

This final version ignores all but PortB pin 3 and even then just lights the C port pin 3 WHILE the button is held! No flashes!

I am ready to give up and beleive me I don't quit easily.

Any ideas please.

Code:
program carswitch2

'Pic16F874a @ 8mHz on Easypic5 dev board
main:
   DIM t as byte
  TRISA = %00000000
  PORTA = %00000000
  TRISB = %11111111
  PORTB = %00000000
  TRISC = %00000000
  PORTC = %00000000
  TRISD = %00000000
  PORTD = %00000000
  TRISE = %00000000
  PORTE = %00000000
  CMCON =0x07
  ADCON1 = 0xCF



   Checkpress:                                 'check for button pressed
        IF (BUTTON(PORTB,0,1,0))then GOTO one
        END IF
        IF (BUTTON(PORTB,1,1,0))then GOTO two
        END IF
        IF (BUTTON(PORTB,2,1,0))then GOTO three
        END IF
        IF (BUTTON(PORTB,3,1,0))then GOTO four
        END IF
        IF (BUTTON(PORTB,4,1,0))then GOTO five
        END IF
        IF (BUTTON(PORTB,5,1,0))then GOTO six
        END IF
        IF (BUTTON(PORTB,6,1,0))then GOTO seven
        END IF
        IF (BUTTON(PORTB,7,1,0))then GOTO eight
        END IF
        GOTO main


one:
  FOR t = 1 to 3

  PORTC = %00000001
  delay_ms (100)         'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

  GOTO Checkpress

two:
  FOR t = 1 to 3

  PORTC = %00000010
  delay_ms (100)         'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

three:
  FOR t = 1 to 3

  PORTC = %00000100
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

four:
  FOR t = 1 to 3

  PORTC = %00001000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

five:
  FOR t = 1 to 3

  PORTC = %00010000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

six:
  FOR t = 1 to 3

  PORTC = %00100000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

seven:
  FOR t = 1 to 3

  PORTC = %01000000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

eight:
  FOR t = 1 to 3

  PORTC = %10000000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
  NEXT t

GOTO Checkpress

end.

Thanks.............Al :(
 
Last edited:
need a little more delay
Code:
eight:
  FOR t = 1 to 3

  PORTC = %10000000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
[COLOR="Red"] delay_ms (100) [/COLOR]
  NEXT t

GOTO Checkpress
And check your hardware to make sure you buttons are pulled right.

Here how you use button
Code:
Example reads RB0, to which the button is connected; on transition from 
1 to 0 (release of button), PORTD is inverted:

while true
  if Button(PORTB, 0, 1, 1) then
    oldstate = 255
  end if
  if oldstate and Button(PORTB, 0, 1, 0) then
    PORTD = not(PORTD)
    oldstate = 0
  end if
wend

Is J17 on VCC or GND It should be set to GND
 
Last edited:
need a little more delay
Code:
eight:
  FOR t = 1 to 3

  PORTC = %10000000
  delay_ms (100)          'flash corresponding led on port c to press on port b
  PORTC = %00000000
[COLOR="Red"] delay_ms (100) [/COLOR]
  NEXT t

GOTO Checkpress
And check your hardware to make sure you buttons are pulled right.

Here how you use button
Code:
Example reads RB0, to which the button is connected; on transition from 
1 to 0 (release of button), PORTD is inverted:

while true
  if Button(PORTB, 0, 1, 1) then
    oldstate = 255
  end if
  if oldstate and Button(PORTB, 0, 1, 0) then
    PORTD = not(PORTD)
    oldstate = 0
  end if
wend

Hi Burt,

Just tried the extra delay and it does make RC3 flash when I press RB3 - but ONLY while I hold the button down and none of the other Bport pins do anything!

Is there something I should be turning off on port B like AD or such?

I have looked at all the ports in the IO section of the sheet and can't find anything like the ADCON on port A that seems relevant to other ports.

PS switches are grounded and port b is pulled high by the onboard resistors.

Thanks........Al
 
The (for t = 1 to 3) should make it blink three times then go back and check for new press
I would say it's hardware not working
 
The (for t = 1 to 3) should make it blink three times then go back and check for new press
I would say it's hardware not working

Hi Burt,

But what mate?

Why is only RB3 responding, and why does it only flash while I keep the button engaged? I still wonder if there is something that should be disabled for the other ports if ADCON1 only affects PORTA?

I must admit I don't completely follow some of the info in the datasheets and have only ever used smaller PICs before with more success.

Also I know the PIC is ok as I tried an older prog on it that I had used before and that had no problems. And the board is ok with other projectss I have tried! Weird!

Don't suppose anyone out there has the same set up and could try it, I realise its a long shot but I live in hope. :)

Al
 
I don't have a easypic5 but I read the manual for it and theirs a lot of jumpers to set
you need to change this to
Code:
program carswitch2

'Pic16F874a @ 8mHz on Easypic5 dev board
main:
   DIM t as byte
  TRISA = %00000000
  PORTA = %00000000
  TRISB = %11111111
  PORTB = %00000000
  TRISC = %00000000
  PORTC = %00000000
  TRISD = %00000000
  PORTD = %00000000
  TRISE = %00000000
  PORTE = %00000000
  'CMCON =0x07 wrong
  'ADCON1 = 0xCF wrong to should be just this 
   ADCON1 = $06
 
I have no idea about different settings on Easypic5 board. But here is what I got from Mikro website. Did you try this?

' * Project name:
' Button_Test
' * Copyright:
' (c) Mikroelektronika, 2005.
' * Description:
' This code demonstrates how to use Button library. Program toggles LEDs
' on PORTD, upon applying high voltage level on PORTB's RB1 pin.
' * Test configuration:
' MCU: PIC16F877A
' Dev.Board: EasyPIC5
' Oscillator: HS, 08.0000 MHz
' Ext. Modules: -
' SW: mikroPascal v5.0 or higher
' * NOTES:
' - In order to work properly, in this example, ports B and D must have pull down
' resistors, also jumper jp20 on EasyPIC5 board has to be in VCC position (that is,
' the voltage level to be applied when a button is pressed should be high (VCC)).

program Button_Test

dim oldstate as byte

amin:
oldstate = 0
TRISB = 0xFF
TRISD = 0
PORTD = 0x0F

while true
if (Button(PORTB, 1, 1, 1)) then
oldstate = 1
end if

if (oldstate and Button(PORTB, 1, 1, 0)) then
PORTD = not PORTD
oldstate = 0
end if
wend

end.

- Raj
Experiments with PIC16F628A
 
I don't have a easypic5 but I read the manual for it and theirs a lot of jumpers to set
you need to change this to
Code:
program carswitch2

'Pic16F874a @ 8mHz on Easypic5 dev board
main:
   DIM t as byte
  TRISA = %00000000
  PORTA = %00000000
  TRISB = %11111111
  PORTB = %00000000
  TRISC = %00000000
  PORTC = %00000000
  TRISD = %00000000
  PORTD = %00000000
  TRISE = %00000000
  PORTE = %00000000
  'CMCON =0x07 wrong
  'ADCON1 = 0xCF wrong to should be just this 
   ADCON1 = $06

Hi again Burt,

Tried the changes but still no joy, but another member says he can try the code for me tomorrow, so if no joy tonight I will see how he gets on.

Al
 
I have no idea about different settings on Easypic5 board. But here is what I got from Mikro website. Did you try this?

' * Project name:
' Button_Test
' * Copyright:
' (c) Mikroelektronika, 2005.
' * Description:
' This code demonstrates how to use Button library. Program toggles LEDs
' on PORTD, upon applying high voltage level on PORTB's RB1 pin.
' * Test configuration:
' MCU: PIC16F877A
' Dev.Board: EasyPIC5
' Oscillator: HS, 08.0000 MHz
' Ext. Modules: -
' SW: mikroPascal v5.0 or higher
' * NOTES:
' - In order to work properly, in this example, ports B and D must have pull down
' resistors, also jumper jp20 on EasyPIC5 board has to be in VCC position (that is,
' the voltage level to be applied when a button is pressed should be high (VCC)).

program Button_Test

dim oldstate as byte

amin:
oldstate = 0
TRISB = 0xFF
TRISD = 0
PORTD = 0x0F

while true
if (Button(PORTB, 1, 1, 1)) then
oldstate = 1
end if

if (oldstate and Button(PORTB, 1, 1, 0)) then
PORTD = not PORTD
oldstate = 0
end if
wend

end.

- Raj
Experiments with PIC16F628A

Hi Raj,

I just tried the code you posted and it does indeed work, but I find the button command very confusing and the While command even more so, and cannot figure out how I would use this method to read every pin on a port and say toggle a corresponding pin on another port.

There are never enough examples in the Mikro texts and those that are there are not so easy to understand as they think.

Bear in mind that the last time I programmed it was with Commodore 64 basic back in the 80s, then it was said that C64 basic was hard to understand but the manual was much better and I could write a program in minutes to do just about anything! I must be getting too old to take in new things! ;) hehe So any help is welcome.

Regards.........Al
 
This is not how your code is Yours is testing for high to low transition
' - In order to work properly, in this example, ports B and D must have pull down
' resistors, also jumper jp20 on EasyPIC5 board has to be in VCC position (that is,
' the voltage level to be applied when a button is pressed should be high (VCC)).
you said it works that's good
The reason your code is not working is the board is not jumper right
 
Last edited:
This is not how your code is Yours is testing for high to low transition

you said it works that's good
The reason your code is not working is the board is not jumper right

Hi Burt,

Yeah, when I said it worked I meant it worked when I'd reconfigured the jumpers to suit.

I do have the jumpers set right on the board to detect a high to low when I'm running the code I wrote. I have had a mate who is really good with electronics here yesterday and he said all the jumpers seemed right to him but sadly he has no knowledge or interest in programming. :(

I am still thinking that there is something in the config part that could be wrong, maybe some feature that needs to be turned off that I am unaware of? I am baffled. The reason I think config may be the problem is that there are more peripherals on the 16F874a than I have ever used before and I cannot get my head round the IO part of the datasheet, the only thing that it actually tells me how to turn off is the part you pointed out with ADCON, all the other parts give NO examples of how to turn off the special features!

Still baffled.........Al
 
Al,
I have not seen a Easypic5 board, but I just read the manual from Mikro website. My understanding is if the code above works then you should be able to change it to make it work for 1 to 0 transition with little bit change in jumper settings. But it does not matter, you can implement 0 to 1 transition in your code too. Just keep the settings as it is in the working code, and try this and see what happens:
program Button_Test

dim oldstate as byte
dim t as byte
amin:
oldstate = 0
TRISB = 0xFF
TRISD = 0
PORTD = 0x00

while true

' For Button on RB.0

if (Button(PORTB, 0, 1, 1)) then
oldstate = 1
end if

if (oldstate and Button(PORTB, 0, 1, 0)) then
for t = 1 to 3
RD0_bit = 1
delay_ms(400)
RD0_bit = 0
delay_ms(400)
next t
oldstate = 0
end if

' For Button on RB.1
if (Button(PORTB, 1, 1, 1)) then
oldstate = 1
end if

if (oldstate and Button(PORTB, 1, 1, 0)) then
for t = 1 to 3
RD1_bit = 1
delay_ms(400)
RD1_bit = 0
delay_ms(400)
next t
oldstate = 0
end if

wend

end.


I don't know Basic programming, so correct any wrong syntaxes. I use mikroC for PIC programming, and I have a working code on my PIC blog for reading push buttons. But the code is not for EasyPic5, I have made my own experiment board. If you want to see, check Experiments with PIC16F628A

- Raj
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top