我们有一个项目,我们完成了60%的程序,但我们面临着2个问题,我们的第一个问题是使引擎应该冷却5秒,然后它应该加热10秒,然后它应该停止,就像它不再工作,除非我再次打开开关
这是我们的项目:
开关0控制小车的操作(0无操作) (1小车正在操作)我们完成了这一部分
安全带开关1:我们完成了这一部分。
开关2用于门:我们完成了这一部分
在这一部分,我们做了它,但问题它应该只做一次!但是因为我们有一个永远的while循环,所以它不会停止!那么我们应该怎么做呢?!
当汽车运行时,发动机将需要15秒来加热:开始时--> 'HH‘将在LCD上显示,加热器led在应用板上亮起,同时电机将向前运行5秒以冷却它。因此“HN”将显示在LCD上,之后发动机将需要10秒才能加热。
这是我们的第二个问题,我们做不到!我们考虑在一个循环中创建一个循环,在另一个循环中创建一个循环,但这是行不通的,我们还尝试通过计时器来做这件事,并在其中添加另一个计时器!!我们应该通过定时器或中断来完成,而不能使用延迟
我们将有4个leds来表示燃油水平。每10秒就有一个led灯熄灭。当最后一个led仍然存在时,将出现警告:(1)‘FL’将显示在led的第2行上。如果开关3变为接通状态,则燃料将满,否则汽车将关闭。
这是我们的代码!!
sbit LCD_RS at RA1_bit;
sbit LCD_RW at RA2_bit;
sbit LCD_EN at RA3_bit;
sbit LCD_D4 at RD4_bit;
sbit LCD_D5 at RD5_bit;
sbit LCD_D6 at RD6_bit;
sbit LCD_D7 at RD7_bit;
sbit LCD_RS_Direction at TRISA1_bit;
sbit LCD_RW_Direction at TRISA2_bit;
sbit LCD_EN_Direction at TRISA3_bit;
sbit LCD_D4_Direction at TRISD4_bit;
sbit LCD_D5_Direction at TRISD5_bit;
sbit LCD_D6_Direction at TRISD6_bit;
sbit LCD_D7_Direction at TRISD7_bit;
int i;
sbit LED0 at RC0_bit;
sbit LED1 at RC1_bit;
sbit LED2 at RC2_bit;
sbit LED4 at RC4_bit;
sbit LED5 at RC5_bit;
sbit LED6 at RC6_bit;
sbit LED7 at RC7_bit;
sbit Switch0 at RB0_bit;
sbit Switch1 at RB1_bit;
sbit Switch2 at RB2_bit;
sbit Switch3 at RB3_bit;
int Num;
void Move_Delay() { // Function used for text moving
Delay_ms(1000); // You can change the moving speed here
}
void main() {
ADCON1 = 0X06; //a port as ordinary i/o.
TRISA=0X00; //a port as output.
TRISD=0X00; //d port as output.
TRISC=0X00;
TRISB=0X0F;
PORTC = 0b00000000;
OPTION_REG = 0xD2;
Num = 0; //clear the number of overflows
OPTION_REG = 0x82; //Timer, Internal cycle clock (Fosc/4)
//Prescaler is assigned to the TMR0 timer/counter
//Prescaler (1:128) is assigned to the timer TMR0
TMR0 = 56; //Timer T0 counts from 39 to 255
INTCON.T0IF=0;
Lcd_Init(); // Initialize LCD
Delay_ms(200);
Lcd_Cmd(_LCD_CLEAR); // Clear display
Lcd_Cmd(_LCD_CURSOR_OFF);
LED0 = 0;
LED1= 0;
do {
if (Switch0)
{
Delay_ms(200); // pause 20 mS
if(INTCON.T0IF) //check for TMR0 register overflow
{
Num ++; // overflow causes Num to be incremented by 1
TMR0 = 56; // TMR0 returns to its initial value
INTCON.T0IF = 0 ; // Bit T0IF is cleared
Lcd_Cmd(_LCD_CLEAR);
Lcd_Out(1,2,"cooling");
}
if(Num ==108)
{
Lcd_Cmd(_LCD_CLEAR);
LED0=~LED0;
Lcd_Out(1,2,"heater ");
Delay_ms(1000);
}
}
else
Lcd_Cmd(_LCD_CLEAR);
if (switch1)
{
Delay_ms(20); // pause 20 mS
Lcd_Out(2,1,"BO");
LED1=0;
}
else
{
if(INTCON.T0IF) //check for TMR0 register overflow
{
Num ++; // overflow causes Num to be incremented by 1
TMR0 = 39; // TMR0 returns to its initial value
INTCON.T0IF = 0 ; // Bit T0IF is cleared
/*Lcd_Cmd(_LCD_CLEAR);*/
Lcd_Out(2,1,"BF ");
LED1=~LED1;
}
if(Num == 108)
{ //after 108 overflows
Num = 0;
}
}
if (switch2)
{
Lcd_Out(2,5,"DO");
LED2=0;
}
else
{
if(INTCON.T0IF) //check for TMR0 register overflow
{
Num ++; // overflow causes Num to be incremented by 1
TMR0 = 39; // TMR0 returns to its initial value
INTCON.T0IF = 0 ; // Bit T0IF is cleared
/*Lcd_Cmd(_LCD_CLEAR);*/
Lcd_Out(2,5,"DF");
LED2=~LED2;
}
if(Num == 108)
{ //after 108 overflows
Num = 0;
}
}//这是错误的
if(switch3)
{
Delay_ms(500); // Clear display
//Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1," FFFFFFFFFF"); // Write text in first row
Delay_ms(500);
for(i=0; i<15; i++) { // Move text to the right 7 times
Lcd_Cmd(_LCD_SHIFT_RIGHT);
Move_Delay();
if(i==14)
{
Lcd_Cmd(_LCD_CLEAR); // Cursor off
Lcd_Out(1,1," warning !! ");
Delay_ms(1000);
}
}
}
} while(1);
}发布于 2014-12-14 02:55:11
回答不够充分,抱歉,但评论的话太多了。
你没有清楚地说明你的两个问题。没有针对第一个问题的代码,这似乎是关于发动机加热的问题。对于第二个问题,你只是说“它不工作”,而没有说是什么。
我也想在你的作品中抛出一个扳手,并询问在你的操作阶段,如果门被打开,或者安全带被释放,会发生什么。有时维护一个单独的状态变量是一个好主意,它为每个值都有一个位字段,比如“门关闭”、“安全带系紧”、“通电”等。
我确实注意到你使用过delay(),尽管你说你不能。在现实世界中,进程控制器能够成功使用delay()函数的唯一方法是,如果有其他线程或中断例程来处理I/O和调度事件。任何嵌入式控制器的一个基本特性是在中断下服务的计时器节拍,允许您在不阻塞其他进程的情况下进行延迟(相同的处理程序还可以轮询和解除键盘和按钮输入)。假设您的常规计时器中断会递增一个名为unsigned ticks的变量。举个例子(与你的任务有些模糊的关联):
unsigned mark, elapsed;
int heating = 0;
while (1) { // main operational loop
if (buttonpress) { // pseudo code
heating = 1; // flag stage one of heater
mark = ticks; // start a delay
}
... // service the fuel usage
... // check the door
... // check the seat belt
if (heating) {
elapsed = ticks - mark; // don't directly compare...
if (elapsed >= 1000) { // ...because of counter wrap
... // heater jobs
heating = 0; // clear flag
}
}
} // repeat main loop这可以扩展到加热器过程的几个阶段。
https://stackoverflow.com/questions/27461932
复制相似问题