This article is about the 1937 timer, just beginning to be crystal oscillator frequency, clock frequency, oscillation period, oscillation frequency, instruction period, instruction frequency and so on the noun around halo. First to solve this problem.
Crystal oscillator frequency is the frequency of the oscillator, that is, the frequency of the crystal oscillator, because a single-chip microcomputer has internal external crystal, such as you choose the internal crystal oscillator, then the frequency of the crystal oscillator is your microcontroller clock frequency,
The frequency of oscillation and the frequency of crystal oscillator are said to be one thing. The oscillation period is 1/(crystal frequency), T = f. Instruction cycle This is different depending on the microcontroller, the 8-bit PIC microcontroller (PIC10/12/16/18 series) is 4 clock cycles for one instruction cycle. The 16-bit PIC24 microcontroller and dspic digital processing chip and the 32-bit PIC32 processor are 2 clock cycles for one instruction cycle. (The above about the instruction cycle content is Baidu to, the content is more reliable http://zhidao.baidu.com/link?url=uEnsn0C-bb-xdDNG_ QEI0HMHIPODNVC4D2LHEZTGKSQMPFLMCPBNLWAGDXYEMZ05FJAHXARDXSRQDLHEDDRCX_)
Clear the above, and start our topic.
TIMER0
/*
* TIMER0 is a 8-bit timer/counter with 8-bit prescaler (1:2-1:256), which is the largest prescaler in all timers (so to speak). )
* Programmable internal and external clock source, programmable outside clock edge selection.
* Interrupt when overflow
* TMR0 can be used in gated Timer1 (not yet tried.) )
* Unable to work in Hibernate mode
* At this time select internal Crystal oscillator 8MHz, Prescaler 1:16, every 2.04s lamp state change once. TMR0 count from 0 to 255
* So it is 255*16*1000/(Fosc (Crystal Frequency)/4) = 2.04
*/
These are the things I write at the beginning of the microcontroller program, roughly describe the TIMER0 module, and then tell you how to use it. (write only about initialization TIMER0, CPU initialization and the like)
1. First initialize the clock source, set the SCS bit in the Osscon register to select the internal oscillator module, IRCF set the internal frequency
2. Now initialize the TIMER0, or the same, first select the clock source, tmr0cs = 0; Indicates the internal instruction period (note is the instruction period, FOSC/4)
3. Select the Prescaler, the PSA bit to select the need for Prescaler,ps<2:0> to set the Prescaler
4. Clocks are related to interrupts, so there is a valid interrupt to be allowed here, GIE = 1;
5. Then allow Tim0ie interrupt, Tim0ie = 1;
6. Finally, the overflow interrupt flag, tmr0if = 0, indicates no overflow, when the timer is opened Tmro will start counting, one instruction period plus one, from 0-255. When added to 255, add one to make tmr0if = 1; The count jumps to 0 to continue.
The 7.tmr0 can be automatically counted without enabling, but because execution delays 2 instruction cycles, the initial value of the TMR0 needs to be set to counteract this delay.
It's the code, haha.
void Inittime ();
void Init_fosc (); Set the internal oscillator, but it seems useless.
unsigned int count = 0;
int main (int argc, char** argv)
{
Initcpu ();
Init_fosc ();
Inittime ();
Trisc = 0x00;
LATC = 0x00;
while (1);
return (exit_success);
}
void Inittime ()
{
INTEDG = 0; BIT6 Interrupt Edge Selection bit, 1 = rising edge, 0 = Falling edge
tmr0cs = 0; BIT5 Timer0 Clock Source Select bit, 0 = internal instruction cycle Clock (FOSC/4)
TMR0SE = 0; BIT4 Timer0 clock source edge selection bit, 1 = incremented at T0cki pin level descent, 0 = rising edge
PSA = 0; Bit3 prescaler Assignment bit, 1 = not assigned to timer0,0 = Prescaler is distributed to Timer0
PS0 = 1;
PS1 = 1;
PS2 = 0; 1:16
Ps<2:0> prescaler divider ratio Select bit
GIE = 1; Allow all valid interrupts
Peie = 0; Disable all peripheral interrupts, pending consideration
Tmr0ie = 1; Allow TMR0 interrupts
tmr0if = 0; Overflow interrupt flag bit, not overflow
TMR0 = 1;
}
void Init_fosc ()
{
Osccon = 0x6a; The following settings are set to set the internal oscillator frequency
SCS0 = 1;
SCS1 = 0; 1x Internal Oscillator Module
IRCF0 = 0;
IRCF1 = 1;
IRCF2 = 1;
IRCF3 = 0; 1101 = 250kHz
}
void Interrupt ISR ()
{
TMR0 = 1;
count++;
if (Count ==10)
{
LATC = ~LATC;
Count = 0;
}
tmr0if = 0;
}
Next is the TIMER1 module, the special place of the TIMER1 module is with gated, is a 16-bit timing counter, has a dedicated 32kHz oscillator circuit.
Let's start with the code, and it'll feel more organized.
unsigned int count = 0;
void Init_timer1 ();
void Init_fosc ();
void interrupt ISR ();
int main (int argc, char** argv)
{
Initcpu ();
Init_fosc ();
Init_timer1 ();
Trisc = 0x00;
LATC = 0x00;
while (1);
return (exit_success);
}
void Init_timer1 ()
{
Tmr1ge = 0; All are 1 o'clock enabled to count.
TMR1CS0 = 0;
TMR1CS1 = 0; These two bits to select the clock source, select the instruction clock
T1CKPS0 = 1;
T1CKPS1 = 0; T1CKPS Select bit for Prescaler
T1oscen = 0; Enable dedicated oscillator
tmr1h = 0xd8; Delay for oscillator stabilization
tmr1l = 0xf0; Ditto
Tmr1ie = 1;
Peie = 1; Peripheral interrupt Allow bit
GIE = 1; Allow all valid interrupts
tmr1if = 0;
tmr1l = 0;
Tmr1on = 1; For 1 o'clock Timer1 Open because the Timer1 oscillator requires a vibration and settling time
}
void Init_fosc ()
{
Osccon = 0x6a; The following settings are set to set the internal oscillator frequency
SCS0 = 1;
SCS1 = 0; 1x Internal Oscillator Module
IRCF0 = 1;
IRCF1 = 0;
IRCF2 = 1;
IRCF3 = 1; 1101 = 250kHz
}
void Interrupt ISR ()
{
tmr1h = 0xd8;
tmr1l = 0xf0;
count++;
if (count = = 100)
{
LATC = ~LATC;
Count = 0;
}
tmr1if = 0;
}
Then write down the steps to summarize.
1. Initial clock source
2. Select the clock source first in the Timer1
3. Set the Prescaler
4. Allow interrupt and overflow bit zeroing
5. Enable Timer1
Although it looks simple, but the function of Timer1 more than Timer0, better himself has not been used, there is a place to pay attention to, TMR1H and tmr1l the initial setting of the best just take the delay back on the line. So it should be put in the last place, just need to start the vibration time
"Single Chip Microcomputer" "pic16f1937" timer