Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC18F65K22 MPASM code not building

Status
Not open for further replies.
T

treez

Guest
Newbie level 1
Hello,
Please advise on why this MPASM code for PIC18F65K22 won't build.

The assembler in MPLABX gives errors which I do not understand. It doesn't seem to recognise that i have used the ";" to indicate comments.

Here is the code...

Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
;ACTIVE HIGH IS ASSUMED THROUGHOUT
;TURN OFF ALL PULLUPS
;WHERE IS THE RESET ADDRESS?
 list p=18f65k22          ;list directive to define processor
#include <p18f65k22.inc>    ;processor specific definitions
;   config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BODEN_OFF & _CPD_OFF)
 
temp1     equ   h'20        ;register for setting ch 1 temperature
temp2     equ   h'21        ;register for setting ch 2 temperature
temp3     equ   h'22        ;register for holding ch 3 temperature
tempb     equ   h'23        ;register for holding board temperature
dip8      equ   h'24         ;holds dipswitch settings
ana_8bit  equ   h'25         ;holds ADC reading converted to 8 bit from 12 bit
ana_hi    equ   h'26         ;holds the ADRESH register
ana_lo    equ   h'27         ;holds the ADRESL register
ext_in    equ   h'28         ;holds the external connector inputs
 
; *** DECLARE MACRO'S ***
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;**** bank switching ****
 
;*** TURN LEDS ON AND OFF
ON1 macro               ;Turn on chan1
    bsf PORTE,RE1
    endm
 
OFF1 macro               ;Turn off chan1
    bcf PORTE,RE1
    endm
 
ON2 macro               ;Turn on chan2
    bsf PORTE,RE0
    endm
 
OFF2 macro               ;Turn off chan2
    bcf PORTE,RE0
    endm
 
ON3 macro               ;Turn on chan2
    bsf PORTG,RG0
    endm
 
OFF3 macro               ;Turn off chan2
    bcf PORTG,RG0
    endm
 
;****** READ TEMP ******************
 
 
; ************************
 
; ********  Read dipswitchs into dip8 register ********
read_dips   macro
    bsf dip8,0      ;DIPSWITCH 1 = MODE
    btfss   PORTB,RB0
    bcf dip8,0
 
    bsf dip8,1      ;DIPSWITCH 2, Power bit 0
    btfss   PORTB,RB1
    bcf dip8,1
 
    bsf dip8,2      ;DIPSWITCH 3, power bit 1
    btfss   PORTB,RB2
    bcf dip8,2
 
    bsf dip8,3      ;DIPSWITCH 4, polarity of 0-10V
    btfss   PORTB,RB3
    bcf dip8,3
 
    bsf dip8,4      ;DIPSWITCH 5, Temp threshold = 80c or 90c
    btfss   PORTB,RB4
    bcf dip8,4
 
    bsf dip8,5      ;DIPSWITCH 6, unused
    btfss   PORTB,RB5
    bcf dip8,5
 
    bsf dip8,6      ;DIPSWITCH 7, unused
    btfss   PORTC,RC5
    bcf dip8,6
 
    bsf dip8,7      ;DIPSWITCH 8, fan or no fan
    btfss   PORTE,RE6
    bcf dip8,7
    endm
;-----------------------------------------------
 
 
;****** READ EXTERNAL CONNECTOR DIGITAL INPUTS *******
read_ext_conn   macro
    bsf ext_in,0   ;extconn PIN 1, UNUSED
    btfss   PORTD,RD4
    bcf ext_in,0
 
    bsf ext_in,1   ;extconn PIN 3; RESET
    btfss   PORTD,RD5
    bcf ext_in,1
 
    bsf ext_in,2   ;extconn PIN 4; CH1_ON/OFF
    btfss   PORTD,RD6
    bcf ext_in,2
 
    bsf ext_in,3   ;extconn PIN 5; CH2_ON/OFF
    btfss   PORTD,RD7
    bcf ext_in,3
 
    bsf ext_in,4   ;extconn PIN 6; CH3_ON/OFF
    btfss   PORTC,RC7
    bcf ext_in,4
 
    bsf ext_in,5   ;extconn PIN 7; UNUSED
    btfss   PORTC,RC6
    bcf ext_in,5
 
    bsf ext_in,6   ;extconn PIN 8; UNUSED
    btfss   PORTC,RC0
    bcf ext_in,6
 
    bsf ext_in,7  ;extconn PIN 10; CLEAR
    btfss   PORTC,RC1
    bcf ext_in,7
    endm
; *****************************************
 
; **** output a tripped signal ****
output_trip macro
    bsf PORTA, RA4
    endm
 
; *****output a non-tripped signal ****
no_output_trip macro
    bcf PORTA, RA4
    endm
 
;***** Make ADC reading 8 bit from 12 bit ***********
ana_to_8bit    macro
    movf    ADRESH,w
    movwf   ana_8bit
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
   
 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;SET UP THE ADC
setupADC    macro
    movlw   h'00
    movwf   ADCON0  ;channel select, godone, ADON
 
    movlw   h'10
    movwf   ADCON1  ;vref
 
    movlw   h'3E
    movwf   ADCON2  ;Right justified, acquisition time
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
    org 04
 
start
    nop
    goto    start
    end

 
Last edited by a moderator:

Hi,

Yes you have a lot of errors in there, basically most of them relate to the Macros you are setting up, they expect a field , Argument, after the word Macro, instead you have a comment which it rejects.

There are also some other basic errors in your code which I have not attempted to correct because I wonder about you use of all those one line macros, which seem wrong to me.

It seems like you are trying to flash /switch a led in line with the status of a switch input ?

If you can explain what it is you are trying to do I can give you some starter code to work from, this is typically a bit of code to flash a led, you add an input switch after that.. not sure what the adc section is for ?

Also is that chip part of a development board, it quiet a large chip to start learning from ?
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
we must light leds, 3 channels, and switch them off if a switch is switched , or if the temperature goes too high. We have a 0-10v input which we put into micro adc, and adjust control voltage to control led current.
 

we must light leds, 3 channels, and switch them off if a switch is switched , or if the temperature goes too high. We have a 0-10v input which we put into micro adc, and adjust control voltage to control led current.

Hi,

