############################################################## # ESD-1 # ############################################################## //Lab1-1 #include #include const char LED[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; void main(void){ char cnt; P2 = 0xFF; for(;;){ for(cnt=0;cnt<8;cnt++){ P2 = ~LED[cnt]; _wait_ms(500); } } } ********************************************************************* //2a #include #include const char LED[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; void main(void){ char cnt; P2 = 0xFF; for(;;){ for(cnt=0;cnt<8;cnt++){ P2 = ~LED[cnt]; if (cnt%2==0){ _wait_ms(500); } else _wait_ms(1000); } } } ********************************************************************* //2b #include #include const char LED[4] = {0x03,0x0C,0x30,0xC0}; void main(void){ char cnt; P2 = 0xFF; for(;;){ for(cnt=0;cnt<4;cnt++){ P2 = ~LED[cnt]; _wait_ms(1000); } } } ********************************************************************** //Disco light (Reduced coding version) #include #include void main(void){ int cnt; P2 = 0xFF; for(;;){ for(cnt=0;cnt<8;cnt++){ P2 = ~(1<=0;cnt--){ P2 = (0x07<=0;cnt--){ P2 = (0x03< #include const char LED1[4] = {0x81,0x42,0x24,0x18}; const char LED2[4] = {0x18,0x24,0x42,0x81}; void main(void){ int cnt; P2 = 0xFF; for(;;){ for(cnt=0;cnt<4;cnt++){ P2 = ~LED1[cnt]; _wait_ms(50); } for(cnt=0;cnt<4;cnt++){ P2 = ~LED2[cnt]; _wait_ms(50); } } } ************************************************************************ Disco light Beta version #include #include const char LED1[8] = {0x01,0x02,0x04,0x08,0x10,0x20,0x40,0x80}; const char LED2[7] = {0x03,0x06,0x0C,0x18,0x30,0x60,0xC0}; const char LED3[6] = {0x07,0x0E,0x1C,0x38,0x70,0xE0}; const char LED4[5] = {0x0F,0x1E,0x3C,0x78,0xF0}; const char LED5[4] = {0x1F,0x3E,0x7C,0xF8}; const char LED6[3] = {0x3F,0x7E,0xFC}; //const char LED7[2] = {0x7F,0xFE}; //const char LED8[1] = {0xFF}; void main(void){ int cnt; P2 = 0xFF; for(;;){ for(cnt=0;cnt<8;cnt++){ P2 = ~LED1[cnt]; _wait_ms(50); } for(cnt=0;cnt<7;cnt++){ P2 = ~LED2[cnt]; _wait_ms(50); } for(cnt=0;cnt<6;cnt++){ P2 = ~LED3[cnt]; _wait_ms(50); } for(cnt=0;cnt<5;cnt++){ P2 = ~LED4[cnt]; _wait_ms(50); } for(cnt=0;cnt<4;cnt++){ P2 = ~LED5[cnt]; _wait_ms(50); } for(cnt=0;cnt<3;cnt++){ P2 = ~LED6[cnt]; _wait_ms(50); } /* for(cnt=0;cnt<2;cnt++){ P2 = ~LED7[cnt]; _wait_ms(50); } P2 = ~LED7[0]; _wait_ms(50);*/ } } ############################################################## # ESD-2 # ############################################################## *************************************************** Q1 *************************************************** #include #include const char rotorleft[4]={0x0A, 0x06, 0x05, 0x09}; const char rotorright[4]={0x06, 0x0A, 0x09, 0x05}; int Left(void){ char repeat; int status; for(repeat=0;repeat<45;repeat++){ char a=0; for(a=0;a<4;a++){ P2=~rotorleft[a]; if(P3==0xBF){ status=0; break; } _wait_ms(100); } } P2=0x0F; return status; } int Right(void){ char repeat; int status; for(repeat=0;repeat<45;repeat++){ char a=0; for(a=0;a<4;a++){ P2=~rotorright[a]; if(P3==0xBF){ status=0; break; } _wait_ms(100); } } P2=0x0F; return status; } void main(void){ int status=0; for(;;){ if(P3==0x7F){ status=1; } else if(P3==0xBF){ status=0; } while(status==1){ status = Left(); _wait_ms(1000); status = Right(); _wait_ms(1000); } } } ********************************************** Q2 ********************************************** #include #include const char rotorleft[4]={0x0A, 0x06, 0x05, 0x09}; const char rotorright[4]={0x06, 0x0A, 0x09, 0x05}; void Left(int *status, int *direction){ char repeat; for(repeat=0;repeat<45;repeat++){ char a=0; if(*status==0||*direction==1){break;} for(a=0;a<4;a++){ P2=~rotorleft[a]; if(P3==0xBF){ *status=0; break; } if(P3==0xEF){ *direction=1; // forward break; } _wait_ms(100); } } P2=0x0F; } void Right(int *status, int *direction){ char repeat; for(repeat=0;repeat<45;repeat++){ char a=0; if(*status==0||*direction==0){break;} for(a=0;a<4;a++){ P2=~rotorright[a]; if(P3==0xBF){ *status=0; break; } if(P3==0xDF){ *direction=0; // backward break; } _wait_ms(100); } } P2=0x0F; } void main(void){ int status=0; int direction=0; for(;;){ if(P3==0x7F){ status=1; } else if(P3==0xBF){ status=0; } while(status==1&&direction==0){ Left(&status, &direction); _wait_ms(1000); } while(status==1&&direction==1){ Right(&status, &direction); _wait_ms(1000); } } } ********************************************** Q3 ********************************************** #include #include const char rotorleft[4]={0x0A, 0x06, 0x05, 0x09}; const char rotorright[4]={0x06, 0x0A, 0x09, 0x05}; void Left(int *status, int *direction){ int n=220; char repeat; for(repeat=0;repeat<45;repeat++){ char a=0; if(*status==0||*direction==1){break;} for(a=0;a<4;a++){ P2=~rotorleft[a]; if(P3==0xBF){ *status=0; break; } if(P3==0xEF){ n-=20; } if(P3==0xDF){ n+=20; } _wait_ms(n); } } P2=0x0F; } void main(void){ int status=0; int direction=0; for(;;){ if(P3==0x7F){ status=1; } else if(P3==0xBF){ status=0; } while(status==1&&direction==0){ Left(&status, &direction); _wait_ms(1000); } } } ********************************************** Q4 ********************************************** #include #include const char rotorleft[4]={0x0A, 0x06, 0x05, 0x09}; const char rotorright[4]={0x06, 0x0A, 0x09, 0x05}; const char SEVEN[10]={0xC0, 0xF9, 0xA4, 0XB0, 0x99, 0x92, 0x82, 0xF8, 0x80, 0x98}; void Left(int *status, int *direction){ int n=220, m=10; char repeat; for(repeat=0;repeat<45;repeat++){ char a=0; if(*status==0||*direction==1){break;} for(a=0;a<4;a++){ P2=~rotorleft[a]; if(P3==0xBF){ *status=0; break; } if(P3==0xEF){ n-=20; m-=1; P0=~SEVEN[m]; } if(P3==0xDF){ n+=20; m+=1; P0=~SEVEN[m]; } _wait_ms(n); } } P2=0x0F; } void main(void){ int status=0; int direction=0; for(;;){ if(P3==0x7F){ status=1; } else if(P3==0xBF){ status=0; } while(status==1&&direction==0){ Left(&status, &direction); _wait_ms(1000); } } } ############################################################## # ESD-3 # ############################################################## ********************************************************************************** Question 1: ********************************************************************************** Crystal Frequency: 12MHz RCAP2H= 0xEC RCAP2L= 0x78 Crystal Frequency: 11.0592MHz RCAP2H= 0xEE RCAP2L= 0x00 ********************************************************************************** Question 2: ********************************************************************************** Flashing frequency of the LED = 5msx100 = 0.5s For LED ON, it will have 0.5s For LED OFF, it will have 0.5s also Total time = (0.5s + 0.5s) = 1s Then, the flashing frequency will be 1Hz ********************************************************************************** (1) Flashing LED ********************************************************************************** #include #include // inclue this file to use IRQ_VECTOR macro unsigned char bit LED_state; unsigned char bit LED_pin @ 0x87; // directly accessing the pin #define LED_DURATION 100 near unsigned led_duration_count; // to place this variable in internal RAM void LED_Init(void); void LED_Update(void); // Function prototype // NOTE: ISR is not explictly called and does not require a prototype void Timer_2_Init(void); // install the interrupt vector IRQ_VECTOR(Timer_2_Tick,TIMER2) /* --------------------------------------------------------------- */ void main(void) { LED_Init(); Timer_2_Init(); // Set up Timer 2 EA = 1; // Globally enable interrupts while(1); // An empty Super Loop } /* --------------------------------------------------------------- */ void Timer_2_Init(void) { T2CON = 0x04; // Load Timer 2 control register TH2 = 0xEC; // Load Timer 2 high byte RCAP2H = 0xEC; // Load Timer 2 reload capt. reg. high byte TL2 = 0x78; // Load Timer 2 low byte RCAP2L = 0x78; // Load Timer 2 reload capt. reg. low byte // Timer 2 interrupt is enabled, and ISR will be called // whenever the timer overflows - see below. ET2 = 1; // Start Timer 2 running TR2 = 1; } /* --------------------------------------------------------------- */ // This ISR is called every timer tick (5ms) void Timer_2_Tick(void) interrupt { // must manually clear timer 2 overflow flag TF2 = 0; // call task LED_Update(); } /* --------------------------------------------------------------- */ void LED_Init(void) { LED_state = 0; LED_pin = 0; led_duration_count = 0; } /* --------------------------------------------------------------- */ void LED_Update(void) { led_duration_count++; if (led_duration_count == LED_DURATION){ led_duration_count = 0; if (LED_state==1){ LED_state = 0; LED_pin = 0; } else{ LED_state = 1; LED_pin = 1; } } } /* --------------------------------------------------------------- */ ********************************************************************************** (2) Stepper Motor Control ********************************************************************************** #include #include #include #include // inclue this file to use IRQ_VECTOR macro unsigned char bit LED_state; unsigned char bit LED_pin @ 0x87; // directly accessing the pin #define LED_DURATION 100 char a = 0; near unsigned led_duration_count; // to place this variable in internal RAM void LED_Init(void); void LED_Update(void); // Function prototype // NOTE: ISR is not explictly called and does not require a prototype void Timer_2_Init(void); // install the interrupt vector IRQ_VECTOR(Timer_2_Tick,TIMER2) /* --------------------------------------------------------------- */ void main(void) { Timer_2_Init(); // Set up Timer 2 EA = 1; // Globally enable interrupts while(1); // An empty Super Loop } /* --------------------------------------------------------------- */ void Timer_2_Init(void) { T2CON = 0x04; // Load Timer 2 control register TH2 = 0xEE; // Load Timer 2 high byte RCAP2H = 0xEE; // Load Timer 2 reload capt. reg. high byte TL2 = 0x00; // Load Timer 2 low byte RCAP2L = 0x00; // Load Timer 2 reload capt. reg. low byte // Timer 2 interrupt is enabled, and ISR will be called // whenever the timer overflows - see below. ET2 = 1; // Start Timer 2 running TR2 = 1; } /* --------------------------------------------------------------- */ // This ISR is called every timer tick (5ms) void Timer_2_Tick(void) interrupt { // must manually clear timer 2 overflow flag TF2 = 0; a++; if (a==3){a=0; run_motor_step(0, 10, 2000);} // call task } const char pattern[] = {1, 5, 4, 6, 2, 10, 8, 9}; #define PHASE_MASK 0x7 void run_motor_phase(char direction) { static char phase; if (direction) phase = (++phase) & PHASE_MASK; else phase = (--phase) & PHASE_MASK; P2 = ~pattern[phase]; } void run_motor_step(char direction, char speed, short steps) { short i; for (i=0; i #include #include #include // inclue this file to use IRQ_VECTOR macro unsigned char bit LED_state; unsigned char bit LED_pin @ 0x87; // directly accessing the pin #define LED_DURATION 100 char a = 0; near unsigned led_duration_count; // to place this variable in internal RAM void LED_Init(void); void LED_Update(void); // Function prototype // NOTE: ISR is not explictly called and does not require a prototype void Timer_2_Init(void); // install the interrupt vector IRQ_VECTOR(Timer_2_Tick,TIMER2) /* --------------------------------------------------------------- */ void main(void) { LED_Init(); Timer_2_Init(); // Set up Timer 2 EA = 1; // Globally enable interrupts while(1); // An empty Super Loop } /* --------------------------------------------------------------- */ void Timer_2_Init(void) { T2CON = 0x04; // Load Timer 2 control register TH2 = 0xEE; // Load Timer 2 high byte RCAP2H = 0xEE; // Load Timer 2 reload capt. reg. high byte TL2 = 0x00; // Load Timer 2 low byte RCAP2L = 0x00; // Load Timer 2 reload capt. reg. low byte // Timer 2 interrupt is enabled, and ISR will be called // whenever the timer overflows - see below. ET2 = 1; // Start Timer 2 running TR2 = 1; } /* --------------------------------------------------------------- */ // This ISR is called every timer tick (5ms) void Timer_2_Tick(void) interrupt { // must manually clear timer 2 overflow flag TF2 = 0; LED_Update(); a++; if (a==3){a=0; run_motor_phase(0, 10);} // call task } /* --------------------------------------------------------------- */ void LED_Init(void) { LED_state = 0; LED_pin = 0; led_duration_count = 0; } /* --------------------------------------------------------------- */ void LED_Update(void) { led_duration_count++; if (led_duration_count == LED_DURATION){ led_duration_count = 0; if (LED_state==1){ LED_state = 0; LED_pin = 0; } else{ LED_state = 1; LED_pin = 1; } } } const char pattern[] = {1, 5, 4, 6, 2, 10, 8, 9}; #define PHASE_MASK 0x7 void run_motor_phase(char direction, char speed) { static char phase; if (direction) phase = (++phase) & PHASE_MASK; else phase = (--phase) & PHASE_MASK; P2 = ~pattern[phase]; _wait_ms(speed); } ********************************************************************************** (4) Extra: Stop and start the running motor ********************************************************************************** #include #include #include #include // inclue this file to use IRQ_VECTOR macro unsigned char bit LED_state; unsigned char bit LED_pin @ 0x87; // directly accessing the pin #define LED_DURATION 100 char a = 0, status = 1; near unsigned led_duration_count; // to place this variable in internal RAM void LED_Init(void); void LED_Update(void); // Function prototype // NOTE: ISR is not explictly called and does not require a prototype void Timer_2_Init(void); // install the interrupt vector IRQ_VECTOR(Timer_2_Tick,TIMER2) /* --------------------------------------------------------------- */ void main(void) { LED_Init(); Timer_2_Init(); // Set up Timer 2 EA = 1; // Globally enable interrupts while(1); // An empty Super Loop } /* --------------------------------------------------------------- */ void Timer_2_Init(void) { T2CON = 0x04; // Load Timer 2 control register TH2 = 0xEE; // Load Timer 2 high byte RCAP2H = 0xEE; // Load Timer 2 reload capt. reg. high byte TL2 = 0x00; // Load Timer 2 low byte RCAP2L = 0x00; // Load Timer 2 reload capt. reg. low byte // Timer 2 interrupt is enabled, and ISR will be called // whenever the timer overflows - see below. ET2 = 1; // Start Timer 2 running TR2 = 1; } /* --------------------------------------------------------------- */ // This ISR is called every timer tick (5ms) void Timer_2_Tick(void) interrupt { // must manually clear timer 2 overflow flag TF2 = 0; LED_Update(); a++; if (a==3){a=0; button(); run_motor_phase(0, 10);} //Call task butoon } /* --------------------------------------------------------------- */ void LED_Init(void) { LED_state = 0; LED_pin = 0; led_duration_count = 0; } /* --------------------------------------------------------------- */ void LED_Update(void) { led_duration_count++; if (led_duration_count == LED_DURATION){ led_duration_count = 0; if (LED_state==1){ LED_state = 0; LED_pin = 0; } else{ LED_state = 1; LED_pin = 1; } } } const char pattern[] = {1, 5, 4, 6, 2, 10, 8, 9}; #define PHASE_MASK 0x7 void button(void){ if(P3==0x7F){status=0;} //Stop the motor else if (P3==0xBF){status=1;} //Start the motor } void run_motor_phase(char direction, char speed) { static char phase; if (status==1){ if (direction) phase = (++phase) & PHASE_MASK; else phase = (--phase) & PHASE_MASK; P2 = ~pattern[phase]; _wait_ms(speed); } }