zkouším proramovat PIC 16F1824, experimentuji s Timerem 2, když ho zapnu T2CONbits.TMR2ON = 1 v inicializaci, pak pracuje, když nechám jeho aktivaci do nekonečné smyčky, pak se nerozběhne, lze takto timer zapnout?
Jinak níže přiložený program:
Led je rozsvicovaná v zavislosti na osvětlení fotorezistoru, druhá led na LATC0 bliká cca dle inkrementace timeru.
Diky.
Kód: Vybrat vše
#include <xc.h>
/***** CONFIGURATION *****/
// ext reset, internal oscillator (no clock out), 4xPLL off
#pragma config MCLRE = ON, FOSC = INTOSC, CLKOUTEN = OFF, PLLEN = OFF
// no watchdog timer, brownout resets enabled, low brownout voltage
#pragma config WDTE = OFF, BOREN = ON, BORV = LO
// no power-up timer, no failsafe clock monitor, two-speed start-up disabled
#pragma config PWRTE = OFF, FCMEN = OFF, IESO = OFF
// no code or data protect, no write protection
#pragma config CP = OFF, CPD = OFF, WRT = OFF
// stack resets on, high-voltage programming
#pragma config STVREN = ON, LVP = OFF
// Pin assignments
#define LED LATCbits.LATC1 // indicator LED
/***** MAIN PROGRAM *****/
void main() {
/*** Initialisation ***/
// configure ports
TRISC = 0b111100; // configure RC1 RC0 as an output
// (RA1/C12IN0- is an input)
ANSELAbits.ANSA1 = 1; // select analog mode for RA1/C12IN0-
// configure fixed voltage reference
FVRCONbits.FVREN = 1; // FVR enabled
FVRCONbits.CDAFVR = 0b01; // Comp+DAC output is 2x (= 1.024 V)
// -> output 2.048 V to comparators
// configure comparator 2
CM2CON0bits.C2ON = 1; // comparator enabled
CM2CON0bits.C2OE = 0; // external output disabled
CM2CON0bits.C2POL = 0; // output not inverted
CM2CON0bits.C2SP = 1; // normal power mode
CM2CON0bits.C2HYS = 1; // hysteresis enabled
CM2CON0bits.C2SYNC = 0; // asynchronous output
CM2CON1bits.C2PCH = 0b10; // + in = fixed voltage ref
CM2CON1bits.C2NCH = 0b00; // - in = C12IN0- pin
// -> C2OUT = 1 if C12IN0- < 2.048 V
//timer 2
T2CONbits.T2CKPS = 0b11; // 1:64
T2CONbits.T2OUTPS = 0b0111; // 1:8
// -> increment TMR2 cca 2s
PIR1bits.TMR2IF = 0; // clear overflow flag
//T2CONbits.TMR2ON = 1; // stop Timer
/*** Main loop ***/
for (;;) {
// continually display comparator output
LED = CM2CON0bits.C2OUT;
T2CONbits.TMR2ON = 1; // start Timer
if (PIR1bits.TMR2IF == 1) {
LATCbits.LATC0 = ~LATCbits.LATC0;
PIR1bits.TMR2IF = 0;
}
}
}