0% found this document useful (0 votes)
15 views4 pages

Usb Code c Chuẩn

The document contains code for a traffic light control system that operates in three modes: automatic, red, and yellow blinking. It includes functionality for reading button inputs, managing light states, and processing USB commands to adjust timing for each light. The system uses interrupts for timing and updates the light states based on the current mode and elapsed time.

Uploaded by

ntv63472
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views4 pages

Usb Code c Chuẩn

The document contains code for a traffic light control system that operates in three modes: automatic, red, and yellow blinking. It includes functionality for reading button inputs, managing light states, and processing USB commands to adjust timing for each light. The system uses interrupts for timing and updates the light states based on the current mode and elapsed time.

Uploaded by

ntv63472
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

#define MODE_AUTO 0

#define MODE_RED 1
#define MODE_YELLOW 2

#define in_size 64
#define out_size 64

unsigned char readbuff[in_size] absolute 0x500;


unsigned char writebuff[out_size] absolute 0x540;

unsigned short currentMode = MODE_AUTO;

int T_RED = 10000;


int T_YELLOW = 6000;
int T_GREEN = 20000;
int T_BLINK = 1000;
const unsigned int CHECK_INTERVAL = 50;

volatile unsigned long millisCounter = 0; // Global time counter


unsigned long lastUpdateTime = 0;
unsigned char lightState = 0; // For blinking mode
static unsigned char state = 0; // Bi?n tr?ng thái c?a ch? d? t? d?ng

// Timer0 Interrupt (Triggers every 1ms)


void interrupt() {
if (INTCON.TMR0IF) {
millisCounter++;
INTCON.TMR0IF = 0;
TMR0L = 100;
}

if (USBIF_bit) {
USBIF_bit = 0;
USB_Interrupt_Proc();
}
}

// Function to get the current time in milliseconds


unsigned long millis() {
return millisCounter;
}

char checkButtons() {
static unsigned char prevState = 0b000;
unsigned char currentState = (PORTB & 0b00000111);

if ((currentState & 0b001) && !(prevState & 0b001)) {


currentMode = MODE_RED;
writebuff[0] = 'D';
HID_Write(&writebuff, out_size);
}
if ((currentState & 0b010) && !(prevState & 0b010)) {
currentMode = MODE_YELLOW;
writebuff[0] = 'V';
HID_Write(&writebuff, out_size);
}
if ((currentState & 0b100) && !(prevState & 0b100)) {
currentMode = MODE_AUTO;
state = 0; // Ð?m b?o ch? d? Auto luôn b?t d?u t? dèn d?
}

prevState = currentState;
return 0;
}

void switchLight(unsigned char red, unsigned char yellow, unsigned char green) {
LATE0_bit = red;
LATE1_bit = yellow;
LATE2_bit = green;
}

void autoMode() {
static unsigned long stateStartTime = 0;
unsigned long currentTime = millis();

if (checkButtons()) return;

switch (state) {
case 0:
switchLight(1, 0, 0);
writebuff[0] = 'D';
HID_Write(&writebuff, out_size);
stateStartTime = currentTime;
state = 1;
break;

case 1:
if (currentTime - stateStartTime >= T_RED) {
switchLight(0, 1, 0);
writebuff[0] = 'V';
HID_Write(&writebuff, out_size);
stateStartTime = currentTime;
state = 2;
}
break;

case 2:
if (currentTime - stateStartTime >= T_YELLOW) {
switchLight(0, 0, 1);
writebuff[0] = 'X';
HID_Write(&writebuff, out_size);
stateStartTime = currentTime;
state = 3;
}
break;

case 3:
if (currentTime - stateStartTime >= T_GREEN) {
state = 0;
}
break;
}
}

void yellowBlinkMode() {
unsigned long currentTime = millis();
if (currentTime - lastUpdateTime >= T_BLINK) {
lastUpdateTime = currentTime;
lightState = !lightState;
switchLight(0, lightState, 0);
writebuff[0] = lightState ? 'O' : 'F';
HID_Write(&writebuff, out_size);
}
}

void processUSB() {
if (HID_Read()) {
LATE1_bit = 1;

switch (readbuff[0]) {
case 'A':
currentMode = MODE_RED;
writebuff[0] = 'D';
break;
case 'B':
currentMode = MODE_YELLOW;
writebuff[0] = 'V';
break;
case 'C':
currentMode = MODE_AUTO;
state = 0; // Reset ch? d? t? d?ng v? dèn d?
writebuff[0] = 'A';
break;

case 'a': T_RED = 6000; break;


case 'b': T_RED = 8000; break;
case 'c': T_RED = 10000; break;
case 'd': T_RED = 12000; break;
case 'e': T_RED = 14000; break;
case 'f': T_RED = 16000; break;
case 'g': T_RED = 18000; break;
case 'h': T_RED = 20000; break;

case 'i': T_YELLOW = 6000; break;


case 'j': T_YELLOW = 8000; break;
case 'k': T_YELLOW = 10000; break;
case 'l': T_YELLOW = 12000; break;
case 'm': T_YELLOW = 14000; break;
case 'n': T_YELLOW = 16000; break;
case 'o': T_YELLOW = 18000; break;
case 'p': T_YELLOW = 20000; break;

case 'q': T_GREEN = 6000; break;


case 'r': T_GREEN = 8000; break;
case 's': T_GREEN = 10000; break;
case 't': T_GREEN = 12000; break;
case 'u': T_GREEN = 14000; break;
case 'v': T_GREEN = 16000; break;
case 'w': T_GREEN = 18000; break;
case 'x': T_GREEN = 20000; break;
}

HID_Write(&writebuff, out_size);
LATE1_bit = 0;
}
}
void setupTimer0() {
T0CON = 0b11000011;
TMR0L = 100;
INTCON.TMR0IE = 1;
INTCON.TMR0IF = 0;
}

void main() {
ADCON1 |= 0x0F;
CMCON |= 7;

PORTB = 0x00;
LATB = 0x00;
TRISB0_bit = 1;
TRISB1_bit = 1;
TRISB2_bit = 1;

PORTE = 0x00;
LATE = 0x00;
TRISE0_bit = 0;
TRISE1_bit = 0;
TRISE2_bit = 0;

delay_ms(100);
UPUEN_bit = 1;
FSEN_bit = 1;
HID_Enable(&readbuff, &writebuff);

USBIF_bit = 0;
USBIE_bit = 1;
GIE_bit = 1;
PEIE_bit = 1;

setupTimer0();

lastUpdateTime = millis();

while (1) {
checkButtons();
processUSB();

switch (currentMode) {
case MODE_AUTO:
autoMode();
break;
case MODE_RED:
switchLight(1, 0, 0);
break;
case MODE_YELLOW:
yellowBlinkMode();
break;
}
}
}

You might also like