Kod: Markera allt
typedef union
{
unsigned char Byte[2];
unsigned int Word;
} T_BYTE_AND_WORD;
T_BYTE_AND_WORD Pulse;
unsigned int Previous, Difference;
unsigned char Update;
void interrupt(void)
{
if(PIR1.CCP1IF)
{
Pulse.Byte[0] = CCPR1L; // Transfer the value, low byte
Pulse.Byte[1] = CCPR1H; // Transfer the value, high byte
PIR1.CCP1IF = false; // Acknowledge interrupt
Difference = Pulse - Previous;
Previous = Pulse;
Update = true;
}
}
void main(void)
{
Initialize();
while(true)
{
if(Update)
{
Update = false;
// Nu gör du med Difference vad du behöver
}
}
}
void Initialize(void)
{
TRISA = 0xEF; // All input but PORTA.4
TRISB = 0x08; // All output but PORTB.3
CMCON = 0x07; // No comparator inputs
CCP1CON = 0x05; // Set CCP1 to capture all rising edges and use Timer 1 as timebase
PIE1 = 0x04; // Allow CCP1 interrupts
PIR1 = 0x00; // Erase any latent interrupts
INTCON = 0xC0; // Allow interrupts
T1CON = 0x31; // Set Timer 1 to lowest possible speed (125KHz which anyway is too high) and start it counting
PORTA = 0x00; // Set to all '0'
PORTB = 0x04; // Set to all '0' but PORTB.2 (Ser out)
}