Abdulkerim
Junior Member level 1
hello dear friends
I need your helps to find that formula to compute timer1 registers
for generate variable frequency... between 8Hz to 30KHz
know that the chip has not compare mode, only overflow
chip: PIC16F630
prescaler: 1, 2, 4 & 8
thank you
I need your helps to find that formula to compute timer1 registers
for generate variable frequency... between 8Hz to 30KHz
know that the chip has not compare mode, only overflow
chip: PIC16F630
prescaler: 1, 2, 4 & 8
thank you
C:
/// header file
#define cycle_plses 4 /// EVERY 4 PULSES FROM XTAL ///
#define pulse_state 2 /// HIGH & LOW ///
#define cpu_freq 8000000 /// EXTERNAL OR ENTERNAL OSC ///
C:
/// interrupt vector
void Interrupt ()
{
if (TMR1IF_BIT)
{
TMR1IF_BIT = false;
TMR1H = high_byte;
TMR1L = low_byte;
gate ^= 1; /// toggle the mosfet
} /// if (TMR1IF_BIT)
}
C:
/// compute function
void SetFrequency (uint16_t freq)
{
bool TMR1ON_OldState, TMR1IE_OldState;
volatile uint16_t RegValue;
uint8_t prescaler;
TMR1IE_OldState = TMR1IE_BIT;
PIE1 &= ~(1<<TMR1IE);
TMR1ON_OldState = TMR1ON_BIT;
T1CON &= ~(1<<TMR1ON);
gate = false;
/// select prescaler of timer1
if (freq >= 31) prescaler = 1;
else if (freq >= 16) prescaler = 2;
else if (freq >= 8) prescaler = 4;
else if (freq >= 4) prescaler = 8;
else prescaler = 0;
/// this formula needed ///
RegValue = cpu_freq / cycle_plses / freq / pulse_state / prescaler;
low_byte = RegValue;
RegValue >>= 8;
high_byte = RegValue;
switch (prescaler)
{
case 1:
T1CON &= ~(1<<T1CKPS1);
T1CON &= ~(1<<T1CKPS0);
break;
case 2:
T1CON &= ~(1<<T1CKPS1);
T1CON |= (1<<T1CKPS0);
break;
case 4:
T1CON |= (1<<T1CKPS1);
T1CON &= ~(1<<T1CKPS0);
break;
case 8:
T1CON |= (1<<T1CKPS1);
T1CON |= (1<<T1CKPS0);
break;
}
TMR1H = high_byte;
TMR1L = low_byte;
TMR1IE_BIT = TMR1IE_OldState;
TMR1ON_BIT = TMR1ON_OldState;
}
Last edited: