Advertisement
bsddeamon

overflow2.c

Jan 18th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.00 KB | None | 0 0
  1. // overflow2.c
  2.  
  3. #include <DS89C4xx.h>
  4. #include <stdio.h> // puts(), getchar().
  5. // http://www.keil.com/support/man/docs/c51/c51_puts.htm
  6.  
  7. #include <stdlib.h> // atoi().
  8. // http://www.keil.com/support/man/docs/c51/c51_atoi.htm
  9.  
  10. #define UC unsigned char
  11.  
  12. UC motor_strength = 0; // Pourcentage.
  13. UC input_buffer[16]; // Trop petit.
  14.  
  15. // Simule une interruption systeme par
  16. // un element exterieur au programme.
  17. void eval_input(void) reentrant;
  18.  
  19. // Convertis une chaine de caracteres
  20. // en nombre (unsigned char).
  21. UC atouc(char * string);
  22.  
  23. // N'empeche pas d'ecrire sur les
  24. // variables apres la fin du tampon.
  25. void get_input(void);
  26.  
  27. void verify_centrifuge(void); // Test.
  28.  
  29. // Peut-on forcer le moteur a
  30. // tourner a plus de 80%?
  31. void set_motor_strength(UC * input);
  32.  
  33. void main(void) {
  34.   SCON0 = 0x50;
  35.   TMOD |= 0x20;
  36.   TH1 = 0xFF;
  37.   PCON = 0x80;
  38.   TR1 = 1;
  39.   TI_0 = 1;
  40.  
  41.   puts("System power up…");
  42.   puts("Welcome to our SCADA nuclear power plant facility.");
  43.   puts("WARNING! Setting motor strength above 80% may result in permanent damage or even injury or death!");
  44.  
  45.   while (1) {
  46.     eval_input();
  47.   }
  48. }
  49.  
  50. void eval_input(void) reentrant {
  51.   puts("Please set motor strength in percentage :");
  52.   get_input();
  53.  
  54.   // On s'assure qu'on ne depasse pas 80%.
  55.   if (atouc(input_buffer) <= 80) {
  56.     set_motor_strength(input_buffer);
  57.   } else {
  58.     puts("Error: Cannot set motor strength above 80%!");
  59.   }
  60. }
  61.  
  62. // Voir documentation. Lien ci-haut.
  63. UC atouc(char * string) {
  64.   return (UC)atoi(string);
  65. }
  66.  
  67. // Prend une ligne du clavier.
  68. void get_input(void) {
  69.   char i = 0;
  70.   do { input_buffer[i] = getchar();
  71.   } while (input_buffer[i++] != '\n');
  72.  
  73.   // Transforme le buffer en string.
  74.   input_buffer[i - 1] = '\0';
  75. }
  76.  
  77. void verify_centrifuge(void) {
  78.   if (motor_strength <= 80) {
  79.     puts("Motors doing good!");
  80.   } else {
  81.     while (1) {
  82.       puts("BOOOOOOOM!!!");
  83.     }
  84.   }
  85. }
  86.  
  87. void set_motor_strength(UC * input) {
  88.   motor_strength = atouc(input);
  89.   verify_centrifuge();
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement