Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Imparando il C (pic18F)

Raccolta di codici sorgenti

Moderatore: Foto UtentePaolino

0
voti

[1] Imparando il C (pic18F)

Messaggioda Foto UtenteDryden » 11 lug 2013, 13:46

salve a tutti,

dopo qualche anno passato a scrivere FW in mikrobasic ho deciso di imparare il C.

Ho installato MPLAB X con XC8 e dopo aver creato un progetto mi sono messo a fare un po' di pratica ma riscontro un problema strano.

in pratica faccio lampeggiare banalmente la PORTB e non riscontro problemi ma se aggiungo un ritardo il PIC sembra morto...

Il PIC è un 18F4620 e utilizzo il seguente codice:

Codice: Seleziona tutto
// PIC18F4620 Configuration Bit Settings

#include <xc.h>
#include <pic18f4620.h>
// CONFIG1H
#pragma config OSC = HSPLL      // Oscillator Selection bits (HS oscillator, PLL enabled (Clock Frequency = 4 x FOSC1))
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = OFF       // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = SBORDIS  // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = ON       // MCLR Pin Enable bit (MCLR pin enabled; RE3 input pin disabled)

// CONFIG4L
#pragma config STVREN = ON      // Stack Full/Underflow Reset Enable bit (Stack full/underflow will cause Reset)
#pragma config LVP = ON         // Single-Supply ICSP Enable bit (Single-Supply ICSP enabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-003FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (004000-007FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (008000-00BFFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (00C000-00FFFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-003FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (004000-007FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (008000-00BFFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (00C000-00FFFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot Block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (004000-007FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (008000-00BFFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (00C000-00FFFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot Block (000000-0007FFh) not protected from table reads executed in other blocks)

#define _XTAL_FREQ 40000000

void delay_s(unsigned int count);


void delay_s(unsigned int count){
unsigned int k;
    for (k=0; k=count; k++)
{
   __delay_ms(10);
}
        }
   

void main(void){

    TRISA = 0x00;
    TRISB = 0x00;
    TRISC = 0x00;
    TRISD = 0x00;
    TRISE = 0x00;
    PORTA = 0X00;
    PORTB = 0X00;
    PORTC = 0X00;
    PORTD = 0X00;
    PORTE = 0X00;
    ADCON1 = 0b1111;
    CMCON = 0b111;
   

  while(1){

      PORTB = 0;
      delay_s(100);
      PORTB = 255;
 
}
}




Se io tolgo l'istruzione delay_s(100); il PIC lavora correttamente (controllo l'uscita sia con un led che con l'oscilloscopio) se invece inserisco tale istruzione tutto sembra morto.

Ho provato a sostituire il ciclo FOR con un ciclo WHILE(1) ma con il medesimo risultato.

Qualcuno potrebbe gentilmente spiegarmi perché non funziona?

Non ci sono problemi HW in quanto sto usando una scheda di sviluppo easypic6.

quando compilo il compilatore restituisce i seguenti messaggi:

Codice: Seleziona tutto
make -f nbproject/Makefile-default.mk SUBPROJECTS= .build-conf
make[1]: Entering directory `C:/Users/Nicola/MPLABXProjects/Test led.X'
make  -f nbproject/Makefile-default.mk dist/default/production/Test_led.X.production.hex
make[2]: Entering directory `C:/Users/Nicola/MPLABXProjects/Test led.X'
"C:\Program Files\Microchip\xc8\v1.12\bin\xc8.exe" --pass1  --chip=18F4620 -Q -G --asmlist  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s"  -obuild/default/production/Main.p1  Main.c
"C:\Program Files\Microchip\xc8\v1.12\bin\xc8.exe"  --chip=18F4620 -G --asmlist -mdist/default/production/Test_led.X.production.map  --double=24 --float=24 --emi=wordwrite --opt=default,+asm,-asmfile,+speed,-space,-debug --addrqual=ignore --mode=free -P -N255 --warn=0 --summary=default,-psect,-class,+mem,-hex,-file --output=default,-inhx032 --runtime=default,+clear,+init,-keep,-no_startup,-download,+config,+clib,+plib "--errformat=%%f:%%l: error: %%s" "--warnformat=%%f:%%l: warning: %%s" "--msgformat=%%f:%%l: advisory: %%s"   -odist/default/production/Test_led.X.production.cof  build/default/production/Main.p1     
Microchip MPLAB XC8 C Compiler V1.12
Copyright (C) 2012 Microchip Technology Inc.
License type: Node Configuration

:: advisory: Employing 18F4620 errata work-arounds:
:: advisory:  * Corrupted fast interrupt shadow registers
:: advisory:  * Data in RAM location can be corrupted if async. reset occurs during write
:: warning: Omniscient Code Generation not available in Free mode

