My first program

A place to discuss software and code that isn't automotive related. Free and open source preferred but not compulsory.
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

Thanks, I'll have to check out the Arduino site.

Regarding programming the UBW32 with the PICkit 2, here are some things to do:

1. You can prolly use the PICkit 2 programming software to program the UBW32. The GUI has a voltage select box in the header. But when you select the device, the Vpp and Vdd may set automatically.

The PIC32MX460F512L is on the current PICkit 2 device support list:
http://www.microchip.com/stellent/idcpl ... e=en027813

Note: If you run the PICkit 2 programming software and MPLAB at the same time, be sure to choose
'Debugger>Select Tool>MPLAB Sim' and 'Programmer>Select Programmer>None from the MPLAB menu. Otherwise both applications will try to access the PICkit 2.

- - - - -

2. I think the latest version of MPLAB (v8.33?) can adjust the PICkit 2 Vdd from the menu bar. I haven't checked. If it does, then you can use MPLAB to directly control the PICkit 2 as a programmer, instead of running the separate PICkit programming app.

Hey, maybe v8.33 will also operate the PICkit 2 as a debugger on the PIC32MX devices?

- - - - -

3. If the new version of MPLAB doesn't adjust the Vdd, the output voltage for the PICkit2 can be changed by editing an MPLAB device config file. This works with the 16F690, I built prototypes on the PICkit 2 demo board using 3.3V devices. So if this doesn't work for the 32 bit parts at least you'll be able to use 3.3V components with the demo board and 16F690.

Note: You'll have to edit the PIC32MX460F512L.dev file. Maybe the .dev file is already set to a 3.3 volt programing voltage and Vdd.

To change the default Vdd for a device in MPLAB, follow these steps:

1) Close MPLAB and browse to "C:\Program Files\Microchip\MPLAB IDE\Device"
2) Make a copy of "PIC16F690.dev" then open the original in Notepad
3) Edit the "nominal" voltage to the desired voltage, for example "3.300"

file excerpt:
format=0.1
#device=PIC16F690
# DOS: DOS-00488
# PS: DS-41204
# DS: DS-41262
vpp (range=10.000-12.000 dflt=11.000)
vdd (range=2.000-5.500 dfltrange=4.500-5.500 nominal=5.000)

4) Save and close the file, then reopen MPLAB. The default VDD for the device should be the new nominal voltage.

- - - - -

4. Power the target board or UBW32 externally

- - - -

Hope that info is of some use to you.

- Jim
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

One last thing does anyone have any suggestions for changing the code for the safety circuit?
I have put #defines in but now I am trying to make a "battery type select" by a jumper. It uses one of the inputs with a weak pull-up being open or jumpered to ground. This is going to be used to change the battery values to a set for lithium type batteries or a set for SLA batteries as their voltage discharge profile are different.
If you post it up I'll take a look at it.

- Jim
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

longracing wrote:I am trying to make a "battery type select" by a jumper. It uses one of the inputs with a weak pull-up being open or jumpered to ground.
Generally, you can do that with a conditional bit test directly on a port pin. In the same way as a conditional bit test on a ram register bit.
example:
btfss portC,3

Or, read the whole port into the w register, move it to a ram location, and test it there.
example:
movf portC,w ;copy to w register
movwf 0x26 ;write to ram location 0x26
btfss 0x26,3 ;test bit 3

These other tips may be useful if you plan on writing more assembly.

The literal values in your code can be labeled/equated with the 'equ' Assember Directive. Possible candidates are the battery voltage values. Assign the labels/equates and place them in the code up near the cblock ram address defines.
Equates example:
batt_min_voltage equ .187
batt_low_voltage equ .198

Example of using the label:
movlw batt_low_voltage

You can name ram bytes and ports and pins the same way.
bsf portC,2 can be equated to:
bsf output,piezo

More label possibilities:
output equ 0x07 ;port C
input equ 0x07 ;port C
piezo equ 0x02 ;piezo pin
voltage_jumper equ 0x03 ;voltage jumper pin

