|
大概代碼,但是總報錯for等語句,哪里有問題
我想做一個,按下接p1.0的K1開關(guān),模擬車輛進(jìn)入,步進(jìn)電機(jī)轉(zhuǎn)動模擬抬桿,同時下面由74hc5 95控制的紅燈亮起時,lm016l 切換到第二頁,同時開始計時收費(fèi),每小時3塊錢,前面計時,后面收費(fèi),兩個車位一頁,16個車位共八頁,第10頁記錄所有收款金額數(shù)?偣10頁(包含首頁車輛進(jìn)出情況)。當(dāng)我按下接p1.1開關(guān)K2時,步進(jìn)電機(jī)轉(zhuǎn)動模擬抬桿,同時紅燈熄滅,停止計時收費(fèi),5秒后自動清零清零,但是所收費(fèi)用歸入第十頁的總收入里。接p1.3的K3是人工控制抬桿進(jìn),接p1.4d的K4是人工控制抬杠出。接p2.5的開關(guān)k5控制翻頁。
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
#include <intrins.h>
/************************** 硬件引腳定義 **************************/
// 按鍵
sbit K1 = P1^0; // 車輛進(jìn)入
sbit K2 = P1^1; // 車輛離開
sbit K3 = P1^2; // 人工抬桿進(jìn)
sbit K4 = P1^3; // 人工抬桿出
sbit K5 = P2^5; // 翻頁控制
sbit beep = P1^7;// 蜂鳴器
// 74HC595控制
sbit SH = P3^5;
sbit ST = P3^6;
sbit DS = P3^7;
// 步進(jìn)電機(jī)控制(ULN2003A)
#define MOTOR_PORT P2
uchar code MOTOR_CW[] = {0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; // 正轉(zhuǎn)序列
uchar code MOTOR_CCW[] = {0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; // 反轉(zhuǎn)序列
/************************** 全局變量定義 **************************/
uchar enter_count = 0; // 進(jìn)入車輛數(shù)
uchar exit_count = 0; // 離開車輛數(shù)
uchar current_cars = 0; // 當(dāng)前車輛數(shù)
uint current_page = 1; // 當(dāng)前顯示頁(1-10)
uint total_income = 0; // 總收入(第10頁顯示)
uint timer[16] = {0}; // 每個車位計時(秒)
uchar display_cars[2]; // 當(dāng)前頁顯示的2個車位狀態(tài)
/************************** LCD1602驅(qū)動 **************************/
#ifndef LCD1602_H
#define LCD1602_H
sbit LCD_RS = P1^4;
sbit LCD_RW = P1^5;
sbit LCD_E = P1^6;
// 微延時
void lcd_delay(uint ms) {
uint i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 120; j++);
}
// 寫命令
void lcd_write_cmd(uchar cmd) {
LCD_E = 0;
LCD_RS = 0;
LCD_RW = 0;
P0 = cmd;
lcd_delay(3);
LCD_E = 1;
lcd_delay(25);
LCD_E = 0;
}
// 寫數(shù)據(jù)
void lcd_write_data(uchar dat) {
LCD_E = 0;
LCD_RS = 1;
LCD_RW = 0;
P0 = dat;
lcd_delay(3);
LCD_E = 1;
lcd_delay(25);
LCD_E = 0;
}
// 初始化LCD
void lcd_init() {
lcd_write_cmd(0x38); // 8位接口,2行顯示
lcd_write_cmd(0x0c); // 顯示開,光標(biāo)關(guān)
lcd_write_cmd(0x06); // 地址自增
lcd_write_cmd(0x01); // 清屏
}
// 顯示字符串(指定位置)
void lcd_show_string(uchar line, uchar col, uchar *str) {
if(line == 1) lcd_write_cmd(0x80 + col);
else lcd_write_cmd(0x80 + 0x40 + col);
while(*str != '\0') {
lcd_write_data(*str);
str++;
}
}
// 顯示2位數(shù)字(自動補(bǔ)位)
void lcd_show_num2(uchar line, uchar col, uint num) {
if(line == 1) lcd_write_cmd(0x80 + col);
else lcd_write_cmd(0x80 + 0x40 + col);
if(num >= 100) {
lcd_write_data(0x30 + num/100%10);
lcd_write_data(0x30 + num/10%10);
} else {
if(num < 10) lcd_write_data(' ');
lcd_write_data(0x30 + num/10%10);
}
lcd_write_data(0x30 + num%10);
}
#endif
/************************** 通用延時函數(shù) **************************/
void delay_ms(uint ms) {
uint i, j;
for(i = 0; i < ms; i++)
for(j = 0; j < 120; j++);
}
/************************** 步進(jìn)電機(jī)控制 **************************/
// 正轉(zhuǎn)抬桿
void motor_raise() {
uint i, j;
for(i = 0; i < 5; i++) { // 轉(zhuǎn)動5圈
for(j = 0; j < 8; j++) { // 8拍控制
MOTOR_PORT = MOTOR_CW[j];
delay_ms(20); // 控制速度
}
}
MOTOR_PORT = 0x00; // 停止
}
// 反轉(zhuǎn)落桿
void motor_lower() {
uint i, j;
for(i = 0; i < 5; i++) { // 轉(zhuǎn)動5圈
for(j = 0; j < 8; j++) { // 8拍控制
MOTOR_PORT = MOTOR_CCW[j];
delay_ms(20); // 控制速度
}
}
MOTOR_PORT = 0x00; // 停止
}
/************************** 74HC595控制 **************************/
void hc595_write(uint data) {
uchar i;
ST = 0;
for(i = 0; i < 16; i++) {
SH = 0;
DS = (data & 0x0001) ? 1 : 0;
SH = 1;
data >>= 1;
}
ST = 1;
}
uint get_led_state(uchar car_count) {
uchar i;
uint state = 0;
for(i = 0; i < car_count; i++) {
state |= 1 << i;
}
return state;
}
/************************** 計時收費(fèi)邏輯 **************************/
void update_timer() {
uchar i;
for(i = 0; i < 16; i++) {
if( (get_led_state(current_cars) & (1 << i)) ) {
timer[ i]++; // 僅對占用車位計時
}
}
}
uint calculate_fee(uint seconds) {
return (seconds / 3600 + (seconds % 3600 ? 1 : 0)) * 3;
}
/************************** 頁面顯示邏輯 **************************/
void show_page1() {
lcd_init();
lcd_show_string(1, 0, "Enter: Exit:");
lcd_show_string(2, 0, "Cars: Empty:");
lcd_show_num2(1, 6, enter_count);
lcd_show_num2(1, 14, exit_count);
lcd_show_num2(2, 5, current_cars);
lcd_show_num2(2, 13, 16 - current_cars);
}
void show_car_page(uchar page_idx) {
uchar start = (page_idx - 2) * 2; // 計算起始車位
lcd_init();
lcd_show_string(1, 0, "Car ");
lcd_show_num2(1, 4, start + 1);
lcd_show_string(1, 6, "-");
lcd_show_num2(1, 7, start + 2);
lcd_show_string(1, 9, "Fee:");
// 獲取車位狀態(tài)
display_cars[0] = (get_led_state(current_cars) & (1 << start)) ? 1 : 0;
display_cars[1] = (get_led_state(current_cars) & (1 << (start + 1))) ? 1 : 0;
// 顯示車位1狀態(tài)
lcd_show_string(2, 0, "State:");
lcd_show_string(2, 6, display_cars[0] ? "Occupy" : "Empty");
// 顯示費(fèi)用1
if(display_cars[0]) {
lcd_show_num2(2, 12, calculate_fee(timer[start]));
} else {
lcd_show_string(2, 12, " 0");
}
delay_ms(50);
|
|