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.

How to set oscillator frequency in microchip C (free XC8)

Status
Not open for further replies.
T

treez

Guest
Hello,
I need to move 0x50 into the OSCCON register , but in C, the command "OSCCON = 0x50" gives an error.
Do you know how I do this, the manual does not say?

Here is my code so far..its for pic18f65k22 and is in mplab.x

Code C - [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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
// * File:   3CH TEST.c
 
//;Code for 3 channel led driver on the 'updated_550587' PCB
//;WE ARE TRUNCATING THE ADC REGISTER SO JUST USE UPPER 8 BITS
//This code assumes LED MCPCB thermistors with B=3970 and 10K @ 25degC
//Make sure the current level is never changed more than about 4 times
//per second.
 
 
//;DEFINE INPUTS FROM DIPSWITCH
//;Note that the new board has pullups on the dipswitch,
//;whereas 550587 PCB had pull downs, thus logic is reversed.
//; 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 ADCON0=0x10
//;RF1 = AN6 ADCON0=0x18
//;RF2 = AN7
//;RF3 = AN8
//;RF4 = AN9
//;RF5 = AN10 ADCON0=0x28
//;RF6 = AN11 ADCON0=0x2C
//;RF7 = AN5  ADCON0=0x1C
//
//;**********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 =    RE4
//;Shutdown-Ch3 =  RG0
//
//;NCNCNCNCNCNCNCNCNC LIST OF NON CONNECTED PINS:
//;RA0
//;RA1
//;RA6
//;RA7
//;    RB6  = PGC
//;    RB7  = PGD
//;RC0
//;RC6
//;    RD0
//;    RD1
//;    RD2
//;    RD3
//;    RD4
//;RE2
//;RE3
//;RE5
//;RE7
//;    RG1
//;    RG3
//;    RG4/*
#include <stdint.h>
#include <xc.h>
 
void    set_ana_dig_ports(void);
void    disable_interrupts(void);
void    disable_pullups(void);
void    disable_opendrain(void);
void    disable_comparators(void);
void    setup_adc(void);
void    setup_ports(void);
void    NC_pins_low(void);  //make all NC pins low.
void    read_dips(void);
void    set_initials_int(void);
void    set_initials_ext(void);
void    read_extreg5(void);
void    pre_internal(void);
void    pre_external(void);
void    set_initials_int(void); //dip/curr
void    set_initials_ext(void); //dip/curr
void    externalmode(void);
void    internalmode(void);
void    set_current_int(void);
void    set_current_ext_AH(void);
void    set_current_ext_AL(void);
void    setcurrent_ext(void);
void    top_mcp4013(void);
void    int_change(void);
void    ext_change(void);
void    change_current_int(void);
void    change_current_ext(void);
 
//CONFIGS
 
/*DEFINE OUTPUTS*/
#define ch1out      LATEbits.LATE1
#define ch2out      LATEbits.LATE0
#define ch3out      LATGbits.LATG0
#define ind_ledout  LATCbits.LATC2
#define fanconout   LATEbits.LATE4
#define tripout     LATAbits.LATA4
#define mcp4013_csout  LATCbits.LATC3
#define mcp4013_udout  LATCbits.LATC4
//DIPSWITCH INPUTS
#define dip1pin     PORTBbits.RB0
#define dip2pin     PORTBbits.RB1
#define dip3pin     PORTBbits.RB2
#define dip4pin     PORTBbits.RB3
#define dip5pin     PORTBbits.RB4
#define dip6pin     PORTBbits.RB5
#define dip7pin     PORTCbits.RC5
#define dip8pin     PORTEbits.RE6
 
//DEFINE INPUTS FROM EXT. CONNECTOR
#define resetext       PORTDbits.RD5
#define ch1ext    PORTDbits.RD6
#define ch2ext    PORTDbits.RD7
#define ch3ext    PORTCbits.RC7
#define clearext       PORTCbits.RC1
 
/* TURN LEDS ON AND OFF*/
#define ON1    LATEbits.LATE1 = 1;  /*Turn on chan1*/
#define OFF1   LATEbits.LATE1 = 0;  /*Turn off chan1*/
#define ON2    LATEbits.LATE0 = 1; /*Turn on chan2*/
#define OFF2   LATEbits.LATE0 = 0; /*Turn off chan2*/
#define ON3    LATGbits.LATG0 = 1; /*Turn on chan2*/
#define OFF3   LATGbits.LATG0 = 0;  /*Turn off chan2*/
#define FANON  LATEbits.LATE2 = 1;   /*Turn fan on*/
#define FANOFF LATEbits.LATE2 = 0;  /*Turn fan off*/
 
/*DEFINE INPUTS FROM DIPSWITCH*/
/*Note that the new board has pullups on the dipswitch,*/
/*whereas 550587 PCB had pull downs, thus logic is reversed.*/
uint8_t    dipsw1;
uint8_t    dipsw2;
uint8_t    dipsw3;
uint8_t    dipsw4;
uint8_t    dipsw5;
uint8_t    dipsw8;
uint8_t    dip8;   //bit0=dip1...
uint8_t    dip8_init;
uint8_t    extreg5;    //bit0=reset,bit1=ch1...etc, to bit4=clear
                            //external connector pins
uint8_t    extreg5_init;
uint8_t    extp3;    //reset
uint8_t    extp4;    //ch1
uint8_t    extp5;    //ch2
uint8_t    extp6;    //ch3
uint8_t    extp9;    //an in
uint8_t    extp10;   //clear
uint8_t    extp11;   //TRIP OUT
uint8_t    temp1;
uint8_t    temp2;
uint8_t    temp3;
uint8_t    tempb;
uint8_t    ana8;           //holds ADRESH
uint8_t    ana8_init;
uint8_t    max_led_temp;
uint8_t    max_pcb_temp;
uint8_t    board_90c;      //max board temp value
uint8_t    board_80c;
uint8_t    led_90c;
uint8_t    led_80c;        //max led temp value
uint8_t    ledthermopen;   //trip value when thermistor open
uint8_t    pcbthermopen;
uint8_t    currentint;        //represents 0A, 1.76A, 2.64A, 3.52A
                                //bit0=0,bit1=1a76...etc
uint8_t    iset_mcp4013;   //numer of down pulses needed to get current.
uint8_t    mcp4013pulses;
uint8_t    i;
uint8_t    just_lsbmsb_init;
uint8_t    just_lsbmsb;
uint8_t    extreg5;
uint8_t    extp3;
uint8_t    extp4;
uint8_t    extp5;
uint8_t    extp6;
uint8_t    extp10;
 
void main(void) {
   
    disable_interrupts();
    disable_pullups();
    disable_opendrain();
    disable_comparators();
    setup_adc();
    setup_ports();
    NC_pins_low();  //make all NC pins low.
 
    here:
    while(1){
    OFF1;
    OFF2;
    OFF3;
    FANOFF;
 
    read_dips();
    if (currentint == 1) {goto  here;}
 
    here_1:
    if (dipsw1)  pre_internal();  //OFF (actually high) = INTERNAL MODE
    if (currentint == 1) {goto  here;}
    if (dipsw1)  internalmode();
    if (currentint == 1) {goto  here;}
    if (dipsw1) {goto    here_1;}
 
    if (!dipsw1) pre_external();
    if (currentint == 1) {goto  here;}
    if  (!dipsw1) externalmode();
    if  (currentint == 1) {goto  here;}
    goto    here_1;
    }
    return;
}
void    set_ana_dig_ports(void){
    ANCON0 = 0xFC;
    ANCON1 = 0x0F;
    ANCON2 = 0x00;
    return;
}
void    disable_interrupts(void) {
    INTCON =0x00;
    INTCON2 =0x80;
    INTCON3 = 0x00;
    PIE1 = 0x00;
    PIE2 = 0x00;
    PIE3 = 0x00;
    PIE4 = 0x00;
    PIE5 = 0x00;
    PIE6 = 0x00;
    return;
}
void    disable_pullups(void){
    PADCFG1 = 0;
    return;
}
void    disable_opendrain(void){
    ODCON1 = 0;
    ODCON2 = 0;
    ODCON3 = 0;
    return;
}
void    disable_comparators(void){
    CM1CON = 0;
    CM2CON = 0;
    CM3CON = 0;
    return;
}
void    setup_adc(void){
    ADCON0 = 0;
    ADCON1 = 0x10;  //VREF
    ADCON2 = 0x3E;  // Left jus/Tacq
    return;
}
void    setup_ports(void){
    TRISA =   0x2C;
    TRISB   = 0x3F;
    TRISC   = 0xA2;
    TRISD   = 0xD0;
    TRISE   = 0x40;
    TRISF   = 0xFE;
    TRISG   = 0x24;
    return;
}
void    NC_pins_low(void){
//Make all NC pins low (they are already made to outputs)
//They are outputs for noise immunity reasons.
    LATAbits.LATA0 = 0;
    LATAbits.LATA1 = 0;
    LATAbits.LATA6 = 0;
    LATAbits.LATA7 = 0;
 
    LATBbits.LATB6  = 0;    //PGC
    LATBbits.LATB7  = 0;    //PGD
 
    LATCbits.LATC0 = 0;
    LATCbits.LATC6 = 0;
 
    LATDbits.LATD0 = 0;
    LATDbits.LATD1 = 0;
    LATDbits.LATD2 = 0;
    LATDbits.LATD3 = 0;
    LATDbits.LATD4 = 0;
 
    LATEbits.LATE2 = 0;
    LATEbits.LATE3 = 0;
    LATEbits.LATE5 = 0;
    LATEbits.LATE7 = 0;
 
    LATGbits.LATG1 = 0;
    LATGbits.LATG3 = 0;
    LATGbits.LATG4 = 0;
    return;
}
void    read_extreg5(void){
    //read just the five digital inputs from ext conn.
    //put them in extreg5;
    extreg5 = 0x00;    //first 5 bits of ext5 holds ext conn res,1,2,3,cl
    if (resetext) {extp3 = 0x01; extreg5 = extreg5 || 0x01;}
    else    {extp3 = 0x00;}
    if (ch1ext) {extp4 = 0x01; extreg5 = extreg5 || 0x02;}
    else    {extp4 = 0x00;}
    if (ch2ext) {extp5 = 0x01; extreg5 = extreg5 || 0x04;}
    else    {extp5 = 0x00;}
    if (ch3ext) {extp6 = 0x01; extreg5 = extreg5 || 0x08;}
    else    {extp6 = 0x00;}
    if (clearext) {extp10 = 0x01; extreg5 = extreg5 || 0x10;}
    else    {extp10 = 0x00;}
    return;
}
void    read_dips(void){
    //dip8 holds the state of dipswitch in correcponding bit places.
    dip8 = 0x00;
    if (dip1pin) {dipsw1 = 0x01; dip8 = dip8 || 0x01;}
    else    {dipsw1 = 0x00;}
    if (dip2pin) {dipsw2 = 0x01; dip8 = dip8 || 0x02;}
    else    {dipsw2 = 0x00;}
    if (dip3pin) {dipsw3 = 0x01; dip8 = dip8 || 0x04;}
    else    {dipsw3 = 0x00;}
    if (dip4pin) {dipsw4 = 0x01; dip8 = dip8 || 0x08;}
    else    {dipsw4 = 0x00;}
    if (dip5pin) {dipsw5 = 0x01; dip8 = dip8 || 0x10;}
    else    {dipsw5 = 0x00;}
    if (dip8pin) {dipsw8 = 0x01; dip8 = dip8 || 0x80;}
    else    {dipsw8 = 0x00;}
    if (!dipsw2 && !dipsw3) {currentint = 4;}   //set the currentint reg
    if (dipsw2 && !dipsw3)  {currentint = 3;}
    if (!dipsw2 && dipsw3)  {currentint = 2;}
    if (dipsw2 && dipsw3)   {currentint = 1;}
    return;
}
 
void    read_analog(void){
    ADCON0 = 0x19;  //turn on ADC module FOR AN6 (0-10V)
    ADCON0 = 0x1B;  //START ADC CONVERSION
    while(GODONE) {;}   //Wait till conversion complete.
    ana8 = ADRESH;
    ADCON0 = 0x18;  //turn off ADC MODULE.
    return;
}
void    set_initials_int(void){
    read_dips();
    dip8_init = dip8;                   //for comparison
    return;
}
void    set_initials_ext(void){
    read_dips();
    dip8_init = dip8;                   //for comparison
    read_extreg5();
    extreg5_init = extreg5;
    read_analog();       //read the 0-10v signal.
    ana8_init = ana8;   //0-10V signal
return;
}
void    pre_internal(void){
    set_initials_int();
    set_current_int();
    return;
    }
void    pre_external(void){
    set_initials_ext();
    if (dipsw4) {set_current_ext_AH();}
    if (!dipsw4) {set_current_ext_AL();}
    return;
    ;}
void    internalmode(void){
    while(1){
    __delay_ms(100);
    read_dips();
    if (dip8 != dip8_init) {break;}
    ON1;
    ON2;
    ON3;   }
return;}
 
void    externalmode(void) {
        while(1){
    __delay_ms(100);
    read_dips();
    if (dip8 != dip8_init) {break;}
    read_extreg5();
    if (extreg5 != extreg5_init){break;}
    read_analog();
    if (ana8 != ana8_init) {break;}
    if (extp3 == 1) {ON1;}
    if (extp3 == 1) {ON2;}
    if (extp3 == 1) {ON3;}
 
    if (extp3 == 0) {OFF1;}
    if (extp3 == 0) {OFF2;}
    if (extp3 == 0) {OFF3;}
        }
    return;
}
 
void    set_current_int(void){
    //set mcp4013 by topping the mcp4013, then decrementing
    if (currentint == 2) mcp4013pulses = 31;    //no. of downpulses.
    if (currentint == 3) mcp4013pulses = 17;
    if (currentint == 4) mcp4013pulses = 4;
    top_mcp4013();      //top out the mcp4013
    mcp4013_csout = 1;  //disable mcp4013
    mcp4013_csout = 1;
    mcp4013_udout = 0;  //ready to decrement
    mcp4013_udout = 0;
    mcp4013_csout = 0;  //enable mcp4013
    mcp4013_csout = 0;
    for (i== 0; i== mcp4013pulses; i++){
    mcp4013_udout = 1;  //pulse it to the level
    mcp4013_udout = 1;
    mcp4013_udout = 0;
    mcp4013_udout = 0;
    }
    mcp4013_csout = 1;
    mcp4013_csout = 1;  //disable mcp4013
    return;
}
void    top_mcp4013(void) {
        mcp4013_csout = 1;
        mcp4013_csout = 1;  //disable mcp4013
        mcp4013_udout = 1;  //ready to increment wiper
        mcp4013_udout = 1;
        mcp4013_csout = 0;  //enable mcp4013
        mcp4013_csout = 0;
        for(i==64;i==0;i--){
        mcp4013_udout = 0;
        mcp4013_udout = 0;
        mcp4013_udout = 1;
        mcp4013_udout = 1;
        }
        mcp4013_csout = 1;  //disable mcp4013
        mcp4013_csout = 1;
        return;
}
 
void    set_current_ext_AH(void){
    //This is to set the current in external mode when the 4th dipswitch
    //asks for the 0-10V signal to be "10V = Highest current"
    //["AH" stands for Active High"]
    //MCP4013pulses will here be the number of downpulses to pulse the digital
    //pot with. (starting from the top of the resistor 'chain')
    //Never go below 11 POT steps (ie, never below 0.176V -> 300mA)
    //Make max current correspond to 235bits to 255bits on the 0-10V input
    //(9.2V)
 
//CALCULATING NUMBER OF DOWNPULSES.
    if (currentint == 2) {
    //9 apart
    if ((ana8 >= 235) && (ana8 <= 255)) {mcp4013pulses = 31;}
    if ((ana8 >= 225) && (ana8 <= 234)) {mcp4013pulses = 32;}
    if ((ana8 >= 215) && (ana8 <= 224)) {mcp4013pulses = 33;}
    if ((ana8 >= 205) && (ana8 <= 214)) {mcp4013pulses = 34;}
    if ((ana8 >= 195) && (ana8 <= 204)) {mcp4013pulses = 35;}
    if ((ana8 >= 185) && (ana8 <= 194)) {mcp4013pulses = 36;}
    if ((ana8 >= 175) && (ana8 <= 184)) {mcp4013pulses = 37;}
    if ((ana8 >= 165) && (ana8 <= 174)) {mcp4013pulses = 38;}
    if ((ana8 >= 155) && (ana8 <= 164)) {mcp4013pulses = 39;}
    if ((ana8 >= 145) && (ana8 <= 154)) {mcp4013pulses = 40;}
    if ((ana8 >= 135) && (ana8 <= 144)) {mcp4013pulses = 41;}
    if ((ana8 >= 125) && (ana8 <= 134)) {mcp4013pulses = 42;}
    if ((ana8 >= 115) && (ana8 <= 124)) {mcp4013pulses = 43;}
    if ((ana8 >= 105) && (ana8 <= 114)) {mcp4013pulses = 44;}
    if ((ana8 >= 95) && (ana8 <= 104))  {mcp4013pulses = 45;}
    if ((ana8 >= 85) && (ana8 <= 94))   {mcp4013pulses = 46;}
    if ((ana8 >= 75) && (ana8 <= 84))   {mcp4013pulses = 47;}
    if ((ana8 >= 65) && (ana8 <= 74))   {mcp4013pulses = 48;}
    if ((ana8 >= 55) && (ana8 <= 64))   {mcp4013pulses = 49;}
    if ((ana8 >= 45) && (ana8 <= 54))   {mcp4013pulses = 50;}
    if ((ana8 >= 35) && (ana8 <= 44))   {mcp4013pulses = 51;}
    if ((ana8 >= 25) && (ana8 <= 34))   {mcp4013pulses = 52;}
    if ((ana8 >= 0) && (ana8 <= 24))    {mcp4013pulses = 53;}
    }
 
    if (currentint == 3) {
     //6 apart
    if ((ana8 >= 235) && (ana8 <= 255)) {mcp4013pulses = 17;}
    if ((ana8 >= 228) && (ana8 <= 234)) {mcp4013pulses = 18;}
    if ((ana8 >= 221) && (ana8 <= 227)) {mcp4013pulses = 19;}
    if ((ana8 >= 214) && (ana8 <= 220)) {mcp4013pulses = 20;}
    if ((ana8 >= 207) && (ana8 <= 213)) {mcp4013pulses = 21;}
    if ((ana8 >= 200) && (ana8 <= 206)) {mcp4013pulses = 22;}
    if ((ana8 >= 193) && (ana8 <= 199)) {mcp4013pulses = 23;}
    if ((ana8 >= 186) && (ana8 <= 192)) {mcp4013pulses = 24;}
    if ((ana8 >= 179) && (ana8 <= 185)) {mcp4013pulses = 25;}
    if ((ana8 >= 172) && (ana8 <= 178)) {mcp4013pulses = 26;}
    if ((ana8 >= 165) && (ana8 <= 171)) {mcp4013pulses = 27;}
    if ((ana8 >= 158) && (ana8 <= 164)) {mcp4013pulses = 28;}
    if ((ana8 >= 151) && (ana8 <= 157)) {mcp4013pulses = 29;}
    if ((ana8 >= 144) && (ana8 <= 150)) {mcp4013pulses = 30;}
    if ((ana8 >= 137) && (ana8 <= 143)) {mcp4013pulses = 31;}
    if ((ana8 >= 130) && (ana8 <= 136)) {mcp4013pulses = 32;}
    if ((ana8 >= 123) && (ana8 <= 129)) {mcp4013pulses = 33;}
    if ((ana8 >= 116) && (ana8 <= 122)) {mcp4013pulses = 34;}
    if ((ana8 >= 109) && (ana8 <= 115)) {mcp4013pulses = 35;}
    if ((ana8 >= 102) && (ana8 <= 108)) {mcp4013pulses = 36;}
    if ((ana8 >= 95) && (ana8 <= 101))  {mcp4013pulses = 37;}
    if ((ana8 >= 88) && (ana8 <= 94))   {mcp4013pulses = 38;}
    if ((ana8 >= 81) && (ana8 <= 87))   {mcp4013pulses = 39;}
    if ((ana8 >= 74) && (ana8 <= 80))   {mcp4013pulses = 40;}
    if ((ana8 >= 67) && (ana8 <=73))    {mcp4013pulses = 41;}
    if ((ana8 >= 60) && (ana8 <= 66))   {mcp4013pulses = 42;}
    if ((ana8 >= 53) && (ana8 <= 59))   {mcp4013pulses = 43;}
    if ((ana8 >= 46) && (ana8 <= 52))   {mcp4013pulses = 44;}
    if ((ana8 >= 39) && (ana8 <= 45))   {mcp4013pulses = 45;}
    if ((ana8 >= 32) && (ana8 <= 38))   {mcp4013pulses = 46;}
    if ((ana8 >= 25) && (ana8 <= 31))   {mcp4013pulses = 47;}
    if ((ana8 >= 18) && (ana8 <= 24))   {mcp4013pulses = 48;}
    if ((ana8 >= 11) && (ana8 <= 17))   {mcp4013pulses = 49;}
    if ((ana8 >= 4) &&  (ana8 <= 10))   {mcp4013pulses = 50;}
    if ((ana8 >= 0) &&  (ana8 <= 3))    {mcp4013pulses = 50;}
    }
 
    if (currentint == 4){
    //4 apart
    if ((ana8 >= 235) && (ana8 <= 255)) {mcp4013pulses = 4;}
    if ((ana8 >= 230) && (ana8 <= 234)) {mcp4013pulses = 5;}
    if ((ana8 >= 225) && (ana8 <= 229)) {mcp4013pulses = 6;}
    if ((ana8 >= 220) && (ana8 <= 224)) {mcp4013pulses = 7;}
    if ((ana8 >= 215) && (ana8 <= 219)) {mcp4013pulses = 8;}
    if ((ana8 >= 210) && (ana8 <= 214)) {mcp4013pulses = 9;}
    if ((ana8 >= 205) && (ana8 <= 209)) {mcp4013pulses = 10;}
    if ((ana8 >= 200) && (ana8 <= 204)) {mcp4013pulses = 11;}
    if ((ana8 >= 195) && (ana8 <= 199)) {mcp4013pulses = 12;}
    if ((ana8 >= 190) && (ana8 <= 194)) {mcp4013pulses = 13;}
    if ((ana8 >= 185) && (ana8 <= 189)) {mcp4013pulses = 14;}
    if ((ana8 >= 180) && (ana8 <= 184)) {mcp4013pulses = 15;}
    if ((ana8 >= 175) && (ana8 <= 179)) {mcp4013pulses = 16;}
    if ((ana8 >= 170) && (ana8 <= 174)) {mcp4013pulses = 17;}
    if ((ana8 >= 165) && (ana8 <= 169)) {mcp4013pulses = 18;}
    if ((ana8 >= 160) && (ana8 <= 164)) {mcp4013pulses = 19;}
    if ((ana8 >= 155) && (ana8 <= 159)) {mcp4013pulses = 20;}
    if ((ana8 >= 150) && (ana8 <= 154)) {mcp4013pulses = 21;}
    if ((ana8 >= 145) && (ana8 <= 149)) {mcp4013pulses = 22;}
    if ((ana8 >= 140) && (ana8 <= 144)) {mcp4013pulses = 23;}
    if ((ana8 >= 135) && (ana8 <= 139)) {mcp4013pulses = 24;}
    if ((ana8 >= 130) && (ana8 <= 134)) {mcp4013pulses = 25;}
    if ((ana8 >= 125) && (ana8 <= 129)) {mcp4013pulses = 26;}
    if ((ana8 >= 120) &&  (ana8 <= 124)) {mcp4013pulses = 27;}
    if ((ana8 >= 115) &&  (ana8 <= 119))  {mcp4013pulses = 28;}
    if ((ana8 >= 110) &&  (ana8 <= 114))  {mcp4013pulses = 29;}
    if ((ana8 >= 105) &&  (ana8 <= 109))  {mcp4013pulses = 30;}
    if ((ana8 >= 100) &&  (ana8 <= 104))  {mcp4013pulses = 31;}
    if ((ana8 >= 95) &&  (ana8 <= 99))  {mcp4013pulses = 32;}
    if ((ana8 >= 90) &&  (ana8 <= 94))  {mcp4013pulses = 33;}
    if ((ana8 >= 85) &&  (ana8 <= 89))  {mcp4013pulses = 34;}
    if ((ana8 >= 80) &&  (ana8 <= 84))  {mcp4013pulses = 35;}
    if ((ana8 >= 75) &&  (ana8 <= 79))  {mcp4013pulses = 36;}
    if ((ana8 >= 70) &&  (ana8 <= 74))  {mcp4013pulses = 37;}
    if ((ana8 >= 65) &&  (ana8 <= 69))  {mcp4013pulses = 38;}
    if ((ana8 >= 60) &&  (ana8 <= 64))  {mcp4013pulses = 39;}
    //3 apart
    if ((ana8 >= 56) &&  (ana8 <= 59))  {mcp4013pulses = 40;}
    if ((ana8 >= 52) &&  (ana8 <= 55))  {mcp4013pulses = 41;}
    if ((ana8 >= 48) &&   (ana8 <= 51)) {mcp4013pulses = 42;}
    if ((ana8 >= 44) &&   (ana8 <= 47)) {mcp4013pulses = 43;}
    if ((ana8 >=40) &&  (ana8 <= 43))   {mcp4013pulses = 39;}
    if ((ana8 >= 36) &&  (ana8 <= 39))  {mcp4013pulses = 40;}
    if ((ana8 >= 32) &&  (ana8 <= 35))  {mcp4013pulses = 41;}
    if ((ana8 >= 28) &&   (ana8 <= 31))  {mcp4013pulses = 42;}
    if ((ana8 >= 24) &&   (ana8 <= 27))   {mcp4013pulses = 43;}
    if ((ana8 >= 20) &&  (ana8 <= 23))  {mcp4013pulses =44;}
    if ((ana8 >= 16) &&  (ana8 <= 19))  {mcp4013pulses = 45;}
    if ((ana8 >= 12) &&  (ana8 <= 15))  {mcp4013pulses = 46;}
    if ((ana8 >= 8) &&   (ana8 <= 11))   {mcp4013pulses = 47;}
    if ((ana8 >= 4) &&  (ana8 <= 7))    {mcp4013pulses =48;}
    if ((ana8 >= 0) &&  (ana8 <= 3))    {mcp4013pulses = 49;}
    }
 
    top_mcp4013();      //top out the mcp4013
    mcp4013_csout = 1;  //disable mcp4013
    mcp4013_csout = 1;
    mcp4013_udout = 0;  //ready to decrement
    mcp4013_udout = 0;
    mcp4013_csout = 0;  //enable mcp4013
    mcp4013_csout = 0;
    for (i== 0; i== mcp4013pulses; i++){
    mcp4013_udout = 1;  //pulse it to the level
    mcp4013_udout = 1;
    mcp4013_udout = 0;
    mcp4013_udout = 0;
    }
    mcp4013_csout = 1;
    mcp4013_csout = 1;  //disable mcp4013
 
    return;
}
 
void    set_current_ext_AL(void){
    //This is to set the current in external mode when the 4th dipswitch
    //asks for the 0-10V signal to be "0V = Highest current"
    //["AL" stands for Active Low"]
    //MCP4013pulses will here be the number of downpulses to pulse the digital
    //pot with. (starting from the top of the resistor 'chain')
    //Never go below 11 POT steps (ie, never below 0.176V -> 300mA)
    //Make max current correspond to 0 bits to 9 bits on the 0-10V input
    //(ie anything below 0.3V will be max)
    if (currentint == 2) {
    //12 apart
    if ((ana8 >= 0  )  && (ana8 <= 9))   {mcp4013pulses = 31;}
    if ((ana8 >= 10  ) && (ana8 <= 22))  {mcp4013pulses = 32;}
    if ((ana8 >= 23  ) && (ana8 <= 35))  {mcp4013pulses = 33;}
    if ((ana8 >= 36  ) && (ana8 <= 48))  {mcp4013pulses = 34;}
    if ((ana8 >= 49  ) && (ana8 <= 61))  {mcp4013pulses = 35;}
    if ((ana8 >= 62  ) && (ana8 <= 74))  {mcp4013pulses = 36;}
    if ((ana8 >= 75  ) && (ana8 <= 87))  {mcp4013pulses = 37;}
    if ((ana8 >= 88  ) && (ana8 <= 100))  {mcp4013pulses = 38;}
    if ((ana8 >= 101  ) && (ana8 <= 113))  {mcp4013pulses = 39;}
    if ((ana8 >= 114  ) && (ana8 <= 126))  {mcp4013pulses = 40;}
    if ((ana8 >= 127  ) && (ana8 <= 139))  {mcp4013pulses = 41;}
    if ((ana8 >= 140 ) && (ana8 <= 152))  {mcp4013pulses = 42;}
    if ((ana8 >= 153  ) && (ana8 <= 165)) {mcp4013pulses = 43;}
    if ((ana8 >= 166  ) && (ana8 <= 178)) {mcp4013pulses = 44;}
    if ((ana8 >= 179  ) && (ana8 <= 191)) {mcp4013pulses = 45;}
    if ((ana8 >= 192 )  && (ana8 <= 204)) {mcp4013pulses = 46;}
    if ((ana8 >= 205  ) && (ana8 <= 217)) {mcp4013pulses = 47;}
    if ((ana8 >= 218  ) && (ana8 <= 230)) {mcp4013pulses = 48;}
    if ((ana8 >= 231  ) && (ana8 <= 243)) {mcp4013pulses = 49;}
    if ((ana8 >= 244 )  && (ana8 <= 255)) {mcp4013pulses = 50;}
    }
    if (currentint == 3) {
    //7 apart
    if ((ana8 >= 0  )  && (ana8 <= 9))     {mcp4013pulses = 17;}
    if ((ana8 >= 10  )  && (ana8 <= 17))   {mcp4013pulses = 18;}
    if ((ana8 >= 18  )  && (ana8 <= 25))   {mcp4013pulses = 19;}
    if ((ana8 >= 26  )  && (ana8 <= 33))   {mcp4013pulses = 20;}
    if ((ana8 >= 34  )  && (ana8 <= 41))   {mcp4013pulses = 21;}
    if ((ana8 >= 42  )  && (ana8 <= 49))   {mcp4013pulses = 22;}
    if ((ana8 >= 50  )  && (ana8 <= 57))   {mcp4013pulses = 23;}
    if ((ana8 >= 58  )  && (ana8 <= 65))   {mcp4013pulses = 24;}
    if ((ana8 >= 66  )  && (ana8 <= 73))   {mcp4013pulses = 25;}
    if ((ana8 >= 74  )  && (ana8 <= 81))   {mcp4013pulses = 26;}
    if ((ana8 >= 82  )  && (ana8 <= 89))   {mcp4013pulses = 27;}
    if ((ana8 >= 90  )  && (ana8 <= 97))   {mcp4013pulses = 28;}
    if ((ana8 >= 98  )  && (ana8 <= 105))  {mcp4013pulses = 29;}
    if ((ana8 >= 106 )  && (ana8 <= 113))  {mcp4013pulses = 30;}
    if ((ana8 >= 114  )  && (ana8 <= 121)) {mcp4013pulses = 31;}
    if ((ana8 >= 122  )  && (ana8 <= 129)) {mcp4013pulses = 32;}
    if ((ana8 >= 130  )  && (ana8 <= 137)) {mcp4013pulses = 33;}
    if ((ana8 >= 138  )  && (ana8 <= 145)) {mcp4013pulses = 34;}
    if ((ana8 >= 146  )  && (ana8 <= 153)) {mcp4013pulses = 35;}
    if ((ana8 >= 154 )  && (ana8 <= 161))  {mcp4013pulses = 36;}
    if ((ana8 >= 162  )  && (ana8 <= 169)) {mcp4013pulses = 37;}
    if ((ana8 >= 170  )  && (ana8 <= 177)) {mcp4013pulses = 38;}
    if ((ana8 >= 178  )  && (ana8 <= 185)) {mcp4013pulses = 39;}
    if ((ana8 >= 186 )  && (ana8 <= 193))  {mcp4013pulses = 40;}
    if ((ana8 >= 194 )  && (ana8 <= 201))  {mcp4013pulses = 41;}
    if ((ana8 >= 202  )  && (ana8 <= 209)) {mcp4013pulses = 42;}
    if ((ana8 >= 210  )  && (ana8 <= 217)) {mcp4013pulses = 43;}
    if ((ana8 >= 218  )  && (ana8 <= 225)) {mcp4013pulses = 44;}
    if ((ana8 >= 226 )  && (ana8 <= 233))  {mcp4013pulses = 45;}
    if ((ana8 >= 234 )  && (ana8 <= 241))  {mcp4013pulses = 46;}
    if ((ana8 >= 242  )  && (ana8 <= 249)) {mcp4013pulses = 47;}
    if ((ana8 >= 250  )  && (ana8 <= 255)) {mcp4013pulses = 48;}
    }
    if (currentint == 4)    {
    //4 apart
    if ((ana8 >= 0  )  && (ana8 <= 9))     {mcp4013pulses = 4;}
    if ((ana8 >= 10  )  && (ana8 <= 14))   {mcp4013pulses = 5;}
    if ((ana8 >= 15  )  && (ana8 <= 19))   {mcp4013pulses = 6;}
    if ((ana8 >= 20  )  && (ana8 <= 24))   {mcp4013pulses = 7;}
    if ((ana8 >= 25  )  && (ana8 <= 29))   {mcp4013pulses = 8;}
    if ((ana8 >= 30  )  && (ana8 <= 34))   {mcp4013pulses = 9;}
    if ((ana8 >= 35  )  && (ana8 <= 39))   {mcp4013pulses = 10;}
    if ((ana8 >= 40  )  && (ana8 <= 44))   {mcp4013pulses = 11;}
    if ((ana8 >= 45  )  && (ana8 <= 49))   {mcp4013pulses = 12;}
    if ((ana8 >= 50  )  && (ana8 <= 54))   {mcp4013pulses = 13;}
    if ((ana8 >= 55  )  && (ana8 <= 59))   {mcp4013pulses = 14;}
    if ((ana8 >= 60  )  && (ana8 <= 64))   {mcp4013pulses = 15;}
    if ((ana8 >= 65  )  && (ana8 <= 69))  {mcp4013pulses = 16;}
    if ((ana8 >= 70  )  && (ana8 <= 74))   {mcp4013pulses = 17;}
    if ((ana8 >= 75  )  && (ana8 <= 79))   {mcp4013pulses = 18;}
    if ((ana8 >= 80  )  && (ana8 <= 84))   {mcp4013pulses = 19;}
    if ((ana8 >= 85  )  && (ana8 <= 89))   {mcp4013pulses = 20;}
    if ((ana8 >= 90  )  && (ana8 <= 94))   {mcp4013pulses = 21;}
    if ((ana8 >= 95  )  && (ana8 <= 99))   {mcp4013pulses = 22;}
    if ((ana8 >= 100  )  && (ana8 <= 104))   {mcp4013pulses = 23;}
    if ((ana8 >= 105  )  && (ana8 <= 109))   {mcp4013pulses = 24;}
    if ((ana8 >= 110  )  && (ana8 <= 114))   {mcp4013pulses = 25;}
    if ((ana8 >= 115  )  && (ana8 <= 119))   {mcp4013pulses = 26;}
    if ((ana8 >= 120  )  && (ana8 <= 124))   {mcp4013pulses = 27;}
    if ((ana8 >= 125  )  && (ana8 <= 129))   {mcp4013pulses = 28;}
    if ((ana8 >= 130  )  && (ana8 <= 134))   {mcp4013pulses = 29;}
    if ((ana8 >= 135  )  && (ana8 <= 139))   {mcp4013pulses = 30;}
    if ((ana8 >= 140  )  && (ana8 <= 144))   {mcp4013pulses = 31;}
    if ((ana8 >= 145  )  && (ana8 <= 149))   {mcp4013pulses = 32;}
    if ((ana8 >= 150  )  && (ana8 <= 154))   {mcp4013pulses = 33;}
    if ((ana8 >= 155  )  && (ana8 <= 159))   {mcp4013pulses = 34;}
    if ((ana8 >= 160  )  && (ana8 <= 164))   {mcp4013pulses = 35;}
    if ((ana8 >= 165  )  && (ana8 <= 169))   {mcp4013pulses = 36;}
    if ((ana8 >= 170  )  && (ana8 <= 174))   {mcp4013pulses = 37;}
    if ((ana8 >= 175  )  && (ana8 <= 179))   {mcp4013pulses = 38;}
    if ((ana8 >= 180  )  && (ana8 <= 184))   {mcp4013pulses = 39;}
    if ((ana8 >= 185  )  && (ana8 <= 189))   {mcp4013pulses = 40;}
    if ((ana8 >= 190  )  && (ana8 <= 194))   {mcp4013pulses = 41;}
    if ((ana8 >= 195  )  && (ana8 <= 199))   {mcp4013pulses = 42;}
    if ((ana8 >= 200  )  && (ana8 <= 204))   {mcp4013pulses = 43;}
    if ((ana8 >= 205  )  && (ana8 <= 209))   {mcp4013pulses = 44;}
    if ((ana8 >= 210  )  && (ana8 <= 214))   {mcp4013pulses = 45;}
    if ((ana8 >= 215  )  && (ana8 <= 219))   {mcp4013pulses = 46;}
    if ((ana8 >= 220  )  && (ana8 <= 224))   {mcp4013pulses = 47;}
    if ((ana8 >= 225  )  && (ana8 <= 229))   {mcp4013pulses = 48;}
    if ((ana8 >= 230  )  && (ana8 <= 234))   {mcp4013pulses = 49;}
    if ((ana8 >= 235  )  && (ana8 <= 239))   {mcp4013pulses = 50;}
    if ((ana8 >= 240  )  && (ana8 <= 244))   {mcp4013pulses = 51;}
    if ((ana8 >= 245  )  && (ana8 <= 249))   {mcp4013pulses = 52;}
    if ((ana8 >= 250  )  && (ana8 <= 255))   {mcp4013pulses = 53;}
}
    top_mcp4013();      //top out the mcp4013
    mcp4013_csout = 1;  //disable mcp4013
    mcp4013_csout = 1;
    mcp4013_udout = 0;  //ready to decrement
    mcp4013_udout = 0;
    mcp4013_csout = 0;  //enable mcp4013
    mcp4013_csout = 0;
    for (i== 0; i== mcp4013pulses; i++){
    mcp4013_udout = 1;  //pulse it to the level
    mcp4013_udout = 1;
    mcp4013_udout = 0;
    mcp4013_udout = 0;
    }
    mcp4013_csout = 1;
    mcp4013_csout = 1;  //disable mcp4013
return;
 
}

 
Last edited by a moderator:

if I try compiling using MPLABX with XC8 your code with the modification
Code:
void main(void) {
OSCCON = 0x50;         // line added
that line gives no errors
however, the line
Code:
if ((ana8 >= 235) && (ana8 <= 255)) {mcp4013pulses = 31;}
and a few others give warnings
Code:
main.c:502: warning: (750) constant operand to || or &&
main.c:502: warning: (765) degenerate unsigned comparison
main.c:524: warning: (765) degenerate unsigned comparison
main.c:529: warning: (750) constant operand to || or &&
main.c:529: warning: (765) degenerate unsigned comparison
if ana8 is a UNIT8 the expression (ana8 <= 255) is always true???
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
that line gives no errors
As expectable because OSCCON is defined as unsigned char in PIC18F65K22 include file.
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
thanks, so I have to use OSCCON = b'01010000'?

I see the 255 point.....true...at least its just harmless stupidity on my part
 

thanks, so I have to use OSCCON = b'01010000'?

I see the 255 point.....true...at least its just harmless stupidity on my part
the simulator watch window shows 0x50
PIC18simulator.jpg
 
  • Like
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
General warning: although 'goto' is a valid instruction in C, you will regret using it later! In a large program it is like a signpost to go somewhere but you are never sure where or other places you might reach it from without looking through the rest of the code. Try to structure your programs in a more modular fashion and avoid the 'goto' instruction if possible.

Your mistake as already pointed out though is mixing variable sizes. An 8-bit value can never exceed 0xff/255.

Incidentally, I think you used the quote facility to post your code. If you use code tags instead (mouse over the icons to see which one) it will retain the formatting and indentation of the original program.

Brian.
 

thanks, so I have to use OSCCON = b'01010000'?

I see the 255 point.....true...at least its just harmless stupidity on my part
also in the statement
Code:
if ((ana8 >= 0) && (ana8 <= 24))    {mcp4013pulses = 53;}
if ana8 is unsigned the expression (ana8 >= 0) is always true
 

thanks
Your mistake as already pointed out though is mixing variable sizes. An 8-bit value can never exceed 0xff/255.
I hope I haven't mixed variable sizes, I wish all the variables to be just 8 bit.

- - - Updated - - -

i see your point about goto........im sure you are right and theres a better way. In this code, i found i was returning back to main at the point just after where i left for the function in the first place......but i often wanted to return back to the start of main, so i just added an "if" and a goto to shovel execution back to the start of main......do you think that goto'ing when you only goto the start is slightly less bad..i appreciate that jumping all over the place is getting crazy....but when you only ever jump back to the start point its a little less mad
 

could you do something along the lines of
Code:
 while(1){
   int exit=0;
   OFF1;
   OFF2;
   OFF3;
   FANOFF;
 
   read_dips();
   if (currentint == 1) exit=1;
 
   while(!exit)
    {
    if (dipsw1)  pre_internal();  //OFF (actually high) = INTERNAL MODE
    if (currentint == 1) exit=1;
    if (dipsw1)  internalmode();
    if (currentint == 1) exit=1;
    if (dipsw1) continue;
 
    if (!dipsw1) pre_external();
    if (currentint == 1) exit=1;
    if  (!dipsw1) externalmode();
    if  (currentint == 1) exit=1;
    }
   }

or even
Code:
  while(1){
   OFF1;
   OFF2;
   OFF3;
   FANOFF;
 
   read_dips();
   if (currentint == 1) continue;
 
   while(1)
    {
    if (dipsw1)  pre_internal();  //OFF (actually high) = INTERNAL MODE
    if (currentint == 1) break;
    if (dipsw1)  internalmode();
    if (currentint == 1) break;
    if (dipsw1) continue;
 
    if (!dipsw1) pre_external();
    if (currentint == 1) break;
    if  (!dipsw1) externalmode();
    if  (currentint == 1)break;
    }
}
 
Last edited:

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top