Multiple names can be assigned to the same value. For instance, if there are input and output pins on portC, you can name the portC address both 'input' and 'output'
and have:
movf input,w ;copy port C to w register
movwf port_test ;move to a ram location named 'port_test'
btfss port_test,voltage_jumper ;test the voltage_jumper bit (same as testing the port)

If you like, make another file with an .inc extension, like battery_control_equates.inc, and place all the equates there. Sure, it isn't such a big deal with a small program. Note: If you place the .inc file in the same folder as the main .asm file, and list it near the processor include file at the top of the code, then you do not have to refer to it in the View>Project>Header Files folder in MPLAB.
Example:
#include <p16F690.inc>
#include <battery_control_equates.inc>

Check Chapter 4 for assembler directives. In this pdf file, the 'see example' and 'see also' are links to that section of the document.

MPASM Assembler, MPLINK Object Linker, MPLIB Object Librarian User Guide
http://ww1.microchip.com/downloads/en/D ... 33014K.pdf

MPASM/MPLINK Quick Chart
http://ww1.microchip.com/downloads/en/D ... 30400G.pdf

One last thing, search the microchip site and read up on read-modify-write issues when addressing ports directly. You can really step in it and not know what's causing the undesired operation.
A quick google with 'microchip read-modify-write issues'
http://www.microchip.com/wiki/Wiki.aspx?WikiID=50

- Jim
User avatar
longracing
LQFP112 - Up with the play
Posts: 140
Joined: Wed Jul 16, 2008 9:21 am
Location: NSW, Australia

Re: My first program

Post by longracing »

Thanks for all the replies.

I'll have a good read through all the information and do some work on the code before I re-post it.
The main difference is the Lithium packs will die if they are discharged below 3Volts/cell but the battery packs do have a built in protection circuit much like mobile phones. The circuit protects from over-discharge, over-charge (causes spectacular fires :twisted: ), over-current & short-cicuits.

Will have another try at the Pic32 as well. I might test the nominal voltage settings with the P16f690 first.
;)
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

Sure thing. If something is broken in your code or if you have any questions about that 'jumper select' post up. It's pretty much just the one instruction, the btfss or btfsc conditional bit test on the port pin. That and turn on the weak pull up.

I was thinking about that battery select feature, and the people using it, and there may be a better/safer way to select the battery type than a jumper. Use two input pins with two pushbuttons, and two output pins with two LEDS. After power up, the user must push one or the other button before continuing.

For sure check out the PICkit2 software (separate from MPLAB) for the selectable Vdd output. And check out the documentation for the 3.3V Vdd and 3.3V Vpp when the PIC32 is selected.
User avatar
longracing
LQFP112 - Up with the play
Posts: 140
Joined: Wed Jul 16, 2008 9:21 am
Location: NSW, Australia

Re: My first program

Post by longracing »

:) Have had some success with the UBW32. The latest version of MPLAB v8.33 won't program the Pic32 with the PICKit2. I checked the nominal voltage is already set at 3.3v but MPLAB won't allow you to set the PICKit2 as programmer/debugger. You can however use MPLAB to generate the code and hex file then use the PICKit2 software to program.
Got the LEDs flashing "Hello World". :D

Back to the safety circuit. The main reason for using the jumper is that the battery type will be set in the workshop when the unit is built, as the batteries are hard-wired in. The less the customer has to play with the less to go wrong (hopefully).
The attached file "SafetyCir Ver-1_0_2" is the current working code.
The file "Batt_Safety" is the first attempt at modifying the code to allow battery type selection. I'm not sure if the initialising of portB is correct or if it is not loading the values correctly. At the moment it simply turns on and indicates "Green = Good" no matter what the voltage. :?
Attachments

[The extension txt has been deactivated and can no longer be displayed.]

[The extension txt has been deactivated and can no longer be displayed.]

User avatar
jharvey
1N4001 - Signed up
Posts: 1607
Joined: Tue Jun 10, 2008 5:17 pm

Re: My first program

Post by jharvey »

I seem to recall the PIC32 is an ARM based processor. You might find gcc with the -m switch interesting. gcc is a very powerful compiler, and very well tested. Also it's free, and integrates well with eclipse.

Can you confirm or deny if the PIC32 is an ARM processor?