So its a college project by the sound of it, so I will only be able to give you some starter code as I am sure you will understand, give me an hour I will post it.

However have to ask again why the 18F65K22 chip as its certainly a very unfriendly chip for a beginner to start coding in Assembly, can you use a more common 16F chip instead ?

Also you use of Macros, have you be told to use them for some reason ? for what you need to do I cannot say they are really needed ..?
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Macros we believe make the code easy to read.
Its not a college project, its admittedly simple, but this is real code for a real app....I left college in 2002. For my opinion, and I am sure you agree, university is not for a "general" engineering job ..at least not directly, a uni degree is a pre-course for a PhD study....ie, university is all about making the world's next leading scientists, not the next generation of engineers.

btw, ive got it building so far,except with the config statement, do you know how to do that for PIC18F65K22?


Code ASM - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
;REMEMBER RESET ADDRESS
 list p=18f65k22          ;list directive to define processor
#include <p18f65k22.inc>    ;processor specific definitions
;   config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BODEN_OFF & _CPD_OFF)
 
 
;DEFINE OUTPUTS
#define ch1  PORTE,RE1
#define ch2  PORTE,RE0
#define ch3  PORTG,RG0
#define ind_led PORTC,RC2
#define fan_con PORTE,RE2
#define trip    PORTA,RA4
 
;DEFINE INPUTS FROM DIPSWITCH
;Note that the new board has pullups on the dipswitch,
;whereas 550587 PCB had pull downs, thus logic is reversed.
#define mode        dip8,0  ;low = external connector sets powervia 0-10V
#define current_LSB dip8,1  ;00 -> 3.52A, 01 -> 2.64A, 10 -> 1.76A, 11 -> OFF
#define current_MSB dip8,2
#define polarity    dip8,3  ;high = 10V gives max current
#define T80_T90     dip8,4  ;high = 80degC, low=90degC
#define fan_fitted  dip8,8  ;high = fan fitted, low = fan no fitted
 
;DEFINE INPUTS FROM EXTERNAL CONENCTOR
#define re_set  ext_in,1
#define ch1_ext ext_in,2
#define ch2_ext ext_in,3
#define ch3_ext ext_in,4
#define clear   ext_in,7
 
temp1     equ   0x20         ;register for setting ch 1 temperature
temp2     equ   0x21         ;register for setting ch 2 temperature
temp3     equ   0x22         ;register for holding ch 3 temperature
tempb     equ   0x23         ;register for holding board temperature
dip8      equ   0x24         ;holds dipswitch settings
ana_8bit  equ   0x25         ;holds ADC reading converted to 8 bit from 12 bit
ana_hi    equ   0x26         ;holds the ADRESH register
ana_lo    equ   0x27         ;holds the ADRESL register
ext_in    equ   0x28         ;holds the external connector inputs
 
; *** DECLARE MACRO'S ***
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
;**** bank switching ****
 
;*** TURN LEDS ON AND OFF
ON1 macro               ;Turn on chan1
    bsf PORTE,RE1
    endm
 
OFF1 macro               ;Turn off chan1
    bcf PORTE,RE1
    endm
 
ON2 macro               ;Turn on chan2
    bsf PORTE,RE0
    endm
 
OFF2 macro               ;Turn off chan2
    bcf PORTE,RE0
    endm
 
ON3 macro               ;Turn on chan2
    bsf PORTG,RG0
    endm
 
OFF3 macro               ;Turn off chan2
    bcf PORTG,RG0
    endm
 
;****** READ TEMP ******************
 
 
; ************************
 
; ********  Read dipswitchs into dip8 register ********
;Remember that the new PCB has pullups, wheras 550587 PCB had pull downs
;..thus logic in the program is reversed
read_dips   macro
    bsf dip8,0      ;DIPSWITCH 1 = MODE
    btfss   PORTB,RB0
    bcf dip8,0
 
    bsf dip8,1      ;DIPSWITCH 2, Power bit 0
    btfss   PORTB,RB1
    bcf dip8,1
 
    bsf dip8,2      ;DIPSWITCH 3, power bit 1
    btfss   PORTB,RB2
    bcf dip8,2
 
    bsf dip8,3      ;DIPSWITCH 4, polarity of 0-10V
    btfss   PORTB,RB3
    bcf dip8,3
 
    bsf dip8,4      ;DIPSWITCH 5, Temp threshold = 80c or 90c
    btfss   PORTB,RB4
    bcf dip8,4
 
    bsf dip8,5      ;DIPSWITCH 6, unused
    btfss   PORTB,RB5
    bcf dip8,5
 
    bsf dip8,6      ;DIPSWITCH 7, unused
    btfss   PORTC,RC5
    bcf dip8,6
 
    bsf dip8,7      ;DIPSWITCH 8, fan or no fan
    btfss   PORTE,RE6
    bcf dip8,7
    endm
;-----------------------------------------------
 
 
;****** READ EXTERNAL CONNECTOR DIGITAL INPUTS *******
read_ext_conn   macro
    bsf ext_in,0   ;extconn PIN 1, UNUSED
    btfss   PORTD,RD4
    bcf ext_in,0
 
    bsf ext_in,1   ;extconn PIN 3; RESET
    btfss   PORTD,RD5
    bcf ext_in,1
 
    bsf ext_in,2   ;extconn PIN 4; CH1_ON/OFF
    btfss   PORTD,RD6
    bcf ext_in,2
 
    bsf ext_in,3   ;extconn PIN 5; CH2_ON/OFF
    btfss   PORTD,RD7
    bcf ext_in,3
 
    bsf ext_in,4   ;extconn PIN 6; CH3_ON/OFF
    btfss   PORTC,RC7
    bcf ext_in,4
 
    bsf ext_in,5   ;extconn PIN 7; UNUSED
    btfss   PORTC,RC6
    bcf ext_in,5
 
    bsf ext_in,6   ;extconn PIN 8; UNUSED
    btfss   PORTC,RC0
    bcf ext_in,6
 
    bsf ext_in,7  ;extconn PIN 10; CLEAR
    btfss   PORTC,RC1
    bcf ext_in,7
    endm
; *****************************************
 
; **** output a tripped signal ****
output_trip macro
    bsf PORTA, RA4
    endm
 
; *****output a non-tripped signal ****
no_output_trip macro
    bcf PORTA, RA4
    endm
 
