code.mickrocusb
code.mickrocusb
#define MODE_RED 1
#define MODE_YELLOW 2
#define in_size 64
#define out_size 64
if (USBIF_bit) {
USBIF_bit = 0;
USB_Interrupt_Proc();
}
}
// Function to get the current time in milliseconds
unsigned long millis() {
return millisCounter;
}
char checkButtons() {
if (choPhepNut) {
if (Button(&PORTB, 0, 10, 0)) {
while (Button(&PORTB, 0, 10, 0));
currentMode = MODE_RED;
return 1;
}
if (Button(&PORTB, 1, 10, 0)) {
while (Button(&PORTB, 1, 10, 0));
currentMode = MODE_YELLOW;
return 1;
}
if (Button(&PORTB, 2, 10, 0)) {
while (Button(&PORTB, 2, 10, 0));
currentMode = MODE_AUTO;
return 1;
}
}
return 0;
}
void switchLight(unsigned char red, unsigned char yellow, unsigned char green) {
LATE0_bit = red;
LATE1_bit = yellow;
LATE2_bit = green;
}
void processUSB() {
if (HID_Read()) { // Check if data was received
LATE1_bit = 1; // Turn on Yellow LED as an indicator
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;
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 = 200000; break;
}
// vang
{
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 = 200000; break;
}
// xanh
{
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 = 200000; break;
}
}
HID_Write(&writebuff, out_size);
LATE1_bit = 0; // Turn off the indicator after processing
}
}
void autoMode() {
// Ðèn d?
LATE0_bit = 1; LATE1_bit = 0; LATE2_bit = 0;
writebuff[0] = 'D';
HID_Write(&writebuff, out_size);
for (boDem = 0; boDem < T_RED; boDem += CHECK_INTERVAL) {
if (checkButtons()) return;
if (HID_Read()) {
processUSB();
return;
}
delay_ms(CHECK_INTERVAL);
}
// Ðèn vàng
// Ðèn vàng nh?p nháy trong ch? d? t? d?ng
LATE0_bit = 0;
LATE2_bit = 0; // T?t dèn d? và xanh
LATE1_bit = 1; // B?t dèn vàng
writebuff[0] = 'V';
HID_Write(&writebuff, out_size);
for (boDem = 0; boDem < T_YELLOW; boDem += CHECK_INTERVAL) {
if (checkButtons()) return; // N?u có nút b?m, thoát kh?i ch? d? t? d?ng
if (HID_Read()) {
processUSB();
return;
}
delay_ms(CHECK_INTERVAL); // Gi? vòng l?p trong th?i gian c?n thi?t
}
// Sau khi hoàn thành dèn vàng, chuy?n sang dèn xanh
LATE0_bit = 0; LATE1_bit = 0; LATE2_bit = 1; // B?t dèn xanh
writebuff[0] = 'X';
HID_Write(&writebuff, out_size);
for (boDem = 0; boDem < T_GREEN; boDem += CHECK_INTERVAL) {
if (checkButtons()) return;
if (HID_Read()) {
processUSB();
return;
}
delay_ms(CHECK_INTERVAL);
}
}
void yellowBlinkMode() {
unsigned long currentTime = millis();
if (currentTime - lastUpdateTime >= T_BLINK) {
lastUpdateTime = currentTime;
lightState = !lightState;
switchLight(0, lightState, 0);
if (lightState) {
writebuff[0] = 'O'; // Ðèn vàng sáng
} else {
writebuff[0] = 'F'; // Ðèn vàng t?t
}
HID_Write(&writebuff, out_size);
}
}
void setupTimer0() {
T0CON = 0b11000011; // Enable Timer0, 8-bit mode, 1:16 prescaler
TMR0L = 100; // Initial value for 1ms interrupt
INTCON.TMR0IE = 1; // Enable Timer0 Interrupt
INTCON.TMR0IF = 0; // Clear Timer0 Interrupt Flag
}
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;
while (1) {
checkButtons();
processUSB();
switch (currentMode) {
case MODE_AUTO:
autoMode();
break;
case MODE_RED:
switchLight(1, 0, 0);
break;
case MODE_YELLOW:
yellowBlinkMode();
break;
}
}
}