0% found this document useful (0 votes)
2 views

code.mickrocusb

The document contains code for a microcontroller program that manages traffic light modes (auto, red, yellow) and responds to button presses and USB commands. It utilizes a timer interrupt for time tracking and includes functions for switching lights and processing USB data. The program operates in a loop, checking for button inputs and executing the appropriate light control based on the current mode.

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)
2 views

code.mickrocusb

The document contains code for a microcontroller program that manages traffic light modes (auto, red, yellow) and responds to button presses and USB commands. It utilizes a timer interrupt for time tracking and includes functions for switching lights and processing USB data. The program operates in a loop, checking for button inputs and executing the appropriate light control based on the current mode.

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/ 5

#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 int boDem = 0;

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;
char choPhepNut = 1;
volatile unsigned long millisCounter = 0; // Global time counter
unsigned long lastUpdateTime = 0;
unsigned char lightState = 0; // For blinking mode

// Timer0 Interrupt (Triggers every 1ms)


void interrupt() {
if (INTCON.TMR0IF) {
millisCounter++; // Increment millis counter
INTCON.TMR0IF = 0; // Clear Timer0 interrupt flag
TMR0L = 100; // Reload Timer (for next 1ms)
}

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
}

// Ð?m b?o dèn vàng v?n sáng sau vòng l?p


LATE1_bit = 1;

// 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;

setupTimer0(); // Start Timer0

lastUpdateTime = millis(); // Initialize time tracking

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