;***** Make ADC reading 8 bit from 12 bit ***********
ana_to_8bit    macro
    movf    ADRESH,w
    movwf   ana_8bit
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;SET UP THE ADC
setupADC    macro
    movlw   h'00
    movwf   ADCON0  ;channel select, godone, ADON
 
    movlw   h'10
    movwf   ADCON1  ;vref
 
    movlw   h'3E
    movwf   ADCON2  ;Right justified, acquisition time
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
; ************ Define analog pins ***********8
ana_pins    macro
    movlw   h'FC    ;
    movwf  ANCON0
    movlw   h'0F    ;
    movwf  ANCON1
    movlw   h'00    ;
    movwf   ANCON2
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
; Disable the comparators
comparator_off    macro
    movlw   h'00
    movwf   CM1CON
    movlw   h'00
    movwf   CM2CON
    movlw   h'00
    movwf   CM3CON
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
; Disable interrupts
no_interrupts   macro
    clrf    INTCON
    movlw   h'80
    movwf   INTCON2
    clrf    INTCON3
    clrf        PIE1
    clrf        PIE2
    clrf        PIE3
    clrf        PIE4
    clrf        PIE5
    clrf        PIE6
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
; Disable pullups
no_pullups  macro
    clrf    PADCFG1
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
 
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; disable open drain outputs
no_opendrain    macro
    clrf    ODCON1
    clrf    ODCON2
    clrf    ODCON3
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXX
 
 
;xxxxxxxxxxxxxxxxxxxxxxx
;set up ports
setup_ports    macro
    movlw   h'2C
    movwf   TRISA
    movlw   h'3F
    movwf   TRISB
    movlw   h'E3
    movwf   TRISC
    movlw   h'F0
    movwf   TRISD
    movlw   h'40
    movwf   TRISE
    movlw   h'FE
    movwf   TRISF
    movlw   h'24
    movwf   TRISG
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXX
 
;XXXXXXXXXXXXXXXXXXXXXXXXXX
;Make all NC pins low (they are already made to outputs)
;They are outputs for noise immunity reasons
NC_pins_low macro
    bcf PORTA,RA0
    bcf PORTA,RA1
    bcf PORTA,RA6
    bcf PORTA,RA7
 
    bcf PORTD,RD0
    bcf PORTD,RD1
    bcf PORTD,RD2
    bcf PORTD,RD3
 
    bcf PORTE,RE3
    bcf PORTE,RE4
    bcf PORTE,RE5
    bcf PORTE,RE7
 
    bcf PORTF,RF0
    endm
 
;#DEFINES TO NAME OUTPUTS
;INITIALISE OUTPUTS THAT ARE CONNECTED
;defines for dip8 bits and ext_con bits
 
 
 
 
 
 
 
    org 0x04
 
start
    nop
    goto    start
    end




Also, do you know why there is almost no mpasm example code available almost anywhere on the web, and also, do you know where are the user's storage resistors, ex 0x20 and upwards?...in pic18f65k22
Also,do you know where the origin of the code should be?..is it "org 04"?
 
Last edited by a moderator:

Hi,

Just having the same problem with X , build error in the config code, though I cannot see where it gets the info from in the .inc file.

Am just going to try the code in MPLAB IDE v8.92, think you might find X does not give much attention to Assembler
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
I'm siding with wp100 regarding macros.

Most, if not all of those functions are better done as subroutines if they are needed at all. The problem with macros is twofold, the first you have already encountered, they have no protection against missing or invalid arguments. The second reason is they do not use code space efficiently and may stop the compiler performing optimizations. The macro code is basically copied verbatim each time you use it, a subroutine is written once and can be called many times.

They have their uses but it's better to be more selective with them than try to write your own high level language constructs which is what you seem to be attempting.

I would also recommend using a 'cblock' directive for your variables instead of lots of 'equ' statements. It has two benefits, it produces a compact block of variables without having to manually assign addresses and secondly, it lets you use different storage sizes for the variables.

The best way to start a project like this is to copy the template Microchip provide for you, rename it as you wish and then add your own code to it. It is a good 'skeleton' to add your own flesh to. It also has the correct addresses assigned to it for the program itself and interrupt routines.

Brian.
 

Thanks, but I downloaded the code module library from microchip, and it didn't give mpasm code for pic18f65k22.

the code above builds fine....so I don't know what is meant be "missing arguments" or "invalid arguments" ....its just that I don't know how to do the config statement.
 

Thanks, but I downloaded the code module library from microchip, and it didn't give mpasm code for pic18f65k22.

the code above builds fine....so I don't know what is meant be "missing arguments" or "invalid arguments" ....its just that I don't know how to do the config statement.

Hi,

First off, I'm only just getting into using X after having use the old MPLAB IDE v8 for many years so I may be missing some easier /important ways of using X properly.

However your code must have a valid Config line or it assumes all its own default settings which can really upset you code if they are not set correctly.

At the moment your config line is commented out.

When I tried to get my code example to work in X if gave errors suggesting that I was missing something in the 18F65K22.inc file.

Have run the same code in IDE V8 and it works fine .

I renamed the 18F65K22.inc file in the X ASM Includes folder to ...K22oldx and pasted in the ..K22 file from IDE v8 and it now build ok as you can see.
( the V8 .inc file is in the .zip file)

The enclosed led flasher code works in simulation as turning on Port B,0

Have a look at the code and my comments as you will see some very important code values you need to use before your 'main' code begins.

As for your method of using macros etc I will leave you to sort them out, but look in Xs Help Asm section where is shows how to construct and use macros.
 

Attachments

  • treez.zip
    132.5 KB · Views: 108

I don't have my programming computer here until my new lab is finished but I think you will find the templates in the MPLABX folder on your computer already. The assembler is the same in MPLAB and MPLABX so it should work in both, MPLABX just calls it from a different IDE. In theory you can use the assembler from the command line without using MPLAB or MPLABX at all.

You might find two different templates, one for absolute code and one for relative code. In your program you should use the absolute one because you have already specified absolute addresses. For future reference, the relative one is more versatile because it allows you to mix your code souce with library functions, even ones written in a different language. The only difference is some changes to the way you describe the variable locations and you let the linker program find suitable addresses for you.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
I don't have my programming computer here until my new lab is finished but I think you will find the templates in the MPLABX folder on your computer already. The assembler is the same in MPLAB and MPLABX so it should work in both, MPLABX just calls it from a different IDE. In theory you can use the assembler from the command line without using MPLAB or MPLABX at all.

