📄 logicchecker.c
字号:
#include <p18f8680.h>
#include <delays.h>
struct Bits_struct // Set up structure to handle different bits used in program
{
unsigned char X1:1; // Reserve 1 bit for X1
unsigned char X2:1; // Reserve 1 bit for X2
unsigned char FlashX1:1;
unsigned char FlashX2:1;
unsigned char a:1;
unsigned char b:1;
unsigned char c:1;
unsigned char d:1;
};
unsigned char Truth = 0;
struct Bits_struct Bits;
void high_isr(void);
void low_isr(void);
#pragma code interrupt_at_high_vector = 0x08
void interrupt_at_high_vector(void)
{
_asm
GOTO high_isr
_endasm
}
#pragma code
#pragma interrupt high_isr
void high_isr(void)
{
INTCONbits.GIE = 0; //Disable all interrupts
INTCONbits.T0IF = 0; //Clear TMR0 interrupt flag
TMR0H = 0x0B; // Reset Timer 0
TMR0L = 0xDB;
if (Bits.FlashX1 == 1)
PORTDbits.RD1 = ~PORTDbits.RD1;
if (Bits.FlashX2 == 1)
PORTDbits.RD0 = ~PORTDbits.RD0;
INTCONbits.GIE = 1; //Enable all interrupts
}
void main (void)
{
INTCON = 0xE0; // Enable global interrupts, enable TMR0 interrupt, clear TMR0 overflow flag
RCONbits.IPEN = 0; // Disable interrupt priorities
T0CON = 0x05; // Set Timer 0 as 16 bit, increment on instruction cycle, 64 prescaler
TMR0H = 0x0B; // Load 3035 (0x0BDB) into TMR0 register
TMR0L = 0xDB;
T0CONbits.TMR0ON = 1; // Turn Timer 0 on
TRISD = 0x00;
TRISB = 0x0F;
TRISG = 0xFF;
PORTD = 0xFF;
while (1)
{
if (Truth == 0x10) // Reset truth after it reaches 16
Truth = 0x00;
// Apply the value of Truth to a, b, c, and d bits
Bits.a = Truth >> 3;
Bits.b = (Truth & 0x04) >> 2;
Bits.c = (Truth & 0x02) >> 1;
Bits.d = (Truth & 0x01);
// Apply a, b, c, and d bits to PORTA pins
PORTBbits.RB7 = Bits.a;
PORTBbits.RB6 = Bits.b;
PORTBbits.RB5 = Bits.c;
PORTBbits.RB4 = Bits.d;
// Conditional statements to implement logic:
Bits.X1 = ((!Bits.b && !Bits.c) || (Bits.b && Bits.c) || (Bits.a && !Bits.b && Bits.d)) ? 1 : 0;
Bits.X2 = ((!Bits.c && Bits.d) || (Bits.c && !Bits.d) || (Bits.a && Bits.b && Bits.c)) ? 1 : 0;
// Clear the upper four bits of PORTD
// and copy inverted value of Truth to upper 4 bits of PORTD
PORTD = (PORTD & 0x0F) | ~Truth << 4;
Delay10KTCYx(100); // Long delay to allow for propagation delay in logic
// Test if X1 is the same as "Black box" X1
if (PORTGbits.RG0 == Bits.X1)
{
PORTDbits.RD1 = !Bits.X1; // Turn on LED representing X1
Bits.FlashX1 = 0;
PORTDbits.RD3 = 1;
}
else
{
Bits.FlashX1 = 1; // Flash X1
}
// Test if X2 is the same as "Black box" X2
if (PORTGbits.RG2 == Bits.X2)
{
PORTDbits.RD0 = !Bits.X2; // Turn on LED representing X2
Bits.FlashX2 = 0;
PORTDbits.RD2 = 1;
}
else
{
Bits.FlashX2 = 1; // Flash X2
}
while (PORTBbits.RB0); // Wait until RB0 is pushed
while (!PORTBbits.RB0); // Hold in loop until RB0 is released
Truth++;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -