Subversion Repositories svnkaklik

Rev

Rev 874 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log

Rev 874 Rev 876
Line 1... Line 1...
1
#include <16F887.h>
1
#include <16F887.h>
2
#device *=16
2
#device *=16
3
#device adc=8
3
#device adc=8
4
#device PASS_STRINGS=IN_RAM
4
#device PASS_STRINGS=IN_RAM
5
#zero_ram
5
#zero_ram
6
#FUSES NOWDT                     //No Watch Dog Timer
6
#FUSES NOWDT                     //No Watch Dog Timer
7
#FUSES INTRC_IO                  //Internal RC Osc, no CLKOUT
7
#FUSES INTRC_IO                  //Internal RC Osc, no CLKOUT
8
#FUSES PUT                       //Power Up Timer
8
#FUSES PUT                       //Power Up Timer
9
#FUSES MCLR                      //Master Clear pin enabled
9
#FUSES MCLR                      //Master Clear pin enabled
10
#FUSES NOPROTECT                 //Code protected from reads
10
#FUSES NOPROTECT                 //Code protected from reads
11
#FUSES NOCPD                     //No EE protection
11
#FUSES NOCPD                     //No EE protection
12
#FUSES BROWNOUT                  //Reset when brownout detected
12
#FUSES BROWNOUT                  //Reset when brownout detected
13
#FUSES NOIESO                    //Internal External Switch Over mode disabled
13
#FUSES NOIESO                    //Internal External Switch Over mode disabled
14
#FUSES NOFCMEN                   //Fail-safe clock monitor disabled
14
#FUSES NOFCMEN                   //Fail-safe clock monitor disabled
15
#FUSES NOLVP                     //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
15
#FUSES NOLVP                     //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
16
#FUSES NODEBUG                   //No Debug mode for ICD
16
#FUSES NODEBUG                   //No Debug mode for ICD
17
#FUSES NOWRT                     //Program memory not write protected
17
#FUSES NOWRT                     //Program memory not write protected
18
#FUSES BORV21                    //Brownout reset at 2.1V
18
#FUSES BORV21                    //Brownout reset at 2.1V
19
#use delay(clock=8000000,RESTART_WDT)
19
#use delay(clock=8000000,RESTART_WDT)
20
#use rs232(baud=BAUDRATE,parity=N,xmit=TXD,rcv=RXD,bits=8,errors)
20
//#use rs232(baud=9600,parity=N,xmit=TXD,rcv=RXD,bits=8,errors)
21
 
21
#use rs232(baud=9600,parity=N,xmit=PIN_B7,rcv=PIN_B6,bits=8)
22
 
22
 
23
/*$F*************************************************************************
23
 
24
*
24
/*$F*************************************************************************
25
* Copyright (C)pa 2004 Mark Norton
25
*
26
*
26
* Copyright (C)pa 2004 Mark Norton
27
* Permission is hereby granted, free of charge, to any person obtaining
27
*
28
* a copy of this software and associated documentation files (the
28
* Permission is hereby granted, free of charge, to any person obtaining
29
* "Software"), to deal in the Software without restriction, including
29
* a copy of this software and associated documentation files (the
30
* without limitation the rights to use, copy, modify, merge, publish,
30
* "Software"), to deal in the Software without restriction, including
31
* distribute, sublicense, and/or sell copies of the Software, and to
31
* without limitation the rights to use, copy, modify, merge, publish,
32
* permit persons to whom the Software is furnished to do so, subject to
32
* distribute, sublicense, and/or sell copies of the Software, and to
33
* the following conditions:
33
* permit persons to whom the Software is furnished to do so, subject to
34
*
34
* the following conditions:
35
* The above copyright notice and this permission notice shall be included
35
*
36
* in all copies or substantial portions of the Software.
36
* The above copyright notice and this permission notice shall be included
37
*
37
* in all copies or substantial portions of the Software.
38
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
38
*
39
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
39
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
40
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
40
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
41
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
41
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
42
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
42
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
43
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
43
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
44
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
44
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
45
*
45
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
46
* Functional
46
*
47
* Description:  Implementation of sscanf() function for the CCS compiler
47
* Functional
48
*
48
* Description:  Implementation of sscanf() function for the CCS compiler
49
*****************************************************************************/
49
*
50
#include <string.h>
50
*****************************************************************************/
51
#include <stdlib.h>
51
#include <string.h>
52
 
52
#include <stdlib.h>
53
/* Uncomment any of these to reduce the code size
53
 
54
   Note that the HEX is a big hog
54
/* Uncomment any of these to reduce the code size
55
*/
55
   Note that the HEX is a big hog
56
#define NO_FLOAT
56
*/
57
// #define NO_INT
57
#define NO_FLOAT
58
// #define NO_SIGNED_INT
58
// #define NO_INT
59
// #define NO_STRING
59
// #define NO_SIGNED_INT
60
// #define NO_HEX
60
// #define NO_STRING
61
 
61
// #define NO_HEX
62
/* *************************************************************************
62
 
63
  DESCRIPTION:  Converts string pointed to by s to an unsigned long (16 bit)
63
/* *************************************************************************
64
  RETURN: result of the conversion
64
  DESCRIPTION:  Converts string pointed to by s to an unsigned long (16 bit)
65
  ALGORITHM:  none
65
  RETURN: result of the conversion
66
  NOTES:  the next position in the string is returned in endptr
66
  ALGORITHM:  none
67
 *************************************************************************** */
67
  NOTES:  the next position in the string is returned in endptr
68
long my_atoul(char *s, char *endptr, int base)
68
 *************************************************************************** */
69
{
69
long my_atoul(char *s, char *endptr, int base)
70
  signed long result;
70
{
71
  int index;
71
  signed long result;
72
  char c;
72
  int index;
73
 
73
  char c;
74
  index = 0;
74
 
75
  result = 0;
75
  index = 0;
76
  *endptr = s;
76
  result = 0;
77
 
77
  *endptr = s;
78
  if (( !s ) || ( !*s ))
78
 
79
    return ( 0 );
79
  if (( !s ) || ( !*s ))
80
 
80
    return ( 0 );
81
  c = *s;
81
 
82
 
82
  c = *s;
83
  // increase index if positive sign is detected
83
 
84
  if (c == '+')
84
  // increase index if positive sign is detected
85
  {
85
  if (c == '+')
86
    c = *(++s);
86
  {
87
  }
87
    c = *(++s);
88
 
88
  }
89
  // The number is a decimal number
89
 
90
  if (base == 10)
90
  // The number is a decimal number
91
  {
91
  if (base == 10)
92
    while (c >= '0' && c <= '9')
92
  {
93
    {
93
    while (c >= '0' && c <= '9')
94
      result = 10*result + (c - '0');
94
    {
95
      c = *(++s);
95
      result = 10*result + (c - '0');
96
    }
96
      c = *(++s);
97
  }
97
    }
98
  else if (base == 16)    // The number is a hexa number
98
  }
99
  {
99
  else if (base == 16)    // The number is a hexa number
100
    if (c == '0' && (*(s+1) == 'x' || *(s+1) == 'X'))
100
  {
101
    {
101
    if (c == '0' && (*(s+1) == 'x' || *(s+1) == 'X'))
102
      s += 2;
102
    {
103
      c = *s;
103
      s += 2;
104
    }
104
      c = *s;
105
 
105
    }
106
    c = toupper(c);
106
 
107
    while ( 1 )
107
    c = toupper(c);
108
    {
108
    while ( 1 )
109
      if (c >= '0' && c <= '9')
109
    {
110
        result = (result << 4) + (c - '0');
110
      if (c >= '0' && c <= '9')
111
      else if (c >= 'A' && c <='F')
111
        result = (result << 4) + (c - '0');
112
        result = (result << 4) + (c - 'A' + 10);
112
      else if (c >= 'A' && c <='F')
113
      else
113
        result = (result << 4) + (c - 'A' + 10);
114
        break;
114
      else
115
      c = toupper(*(++s));
115
        break;
116
    }
116
      c = toupper(*(++s));
117
  }
117
    }
118
  *endptr = s;
118
  }
119
  return(result);
119
  *endptr = s;
120
}
120
  return(result);
121
 
121
}
122
/* *************************************************************************
122
 
123
  DESCRIPTION:  Converts string pointed to by s to a float
123
/* *************************************************************************
124
  RETURN: result of the conversion
124
  DESCRIPTION:  Converts string pointed to by s to a float
125
  ALGORITHM:  none
125
  RETURN: result of the conversion
126
  NOTES:  the next position in the string is returned in endptr
126
  ALGORITHM:  none
127
 *************************************************************************** */