You might find two different templates, one for absolute code and one for relative code. In your program you should use the absolute one because you have already specified absolute addresses. For future reference, the relative one is more versatile because it allows you to mix your code souce with library functions, even ones written in a different language. The only difference is some changes to the way you describe the variable locations and you let the linker program find suitable addresses for you.

Brian.

Hi Brian,

Would have suggested templates but seems they are rather lacking when coming to the 18F K series.

Have not been able to identify the exact difference but seems for the 18F65K22 the .include file is somehow different between MPlab and MplabX

Don't think they are the easiest chips for a beginner with Assembly to use when you have things like some system registers outside the 'normal boundaries' particularly when they are basic ones like ANCON for selecting analogue or digital ports, yet very little info to let the user know that fact.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
I certainly agree about not being easy for beginners. I confess I've never used that particular device but I use lots of 18F46J11 which is similar in architecture. When it comes to 'complex' processors it's much easier to use 'C' or similar, especially when the IC is optimized for high level languages.

It is extremely frustrating that I haven't yet got my lab back, all my work and datasheets are packed away in storage units until the new building is finished. I'm actually on the building site at the moment watching concrete being poured to make the new floor so it wont be long before I can 'move in' again.

Being perfectly honest, looking at Treez's previous postings and the code itself, I wonder if this type of PIC isn't massive overkill for the job in hand.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Also, do you know why there is almost no mpasm example code available almost anywhere on the web, and also, do you know where are the user's storage resistors, ex 0x20 and upwards?...in pic18f65k22
Also,do you know where the origin of the code should be?..is it "org 04"?

Hi,

Just to answer the above points I did not get chance to look at last night.

There are quiet a few assembler tutorials, see the links below, however they are mainly for the simpler 16F chips , whereas the more complex 18FJ chips are seldom covered, probably because many folk use C.

You need to look at the chips datasheet for the full layout of Ram memory, system and user areas..
Basically you have banks 1 to 6 selectable by the Bank Select Register as shown in my code example.
Bank 0 is called Access ram as irrespective of what bank you are in the Access bank can always be used.

On most 18F chips all the system registers are held in the top half of bank 15, however these K chip overflow into the lower half and to address them you have to use the Bank Select register each time, not a good way to do things in assembler.

Again you might have now seen from my code example the need to use the ORG statement carefully.
Most chips have Interrupt Vectors which your 'main' code must be located after, so you typically just start your code with this layout - which jumps over the Interrut areas.
ORG 0x0000
bootup goto Main

You might find this directive easier to use than Macros for locating your ports; examples
#define KEYA PORTE, 1
#define KEYB PORTE, 0
#define TIMER3SW PORTC, 2
#define TIMER4SW PORTC, 3

http://www.winpicprog.co.uk/pic_tutorial.htm
**broken link removed**
http://www.amqrp.org/elmer160/lessons/
http://www.epemag.wimborne.co.uk/resources.htm
http://www.mikroe.com/chapters/view/5/chapter-4-timers/

- - - Updated - - -

It is extremely frustrating that I haven't yet got my lab back, all my work and datasheets are packed away in storage units until the new building is finished. I'm actually on the building site at the moment watching concrete being poured to make the new floor so it wont be long before I can 'move in' again.
.

Hi,

Certainly the weather for "pouring " , good job concrete sets under water :-D

Is the building ( home/work/both?) pre-fabricated as cannot see a conventional building being ready for many months...
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Certainly the weather for "pouring " , good job concrete sets under water
We had horrible storms overnight with 70 MPH winds and constant heavy rain but believe it or not, about 9am the wind dropped to absolute calm and the rest of the day was sunny and hot. Couldn't have been better for concrete pouring.

Is the building ( home/work/both?) pre-fabricated as cannot see a conventional building being ready for many months...
See for yourself, there's a webcam at www.dyfi.com/villa.html looking at the building site. It takes a snapshot every 30 seconds. I turn it off at the end of the working day and at weekends because all the images are saved to make a time lapse video when the work is finished.

