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.

what am I doin' wrong?

Status
Not open for further replies.
Damn it's broken..lol

What is your Oscillator setting? XT, HT, Etc ??

-BaC
I hate to say it, but still nothing...
 
Last edited:
Osc set as HS...
If it aint broke now it might be when I get done with it..
 
Last edited:
Save all and close the project, start fresh with Project, New. Use this example from ME as your template;

Code:
' * Project name:
'     Button_Test
' * Copyright:
'     (c) Mikroelektronika, 2005.
' * Description:
'     This code demonstrates how to use Button library. Program toggles LEDs
'     on PORTD, upon falling edge on PORTB's RB1 pin.
' * Test configuration:
'     MCU:             PIC16F877A
'     Dev.Board:       EasyPic4
'     Oscillator:      HS, 08.0000 MHz
'     Ext. Modules:    -
'     SW:              mikroBasic v6.0
' * NOTES:
'     - In order to work properly, in this example, ports B and D must have pull down
'       resistors, also jumper jp20 on EasyPic4 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

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

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

-BaC
 
Last edited:
Code was written for 16F877a and I'll have to wait til' monday before I go back to work to get one. Meanwhile I tried to change portd to porta (since the 16F628 doesn't have a portd)
and that didn't work. the only thing that happened was leds ra0 to ra3 lit up. Jumpers were all in the right place. I also have a Easy Pic 5 at work and I'll swap to that platform...
 
It can be modified to fit, but let's wait till you have that 877A so we can rule out board setup and such.

-BaC
Let me know when your ready to proceed.
Code was written for 16F877a and I'll have to wait til' monday before I go back to work to get one. Meanwhile I tried to change portd to porta (since the 16F628 doesn't have a portd)
and that didn't work. the only thing that happened was leds ra0 to ra3 lit up. Jumpers were all in the right place. I also have a Easy Pic 5 at work and I'll swap to that platform...
 
The last 2 test programs on page 3 of this mess work. I tried both boards EP 4&5 and they reacted the same. Post #10 on page 3 makes a led come on and latches it on with 628.
Post # 14 page 3 toggles leds on/off on portd with the 877. I don't know whats different now from the other night? I tried the code from the previous posts and that still didn't work.
All that said ,it appears the EP 4 is ok, my comprehension skills on the other hand?????
 
Go through the working code examples, and for each line see what they do. I would be happy to go through it with you line-by-line if needed.

Glad it is working now;)
-BaC
The last 2 test programs on page 3 of this mess work. I tried both boards EP 4&5 and they reacted the same. Post #10 on page 3 makes a led come on and latches it on with 628.
Post # 14 page 3 toggles leds on/off on portd with the 877. I don't know whats different now from the other night? I tried the code from the previous posts and that still didn't work.
All that said ,it appears the EP 4 is ok, my comprehension skills on the other hand?????
 
You need to first use the TRIS command to initialize the direction Input/Output. Think of the ports as a block of 8 bytes/Bits, called a word. Each %00000000 is a port/pin (For the translation from PortsA/B/C to actual PIN numbers, please see the data-sheet for the PIC type your working with). PICs work on MSBF (Most Significant Byte First) % <--tells the compiler the next sections are in Binary notation(you can use Hex or Dec notation but to better understand I do recommend starting with Binary since you can see the visual pattern much easier).

MSBF Means the highest port pin first. So TRISA <--Set states port A TRISA = %<--ports in binary notation, ports 7 6 5 4 3 2 1 0 on Port A. So if we want to set PortA.0 to inputs
Code:
TRISA = %00000001
If we want PortB.4,5,7 inputs that would be
Code:
TRISB = %10110000
.

If you want to set the pin High (1,) or Low (0) you use the PortA/B/C/D/E command again MSBF, set pins 5,2 high leaving 0,1,3,4,6,7 Low, Your code would be
Code:
 PortB = %00100100

Hope that helps at least a little;)

-BaC
The last 2 test programs on page 3 of this mess work. I tried both boards EP 4&5 and they reacted the same. Post #10 on page 3 makes a led come on and latches it on with 628.
Post # 14 page 3 toggles leds on/off on portd with the 877. I don't know whats different now from the other night? I tried the code from the previous posts and that still didn't work.
All that said ,it appears the EP 4 is ok, my comprehension skills on the other hand?????
 