127
  NOTES:  the next position in the string is returned in endptr
128
float my_atof(char * s, char *endptr)
128
 *************************************************************************** */
129
{
129
float my_atof(char * s, char *endptr)
130
  float pow10 = 1.0;
130
{
131
  float result = 0.0;
131
  float pow10 = 1.0;
132
  int sign = 0;
132
  float result = 0.0;
133
  char c;
133
  int sign = 0;
134
 
134
  char c;
135
  c = *s;
135
 
136
 
136
  c = *s;
137
  if(c == '-')
137
 
138
  {
138
  if(c == '-')
139
    sign = 1;
139
  {
140
    c = *(++s);
140
    sign = 1;
141
  }
141
    c = *(++s);
142
  else if(c == '+')
142
  }
143
    c = *(++s);
143
  else if(c == '+')
144
 
144
    c = *(++s);
145
 
145
 
146
  while((c >= '0' && c <= '9'))
146
 
147
  {
147
  while((c >= '0' && c <= '9'))
148
    result = 10*result + c - '0';
148
  {
149
    c = *(++s);
149
    result = 10*result + c - '0';
150
  }
150
    c = *(++s);
151
 
151
  }
152
  if (c == '.')
152
 
153
  {
153
  if (c == '.')
154
    c = *(++s);
154
  {
155
    while((c >= '0' && c <= '9'))
155
    c = *(++s);
156
    {
156
    while((c >= '0' && c <= '9'))
157
      pow10 = pow10*10;
157
    {
158
      result += (c - '0')/pow10;
158
      pow10 = pow10*10;
159
      c = *(++s);
159
      result += (c - '0')/pow10;
160
    }
160
      c = *(++s);
161
  }
161
    }
162
 
162
  }
163
   if (sign == 1)
163
 
164
     result = -1*result;
164
   if (sign == 1)
165
 
165
     result = -1*result;
166
  *endptr = s;
166
 
167
  return(result);
167
  *endptr = s;
168
}
168
  return(result);
169
 
169
}
170
 
170
 
171
/* *************************************************************************
171
 
172
  DESCRIPTION:  Implementation of scanf() using CCS C compiler
172
/* *************************************************************************
173
  RETURN: total number of arguments read
173
  DESCRIPTION:  Implementation of scanf() using CCS C compiler
174
  ALGORITHM:  none
174
  RETURN: total number of arguments read
175
  NOTES:  none
175
  ALGORITHM:  none
176
 *************************************************************************** */
176
  NOTES:  none
