-
#include
#include
#include
#define vk_esc 0x11b /* escape key */
define timer 0x1c the interrupt number of the clock interrupt
int timercounter=0;* Timekeeping variable, increases by 18 per second. */
The interrupt handler pointer (handle) to the entry of the original clock interrupt handler
void interrupt ( oldhandler)()
New Clock Interrupt Handler *
void interrupt newhandler( )
increase the global counter */
timercounter++;
call the old routine */
oldhandler();
Set up a new clock interrupt handler*
void settimer(void interrupt (*intproc)()
oldhandler=getvect(timer);
disable();When a new clock interrupt handler is set, all interrupts are disabled
setvect(timer,intproc);
enable();Turn on interrupts
Revert to the original clock interrupt handling*
void killtimer()
disable();
setvect(timer,oldhandler);
enable();
void main(void)
int key,time=0;
settimer(newhandler);Modify Clock Interrupt
for (;
if (bioskey(1))
key=bioskey(0);
if (key==vk esc) * press the escape key to exit the program early*
printf("user cancel!");
break;
if (timercounter>18) * once every 1 second *
Restore timing variable *
timercounter=0;
time++;
printf("%d",time);
if (time==10) * end the program in 10 seconds
printf("program terminated normally!");
break;
killtimer();Resume Clock Interrupt
-
Here's an idea.
Let's start with a keyboard scanner. Whether the acquisition is pressed by the A button or the B button. If the A key is pressed, the output level will be high to the LED1 port and then the delay will be 1s, and the delay program can be roughly delayed with delay, or it can be accurately delayed with a timer; After the delay of 1s, let the LED port output low, and then delay for 1s.
Loop this program 5 times with a while or for statement, so that you can get the action of starting led1 (flashing once in 1s for 10s);
Then start another timer after this statement, which is used to time 20s. You can schedule this timer for 50ms, and then add 400 times to the cyclic variable, which is the timing of 20s.
During this period, if the A key is pressed again, it is judged to see if the timer is cycled more than 400 times, if it is exceeded, it means that the A key has been pressed for more than 20S from the previous state, in this way, it is repeated to start the action of LED1 (1S flashes once, lasts 10S), if the cycle variable does not exceed 400 times, it means that the A button has been pressed again within 20S, in this way, it is executed LED2 (flashes once in seconds, lasts 20S) This action.
The program of LED2 (flashes once in a second for 20s) is that the IO port of LED2 outputs a high level, and then delays the seconds, and then the LED2 port outputs a low level, delaying the seconds. After that, use the while or for statement to loop this program 10 times, and you can complete the action of led2 (blinking once in seconds for 20s).
Of course, in the keyboard scanning program, if you press the B key, you will directly let the LED2 port output high. In this way, the LED2 will stay on.
-
It's so weird.,I actually use a serial port as an io port.。
*Find 2+4+6+8+....+100 and *
#include "" >>>More
bool f=0;
for(int i=1000;i>=1;i--) Start with the largest number of 1000 and try to the smaller number. >>>More
Summary. Here's a simple clock written in C** that prints out the current time (in hours, minutes, and seconds): include include int main() return 0; This ** uses the time function and the localtime function in the header file, as well as the printf function and the fflush function in the header file. >>>More
The main thing is that you put this line using namespace std; Put it in main(), and then call cout to output it, so you should put it in front, or add it directly to the header file. h can be used, or it can be placed in front of the function in which iostream is called. Such as: >>>More
You can add a 0 to the end of the array, but you can't have a 0 in the middle. Then change it to something like this: >>>More