Cos'è ElectroYou | Login Iscriviti

ElectroYou - la comunità dei professionisti del mondo elettrico

Delay e lampeggio di un led

Raccolta di codici sorgenti

Moderatore: Foto UtentePaolino

0
voti

[1] Delay e lampeggio di un led

Messaggioda Foto Utentealle96 » 27 ott 2014, 22:13

Salve a tutti, dopo un periodo di stop di ben 3 anni eccomi tornato :D
Sto iniziando a programmare i PIC (pic18f4550). Ho già fatto alcuni progetti con un compilatore visto a scuola un po' diciamo assistito ma adesso sto provando a passare a MPLABX C18.
Come inzio pensavo di far lampeggiare un led ma ecco già dei problemi :-M
Sembra quasi che il PIC si perda nel Delay, non passa all'istruzione seguente... e non so come fare... saranno 3 giorni che ogni tanto provo a risolverlo ma non c'è storia. Vi allego il codice
Codice: Seleziona tutto
#include <stdio.h>
#include <stdlib.h>
#include <delays.h>
#include <p18f4550.h>



// PIC18F4550 Configuration Bit Settings

// 'C' source line config statements



// CONFIG1L
#pragma config PLLDIV = 2       // PLL Prescaler Selection bits (Divide by 2 (8 MHz oscillator input))
#pragma config CPUDIV = OSC1_PLL2// System Clock Postscaler Selection bits ([Primary Oscillator Src: /1][96 MHz PLL Src: /2])
#pragma config USBDIV = 1       // USB Clock Selection bit (used in Full-Speed USB mode only; UCFG:FSEN = 1) (USB clock source comes directly from the primary oscillator block with no postscale)

// CONFIG1H
#pragma config FOSC = INTOSC_EC // Oscillator Selection bits (Internal oscillator, HS oscillator used by USB (INTHS))
#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 BOR = ON         // Brown-out Reset Enable bits (Brown-out Reset enabled in hardware only (SBOREN is disabled))
#pragma config BORV = 2         // Brown-out Reset Voltage bits ()
#pragma config VREGEN = OFF     // USB Voltage Regulator Enable bit (USB voltage regulator disabled)

// 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 = ON      // 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 Timer 1 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 = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config ICPRT = ON       // Dedicated In-Circuit Debug/Programming Port (ICPORT) Enable bit (ICPORT 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-001FFFh) is not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) is not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) is not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) is not code-protected)

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

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) is not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) is not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) is not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) is not write-protected)

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

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) is not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) is not protected from table reads executed in other blocks)

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

#define _XTAL_FREQ 4000000L /*FOSC/2*/




/*
*
*/


void main()
{
    TRISBbits.TRISB1 = 0;

    while(1)
    {

        PORTB = 0x00;

        LATBbits.LATB1 = 1;

        Delay10KTCYx(125);

        LATBbits.LATB1 = 0;

        Delay10KTCYx(125);


    }

}


Tutta la parte che riguarda il settaggio iniziale l'ho fatta utilizando l'help perciò non sono sicuro sia giusta comunque come quarzo sto usando il quarzo interno.
Sapete come mai non riesco a far lampeggiare il led? io sto impazzendo...

EDIT: dimenticavo di dire che il led è collegato a RB1
Avatar utente
Foto Utentealle96
15 4
 
Messaggi: 47
Iscritto il: 21 feb 2012, 16:21

0
voti

[2] Re: Delay e lampeggio di un LED

Messaggioda Foto Utentelelerelele » 28 ott 2014, 12:04

Sono alle prime armi anche io, e non ho mai usato pic18, ma qualche cosa da consigliarti c'e l'ho.

Usando Mplabx raccomandano di usare
Codice: Seleziona tutto
#include <xc.h>
al posto di
Codice: Seleziona tutto
#include <p18f4550.h>
Per una maggiore portabilità del codice, mi pare, ed il tipo di processore usato lo imposti da progetto, mi spiace che ora non ho disponibile MPLABX su questo PC e non posso inviarti degli screenshot.

Sempre Mplabx se selezioni sul menu ... non mi ricordo.......guarda tra i menu trovi la schermata che ti da le impostazioni relative alla configword, le imposti con le finestre a tendina e ti da il codice da incollare sul programma.

Sei sicuro che l'oscillatore funzioni? hai provato ad eliminare il delay per vedere se i led si accendono un po'? io non ho trovato sul datasheets INTOSC_EC. Come ti dicevo non avendolo mai usato magari va bene così.

Saluti.
Avatar utente
Foto Utentelelerelele
4.899 3 7 9
Master
Master
 
Messaggi: 5505
Iscritto il: 8 giu 2011, 8:57
Località: Reggio Emilia

1
voti

[3] Re: Delay e lampeggio di un LED

Messaggioda Foto UtenteRiddik89 » 28 ott 2014, 13:15

Ciao,


Il pic18f4550 ha un sacco di periferiche e dovresti prima disattivarle tutte....tipo se non disattivi il convertitore D/A quella porta non potrà lavorare come output digitale.
Stessa cosa vale per il modulo PWM e mille altri moduli che generalmente sono presenti nella famiglia 18....
Ti metto un pezzo di codice che avevo scritto per un PIC 18f2550 giusto per darti un'idea di quante cose dovresti disattivare.
E' stato scritto molto alla veloce quindi anche se funzionante non va preso come oro colato e poi il tuo dovrà essere un po' diverso.


Codice: Seleziona tutto
void inizializzazione(void)
{
    TRISA=0b00001111;
    TRISB=0b11111111;
    TRISC=0b01100100;

OSCCON=0b11110110;

//            bit 7 IDLEN: Idle Enable bit
//1 = Device enters Idle mode on SLEEP instruction
//0 = Device enters Sleep mode on SLEEP instruction
//bit 6-4 IRCF2:IRCF0: Internal Oscillator Frequency Select bits
//111 = 8 MHz (INTOSC drives clock directly)
//110 = 4 MHz
//101 = 2 MHz
//100 = 1 MHz
//(3)
//
//011 = 500 kHz
//010 = 250 kHz
//001 = 125 kHz
//000 = 31 kHz (from either INTOSC/256 or INTRC directly)
//bit 3 OSTS: Oscillator Start-up Time-out Status bit
//(1)
//(2)
//1 = Oscillator Start-up Timer time-out has expired; primary oscillator is running
//0 = Oscillator Start-up Timer time-out is running; primary oscillator is not ready
//bit 2 IOFS: INTOSC Frequency Stable bit
//1 = INTOSC frequency is stable
//0 = INTOSC frequency is not stable
//bit 1-0 SCS1:SCS0: System Clock Select bits
//1x = Internal oscillator
//01 = Timer1 oscillator
//00 = Primary oscillator
    T0CON=0b11001000;


//            bit 7 TMR0ON: Timer0 On/Off Control bit
//1 = Enables Timer0
//0 = Stops Timer0
//bit 6 T08BIT: Timer0 8-Bit/16-Bit Control bit
//1 = Timer0 is configured as an 8-bit timer/counter
//0 = Timer0 is configured as a 16-bit timer/counter
//bit 5 T0CS: Timer0 Clock Source Select bit
//1 = Transition on T0CKI pin
//0 = Internal instruction cycle clock (CLKO)
//bit 4 T0SE: Timer0 Source Edge Select bit
//1 = Increment on high-to-low transition on T0CKI pin
//0 = Increment on low-to-high transition on T0CKI pin
//bit 3 PSA: Timer0 Prescaler Assignment bit
//1 = TImer0 prescaler is NOT assigned. Timer0 clock input bypasses prescaler.
//0 = Timer0 prescaler is assigned. Timer0 clock input comes from prescaler output.
//bit 2-0 T0PS2:T0PS0: Timer0 Prescaler Select bits
//111 = 1:256 Prescale value
//110 = 1:128 Prescale value
//101 = 1:64 Prescale value
//100 = 1:32 Prescale value
//011 = 1:16 Prescale value
//010 = 1:8 Prescale value
//001 = 1:4 Prescale value
//000 = 1:2 Prescale value

ADCON0=0x00;

// bit 7-6 Unimplemented: Read as ?0?
//bit 5-2 CHS3:CHS0: Analog Channel Select bits
//0000 = Channel 0 (AN0)
//0001 = Channel 1 (AN1)
//0010 = Channel 2 (AN2)
//0011 = Channel 3 (AN3)
//0100 = Channel 4 (AN4)
//0101 = Channel 5 (AN5)
//
//0110 = Channel 6 (AN6)
//
//0111 = Channel 7 (AN7)
//1000 = Channel 8 (AN8)
//1001 = Channel 9 (AN9)
//1010 = Channel 10 (AN10)
//1011 = Channel 11 (AN11)
//1100 = Channel 12 (AN12)
//1101 = Unimplemented
//1110 = Unimplemented
//1111 = Unimplemented
//(1,2)
//(1,2)
//(1,2)
//(2)
//(2)
//(2)
//bit 1 GO/DONE
//: A/D Conversion Status bit
//When ADON =
//1:
//1 = A/D conversion in progress
//0 = A/D Idle
//bit 0 ADON: A/D On bit
//1 = A/D converter module is enabled
//0 = A/D converter module is disabled
ADCON1=0b00001111;
//    A/D CONTROL REGISTER 1      bit 7-6 Unimplemented: Read as ?0?
//bit 5 VCFG1: Voltage Reference Configuration bit (V
//REF- source)
//1 = VREF- (AN2)
//0 = VSS
//bit 4 VCFG0: Voltage Reference Configuration bit (VREF+ source)
//1 = VREF+ (AN3)
//0 = VDD
//bit 3-0 PCFG3:PCFG0: A/D Port Configuration Control bits:
// VEDI TABELLA DATASHEET P266



CCP1CON=0; //capture/compare/PWM
CCP2CON=0;



CMCON=0b00000111;
//comparator control register
//C2OUT: Comparator 2 Output bit
//When C2INV =
//0:
//1 = C2 VIN+ > C2 VIN0
//=
//C2
//VIN+
//<
//C2
//VIN-
//When C2INV = 1:
//1 = C2 VIN+ < C2 VIN0
//=
//C2
//VIN+
//>
//C2
//VIN-
//bit 6 C1OUT: Comparator 1 Output bit
//When C1INV =
//0:
//1 = C1 VIN+ > C1 VIN0
//=
//C1
//VIN+
//<
//C1
//VIN-
//When C1INV = 1:
//1 = C1 VIN+ < C1 VIN0
//=
//C1
//VIN+
//>
//C1
//VIN-
//bit 5 C2INV: Comparator 2 Output Inversion bit
//1 = C2 output inverted
//0 = C2 output not inverted
//bit 4 C1INV: Comparator 1 Output Inversion bit
//1 = C1 output inverted
//0 = C1 output not inverted
//bit 3 CIS: Comparator Input Switch bit
//When CM2:CM0 =
//110:
//1 =C1 VIN- connects to RA3/AN3/VREF+
//C2 VIN- connects to RA2/AN2/VREF-/CVREF
//0 =C1 VIN- connects to RA0/AN0
//C2 VIN- connects to RA1/AN1
//bit 2-0 CM2:CM0: Comparator Mode bits
//Figure 22-1 shows the Comparator modes and the CM2:CM0 bit settings.
//p275



CVRCON=0x00;
//COMPARATOR VOLTAGE REFERENCE CONTROL REGISTER
//bit 7 CVREN: Comparator Voltage Reference Enable bit
//1 =CV
//REF circuit powered on
//0 =CVREF circuit powered down
//bit 6 CVROE: Comparator VREF Output Enable bit
//(1)
//1 =CVREF voltage level is also output on the RA2/AN2/VREF-/CVREF pin
//0 =CVREF voltage is disconnected from the RA2/AN2/VREF-/CVREF pin
//bit 5 CVRR: Comparator VREF Range Selection bit
//1 = 0 to 0.667 CVRSRC, with CVRSRC/24 step size (low range)
//0 = 0.25 CVRSRC to 0.75 CVRSRC, with CVRSRC/32 step size (high range)
//bit 4 CVRSS: Comparator VREF Source Selection bit
//1 = Comparator reference source, CVRSRC = (VREF+) ? (VREF-)
//0 = Comparator reference source, CVRSRC = VDD ? VSS
//bit 3-0 CVR3:CVR0: Comparator VREF Value Selection bits (0 ? (CVR3:CVR0) ? 15)
//When CVRR =
//1:
//CVREF = ((CVR3:CVR0)/24) ? (CVRSRC)
//When CVRR =
//0:
//CVREF = (CVRSRC/4) + ((CVR3:CVR0)/32) ? (CVRSRC)


UCON=0b00010000;
//USB CONTROL REGISTER
/*bit 7 Unimplemented: Read as ?0?
bit 6 PPBRST: Ping-Pong Buffers Reset bit
1 = Reset all Ping-Pong Buffer Pointers to the Even Buffer Descriptor (BD) banks
0 = Ping-Pong Buffer Pointers not being reset
bit 5 SE0: Live Single-Ended Zero Flag bit
1 = Single-ended zero active on the USB bus
0 = No single-ended zero detected
bit 4 PKTDIS: Packet Transfer Disable bit
1 = SIE token and packet processing disabled, automatically set when a SETUP token is received
0 = SIE token and packet processing enabled
bit 3 USBEN: USB Module Enable bit
1 = USB module and supporting circuitry enabled (device attached)
0 = USB module and supporting circuitry disabled (device detached)
bit 2 RESUME: Resume Signaling Enable bit
1 = Resume signaling activated
0 = Resume signaling disabled
bit 1 SUSPND: Suspend USB bit
1 = USB module and supporting circuitry in Power Conserve mode, SIE clock inactive
0 = USB module and supporting circuitry in normal operation, SIE clock clocked at the configured rate
bit 0 Unimplemented: Read as ?0?*/
TXSTA=0X00;
RCSTA=0X00;

}



Morale della favola o ti leggi il datasheet con più attenzione e ti ci vorrà del tempo oppure passi a un'altra famiglia di PIC con meno periferiche e più facile da gestire.
Avatar utente
Foto UtenteRiddik89
254 4
Frequentatore
Frequentatore
 
Messaggi: 121
Iscritto il: 6 nov 2009, 22:01
Località: Torino

0
voti

[4] Re: Delay e lampeggio di un LED

Messaggioda Foto Utentealle96 » 2 nov 2014, 18:17

Ho risolto il problema che era semplicemente una resistenza di pull-up per il MCLR collegata al pin adiacente anziché a quello giusto :)

comunque rispondo a Foto UtenteRiddik89... I registri erano tutti settati e anche tutti bene.
Invece Foto Utentelelerelele purtroppo la libreria xc.h è solo per XC8 ma grazie comunque :)
Avatar utente
Foto Utentealle96
15 4
 
Messaggi: 47
Iscritto il: 21 feb 2012, 16:21

0
voti

[5] Re: Delay e lampeggio di un LED

Messaggioda Foto UtenteRiddik89 » 2 nov 2014, 18:51

alle96 ha scritto:comunque rispondo a Foto UtenteRiddik89... I registri erano tutti settati e anche tutti bene.


Allora mi sfugge la parte di programma in cui li hai settati tutti e bene.... che poi una porta possa funzionare lo stesso per pura fortuna ci può anche stare .
Avatar utente
Foto UtenteRiddik89
254 4
Frequentatore
Frequentatore
 