If you can go gcc with eclipse, you'll be developing with the same tools used in FreeEMS.
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

Good stuff for your first program! Good deal that you can use the UBW32 now!

This is probably it. The original instruction 'movlw PORTB' puts the literal value 0x06 into w.

; test for batt. type
  movf  portB,w  ; make this change
  movwf  PORTBcopy
  btfss  PORTBcopy,5
  call  Batt_type_SLA  ; if jumper is CLOSED
  btfsc  PORTBcopy,5
  call  Batt_type_lithium  ; if jumper is OPEN

Note, I don't know if a lower case 'portB,w' will work. Lower case for port registers and destination is a personal preference. I can't remember if the original Program Files\Microchip\MPASM Suite\PIC16F690.INC file has those lower case equates. You can edit the processor include file with your preferences. I made a univeral .inc file with my personal preferences and always #include it.

Along those lines of personal preferences, the following #define text substitutions may be useful to you.
  #define cbit  STATUS,0
  #define zbit  STATUS,2
You can put those 'personal' defines into the include file as well.


Here's something that may not cause a problem, but it's good practice. Initialize unused pins as outputs. Or for maximum fail safe, lay out the board with unused pins to 10K resistors tied to either rail.
Initialize PORTB:

  movlw  b'00100000'  ; make bit 5 of PORTB an input
  movwf  TRISB
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

jharvey wrote:I seem to recall the PIC32 is an ARM based processor. You might find gcc with the -m switch interesting. gcc is a very powerful compiler, and very well tested. Also it's free, and integrates well with eclipse.

Can you confirm or deny if the PIC32 is an ARM processor?
The PIC32 is a MIPS core, link to a related story below.
As to what free compiler is available for a MIPS core, I have no idea.

Powered by the MIPS32 M4K processor core, the PIC32 MCU family now includes IP from both the analog and processor business groups of MIPS.
http://www.edn.com/article/CA6535501.html
MotoFab
1N4001 - Signed up
Posts: 307
Joined: Thu May 29, 2008 1:23 am
Location: Long Beach CA

Re: My first program

Post by MotoFab »

I had your file open in MPLAB while I was doing some other coding and caught a look at something before I closed it. This might be another problem.

The routine below loads two literals into two ram registers, which is fine.

Batt_type_lithium
; load values for lithium type battery packs
  movlw  lithium_minV  ; load literal to w
  movwf  MinV      ; write to MinV register
  movlw  lithium_lowV
  movwf  LowV
  movlw  lithium_getting_low
  movwf  getting_low
 return


Subsequent code is supposed to use the literal values written to MinV and LowV to compare with Batt, but instead are using the addresses of MinV and LowV as literals. That won't work for a battery voltage compare.

; Check Batt above minimum voltage
  movf  ADRESH,w
  movwf  Batt     ; put ADC in Batt
  bcf    STATUS,C  ; prep for compare
  movlw  MinV    ; w now contains the address of the MinV register defined in the cblock statement (currently that is 0x27)
  subwf  Batt,w
  btfsc  STATUS,C  ; skip next if Batt <= Minimum Voltage
  goto  Power_on  ; goto Power_on if Voltage > MinV
  goto  Shutdown


Try this instead.

; Check Batt above minimum voltage
  movf  ADRESH,w
  movwf  Batt      ; put ADC in Batt
               ; clear carry instr isn't needed, the carry bit is 'refreshed' after every add and subtract
  movf  MinV,w    ; make this change
  subwf  Batt,w
  btfsc  STATUS,C  ; skip next if Batt <= Minimum Voltage
  goto  Power_on  ; goto Power_on if Voltage > MinV
  goto  Shutdown


Something else I was thinking... If the battery jumper is set at the 'factory' and not generally changed, then there's no need to write the battery level literals into ram. You can use the 'literal operation' instructions instead. But that will take some restructuring.

I'm not saying doing it that way is better. There are many ways to skin every cat, and the way you are doing it just fine. As you discover different ways to do things you can choose among them for the 'best' in any given circumstance.

Note, I used some unicode characters to get this formatting instead of using the CODE tags. (You can't color the text within the CODE tags.) The unicode characters are probably not compatible with the compiler.
Post Reply