177
int8 sscanf(
177
 *************************************************************************** */
178
  char  *buf,    /* pointer to the buffer that we are scanning */
178
int8 sscanf(
179
  char  *fmt,    /* pointer to the format string */
179
  char  *buf,    /* pointer to the buffer that we are scanning */
180
  char  *pArgs)  /* pointer to array of arguments */
180
  char  *fmt,    /* pointer to the format string */
181
{
181
  char  *pArgs)  /* pointer to array of arguments */
182
 
182
{
183
  int8        count = 0;
183
 
184
  char        *p;
184
  int8        count = 0;
185
  int1        size_long;
185
  char        *p;
186
  int1        sign;
186
  int1        size_long;
187
  char        *endptr;
187
  int1        sign;
188
 
188
  char        *endptr;
189
  while (1)
189
 
190
  {
190
  while (1)
191
   /* Look to see if we are out of arguments */
191
  {
192
   if ( !pArgs )
192
   /* Look to see if we are out of arguments */
193
     return( count );
193
   if ( !pArgs )
194
 
194
     return( count );
195
   /* Gobble up the fmt string */
195
 
196
    while (*buf == *fmt)
196
   /* Gobble up the fmt string */
197
    {
197
    while (*buf == *fmt)
198
      if ((*buf == 0) || (*fmt == 0))
198
    {
199
        return (count);
199
      if ((*buf == 0) || (*fmt == 0))
200
      buf++;
200
        return (count);
201
      fmt++;
201
      buf++;
202
    }
202
      fmt++;
203
 
203
    }
204
    /* Check for the % */
204
 
205
    if (*fmt != '%')
205
    /* Check for the % */
206
      break;
206
    if (*fmt != '%')
207
 
207
      break;
208
    /* fmt should be '%' go to the next character */
208
 
209
    fmt++;
209
    /* fmt should be '%' go to the next character */
210
 
210
    fmt++;
211
    /* get the size modifier */
211
 
212
    switch (*fmt)
212
    /* get the size modifier */
213
    {
213
    switch (*fmt)
214
      case 'l':
214
    {
215
      case 'L':
215
      case 'l':
216
        fmt++;
216
      case 'L':
217
        size_long = TRUE;
217
        fmt++;
218
        break;
218
        size_long = TRUE;
219
      default:
219
        break;
220
        size_long = FALSE;
220
      default:
221
        break;
221
        size_long = FALSE;
222
    }
222
        break;
223
 
223
    }
224
    /* fmt should point to our first conversion letter at this point */
224
 
225
    switch (*fmt)
225
    /* fmt should point to our first conversion letter at this point */
226
    {
226
    switch (*fmt)
227
#ifndef NO_FLOAT
227
    {
228
      case 'f':
228
#ifndef NO_FLOAT
229
      case 'F':
229
      case 'f':
230
        /* Get a pointer to this argument */
230
      case 'F':
231
        p = (float *)(*pArgs);
231
        /* Get a pointer to this argument */
232
 
232
        p = (float *)(*pArgs);
233
        /* convert to a number */
233
 
234
        *(float *)p = (float)my_atof(buf, &endptr);
234
        /* convert to a number */
235
 
235
        *(float *)p = (float)my_atof(buf, &endptr);
236
        /* Make sure that we succeeded */
236
 
237
        if ( buf == endptr )
237
        /* Make sure that we succeeded */
238
          return ( count );
238
        if ( buf == endptr )
239
        buf = endptr;
239
          return ( count );
240
 
240
        buf = endptr;
241
        /* count this one */
241
 
242
        count++;
242
        /* count this one */
243
        break;
243
        count++;
244
#endif
244
        break;
245
#ifndef NO_SIGNED_INT
245
#endif
246
      case 'd':
246
#ifndef NO_SIGNED_INT
247
      case 'D':
247
      case 'd':
248
        /* Get a pointer to this argument */
248
      case 'D':
249
        p = (signed int8 *)(*pArgs);
249
        /* Get a pointer to this argument */
250
        if (*buf == '-')
250
        p = (signed int8 *)(*pArgs);
251
        {
251
        if (*buf == '-')
252
          buf++;
252
        {
253
          sign = TRUE;
253
          buf++;
254
        }
254
          sign = TRUE;
255
        else
255
        }
256
          sign = FALSE;
256
        else
257
 
257
          sign = FALSE;
258
        /* convert to a number */
258
 
259
        if ( size_long )
259
        /* convert to a number */
260
        {
260
        if ( size_long )
261
          *(signed int16 *)p = (signed int16)my_atoul(buf, &endptr, 10);
261
        {
262
          if (sign)
262
          *(signed int16 *)p = (signed int16)my_atoul(buf, &endptr, 10);
263
            *(signed int16 *)p = -(*(signed int16 *)p);
263
          if (sign)
264
        }
264
            *(signed int16 *)p = -(*(signed int16 *)p);
265
        else
265
        }
266
        {
266
        else
267
          *(signed int8 *)p = (signed int8)my_atoul(buf, &endptr, 10);
267
        {
268
          if (sign)
268
          *(signed int8 *)p = (signed int8)my_atoul(buf, &endptr, 10);
269
            *(signed int8 *)p = -(*(signed int8 *)p);
269
          if (sign)
270
        }
270
            *(signed int8 *)p = -(*(signed int8 *)p);
271
        /* Make sure that we succeeded */
271
        }
272
        if ( buf == endptr )
272
        /* Make sure that we succeeded */
273
          return ( count );
273
        if ( buf == endptr )
274
        buf = endptr;
274
          return ( count );
275
 
275
        buf = endptr;
276
        /* count this one */
276
 
277
        count++;
277
        /* count this one */
278
        break;
278
        count++;
279
#endif
279
        break;
280
#ifndef NO_INT
280
#endif
281
      case 'u':
281
#ifndef NO_INT
282
      case 'U':
282
      case 'u':
283
        /* Get a pointer to this argument */
283
      case 'U':
284
        p = (int8 *)(*pArgs);
284
        /* Get a pointer to this argument */
285
 
285
        p = (int8 *)(*pArgs);
286
        /* convert to a number */
286
 
287
        if ( size_long )
287
        /* convert to a number */
288
          *(int16 *)p = (int16) my_atoul(buf, &endptr, 10);
288
        if ( size_long )
289
        else
289
          *(int16 *)p = (int16) my_atoul(buf, &endptr, 10);
290
          *(int8 *)p = (int8) my_atoul(buf, &endptr, 10);
290
        else
291
 
291
          *(int8 *)p = (int8) my_atoul(buf, &endptr, 10);
292
        /* Make sure that we succeeded */
292
 
293
        if ( buf == endptr )
293
        /* Make sure that we succeeded */
294
          return ( count );
294
        if ( buf == endptr )
295
        buf = endptr;
295
          return ( count );
296
 
296
        buf = endptr;
297
        /* count this one */
297
 
298
        count++;
298
        /* count this one */
299
        break;
299
        count++;
300
#endif
300
        break;
301
#ifndef NO_STRING
301
#endif
302
      case 's':
302
#ifndef NO_STRING
303
        /* Get a pointer to this argument */
303
      case 's':
304
        p = (char *)(*pArgs);
304
        /* Get a pointer to this argument */
305
 
305
        p = (char *)(*pArgs);
306
        /* copy the chars */
306
 
307
        while (1)
307
        /* copy the chars */
308
        {
308
        while (1)
309
          if ((isspace(*buf)) || (!*buf))
309
        {
310
          {
310
          if ((isspace(*buf)) || (!*buf))
311
            *p = 0;
311
          {
312
            break;
312
            *p = 0;
313
          }
313
            break;
314
          else
314
          }
315
          {
315
          else
316
            *p = *buf;
316
          {
317
            p++;
317
            *p = *buf;
318
            buf++;
318
            p++;
319
          }
319
            buf++;
320
        }
320
          }
321
 
321
        }
322
        /* count this one */
322
 
323
        count++;
323
        /* count this one */
324
        break;
324
        count++;
325
#endif
325
        break;
326
#ifndef NO_HEX
326
#endif
327
      case 'x':
327
#ifndef NO_HEX
328
      case 'X':
328
      case 'x':
329
        /* Get a pointer to this argument */
329
      case 'X':
330
        p = (int8 *)(*pArgs);
330
        /* Get a pointer to this argument */
331
 
331
        p = (int8 *)(*pArgs);
332
        /* convert to a number */
332
 
333
        if ( size_long )
333
        /* convert to a number */
334
          *(int16 *)p = (int16) my_atoul(buf, &endptr, 16);
334
        if ( size_long )
335
        else
335
          *(int16 *)p = (int16) my_atoul(buf, &endptr, 16);
336
          *(int8 *)p = (int8) my_atoul(buf, &endptr, 16);
336
        else
337
 
337
          *(int8 *)p = (int8) my_atoul(buf, &endptr, 16);
338
        /* Make sure that we succeeded */
338
 
339
        if ( buf == endptr )
339
        /* Make sure that we succeeded */
340
          return ( count );
340
        if ( buf == endptr )
341
        buf = endptr;
341
          return ( count );
342
 
342
        buf = endptr;
343
        /* count this one */
343
 
344
        count++;
344
        /* count this one */
345
        break;
345
        count++;
346
#endif
346
        break;
347
      /* unhandled format specifier */
347
#endif
348
      default:
348
      /* unhandled format specifier */
349
        return (count);
349
      default:
350
    }
350
        return (count);
351
    /* Technically this is incorrect but since the size of all pointers
351
    }
352
       are the same, who cares ;)
352
    /* Technically this is incorrect but since the size of all pointers
353
 
353
       are the same, who cares ;)
354
       point to the next argument
354
 
355
    */
355
       point to the next argument
356
    pArgs += sizeof(char *);
356
    */
357
   
357
    pArgs += sizeof(char *);
358
    /* Move to the next format char */
358
   
359
    fmt++;
359
    /* Move to the next format char */
360
  }
360
    fmt++;
361
 
361
  }
362
  return (count);
362
 
363
}
363
  return (count);
364
 
364
}
365
 
365
 
366
 
366
 
367
void main() {
367
 
368
    char x;
368
void main() {
369
    char sbuffer[64];
369
    char x;
370
    char cmd=0,param=0;
370
    char sbuffer[64];
371
    long l;
371
    char cmd=0,param=0;
372
 
372
    long l;
373
    set_tris_a(0x80);
373
 
374
    port_b_pullups(0xFF);
374
    set_tris_a(0x80);
375
    set_tris_b(0xFF);
375
    port_b_pullups(0xFF);
376
    set_tris_d(0);
376
    set_tris_b(0xFF);
377
    set_tris_e(0x01);
377
    set_tris_d(0);
378
    setup_oscillator(OSC_8MHZ);
378
    set_tris_e(0x01);
379
    setup_adc_ports(sAN5|VSS_VDD);
379
    setup_oscillator(OSC_8MHZ);
380
    setup_adc(ADC_CLOCK_INTERNAL);
380
    setup_adc_ports(sAN5|VSS_VDD);
381
    set_adc_channel(5);
381
    setup_adc(ADC_CLOCK_INTERNAL);
382
    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
382
    set_adc_channel(5);
383
    setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);
383
    setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
384
    setup_timer_2(T2_DIV_BY_16, 155, 1);
384
    setup_timer_1(T1_INTERNAL|T1_DIV_BY_2);
385
    setup_ccp1(CCP_PWM);
385
    setup_timer_2(T2_DIV_BY_16, 155, 1);
386
    set_pwm1_duty(75);
386
    setup_ccp1(CCP_PWM);
387
    setup_comparator(NC_NC_NC_NC);
387
    set_pwm1_duty(75);
388
 
388
    setup_comparator(NC_NC_NC_NC);
389
sprintf(sbuffer,"/S30\n\");
389
 
390
x = sscanf(sbuffer,"/%C%d", &cmd, &param);
390
sprintf(sbuffer,"/S30\n\");
391
printf("\n\rsscanf sez: %u found, cmd %X param %X\n\r",x,cmd,param);
391
x = sscanf(sbuffer,"/%C%d", &cmd, &param);
392
 
392
printf("\n\rsscanf sez: %u found, cmd %X param %X\n\r",x,cmd,param);
393
} 
393
 
-
 
394
}