Messaggi: 121
Iscritto il: 6 nov 2009, 22:01
Località: Torino

0
voti

[6] Re: Delay e lampeggio di un LED

Messaggioda Foto UtenteWALTERmwp » 2 nov 2014, 19:02

alle96 ha scritto:Ho risolto il problema che era semplicemente una resistenza di pull-up per il MCLR collegata al pin adiacente anziché a quello giusto :)
... meglio così ma per la prossima volta, controlla meglio facendo più attenzione.
Diversamente induci altri utenti ad inseguire problemi inesistenti e non è per nulla corretto.

Saluti
W - U.H.F.
Avatar utente
Foto UtenteWALTERmwp
30,2k 4 8 13
G.Master EY
G.Master EY
 
Messaggi: 8982
Iscritto il: 17 lug 2010, 18:42
Località: le 4 del mattino

1
voti

[7] Re: Delay e lampeggio di un LED

Messaggioda Foto UtentePaolino » 2 nov 2014, 19:53

alle96 ha scritto:Ho risolto il problema che era semplicemente una resistenza di pull-up per il MCLR collegata al pin adiacente anziché a quello giusto :)


Ma se MCLR non era collegato, come facevi a scaricare il firmware sul PIC? :-M :-M :-M

Ciao.

Paolo.

P.S. RIcorda che C18 è stato ormai sostituito da XC8: pensa seriamente di migrare a tale compilatore. O_/
"Houston, Tranquillity Base here. The Eagle has landed." - Neil A.Armstrong

-------------------------------------------------------------

PIC Experience - http://www.picexperience.it
Avatar utente
Foto UtentePaolino
32,6k 8 12 13
G.Master EY
G.Master EY
 
Messaggi: 4226
Iscritto il: 20 gen 2006, 11:42
Località: Vigevano (PV)

0
voti

[8] Re: Delay e lampeggio di un LED

Messaggioda Foto Utenteivanpascolo » 7 nov 2014, 20:03

Un consiglio, provi ad utilizzare il "code generator" di Microchip. E' un strumento utilissimo ed è possibile impostare le varie periferiche con pochi click.

Saluti
Avatar utente
Foto Utenteivanpascolo
20 3
New entry
New entry
 
Messaggi: 71
Iscritto il: 29 set 2014, 20:44


Torna a Firmware e programmazione

Chi c’è in linea

Visitano il forum: Nessuno e 2 ospiti