Memory Summary:
    Program space        used    8Ah (   138) of 10000h bytes   (  0.2%)
    Data space           used     5h (     5) of   F80h bytes   (  0.1%)
    Configuration bits   used     7h (     7) of     7h words   (100.0%)
    EEPROM space         used     0h (     0) of   400h bytes   (  0.0%)
    ID Location space    used     8h (     8) of     8h nibbles (100.0%)

Running this compiler in PRO mode, with Omniscient Code Generation enabled,
often produces code which is 60% smaller and at least 400% faster than in
Free mode. The MPLAB XC8 PRO compiler output for this code could be
76 bytes smaller and run 4 times faster.
See http://www.microchip.com for more information.

make[2]: Leaving directory `C:/Users/Nicola/MPLABXProjects/Test led.X'
make[1]: Leaving directory `C:/Users/Nicola/MPLABXProjects/Test led.X'

BUILD SUCCESSFUL (total time: 9s)
Loading code from C:/Users/Nicola/MPLABXProjects/Test led.X/dist/default/production/Test_led.X.production.hex...
Loading symbols from C:/Users/Nicola/MPLABXProjects/Test led.X/dist/default/production/Test_led.X.production.cof...
Loading completed


Ringrazio chiunque riesce a darmi delucidazioni in merito! :-)
Avatar utente
Foto UtenteDryden
20 4
New entry
New entry
 
Messaggi: 97
Iscritto il: 1 gen 2011, 14:37
Località: Bolzano

0
voti

[2] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteTardoFreak » 11 lug 2013, 13:50

Se non sbaglio (non ne sono cmpletamente sicuro) la delay_s di genera ritardi in secondi. Quindi delay_s(100) significa quasi due minuti!
Più che morto è ... in letargo. :mrgreen:
"La follia sta nel fare sempre la stessa cosa aspettandosi risultati diversi".
"Parla soltanto quando sei sicuro che quello che dirai è più bello del silenzio".
Rispondere è cortesia, ma lasciare l'ultima parola ai cretini è arte.
Avatar utente
Foto UtenteTardoFreak
73,9k 8 12 13
-EY Legend-
-EY Legend-
 
Messaggi: 15754
Iscritto il: 16 dic 2009, 11:10
Località: Torino - 3° pianeta del Sistema Solare

0
voti

[3] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteDryden » 11 lug 2013, 13:58

ciao, quella è una funzione,

in pratica fa 100 cicli da 10ms ognuno per un totale di 1sec

ho visto un piccolo errore nel codice postato, in realtà sarebbe:

PORTB = 0;
delay_s(100);
PORTB = 255;
delay_s(100);



tra le varie prove che stavo facendo ho postato il codice senza un pezzo...

in pratica il led dovrebbe lampeggiare a 0.5hz
Avatar utente
Foto UtenteDryden
20 4
New entry
New entry
 
Messaggi: 97
Iscritto il: 1 gen 2011, 14:37
Località: Bolzano

0
voti

[4] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteShockwaver » 12 lug 2013, 12:12

2 domande:
Per "morto" cosa intendi? Hai provato a debuggare?
La soluzione che tu stesso hai trovato nell'ultimo post l'hai testata? No, perché col codice di prima non potresti mai vedere il led che lampeggia: resta acceso solo per qualche centinaia di ns...
Anyone who has never made a mistake has never tried anything new
Two things are infinite: universe and human stupidity, and I'm not sure about the former
You did not really understand something unless you can explain it to your grandmother
A. Einstein
Avatar utente
Foto UtenteShockwaver
770 1 5 11
Expert
Expert
 
Messaggi: 859
Iscritto il: 3 mar 2010, 18:56

0
voti

[5] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteDryden » 12 lug 2013, 15:27

Ciao, innanzitutto grazie per la risposta.

la mia non era una soluzione, è solo che mancava quel pezzetto di codice nel primo post. purtroppo ho fatto 1000 prove prima di chiedere e non mi sono reso conto che mancava quel pezzetto.

Sennò per come era scritto, si la potrb sarebbe rimasta accesa solo per alcuni colpi di clock.

No, purtroppo non ho provato a debuggarlo perché oggi non ho avuto tempo, il PIC sembra morto nel senso che lo programmo e non succede assolutamente nulla.... mentre se tolgo la sub con la pausa, tutto funziona regolarmente....

Non riesco a capire come mai, l'istruzione __delay_ms() non ammette valori alti (tipo 100) anche se è dichiarata come INT, mentre l'struzione __delay_us() accetta valori ben più alti (1000 e più)......

non riesco proprio a capire cosa sbaglio....
Avatar utente
Foto UtenteDryden
20 4
New entry
New entry
 
Messaggi: 97
Iscritto il: 1 gen 2011, 14:37
Località: Bolzano

0
voti

[6] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteShockwaver » 12 lug 2013, 16:35

la __delay_ms(x) è una macro che lancia la _delay(x*(__XTAL_FREQ/4000)) (linea 151 in pic18.h)
La _delay è una ricetta a base di Nops, (metterne troppi consecutivi a volte mi ha dato problemi) e ha come codice una limitazione di circa 50,659,000 cicli istruzione al suo interno.
Il valore accettato dalla __delay_ms dipende quindi da __XTAL_FREQ

__delay_us(x) è invece definita come _delay(x*(__XTAL_FREQ/4000000)).

Le delay delle librerie standard sono un po'... "buggose" tant'è vero che quasi sempre nelle varie demo rilasciate dalla Microchip per le più svariate applicazioni, le funzioni di delay vengono riscritte.

Io non le uso già da parecchio tempo...
Anyone who has never made a mistake has never tried anything new
Two things are infinite: universe and human stupidity, and I'm not sure about the former
You did not really understand something unless you can explain it to your grandmother
A. Einstein
Avatar utente
Foto UtenteShockwaver
770 1 5 11
Expert
Expert
 
Messaggi: 859
Iscritto il: 3 mar 2010, 18:56

0
voti

[7] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteDryden » 12 lug 2013, 17:14

grazie per la info.

In generale i Delay li evito come la peste, ma a volte può essere utile da usare per fare un "antirimbalzo" per pulsanti o cose del genere....

Comunque, non riesco a capire perché non riesco a far lampeggiare un led nel modo più stupido.....
Avatar utente
Foto UtenteDryden
20 4
New entry
New entry
 
Messaggi: 97
Iscritto il: 1 gen 2011, 14:37
Località: Bolzano

0
voti

[8] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteShockwaver » 12 lug 2013, 17:46

Prova con una tua delay... non so... a quanto viaggiano le tue istruzioni, 10MHz? Ok cioè un Nop() in 100ns?
Codice: Seleziona tutto
#define MyDelay_1us() {Nop();Nop();Nop();Nop();Nop();Nop();Nop();Nop();Nop();Nop();}
#define MyDelay_10us() {MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();MyDelay_1us();

void DelayMs(unsigned char ms)
{
    unsigned short TenUSs = ms*100;

    while(TenUSs--)
        MyDelay_10us();
}

.
.
.

for (i = 0; i < 10; i ++)
    DelayMs(100);


Bassa manovalanza ma almeno se non funzia, debuggando puoi vedere dove si inchioda (la _delay() delle lib è una funzione richiamata da un extern.. complicata da tracciare in debug)

Però ripeto, non l'ho testata e forse ci sono troppi Nop() di mezzo consecutivi, vedi un po'...

Edit: 1000 ms di ritardo son proprio tanti se implementati con nops.... per un antirimbalzo bastano al max 100ms e già va meglio...
Anyone who has never made a mistake has never tried anything new
Two things are infinite: universe and human stupidity, and I'm not sure about the former
You did not really understand something unless you can explain it to your grandmother
A. Einstein
Avatar utente
Foto UtenteShockwaver
770 1 5 11
Expert
Expert
 
Messaggi: 859
Iscritto il: 3 mar 2010, 18:56

0
voti

[9] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteTardoFreak » 12 lug 2013, 19:13

Una interrupt ciclica?
Una piccola, tenera, semplice intrruptina ... no? :mrgreen:
"La follia sta nel fare sempre la stessa cosa aspettandosi risultati diversi".
"Parla soltanto quando sei sicuro che quello che dirai è più bello del silenzio".
Rispondere è cortesia, ma lasciare l'ultima parola ai cretini è arte.
Avatar utente
Foto UtenteTardoFreak
73,9k 8 12 13
-EY Legend-
-EY Legend-
 
Messaggi: 15754
Iscritto il: 16 dic 2009, 11:10
Località: Torino - 3° pianeta del Sistema Solare

0
voti

[10] Re: Imparando il C (pic18F)

Messaggioda Foto UtenteShockwaver » 12 lug 2013, 20:01

Massì.. ormai uso solo quelle... una bella variabile da incrementare a suon di timer. Ci fai anche i debounce degli interruttori e inoltre non pianti lo stack sul PIC :ok:
Anyone who has never made a mistake has never tried anything new
Two things are infinite: universe and human stupidity, and I'm not sure about the former
You did not really understand something unless you can explain it to your grandmother
A. Einstein
Avatar utente
Foto UtenteShockwaver
770 1 5 11
Expert
Expert
 
Messaggi: 859
Iscritto il: 3 mar 2010, 18:56


Torna a Firmware e programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 12 ospiti