1705052703860.gif (1.85 MB, 下載次數: 36)
下載附件
2024-1-12 17:56 上傳
單片機源程序如下:
- #include "main.h"
- #include "lcd1602.h"
- #include "key.h"
- #include "motor_zhiliu.h"
-
- uchar key_num = 0;
- uchar flag_display = 0;
- uint time_num = 0;
- char display_buf[16];
- bit flag_mode = 0;
- extern bit flag_direction;
- extern uchar pwm_num;
- uchar flag_speed = 0;
-
- void Delay_function(uint x);
- void Key_function(void);
- void Display_function(void);
- void Manage_function(void);
- uchar Simulation_Keyboard_Scan(void);
-
- void main()
- {
- Lcd1602_Init();
- Delay_function(50);
- lcd1602_clean();
- Delay_function(50);
- Timer0_Init();
- Delay_function(50);
-
- while(1)
- {
- Key_function();
- Display_function();
- Manage_function();
- Delay_function(10);
- time_num++;
- if(time_num == 5000)
- {
- time_num = 0;
- }
- }
- }
-
- void Delay_function(uint x)
- {
- uint m,n;
- for(m=x;m>0;m--)
- for(n=110;n>0;n--);
- }
-
- uchar Simulation_Keyboard_Scan(void)
- {
- uchar key_value = 0;
-
-
- key_value = ~P1 & 0x0F;
-
-
-
-
- return key_value;
- }
- void Key_function(void)
- {
- #ifdef SIMULATION
- key_num = Simulation_Keyboard_Scan();
- #else
- key_num = Chiclet_Keyboard_Scan(0);
- #endif
- if(key_num != 0)
- {
- switch(key_num)
- {
- case 1:
- flag_direction = ~flag_direction;
- break;
- case 2:
- if(flag_speed < 3)
- flag_speed++;
- flag_mode = 1;
- break;
- case 3:
- if(flag_speed > 0)
- flag_speed--;
- flag_mode = 1;
- break;
-
- case 4:
- flag_speed = 0;
- flag_mode = 1;
- break;
- default:
- break;
- }
- }
- }
-
- void Display_function(void)
- {
- if(flag_direction == 0)
- lcd1602_display_str(2, 0, "D:Right");
- else
- lcd1602_display_str(2, 0, "D:Lift ");
-
- switch(flag_speed)
- {
- case 0:
- lcd1602_display_str(2, 9, "S:Stop");
- break;
-
- case 1:
- lcd1602_display_str(2, 9, "S:Low ");
- break;
-
- case 2:
- lcd1602_display_str(2, 9, "S:Mid ");
- break;
-
- case 3:
- lcd1602_display_str(2, 9, "S:High");
- break;
-
- default:
- break;
- }
- }
-
- void Manage_function(void)
- {
- if(flag_mode == 0)
- {
- flag_speed = 1;
- }
- else
- {
- switch(key_num)
- {
- case 1:
- flag_direction = ~flag_direction;
- break;
- case 2:
- if(flag_speed < 3)
- flag_speed++;
- flag_mode = 1;
- break;
- case 3:
- if(flag_speed > 0)
- flag_speed--;
- flag_mode = 1;
- break;
-
- case 4:
- flag_speed = 0;
- flag_mode = 1;
- break;
- default:
- break;
- }
- }
- }
復制代碼
|