Subversion Repositories svnkaklik

Rev

Rev 876 | Go to most recent revision | Details | Last modification | View Log

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