It is both home and work, the building has already been pre-fabricated in sections which in theory just bolt together. The roof trusses are also made off site and slightly smaller than normal so they can be lifted into place by hand. I am assured the building shell can be erected in one day, weather permiting and it will take about a further 3 days to fit all the slates to the roof. (I'm in a national park so they insist on slate so it retains the character of other buildings in the area)

I'm doing all the plumbing and wiring before fitting the internal walls so there will be work to do even when the outside looks finished. I can't wait to get on with electronics things again, it's very frustrating when I have a list of projects to get on with and the only 'equipment' to hand is this ancient Celeron 233MHz/256Mb laptop!

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
betwixt;1386021 See for yourself said:
www.dyfi.com/villa.html[/url] looking at the building site. It takes a snapshot every 30 seconds. I turn it off at the end of the working day and at weekends because all the images are saved to make a time lapse video when the work is finished.


Hi,

Looking good ,will follow the web cam with interest , expect we will see the full version with Kevin on Grand Designs !:-D
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
wp100, thanks for your code, but I am not sure how you knew that the PORTA and TRISA registers are in Bank 1? (your use of "movlb 0x1" when you access the TRIS registers and PORT registers)
Page 97 of the datasheet states that all the SFR's are in either bank 15 or bank 14.
 

wp100, thanks for your code, but I am not sure how you knew that the PORTA and TRISA registers are in Bank 1? (your use of "movlb 0x1" when you access the TRIS registers and PORT registers)
Page 97 of the datasheet states that all the SFR's are in either bank 15 or bank 14.


Hi,

Sorry, but the way the code reads I can see how its confused things.

TrisA and PortA are not in bank 1.

To make things clear the sequence is -

Your work/users variables were initially placed in Bank1 0x100 by the Cblock directive.
To be fair for your immediate use that was perhaps not necessary as you could also have placed them in bank 0 0x000 the Access ram, which is available at all times.

Your chip only has ram banks 0 to 7 available for your user registers/variables.

The upper half of bank 15 is reserved for the Pics own System registers like TrisA and PortA etc and whenever it needs to go to them the system automatically points it to bank15 upper.

As mentioned, on some of these bigger 18F chips they have more system registers than the 150 byte upper "half" of bank 15 can take, so they overflow into the lower half of bank 15, but the system does not know they are in there.

Because of that, when you need to access those extra system registers you have to tell it which bank to find it in, even though its still bank 15 ( lower half).

So in that set up code, we need to set the ANCON registers, which are in the lower half, to Digital ( default values Analogue) so we must first tell the system to set the ram bank to 15.

After it has set ANCONs then we need to return it to bank 1 so we can use our 'normal ' user registers.

The TRISA registers etc are in the standard location in the upper half of bank 15 so will be automatically addressed.

To make things simpler change the CBLOCK address to 0x000 the Access Ram and put your variable in there, then your can set the Bank Select Register to 15 and leave it there until you actually need to use bank1, if you have that many variables /data.

Have a look at the 65k22 datasheet page 95 Fig 6.6 which shows the layout of the ram / data banks and importantly Note no. 1
Also see pages 97 / 98 which shows the locations of the special function registers ( system registers) and how they go below that limit address of x 5F

Hope that helps clear things, if not shout... plenty of help from others if my explanation is not good enough..
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Hi,

That code re-written to use just the Access bank 0 for the user variables, so making it easier to follow..
 

Attachments

  • treez2.rar
    1.1 KB · Views: 97
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Thanks, sorry I could not open the zipped file in that case,

By the way, with MPASM and pic18f65k22, is it ok to have a call statement within another function that was 'call'ed...ie like a nested call?

here is my code so far....it just turns led channels on and turns tham off if temperature is too high etc

Code:
;Code for 3 channel led driver on the updated 550587 PCB
;REMEMBER RESET ADDRESS
;remember to   set the oscillator
;WE ARE TRUNCATING THE ADC REGISTER SO JUST USE UPPER 8 BITS

; XXX DIPSWITCH POLARITY:- XXX
;DIP 1...ON = EXTERNAL CONNECTOR CONTROL
;DIP 2...ON = LOGIC HIGH (note 'ON' gives a low input)
;DIP 3...ON = LOGIC HIGH
;DIP 4...OFF = 10V is for max current
;DIP 5...OFF = 80degC, ON = 90degC
;DIP 6... UNUSED
;DIP 7... UNUSED
;DIP 8...ON = NO FAN FITTED, OFF = FAN FITTED

; XXX  EXTERNAL CONTROL CONNECTOR POLARITY:- XXX
;CLEAR = ACTIVE HIGH
;RESET = ACTIVE HIGH
;CH1...HIGH = ON, LOW = OFF
;CH2...HIGH = ON, LOW = OFF
;CH3...HIGH = ON, LOW = OFF

;List of input ports:
; ************** DIPSWITCH INPUTS:
;DIP1 = RB0
;DIP2 = RB1
;DIP3 = RB2
;DIP4 = RB3
;DIP5 = RB4
;DIP6 = RB5
;DIP7 = RC5
;DIP8 = RE6

;************* External connector INPUTS:
;clear =       RC1
;ch3 on/off =  RC7
;RESET =       RD5
;CH1 on/off =  RD6
;ch2 on/off =  RD7

;Other inputs (DIGITAL I/O
;Fan tacho = RG2
;mclr =      RG5

;Inputs that are ADC inputs:
;RA2 = AN2
;RA3 = AN3
;RA5 = AN4
;RF1 = AN6
;RF2 = AN7
;RF3 = AN8
;RF4 = AN9
;RF5 = AN10
;RF6 = AN11
;RF7 = AN5

;**********ADC INPUTS:
;AN0 = NC
;AN1 = NC
;AN2 = VREF- (0V)
;AN3 = VREF+ (3V)
;AN4 = Therm ch1
;AN5 = I_CH1
;AN6 = 0-10V control input
;AN7 = PCB thermistor
;AN8 = I_ch2
;AN9 = I_ch3
;AN10 = Therm-ch2
;AN11 = Therm-ch3

;OOOOOOOOOOO LIST OF OUTPUTS:
;Trip =          RA4

;Indicator LED = RC2
;MCP4013_CS =    RC3
;MCP4013_UD =    RC4

;Shutdown-Ch2 =  RE0
;Shutdown-Ch1 =  RE1
;Fancon PWM =    RE2

;Shutdown-Ch3 =  RG0

;NCNCNCNCNCNCNCNCNC LIST OF NON CONNECTED PINS:
;RA0
;RA1
;RA6
;RA7
;    RB6  = PGC
;    RB7  = PGD
;RC0
;RC6
;    RD0
;    RD1
;    RD2
;    RD3
;    RD4
;RE3
;RE4
;RE5
;RE7
;    RG1
;    RG3
;    RG4





#include "p18F65K22.inc"
 list p=18f65k22       	  ;list directive to define processor
; CONFIG1L
  CONFIG  RETEN = OFF           ; VREG Sleep Enable bit (Disabled - Controlled by SRETEN bit)
  CONFIG  INTOSCSEL = HIGH      ; LF-INTOSC Low-power Enable bit (LF-INTOSC in High-power mode during Sleep)
  CONFIG  SOSCSEL = HIGH        ; SOSC Power Selection and mode Configuration bits (High Power SOSC circuit selected)
  CONFIG  XINST = ON            ; Extended Instruction Set (Enabled)

; CONFIG1H
  CONFIG  FOSC = INTIO1         ; Oscillator (Internal RC oscillator, CLKOUT function on OSC2)
  CONFIG  PLLCFG = OFF          ; PLL x4 Enable bit (Disabled)
  CONFIG  FCMEN = OFF           ; Fail-Safe Clock Monitor (Disabled)
  CONFIG  IESO = OFF            ; Internal External Oscillator Switch Over Mode (Disabled)

; CONFIG2L
  CONFIG  PWRTEN = OFF          ; Power Up Timer (Disabled)
  CONFIG  BOREN = SBORDIS       ; Brown Out Detect (Enabled in hardware, SBOREN disabled)
  CONFIG  BORV = 0              ; Brown-out Reset Voltage bits (3.0V)
  CONFIG  BORPWR = ZPBORMV      ; BORMV Power level (ZPBORMV instead of BORMV is selected)

; CONFIG2H
  CONFIG  WDTEN = OFF           ; Watchdog Timer (WDT disabled in hardware; SWDTEN bit disabled)
  CONFIG  WDTPS = 1048576       ; Watchdog Postscaler (1:1048576)

; CONFIG3L
  CONFIG  RTCOSC = SOSCREF      ; RTCC Clock Select (RTCC uses SOSC)

; CONFIG3H
  CONFIG  CCP2MX = PORTC        ; CCP2 Mux (RC1)
  CONFIG  MSSPMSK = MSK7        ; MSSP address masking (7 Bit address masking mode)
  CONFIG  MCLRE = OFF           ; Master Clear Enable (MCLR Disabled, RG5 Enabled)

; CONFIG4L
  CONFIG  STVREN = ON           ; Stack Overflow Reset (Enabled)
  CONFIG  BBSIZ = BB2K          ; Boot Block Size (2K word Boot Block size)

; CONFIG5L
  CONFIG  CP0 = OFF             ; Code Protect 00800-01FFF (Disabled)
  CONFIG  CP1 = OFF             ; Code Protect 02000-03FFF (Disabled)
  CONFIG  CP2 = OFF             ; Code Protect 04000-05FFF (Disabled)
  CONFIG  CP3 = OFF             ; Code Protect 06000-07FFF (Disabled)

; CONFIG5H
  CONFIG  CPB = OFF             ; Code Protect Boot (Disabled)
  CONFIG  CPD = OFF             ; Data EE Read Protect (Disabled)

; CONFIG6L
  CONFIG  WRT0 = OFF            ; Table Write Protect 00800-017FF (Disabled)
  CONFIG  WRT1 = OFF            ; Table Write Protect 01800-03FFF (Disabled)
  CONFIG  WRT2 = OFF            ; Table Write Protect 04000-05FFF (Disabled)
  CONFIG  WRT3 = OFF            ; Table Write Protect 06000-07FFF (Disabled)

; CONFIG6H
  CONFIG  WRTC = OFF            ; Config. Write Protect (Disabled)
  CONFIG  WRTB = OFF            ; Table Write Protect Boot (Disabled)
  CONFIG  WRTD = OFF            ; Data EE Write Protect (Disabled)

; CONFIG7L
  CONFIG  EBRT0 = OFF           ; Table Read Protect 00800-017FF (Disabled)
  CONFIG  EBRT1 = OFF           ; Table Read Protect 01800-03FFF (Disabled)
  CONFIG  EBRT2 = OFF           ; Table Read Protect 04000-05FFF (Disabled)
  CONFIG  EBRT3 = OFF           ; Table Read Protect 06000-07FFF (Disabled)

; CONFIG7H
  CONFIG  EBRTB = OFF           ; Table Read Protect Boot (Disabled)





;DEFINE OUTPUTS
#define ch1bit     LATE,LATE1
#define ch2bit     LATE,LATE0
#define ch3bit     LATG,LATG0
#define ind_ledbit LATC,LATC2
#define fanconbit LATE,LATE2
#define tripbit    LATA,LATA4
#define mcp4013_csbit  LATC,LATC3
#define mcp4013_udbit  LATC,LATC4

;DEFINE INPUTS FROM DIPSWITCH
;Note that the new board has pullups on the dipswitch,
;whereas 550587 PCB had pull downs, thus logic is reversed.
#define modebit     dip8,0  ;low = external connector sets power via 0-10V
#define LSBbit      dip8,1  ;00 -> 3.52A, 01 -> 2.64A, 10 -> 1.76A, 11 -> OFF
#define MSBbit      dip8,2
#define polaritybit dip8,3  ;high = 10V gives max current
#define t80t90bit   dip8,4  ;high = 80degC, low=90degC
#define fanbit      dip8,8  ;high = fan fitted, low = no fan fitted

;DEFINE INPUTS FROM EXTERNAL CONNECTOR
#define re_setbit  ext_in,0
#define ch1_extbit ext_in,1
#define ch2_extbit ext_in,2
#define ch3_extbit ext_in,3
#define clearbit   ext_in,4

temp1     equ	0x20         ;register for HOLDING ch 1 temperature
temp2     equ	0x21         ;register for holding ch 2 temperature
temp3     equ	0x22         ;register for holding ch 3 temperature
tempb     equ	0x23         ;register for holding board temperature

ana8     equ   0x25         ;holds ADC reading converted to 8 bit from 12 bit
ana_hi    equ   0x26         ;holds the ADRESH register
ana_lo    equ   0x27         ;holds the ADRESL register
ext_in    equ   0x28         ;holds the external connector inputs
del1        equ     0x29         ;Delay register
del2        equ     0x2A         ;Delay register
board_90C   equ     0x2B         ;HOLDS 90C VALUE FOR BOARD
board_80C   equ     0x2C        ;HOLDS 80C VALUE FOR BOARD
led_90C     equ     0x2D        ;HOLDS 90C VALUE FOR LEDS
led_80C     equ     0x2E        ;HOLDS 80C VALUE FOR LEDS
max_temp    equ     0x2F        ;Holds the max temp
current     equ     0x30        ;Holds the current level bits (bits 0=0 to 3)

dip8      equ   0x32         ;holds dipswitch settings
dip8_next      equ 0x33        ;holds updated dipswitch settings
dip8_prev   equ 0x34
count       equ 0x35           ;count
iset_mcp4013    equ     0x36    ;Holds the set current value in mcp4013 steps
iset_mcp4013_A  equ     0x37    ;

; *** DECLARE MACRO'S ***
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

;**** bank switching ****

;*** TURN LEDS ON AND OFF
ON1 macro               ;Turn on chan1
    bsf LATE,LATE1
    endm

OFF1 macro               ;Turn off chan1
    bcf LATE,LATE1
    endm

ON2 macro               ;Turn on chan2
    bsf LATE,LATE0
    endm

OFF2 macro               ;Turn off chan2
    bcf LATE,LATE0
    endm

ON3 macro               ;Turn on chan2
    bsf LATG,LATG0
    endm

OFF3 macro               ;Turn off chan2
    bcf LATG,LATG0
    endm

;****** READ TEMP ******************


; ************************

; ********  Read dipswitchs into dip8 register ********
;Remember that the new PCB has pullups, wheras 550587 PCB had pull downs
;..thus logic in the program is reversed
read_dips   macro
    bsf dip8,0      ;DIPSWITCH 1 = MODE
    btfss   PORTB,RB0
    bcf dip8,0

    bsf dip8,1      ;DIPSWITCH 2, Power bit 0
    btfss   PORTB,RB1
    bcf dip8,1

    bsf dip8,2      ;DIPSWITCH 3, power bit 1
    btfss   PORTB,RB2
    bcf dip8,2

    bsf dip8,3      ;DIPSWITCH 4, polarity of 0-10V
    btfss   PORTB,RB3
    bcf dip8,3

    bsf dip8,4      ;DIPSWITCH 5, Temp threshold = 80c or 90c
    btfss   PORTB,RB4
    bcf dip8,4

    bsf dip8,5      ;DIPSWITCH 6, unused
    btfss   PORTB,RB5
    bcf dip8,5

    bsf dip8,6      ;DIPSWITCH 7, unused
    btfss   PORTC,RC5
    bcf dip8,6

    bsf dip8,7      ;DIPSWITCH 8, fan or no fan
    btfss   PORTE,RE6
    bcf dip8,7
    endm
;-----------------------------------------------


;****** READ EXTERNAL CONNECTOR DIGITAL INPUTS *******
read_ext_conn   macro


    bsf ext_in,0   ;extconn PIN 3; RESET
    btfss   PORTD,RD5
    bcf ext_in,0

    bsf ext_in,1   ;extconn PIN 4; CH1_ON/OFF
    btfss   PORTD,RD6
    bcf ext_in,1

    bsf ext_in,2   ;extconn PIN 5; CH2_ON/OFF
    btfss   PORTD,RD7
    bcf ext_in,2

    bsf ext_in,3   ;extconn PIN 6; CH3_ON/OFF
    btfss   PORTC,RC7
    bcf ext_in,3

    bsf ext_in,4  ;extconn PIN 10; CLEAR
    btfss   PORTC,RC1
    bcf ext_in,4
    endm
; *****************************************

; **** output a tripped signal ****
output_trip macro
    bsf tripbit
    endm

; *****output a non-tripped signal ****
no_output_trip macro
    bcf tripbit
    endm

;***** Make ADC reading 8 bit from 12 bit ***********
ana_to_8bit    macro
    movf    ADRESH,w
    movwf   ana8
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx


;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
;SET UP THE ADC
setupADC    macro
    movlw   0x00
    movwf   ADCON0  ;channel select, godone, ADON

    movlw   0x10
    movwf   ADCON1  ;vref

    movlw   0x3E
    movwf   ADCON2  ;Right justified, acquisition time
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

; ************ Define analog pins ***********8
ana_pins    macro
    movlw   0xFC    ;
    movwf  ANCON0
    movlw   0x0F    ;
    movwf  ANCON1
    movlw   0x00    ;
    movwf   ANCON2
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
; Disable the comparators
comparator_off    macro
    movlw   0x00
    movwf   CM1CON
    movlw   0x00
    movwf   CM2CON
    movlw   0x00
    movwf   CM3CON
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
; Disable interrupts
no_interrupts   macro
    clrf    INTCON
    movlw   0x80
    movwf   INTCON2
    clrf    INTCON3
    clrf        PIE1
    clrf        PIE2
    clrf        PIE3
    clrf        PIE4
    clrf        PIE5
    clrf        PIE6
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX

; Disable pullups
no_pullups  macro
    clrf    PADCFG1
    endm
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

;xxxxxxxxxxxxxxxxxxxxxxxxxxxxx
; disable open drain outputs
no_opendrain    macro
    clrf    ODCON1
    clrf    ODCON2
    clrf    ODCON3
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXXXXXX


;xxxxxxxxxxxxxxxxxxxxxxx
;set up ports as inputs or outputs
setup_ports    macro
    movlw   0x2C
    movwf   TRISA
    movlw   0x3F
    movwf   TRISB
    movlw   0xA2
    movwf   TRISC
    movlw   0xD0
    movwf   TRISD
    movlw   0x40
    movwf   TRISE
    movlw   0xFE
    movwf   TRISF
    movlw   0x24
    movwf   TRISG
    endm
;XXXXXXXXXXXXXXXXXXXXXXXXX

load_temps   macro
    movlw   0xA8        ;80  DEGC
    movwf   board_80C
    movlw   0xB8        ;90  DEGC
    movwf   board_90C
    movlw   0xA8        ;80  DEGC
    movwf   led_80C
    movlw   0xC9        ;90  DEGC
    movwf   led_90C
    endm

;XXXXXXXXXXXXXXXXXXXXXXXXXX
;Make all NC pins low (they are already made to outputs)
;They are outputs for noise immunity reasons
NC_pins_low macro
    bcf LATA,LATA0
    bcf LATA,LATA1
    bcf LATA,LATA6
    bcf LATA,LATA7

    bcf LATB,LATB6  ;PGC
    bcf LATB,LATB7  ;PGD

    bcf LATC,LATC0
    bcf LATC,LATC6

    bcf LATD,LATD0
    bcf LATD,LATD1
    bcf LATD,LATD2
    bcf LATD,LATD3
    bcf LATD,LATD4

    bcf LATE,LATE3
    bcf LATE,LATE4
    bcf LATE,LATE5
    bcf LATE,LATE7

    bcf LATG,LATG1
    bcf LATG,LATG3
    bcf LATG,LATG4

;    bcf PORTF,RF0
    endm

;DEFINES TO NAME OUTPUTS
;INITIALISE OUTPUTS THAT ARE CONNECTED
;defines for dip8 bits and ext_con bits


    org 0x04

start

Main	movlw	b'01100000'		; set internal osc to 8 mhz
		movwf	OSCCON

;SETUP JOBS
    setup_ports
    no_pullups
    no_opendrain
    NC_pins_low
    ana_pins
    comparator_off
    setupADC
    no_interrupts

begin
;Turn LEDs off
    OFF1
    OFF2
    OFF3

    call    delay_ms
;load temperature maximums.
    load_temps  ;load the max temperature values
;Read the dipswitchs
    read_dips
;Load the maximum temperature (either 80C or 90C)
    movf    led_80C,w
    movwf   max_temp
    btfss   t80t90bit
    movf    led_90C,w
    movwf   max_temp

;Find the maximum current level
    clrf    current     ;clear current reg prior to loading
    btfss   MSBbit
    call    a0_or_a1    ;set relevant bit in 'çurrent' register
    btfsc   LSBbit
    call    a2_or_a3    ;set relevant bit in 'çurrent' register
;Load the isetlevel register with relevant no of pulses
    btfsc   current,0
    goto    shutdown    ;bit 0 of 'current' register was set
    btfsc   current,1
    call    a1p76       ;1.76A = 33 PULSES to mcp4013
    btfsc   current,2   ;2.64A = 47 PULSES to mcp4013
    call    a2p64
    btfsc   current,3   ;3.52A = 61 pulses to mcp4013
    call    a3p52






;Find whether need internal  or external mode.
    btfss   modebit
    goto    EXTMODE
    goto    INTMODE

;Load the maximum temperature registers




EXTMODE
    nop


INTMODE
int1
    call    zero_mcp4013    ;zero the mcp4013 (zero volts out)
    call    set_mcp4013     ;Set the led driver current with MCP4013

int2
    movf    dip8,w
    movwf   dip8_prev
    read_dips
    movf    dip8,w
    movwf   dip8_next
    movf    dip8_next,w
    bcf     STATUS,Z
    xorwf   dip8_prev
    btfss   STATUS,Z    ;skip if the registers are the same
    goto    begin

;check temperature too hot
;Read temperture LED1.
    movlw   0x10
    movwf   ADCON0  ;Select AN4 for therm CH1
    movlw   0x11
    movwf   ADCON0     ;Enable ADC
    bsf     ADCON0,1    ;start ADC conversion
int3
    btfsc   ADCON0,1    ;check for ADC done
    goto    int3



;   goto    int2

 








    goto    begin
; ***** END OF MAIN LOOP *******

;------------------------------------------------
; FUNCTIONS   FUNCTIONS    FUNCTIONS    FUNCTIONS
;------------------------------------------------

; **** LOAD THE CURRENT REGISTER LOWER 4 BITS ****
a0_or_a1    ;Find out if its 0a or 1a
    btfss   LSBbit
    bsf     current,0   ;indicate 0 amps set
    bsf     current,1   ;indicate 1.76 amps set
    retlw   0x00
a2_or_a3    ;Find out if its 2a or 3a
    btfss   MSBbit
    bsf     current,2   ;indicate 2.64 amps set
    bsf     current,3   ;indicate 3.52 amps set
    retlw   0x00
; ***************************************************

;xxxxxx Load set current level ((no of pulses to mcp4013)  xxxxxxxxxxxx
shutdown    ;MSB and LSB bits were 0,0
    bcf     ch1bit
    bcf     ch2bit
    bcf     ch3bit
    goto    begin
a1p76       ;MSB = 0, LSB = 1
    movlw   0x21        ;33 pulses for 1A76
    movwf   iset_mcp4013
    retlw   0x00
a2p64       ;MSB = 1, LSB = 0
    movlw   0x2F        ;47 pulses for 2A64
    movwf   iset_mcp4013
    retlw   0x00
a3p52
    movlw   0x3D        ;61 pulses for 3A52
    movwf   iset_mcp4013
    retlw   0x00
;^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

zero_mcp4013        ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    bsf     mcp4013_csbit   ;disable mcp4013
    bcf     mcp4013_udbit   ;prep for decrement
    movlw   0x42            ;enough to decrement to zero volts
    movwf   count           ;
    call    delay_1ms
    bcf     mcp4013_csbit   ;enable mcp4013
mcp1
    bsf     mcp4013_udbit   ;do a decrement
    call    delay_1ms
    bcf     mcp4013_udbit
    call    delay_1ms
    decfsz  count,1
    goto    mcp1
    bsf     mcp4013_udbit   ;disbale change of mcp4013
    call    delay_1ms
    retlw   0x00
;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

set_mcp4013     ;xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
    movf    iset_mcp4013,w
    movwf   iset_mcp4013_A
    bsf     mcp4013_csbit   ;disable mcp4013
    bsf     mcp4013_udbit   ;prep for increment
    call    delay_1ms
    bcf     mcp4013_csbit   ;enable mcp4013
    call    delay_1ms
set1
    bcf     mcp4013_udbit
    call    delay_1ms
    bsf     mcp4013_udbit
    call    delay_1ms
    decfsz  iset_mcp4013_A
    goto    set1
    bsf     mcp4013_csbit   ;disable change of mcp4013
    call    delay_1ms
    retlw   0x00
    


    

 






; xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
delay_1ms
    movlw   0x05
    movwf   del1
b1
    movlw   0xFF
    movwf   del2
a1
    decfsz  del2,f
    goto    a1
    decfsz  del1,f
    goto    b1
    retlw   0x00

delay_10ms
    movlw   0x0A
    movwf   del1
b10
    movlw   0xFF
    movwf   del2
a10
    decfsz  del2,f
    goto    a10
    decfsz  del1,f
    goto    b10
    retlw   0x00


delay_ms
    movlw   0xFF
    movwf   del1
zzz
    movlw   0xFF
    movwf   del2
yyy
    decfsz  del2,f
    goto    yyy
    decfsz  del1,f
    goto    zzz
    retlw   0x00


    end
 

Much better but PLEASE lose all those macro blocks, they are not necessary and later in life you will regret them. If they have a single instruction it is easier to define them, like this:
change
Code:
ON1 macro               ;Turn on chan1
    bsf LATE,LATE1
    endm
to
Code:
#define ON1 bsf LATE,LATE1

it will work just the same, all you need to do is tell it what 'ON1' should be translated to.
When you have multiple instructions in a macro, change them into a subroutine. Just remove the macro and endm lines and add 'return' as the last instuction. When you use them, instead of (example) 'read_dips ' use 'call read_dips '.

You are missing a trick in the 'read_dips' routine by the way, if you add a first line of 'clrf dip8' so all the bits start off at zero, you can simplify the code by only setting the necessary bits, there would no longer be any need clear them individually.

Brian.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top