Last edited:
this is my version of what's going on,my comments in quotes.
Correct me where I'm wrong:
Thanks again...

Code:
   program Button_Test

dim oldstate as byte

main:
  oldstate = 0                   "don't know"
  TRISB    = 0xFF               "tri state on port b ?" 
  TRISD    = 0                   " portd as output"
  PORTD    = 0x0F                " portd  off?  "

  while true
    if (Button(PORTB, 1, 1, 1)) then            "portb.1, input high?,  no clue
      oldstate = 1                                     " update oldstate to high"
    end if
      
    if (oldstate = 1) and (Button(PORTB, 1, 1, 0)) then    "if high, button portb.1, high?, ?  
      PORTD    = not PORTD                                         " portd = low?
      oldstate = 0
    end if
  wend
end

Posted before seeing your last post.
 
Last edited:
As per your last post, I think I have that part under control, as far as the binary..
Still having problems with the distinction between port and tris terminology.
 
TRIS tells the uC if the pin is used for input or output.

PORT is what you READ to determine if input is HIGH or LOW
but can be used to set the pin to a HIGH or LOW state if it is set to output.
 
Last edited:
Yep, as stated below, setting TRIS 0x0F is hex notation, 0F=1111 in binary. So this would set ports 7,6,5,4 to inputs, I believe, or is it 0,1,2,3? I always use Binary, and a full word value, so I am unsure of this.

FYI:ME's compilers have a converter for binary, hex, dec conversion.

-BaC
Not sure about above code but:

PORTD = 0x0F " portd off? " Actually 0x00 would be off
 
it would set 0,1,2,3 to HIGH

This does not set it as input.

To set 0,1,2,3 to input you would use TRIS:
TRISD = 0x0F

PORTD = 0x0F would work only if the 0,1,2,3 pins on PORTD are set to output.

So lets say you have LEDs on pins 0,1,2,3 so thats 4 leds. If you tie 1 side to GND and the other to each pin when you initiate a PORTD = 0x0F command it will turn all 4 on.

Have a better understanding?

(if you try the above dont forget to at least put a resistor on each LED for safety)
 
Last edited:
It's starting to clear up..In the code 6 posts up what do the 3 "1" signify:

if (Button(PORTB, 1, 1, 1)) then
 
Help Files are very useful:

Prototype:
sub function Button(dim byref port as byte, dim pin, time, active_state as byte) as byte

Returns:
Returns 0 or 255.

Requires:
Button pin must be configured as input

Example:
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

OOPS! forgot this:

DESCRIPTION
Function eliminates the influence of contact flickering upon pressing a button (debouncing).

Parameter port specifies the location of the button; parameter pin is the pin number on designated port and goes from 0..7; parameter time is a debounce period in milliseconds; parameter active_state can be either 0 or 1, and it determines if the button is active upon logical zero or logical one.

Your answer:

Check PortB, pin 1, 1ms debounce,1 active when high

So if RB1 = 1 (delay 1ms so wont get false input) if its 1 then do code
 
Last edited:
I agree !! Help files very useful ..But when you don't understand the terminology it's hard to look something up..
 
I'm finally getting there!!
This works, all leds flash while button held down
Thanks for the help


Code:
   program bt
CMCON = 7
trisa =  0x0f
trisb = 0

 if button(porta, 0, 1, 1) then           ' if button pressed

portb = %11111111                         ' turn on all leds
delay_ms(1000)                            'wait 1 sec
portb = %00000000                         ' turn off all leds
delay_ms(1000)                             'wait 1 sec
 end if

end.
 
Fantastic!

Congratulations! Now it becomes addictive..haha

Remember everyone started as a beginner;)

-BaC
I'm finally getting there!!
This works, all leds flash while button held down
Thanks for the help


Code:
   program bt
CMCON = 7
trisa =  0x0f
trisb = 0

 if button(porta, 0, 1, 1) then           ' if button pressed

portb = %11111111                         ' turn on all leds
delay_ms(1000)                            'wait 1 sec
portb = %00000000                         ' turn off all leds
delay_ms(1000)                             'wait 1 sec
 end if

end.
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top