Kod: Markera allt
/*************************
* (C) Icecap 2006-02-10 *
* Ska köra på en PIC16F628A *
* MikroC *
*************************/
#define false 0
#define true 1
typedef unsigned int word;
typedef unsigned char byte;
typedef unsigned long dword;
byte Delay_Counter;
void Initialize(void);
void interrupt(void)
{
if(PIR1.TMR2IF)
{
PIR1.TMR2IF = false; // Acknowledge interrupt
if(Delay_Counter) Delay_Counter--; // If >0 count it down
}
}
void main(void)
{
Initialize(); // Set up timers, interrupts, variables and such
while(true)
{ // Repeat forever
Do_Thing_1();
Delay(25); // Wait 1 sec
Do_Thing_2();
Delay_Counter = 25; // Set to 1 sec delay
while(Delay_Counter) // Wait 1 sec
{
if(Alarm_Input) Delay_Counter = 0; // Terminate wait if something is wrong
}
} // And then do it all over again
}
void Delay(byte Length)
{
Delay_Counter = Length;
while(Delay_Counter); // Wait for it to be finished
}
void Initialize(void)
{
Delay_Counter = 0;
TRISB = 0x01; // All out but PORTB.0
TRISA = 0x00; // All out, unused
CMCON = 0x07; // No comparator inputs
PORTA = 0x00; // Set to all '0'
PORTB = 0x04; // Set to all '0' but PORTB.2 (Ser out)
// Set UART to 19K2,n,8,1
SPBRG = 12; // 19K2 @ 4MHz
TXSTA = 0x26; // Asynk,8,n,1
RCSTA = 0x80; // Kickstart UART, transmit only, no RX
// Set Timer2 to give repeated interrupts, it's the time-tick of the whole system
T2CON = 0x4E;
PR2 = 249; // 124 = 50Hz, 249 = 25Hz
PIR1.TMR2IF = 0;
PIE1.TMR2IE = 1;
// Allow interrupts in general
INTCON = 0xC0;
}