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.

unhappy with code I wrote. It works BUT??

Status
Not open for further replies.
Well back at this project. Have an assembled circuit that needs to be tested but thinking of adding a mosfet or transistor to enable the cathodes of all ten leds.
REASON for considering adding a mosfet or transistor is to pulse (ON/OFF) the enabled leds (each led is 10ma so maximum is 100ma). Toggling after each port scann with a 100ms delay.This should extend battery life. Thought about going with PWM but want to keep things simple.
Any thoughts on my direction?? Yes I need to remove un-nessary code as suggested.
Using three AA batteries for power and an 18lf4520 pic. So a AA battery has 2000mah dividing in half for redundancy so 1000mah thus the batteries would last 7 hours at 100ma current draw. I realize these figures are extreme but why not plan for worst case. I may be wrong in my calculations but ??
Thus my reasoning for toggeling the leds, save on current consumption hopefully.


{
*****************************************************************************
* Name : testing routine.BAS *
* Author : [select VIEW...EDITOR OPTIONS] *
* Notice : Copyright (c) 2014 [select VIEW...EDITOR OPTIONS] *
* : All Rights Reserved *
* Date : 2/4/2014 *
* Version : 1.0 *
* Notes : mexican train routine *
* : *
*****************************************************************************
}
DEVICE = 18F4520 ' Tell the compiler what chip we are using
CLOCK = 8 ' Tell the compiler what we will be setting the clock to (Mhz)
CONFIG MCLRE = OFF

INCLUDE "SetDigitalIO.bas" // Include this file when we compile so that we can use keywords like 'setalldigital'
INCLUDE "utils.bas"

// variable declaration
DIM Swt_B(6)AS BYTE
DIM Swt_D(3)AS BYTE
DIM Led_C(6) AS BYTE
DIM Led_D(3) AS BYTE
DIM X AS BYTE
DIM y AS BYTE


// Start Of Program
SetAllDigital // Make all Pins digital I/O's
OSCCON = %01100000 ' Internal oscillator running at 8mHz



TRISB= %00000000 ' PORTA outputs
TRISC= %00000000 ' PORTB outputs
TRISD= %00000000 ' PORTC outputs


//WE ARE SCANNING THE BUTTONS AND CHECKING LED STAte
// Main Loop
WHILE True()
FOR X = 0 TO 6 //scan ports B and C

//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//SCAN portB and C
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
Swt_B(X)=PORTB.BITS(X) //switches on portB (7 BITS) pins 15-18 & 23-25
Led_C(X) = PORTC.bits(X) //leds on portC (7BITS) pins 33-39

IF Swt_B(X)=1 AND Led_C(X) = 0 'start routine
THEN
Led_D(X)=0
END IF

IF Swt_B(X)=0 AND Led_C(X)=0 'button press with led off
THEN
Led_D(X)=1
DELAYMS(200) 'debounce
END IF

IF Led_C(X)=1 AND Swt_B(X)=1 'verify if led ON and no button press
THEN
Led_D(X)=1
END IF

IF Led_C(X) =1 AND Swt_B(X) =0
THEN Led_D(X) =0
END IF
NEXT
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
//scan portD ' NOTE I used Y+3 to use portD.3 to portD.5
//xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
FOR y = 0 TO 2
Swt_D(y) = PORTD.BITS(y) //switches on portD 3BITS pins 19-21 PORTD.0 to portD.2
Led_D(y+3)=PORTD.bits(y+3) //leds on portD 3BITS pins 22,23,28 PORTD.3 to PORTD.5
IF Swt_D(y) =1 AND Led_D(y+3) = 0 'start routine verify swt state and led state
THEN
Led_D(y+3)=0
END IF

IF Swt_D(y)=0 AND Led_D(y+3)=0 'button press with led off
THEN
Led_D(y+3)=1
DELAYMS(200) 'debounce
END IF

IF Led_D(y+3)=1 AND Swt_D(y)=1 'verify if led ON and no button press
THEN
Led_D(y+3)=1
END IF

IF Led_D(y+3) =1 AND Swt_D(y) =0
THEN
Led_D(y+3) =0
END IF
NEXT
WEND
 
I *think* what you're saying is that you want to pulse the LEDs at higher currents to save battery power?

Sorry, multiplexing doesn't work that way. The purpose of multiplexing is to save on the number of connections and port pins.

An LED driven at 10mA will appear exactly the same brightness as one driven at 100mA with a 10% duty cycle. The average current determines the perceived brightness.
 
As I understand it, if I pulse the leds (10 ma current draw) with an ON time of 500ms and OFF at 500ms the total current draw over a given time span is cut in half since the led is on only half the time compared to on constantly.
Is this not correct??It is a crude method of reducing amount of battery drain as I understand it using POV
 
oups here is my basic schematic
mosfet setup.jpg
 
That is correct. And the apparent brightness is reduced by half as well.

You can accomplish the same thing by driving the LED at 5mA full time.

Brightness is proportional to average current.

10mA × 100% duty cycle = 20mA × 50% duty cycle = 100mA × 10% duty cycle.
 
brightness is not to much of a concern. Have pulsed standard leaded leds with no apparent affect of brightness.
This idea occurred to me while getting into bed last night as I am concerned about battery life. Each game takes about 4 hours to complete and rarely more that 3 leds on at one time BUT planning on worst case.
Having nt much experience using mosfets am uunder the impression that a 100 ohm resistor on the gate should be sufficient for this application. Read that somewhere during researching using mosfets.
 
So run them at lower current to start with.

Make an experiment - test 5mA on 100% of the time next to the exact same type of LED on at 10mA 50% of the time.

At any rate, I'll save my breath and fingers for other people's questions.
 
Thanks for suggestions. I don't think I have ever seen 2 THEN statements together lie that.
NOTE maybe I need to clarify a small bit.
It stars out the LED is off, press the mon switch and LED comes on and stays on UNTIL switch is pressed again.
I tried using the REPEAT UNTIL statement but got no where fast.
This is to replace the decade counter circuit I built. Reason for change is the on/off switches were too large. Made the board profile too tall to allow seeing the dominoes from across the table playing Mexican Train.
The new circuit is to utilize an 18F4520 (need 20 pins)10 LEDs and 10 mon tactile push-buttons.

You DON'T need 20 pins! You could set up your switches as a 2x5 matrix.
 
I thought of using a matrix but then it would involve using the ADC feature on the pic plus a bunch of resistors to form a ladder of resistors assuming this was your idea??
Having to re do my code as the port assignments are different. Have switches on two different ports as well as the leds.
Still trying to figure out how to use all the switches into a 16 bit variable as suggested n post #13
 
I think you would have a tough time implementing a matrix keypad but the linked article shows the idea pretty clearly.

In your case, you could have a 5 x 2 matrix using 7 Port pins. Trading the number of Port pins vs code complexity isn't a good idea for you.

tmp_25249-20170125_0507531336432627.png
 
Still trying to figure out how to use all the switches into a 16 bit variable as suggested in post #13


A "16 bit variable" is a word.

Dim Switches As Word

Search Swordfish Help for variables and alias.

Say your switches are on all of Port B and bits 0 - 3 of Port C. Something like this gets all of your bits into the same place. Read the documentation - it's 5am and I'm doing this from memory so the syntax may not be exactly right.

Switches.lowbyte = Port B

Switches.highbyte = (PortC AND %00001111)

"AND %00001111" masks out the upper 4 bits and gives you the lower 4 bits.

Switches.highbyte = PortC.lowernibble

might work but I don't recall. Check the Swordfish documentation on alias.
 
Last edited:
The above paper concerns high power LEDs, not the indicator type LEDs we are concerned with here.

From a Stack Exchange discussion, Olin makes a reasonable case that low power LEDs do not exhibit apparent increased brightness when driven at higher currents using PWM. If there is any effect of increased brightness the amount is so insignificant as not to justify the additional effort here.

tmp_25249-SmartSelectImage_2017-01-25-07-06-34-22737339.png
tmp_25249-SmartSelectImage_2017-01-25-07-06-57-1072381821.png
tmp_25249-SmartSelectImage_2017-01-25-07-07-56483979893.png
tmp_25249-SmartSelectImage_2017-01-25-07-08-101008978051.png
 
trying to figure out post #35 from Jonsea??
As well as trying to cobble together a code starting with short sections. The first code I posted works well, Push a button, BAM the Led comes on and stays on until the button is depressed again.
Thinking that by scanning the switches using a FOR NEXT loop but ran into a big hiccup. just scanning the leds (tells me the for next loop s working etc)and using this code ALL four leds come on. I am not implementing the arrays properly for sure. Going to research looking at other codes to decipher why?

DEVICE = 18F4520

CLOCK = 8

INCLUDE"InternalOscillator.bas"

INCLUDE "SetDigitalIO.bas"

CONFIG MCLRE = OFF

// alias to port pin...



DIM PWM AS porta.3 ' Enables transistor switch for cathodes to ground

DIM x AS BYTE

DIM led(9) AS WORD

'DIM led(0) AS portc.0

led(1)= portc.1

led(2) = portc.2

led(3) = portc.3

{

led(0) = 0

led(1)=0

led(2)=0

led(3)=0

// main program...



trisc = %00000000



trisa = %00000000

'portc = %00000000

PWM = 1

WHILE true

FOR x = 0 TO 4

led(x)=1

DELAYMS(1000)

led(x) =0

DELAYMS(1000)

NEXT

WEND
been thinking about Jonseas post all day while wife is shopping and am lost.
planning on just a simple code, nothing fancy and that I can comprehend.
 
*shrug* I tried.

You didn't know how to have 16 bits in a variable. Possibly you were waiting for Devine intervention but I explained 16 bits is a word.

You didn't know how to get all the switch pins into a variable. I explained it.

I told you where to look in the documentation. It's all clearly spelled out.

I guess fancy means you have to write your own code rather than copying&pasting someone else's. These concepts are pretty simple and straightforward.

You also didn't understand a switch matrix. I provided an illustration and a link to a clear explanation...not that I think you should do it, just in case you wanted to understand.

Sigh
 
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top