我正在为开发板上模拟的8051微控制器编写代码,我正在尝试在LCD上模拟时钟。我有2个警报和秒表功能,我想通过2个独立的开关激活,但代码似乎没有确认任何一个开关被按下,只是在第二个其他的时候运行主时钟代码。这是代码:
#include <REG51.H>
//Define which microcontroller port connects to the LCD data pins
#define LCD_DATA_PORT P2
//LCD commands
#define FunctionSet 0x3B
#define DisplayOn 0x0E
#define EntryModeSet 0x06
#define ClearScreen 0x01
#define SecondLine 0xC0
//function prototypes
void lcd_data_write(unsigned char display_data) ;
void lcd_command_write(unsigned char display_data);
void delayX1msec(unsigned int no_loops);
void init_lcd();
//Port pin definitions
sbit minute_button = P3^3;
sbit hour_button = P3^2;
sbit twentyfour_button = P3^1;
sbit alarm_button = P3^0;
sbit stopwatch_button = P1^0;
sbit alarm_led = P3^7;
//port pin functions for lcd
sbit EN = P3^5;
sbit RS = P3^4;
sbit RW = P3^6;
//function prototype
void delayX1msec(unsigned int no_loops);
void alarmfunction();
void stopwatchfunction();
//time variables
unsigned char seconds=0, minutes=0, hours=0, hours1=0;
unsigned char alarm_seconds=0, alarm_minutes=0, alarm_hours=0;
unsigned char stopwatch_miliseconds=0, stopwatch_seconds=0, stopwatch_minutes=0;
void main()
{
IT0=1;
IT1=1;
IE=0x85;
init_lcd(); //initialise the LCD
while(1)
{
delayX1msec(950); //1 second delay
if(stopwatch_button==1 && alarm_button==0)
{
stopwatchfunction();
}
else if(alarm_button == 1 && stopwatch_button==0)
{
alarmfunction();
}
else if(alarm_button == 0 && stopwatch_button == 0)
{
if(seconds<59)
{
seconds++; //increment seconds
}
else
{
seconds = 0;
if(minutes<59) //increment minutes every 60 seconds
{
minutes++;
}
else
{
if(hours<23) //increment hour every 60 minutes
{
hours++;
}
else
{
hours = 0;
}
}
}
lcd_command_write(ClearScreen); //clear the screen before every input
if(twentyfour_button == 1)
{
lcd_data_write('2');
lcd_data_write('4');
lcd_data_write('h');
lcd_data_write(' ');
lcd_data_write(hours/10 + 0x30); //hours tens
lcd_data_write(hours%10 + 0x30); //hours units
lcd_data_write(':');
lcd_data_write(minutes/10 + 0x30); //minutes tens
lcd_data_write(minutes%10 + 0x30); //minutes units
lcd_data_write(':');
lcd_data_write(seconds/10 + 0x30); //minutes tens
lcd_data_write(seconds%10 + 0x30); //minutes units
}
else
{
lcd_data_write('1');
lcd_data_write('2');
lcd_data_write('h');
lcd_data_write(' ');
if(hours > 11)
{
hours1 = hours - 12;
lcd_data_write(hours1/10 + 0x30); //hours tens
lcd_data_write(hours1%10 + 0x30); //hours units
}
else
{
lcd_data_write(hours/10 + 0x30); //hours tens
lcd_data_write(hours%10 + 0x30); //hours units
}
lcd_data_write(':');
lcd_data_write(minutes/10 + 0x30); //minutes tens
lcd_data_write(minutes%10 + 0x30); //minutes units
lcd_data_write(':');
lcd_data_write(seconds/10 + 0x30); //minutes tens
lcd_data_write(seconds%10 + 0x30); //minutes units
if(hours > 11)
{
lcd_data_write(' ');
lcd_data_write('P');
lcd_data_write('M');
}
else
{
lcd_data_write(' ');
lcd_data_write('A');
lcd_data_write('M');
}
}
if(alarm_hours == hours && alarm_minutes == minutes)
{
alarm_led = ~alarm_led;
}
}
}
}
void stopwatchfunction()
{
delayX1msec(1); //1 second delay
if(stopwatch_miliseconds<59)
{
stopwatch_miliseconds++; //increment seconds
}
else
{
stopwatch_miliseconds = 0;
if(stopwatch_seconds<59) //increment minutes every 60 seconds
{
stopwatch_seconds++;
}
else
{
stopwatch_seconds = 0;
if(stopwatch_minutes<11) //increment hour every 60 minutes
{
stopwatch_minutes++;
}
else
{
stopwatch_minutes = 0;
}
}
}
lcd_command_write(ClearScreen); //clear the screen before every input
lcd_data_write(stopwatch_minutes/10 + 0x30); //hours tens
lcd_data_write(stopwatch_minutes%10 + 0x30); //hours units
lcd_data_write(':');
lcd_data_write(stopwatch_seconds/10 + 0x30); //minutes tens
lcd_data_write(stopwatch_seconds%10 + 0x30); //minutes units
lcd_data_write(':');
lcd_data_write(stopwatch_miliseconds/10 + 0x30); //minutes tens
lcd_data_write(stopwatch_miliseconds%10 + 0x30); //minutes units
}
void alarmfunction()
{
lcd_command_write(ClearScreen); //clear the screen before every input
lcd_data_write(alarm_hours/10 + 0x30); //hours tens
lcd_data_write(alarm_hours%10 + 0x30); //hours units
lcd_data_write(':');
lcd_data_write(alarm_minutes/10 + 0x30); //minutes tens
lcd_data_write(alarm_minutes%10 + 0x30); //minutes units
}
void minute_isr() interrupt 2
{
//if the minute button is pressed increment minutes
if(alarm_button == 0)
{
if(minutes<59)
minutes++;
else
{
minutes = 0;
hours++;
}
}
else
{
if(alarm_minutes<59)
alarm_minutes++;
else
{
alarm_minutes = 0;
alarm_hours++;
}
}
}
void hour_isr() interrupt 0
{
//if the hour button is pressed increment hours
if(alarm_button == 0)
{
if(hours<23)
hours++;
else
hours = 0;
}
else
{
if(alarm_hours<23)
alarm_hours++;
else
alarm_hours = 0;
}
}
//**************************************************************************
//function to generate a delay of x * 1msec
//e.g. delay(50); generates a 50msec delay
//Assumes a 12MHz crystal oscillator
void delayX1msec(unsigned int no_loops)
{
unsigned int x;
TR0 = 0;
TF0 = 0;
TMOD = 0x01;
TH0 = 0xFC;
TL0 = 0x18;
TR0 = 1;
for(x=0;x<no_loops;x++)
{
while(!TF0);
TF0 = 0;
TH0 = 0xFC;
TL0 = 0x18;
}
TR0 = 0;
}
//function to initialise the LCD display
void init_lcd()
{
lcd_command_write(FunctionSet);
lcd_command_write(DisplayOn);
lcd_command_write(EntryModeSet);
lcd_command_write(ClearScreen);
}
//**************************************************************************
// Function to write a display character to the LCD
void lcd_data_write(unsigned char display_data)
{
RW = 0;
RS = 1;
EN = 1;
LCD_DATA_PORT = display_data;
EN = 0;
delayX1msec(5); //delay to allow write operation to complete
}
//**************************************************************************
// Function to write a command to the LCD
void lcd_command_write(unsigned char command)
{
RW = 0;
RS = 0;
EN = 1;
LCD_DATA_PORT = command;
EN = 0;
delayX1msec(5); //delay to allow write operation to complete
}