Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // overflow2.c
- #include <DS89C4xx.h>
- #include <stdio.h> // puts(), getchar().
- // http://www.keil.com/support/man/docs/c51/c51_puts.htm
- #include <stdlib.h> // atoi().
- // http://www.keil.com/support/man/docs/c51/c51_atoi.htm
- #define UC unsigned char
- UC motor_strength = 0; // Pourcentage.
- UC input_buffer[16]; // Trop petit.
- // Simule une interruption systeme par
- // un element exterieur au programme.
- void eval_input(void) reentrant;
- // Convertis une chaine de caracteres
- // en nombre (unsigned char).
- UC atouc(char * string);
- // N'empeche pas d'ecrire sur les
- // variables apres la fin du tampon.
- void get_input(void);
- void verify_centrifuge(void); // Test.
- // Peut-on forcer le moteur a
- // tourner a plus de 80%?
- void set_motor_strength(UC * input);
- void main(void) {
- SCON0 = 0x50;
- TMOD |= 0x20;
- TH1 = 0xFF;
- PCON = 0x80;
- TR1 = 1;
- TI_0 = 1;
- puts("System power up…");
- puts("Welcome to our SCADA nuclear power plant facility.");
- puts("WARNING! Setting motor strength above 80% may result in permanent damage or even injury or death!");
- while (1) {
- eval_input();
- }
- }
- void eval_input(void) reentrant {
- puts("Please set motor strength in percentage :");
- get_input();
- // On s'assure qu'on ne depasse pas 80%.
- if (atouc(input_buffer) <= 80) {
- set_motor_strength(input_buffer);
- } else {
- puts("Error: Cannot set motor strength above 80%!");
- }
- }
- // Voir documentation. Lien ci-haut.
- UC atouc(char * string) {
- return (UC)atoi(string);
- }
- // Prend une ligne du clavier.
- void get_input(void) {
- char i = 0;
- do { input_buffer[i] = getchar();
- } while (input_buffer[i++] != '\n');
- // Transforme le buffer en string.
- input_buffer[i - 1] = '\0';
- }
- void verify_centrifuge(void) {
- if (motor_strength <= 80) {
- puts("Motors doing good!");
- } else {
- while (1) {
- puts("BOOOOOOOM!!!");
- }
- }
- }
- void set_motor_strength(UC * input) {
- motor_strength = atouc(input);
- verify_centrifuge();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement