Simple subtraction


Closed Thread
Results 1 to 9 of 9
  1. #1

    Default Simple subtraction

    There must be something wrong in my understanding of this but I'm simply trying to
    decrement the value of a variable by 1.

    This is part of the code and all the variables are bytes.

    I'm using a temperature probe and when it gets to a preset temperature, LCD indicates ready.
    All that is working fine but when I create a variable "cunder" that should be 1 degree less I don't get my high at portb.0

    All the temp readings are fine etc -- lots of code I'm not showing -- but it must be "cunder".

    Other languages I've used you can simply subtract a 1 like I did. (98 becomes 97 etc).

    Thanks much for any help.


    if (c = 1) and (CT >= tempsetc) then
    high portb.3
    cunder = (tempsetc - 1)
    rdy = 1
    LCDOUT $FE,1, " READY "
    LCDOUT $FE,$C0," Press Start" 'all this works fine
    endif

    if (c = 1) and (ct <= cunder) then
    high portb.0 'no high with my probe at lower temp
    endif

  2. #2
    Join Date
    May 2013
    Location
    australia
    Posts
    2,388


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    this piece of code indicates that subtracting 1 from a var works as expected, providing vars are indeed bytes and no overflows occur

    Code:
    cunder var byte
    tempsetc var byte
    ct      var byte
    ct=50
    pause 2000
    Debug "Start",13 ,10    
         
        for   tempsetc = 1   to 255
            cunder = (tempsetc - 1)  
            Debug 13 ,10,"cunder = " ,#tempsetc," - 1 = ", #cunder 
            if ct <= cunder then
                Debug 9, #ct ," <= ", #cunder
            else
                Debug 9, #ct ," > ", #cunder
            endif
            next    
     stop
    result

    Code:
    Start
    cunder = 1 - 1 = 0 50 > 0
    cunder = 2 - 1 = 1 50 > 1
    cunder = 3 - 1 = 2 50 > 2
    cunder = 4 - 1 = 3 50 > 3
    cunder = 5 - 1 = 4 50 > 4
    cunder = 6 - 1 = 5 50 > 5
    cunder = 7 - 1 = 6 50 > 6
    cunder = 8 - 1 = 7 50 > 7
    cunder = 9 - 1 = 8 50 > 8
    cunder = 10 - 1 = 9 50 > 9
    cunder = 11 - 1 = 10 50 > 10
    cunder = 12 - 1 = 11 50 > 11
    cunder = 13 - 1 = 12 50 > 12
    cunder = 14 - 1 = 13 50 > 13
    cunder = 15 - 1 = 14 50 > 14
    cunder = 16 - 1 = 15 50 > 15
    cunder = 17 - 1 = 16 50 > 16
    cunder = 18 - 1 = 17 50 > 17
    cunder = 19 - 1 = 18 50 > 18
    cunder = 20 - 1 = 19 50 > 19
    cunder = 21 - 1 = 20 50 > 20
    cunder = 22 - 1 = 21 50 > 21
    cunder = 23 - 1 = 22 50 > 22
    cunder = 24 - 1 = 23 50 > 23
    cunder = 25 - 1 = 24 50 > 24
    cunder = 26 - 1 = 25 50 > 25
    cunder = 27 - 1 = 26 50 > 26
    cunder = 28 - 1 = 27 50 > 27
    cunder = 29 - 1 = 28 50 > 28
    cunder = 30 - 1 = 29 50 > 29
    cunder = 31 - 1 = 30 50 > 30
    cunder = 32 - 1 = 31 50 > 31
    cunder = 33 - 1 = 32 50 > 32
    cunder = 34 - 1 = 33 50 > 33
    cunder = 35 - 1 = 34 50 > 34
    cunder = 36 - 1 = 35 50 > 35
    cunder = 37 - 1 = 36 50 > 36
    cunder = 38 - 1 = 37 50 > 37
    cunder = 39 - 1 = 38 50 > 38
    cunder = 40 - 1 = 39 50 > 39
    cunder = 41 - 1 = 40 50 > 40
    cunder = 42 - 1 = 41 50 > 41
    cunder = 43 - 1 = 42 50 > 42
    cunder = 44 - 1 = 43 50 > 43
    cunder = 45 - 1 = 44 50 > 44
    cunder = 46 - 1 = 45 50 > 45
    cunder = 47 - 1 = 46 50 > 46
    cunder = 48 - 1 = 47 50 > 47
    cunder = 49 - 1 = 48 50 > 48
    cunder = 50 - 1 = 49 50 > 49
    cunder = 51 - 1 = 50 50 <= 50
    cunder = 52 - 1 = 51 50 <= 51
    cunder = 53 - 1 = 52 50 <= 52
    cunder = 54 - 1 = 53 50 <= 53
    cunder = 55 - 1 = 54 50 <= 54
    cunder = 56 - 1 = 55 50 <= 55
    cunder = 57 - 1 = 56 50 <= 56
    cunder = 58 - 1 = 57 50 <= 57
    cunder = 59 - 1 = 58 50 <= 58
    cunder = 60 - 1 = 59 50 <= 59
    cunder = 61 - 1 = 60 50 <= 60
    cunder = 62 - 1 = 61 50 <= 61
    cunder = 63 - 1 = 62 50 <= 62
    cunder = 64 - 1 = 63 50 <= 63
    cunder = 65 - 1 = 64 50 <= 64
    cunder = 66 - 1 = 65 50 <= 65
    cunder = 67 - 1 = 66 50 <= 66
    cunder = 68 - 1 = 67 50 <= 67
    cunder = 69 - 1 = 68 50 <= 68
    cunder = 70 - 1 = 69 50 <= 69
    cunder = 71 - 1 = 70 50 <= 70
    cunder = 72 - 1 = 71 50 <= 71
    cunder = 73 - 1 = 72 50 <= 72
    cunder = 74 - 1 = 73 50 <= 73
    cunder = 75 - 1 = 74 50 <= 74
    cunder = 76 - 1 = 75 50 <= 75
    cunder = 77 - 1 = 76 50 <= 76
    cunder = 78 - 1 = 77 50 <= 77
    cunder = 79 - 1 = 78 50 <= 78
    cunder = 80 - 1 = 79 50 <= 79
    cunder = 81 - 1 = 80 50 <= 80
    cunder = 82 - 1 = 81 50 <= 81
    cunder = 83 - 1 = 82 50 <= 82
    cunder = 84 - 1 = 83 50 <= 83
    cunder = 85 - 1 = 84 50 <= 84
    cunder = 86 - 1 = 85 50 <= 85
    cunder = 87 - 1 = 86 50 <= 86
    cunder = 88 - 1 = 87 50 <= 87
    cunder = 89 - 1 = 88 50 <= 88
    cunder = 90 - 1 = 89 50 <= 89
    cunder = 91 - 1 = 90 50 <= 90
    cunder = 92 - 1 = 91 50 <= 91
    cunder = 93 - 1 = 92 50 <= 92
    cunder = 94 - 1 = 93 50 <= 93
    cunder = 95 - 1 = 94 50 <= 94
    cunder = 96 - 1 = 95 50 <= 95
    cunder = 97 - 1 = 96 50 <= 96
    cunder = 98 - 1 = 97 50 <= 97
    cunder = 99 - 1 = 98 50 <= 98
    cunder = 100 - 1 = 99 50 <= 99
    cunder = 101 - 1 = 100 50 <= 100
    cunder = 102 - 1 = 101 50 <= 101
    cunder = 103 - 1 = 102 50 <= 102
    cunder = 104 - 1 = 103 50 <= 103
    cunder = 105 - 1 = 104 50 <= 104
    cunder = 106 - 1 = 105 50 <= 105
    cunder = 107 - 1 = 106 50 <= 106
    cunder = 108 - 1 = 107 50 <= 107
    cunder = 109 - 1 = 108 50 <= 108
    cunder = 110 - 1 = 109 50 <= 109
    cunder = 111 - 1 = 110 50 <= 110
    cunder = 112 - 1 = 111 50 <= 111
    cunder = 113 - 1 = 112 50 <= 112
    cunder = 114 - 1 = 113 50 <= 113
    cunder = 115 - 1 = 114 50 <= 114
    cunder = 116 - 1 = 115 50 <= 115
    cunder = 117 - 1 = 116 50 <= 116
    cunder = 118 - 1 = 117 50 <= 117
    cunder = 119 - 1 = 118 50 <= 118
    cunder = 120 - 1 = 119 50 <= 119
    cunder = 121 - 1 = 120 50 <= 120
    cunder = 122 - 1 = 121 50 <= 121
    cunder = 123 - 1 = 122 50 <= 122
    cunder = 124 - 1 = 123 50 <= 123
    cunder = 125 - 1 = 124 50 <= 124
    cunder = 126 - 1 = 125 50 <= 125
    cunder = 127 - 1 = 126 50 <= 126
    cunder = 128 - 1 = 127 50 <= 127
    cunder = 129 - 1 = 128 50 <= 128
    cunder = 130 - 1 = 129 50 <= 129
    cunder = 131 - 1 = 130 50 <= 130
    cunder = 132 - 1 = 131 50 <= 131
    cunder = 133 - 1 = 132 50 <= 132
    cunder = 134 - 1 = 133 50 <= 133
    cunder = 135 - 1 = 134 50 <= 134
    cunder = 136 - 1 = 135 50 <= 135
    cunder = 137 - 1 = 136 50 <= 136
    cunder = 138 - 1 = 137 50 <= 137
    cunder = 139 - 1 = 138 50 <= 138
    cunder = 140 - 1 = 139 50 <= 139
    cunder = 141 - 1 = 140 50 <= 140
    cunder = 142 - 1 = 141 50 <= 141
    cunder = 143 - 1 = 142 50 <= 142
    cunder = 144 - 1 = 143 50 <= 143
    cunder = 145 - 1 = 144 50 <= 144
    cunder = 146 - 1 = 145 50 <= 145
    cunder = 147 - 1 = 146 50 <= 146
    cunder = 148 - 1 = 147 50 <= 147
    cunder = 149 - 1 = 148 50 <= 148
    cunder = 150 - 1 = 149 50 <= 149
    cunder = 151 - 1 = 150 50 <= 150
    cunder = 152 - 1 = 151 50 <= 151
    cunder = 153 - 1 = 152 50 <= 152
    cunder = 154 - 1 = 153 50 <= 153
    cunder = 155 - 1 = 154 50 <= 154
    cunder = 156 - 1 = 155 50 <= 155
    cunder = 157 - 1 = 156 50 <= 156
    cunder = 158 - 1 = 157 50 <= 157
    cunder = 159 - 1 = 158 50 <= 158
    cunder = 160 - 1 = 159 50 <= 159
    cunder = 161 - 1 = 160 50 <= 160
    cunder = 162 - 1 = 161 50 <= 161
    cunder = 163 - 1 = 162 50 <= 162
    cunder = 164 - 1 = 163 50 <= 163
    cunder = 165 - 1 = 164 50 <= 164
    cunder = 166 - 1 = 165 50 <= 165
    cunder = 167 - 1 = 166 50 <= 166
    cunder = 168 - 1 = 167 50 <= 167
    cunder = 169 - 1 = 168 50 <= 168
    cunder = 170 - 1 = 169 50 <= 169
    cunder = 171 - 1 = 170 50 <= 170
    cunder = 172 - 1 = 171 50 <= 171
    cunder = 173 - 1 = 172 50 <= 172
    cunder = 174 - 1 = 173 50 <= 173
    cunder = 175 - 1 = 174 50 <= 174
    cunder = 176 - 1 = 175 50 <= 175
    cunder = 177 - 1 = 176 50 <= 176
    cunder = 178 - 1 = 177 50 <= 177
    cunder = 179 - 1 = 178 50 <= 178
    cunder = 180 - 1 = 179 50 <= 179
    cunder = 181 - 1 = 180 50 <= 180
    cunder = 182 - 1 = 181 50 <= 181
    cunder = 183 - 1 = 182 50 <= 182
    cunder = 184 - 1 = 183 50 <= 183
    cunder = 185 - 1 = 184 50 <= 184
    cunder = 186 - 1 = 185 50 <= 185
    cunder = 187 - 1 = 186 50 <= 186
    cunder = 188 - 1 = 187 50 <= 187
    cunder = 189 - 1 = 188 50 <= 188
    cunder = 190 - 1 = 189 50 <= 189
    cunder = 191 - 1 = 190 50 <= 190
    cunder = 192 - 1 = 191 50 <= 191
    cunder = 193 - 1 = 192 50 <= 192
    cunder = 194 - 1 = 193 50 <= 193
    cunder = 195 - 1 = 194 50 <= 194
    cunder = 196 - 1 = 195 50 <= 195
    cunder = 197 - 1 = 196 50 <= 196
    cunder = 198 - 1 = 197 50 <= 197
    cunder = 199 - 1 = 198 50 <= 198
    cunder = 200 - 1 = 199 50 <= 199
    cunder = 201 - 1 = 200 50 <= 200
    cunder = 202 - 1 = 201 50 <= 201
    cunder = 203 - 1 = 202 50 <= 202
    cunder = 204 - 1 = 203 50 <= 203
    cunder = 205 - 1 = 204 50 <= 204
    cunder = 206 - 1 = 205 50 <= 205
    cunder = 207 - 1 = 206 50 <= 206
    cunder = 208 - 1 = 207 50 <= 207
    cunder = 209 - 1 = 208 50 <= 208
    cunder = 210 - 1 = 209 50 <= 209
    cunder = 211 - 1 = 210 50 <= 210
    cunder = 212 - 1 = 211 50 <= 211
    cunder = 213 - 1 = 212 50 <= 212
    cunder = 214 - 1 = 213 50 <= 213
    cunder = 215 - 1 = 214 50 <= 214
    cunder = 216 - 1 = 215 50 <= 215
    cunder = 217 - 1 = 216 50 <= 216
    cunder = 218 - 1 = 217 50 <= 217
    cunder = 219 - 1 = 218 50 <= 218
    cunder = 220 - 1 = 219 50 <= 219
    cunder = 221 - 1 = 220 50 <= 220
    cunder = 222 - 1 = 221 50 <= 221
    cunder = 223 - 1 = 222 50 <= 222
    cunder = 224 - 1 = 223 50 <= 223
    cunder = 225 - 1 = 224 50 <= 224
    cunder = 226 - 1 = 225 50 <= 225
    cunder = 227 - 1 = 226 50 <= 226
    cunder = 228 - 1 = 227 50 <= 227
    cunder = 229 - 1 = 228 50 <= 228
    cunder = 230 - 1 = 229 50 <= 229
    cunder = 231 - 1 = 230 50 <= 230
    cunder = 232 - 1 = 231 50 <= 231
    cunder = 233 - 1 = 232 50 <= 232
    cunder = 234 - 1 = 233 50 <= 233
    cunder = 235 - 1 = 234 50 <= 234
    cunder = 236 - 1 = 235 50 <= 235
    cunder = 237 - 1 = 236 50 <= 236
    cunder = 238 - 1 = 237 50 <= 237
    cunder = 239 - 1 = 238 50 <= 238
    cunder = 240 - 1 = 239 50 <= 239
    cunder = 241 - 1 = 240 50 <= 240
    cunder = 242 - 1 = 241 50 <= 241
    cunder = 243 - 1 = 242 50 <= 242
    cunder = 244 - 1 = 243 50 <= 243
    cunder = 245 - 1 = 244 50 <= 244
    cunder = 246 - 1 = 245 50 <= 245
    cunder = 247 - 1 = 246 50 <= 246
    cunder = 248 - 1 = 247 50 <= 247
    cunder = 249 - 1 = 248 50 <= 248
    cunder = 250 - 1 = 249 50 <= 249
    cunder = 251 - 1 = 250 50 <= 250
    cunder = 252 - 1 = 251 50 <= 251
    cunder = 253 - 1 = 252 50 <= 252
    cunder = 254 - 1 = 253 50 <= 253
    cunder = 255 - 1 = 254 50 <= 254

    the problem lies elsewhere

    you need to provide a compliable example that demonstrates the problem
    Warning I'm not a teacher

  3. #3
    Join Date
    Oct 2009
    Posts
    583


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    Michael, it might be handy if you posted all your code so we could see how the rest of it is meant to work - you need to use the [ CODE ] and [ / CODE ] (with out spaces inside the brackets) to make it clearer.

    I use the following to read a DS18B20 temperature probe and display the temperature on an LCD. I then have the routine below to trigger a pin that drives a fan when the temperature exceeds a set value - you could always change the > for < to trigger the condition you want

    Code:
    '*******************************************************************************
    'Get and display the temperature
    GIE = 0                                 ' turn off interupts
    owin DQ, 1, [],NoDevice                 ' pole 1 wire bus - if no responce then device not pressent
    OWOUT DQ, 1, [$CC, $44]                 ' Start temperature conversion
    OWOUT DQ, 1, [$CC, $BE]                 ' Read the temperature
    OWIN DQ, 0, [temperature.LOWBYTE, temperature.HIGHBYTE]
    GIE = 1                                 ' turn interupts back on
    temperature = temperature */ 1600 
    lcdout $FE,$D4+0,"TEMP ",dec(temperature / 100),$DF,"C"
    
    '******************************************************************************* '
    fan:'check to see if fan is needed
    fantrigger=fanset
    if (temperature / 100)> fantrigger then 
    high portA.4
    endif

  4. #4
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    And remember that BYTEs and WORDS are unsigned so doing something like a "less than 0" will never ever return true because a BYTE or WORD will always be 0 or more in that context.

    /Henrik.

  5. #5


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    Ok thanks -- I'll look at it closer -- at least I know it should work with a minus 1.

    I use Micro code studio by the way. Is there another IDE that is straightforward like it is but has a few more features? For example, you can't even comment out a block of code, just individual lines.

    Also, I have never really had any experience with simulators. My Picbasic interest sort of peaked 20 years ago (although I still think it's fantastic) and back then simulation was a bad word.

    Be nice if they came up with something that could scroll through your code and detect operational errors. ?

  6. #6


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    Here comes a dumb question --

    var1 = word
    var2 = byte

    var1 = 2
    var2 = 3

    Can't you compare a word to a byte?

    If var1 <= var2 then
    do this

    It's in my head that a word is a number with the capability of larger numbers but also higher resolution for smaller numbers?
    (More decimal places?) So a 2 as a word variable is the same as a byte.

    Wrong? Do I need to use var.highbyte instead ? (hobbyist -- so I forget this stuff).

  7. #7
    Join Date
    Aug 2003
    Posts
    985


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    You can use a word somewhat like a float, but essentially, I believe you can compare those two types.
    Even if they didn’t directly compare, you could compare the byte to the lower word byte, and if the upper word byte is zero,
    the first compare with he lower byte is valid. otherwise if the upper word byte is not zero, the word is higher.

  8. #8
    Join Date
    Oct 2005
    Location
    Sweden
    Posts
    3,520


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    In PBP all variable types stores integer values only so only the size of the number they can hold changes. The resolution is always the same.
    I see no, and don't know of any, issues with comparing a BYTE to WORD or the other way around but again remember that BYTES and WORDS are always unsigned integers so a variable containing 253 will be smaller than a BYTE (or WORD) variable containin the result of 100-101.

    /Henrik.

  9. #9


    Did you find this post helpful? Yes | No

    Default Re: Simple subtraction

    Quote Originally Posted by Michael View Post
    Ok thanks -- I'll look at it closer -- at least I know it should work with a minus 1.

    I use Micro code studio by the way. Is there another IDE that is straightforward like it is but has a few more features? For example, you can't even comment out a block of code, just individual lines.
    Hightlite the block of code, then click comment - all lines will then be commented out - same in reverse to uncomment

Similar Threads

  1. Replies: 4
    Last Post: - 2nd November 2012, 09:47
  2. This should be simple?
    By jderson in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 29th September 2008, 21:20
  3. Addition and subtraction
    By Christopher4187 in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 25th January 2008, 14:24
  4. This is probably quite simple
    By Russ Kincaid in forum mel PIC BASIC
    Replies: 11
    Last Post: - 9th August 2006, 14:30
  5. Simple If.. Then help:
    By Tear in forum mel PIC BASIC Pro
    Replies: 6
    Last Post: - 17th June 2005, 20:05

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts