用c语言写的一个时闹钟程序
单片机 用16F877,主时钟用20MHz,用32768作定时时间。可以实现2路定闹,每一路都可分别设置和开关,采用4x4键盘,16x2的字符型LCD显示。连线在程序开头有说明。
程序的功能:
(1)上电后LCD背光打开,并显示倒计时5秒,然后时钟开始工作。
(2)用模式键(*)切换模式,如显示时间、日期、闹钟1、闹钟2等,并且可以用上、下键控制加1、减1或是闹钟的On、Off。
(3)原程序有16个键,包括0~9数字键,可以直接输入要设置的时间值,但后来将数字键取消了,你仍然可以通过修改程序的部分注释恢复此功能。
(4)闹钟有2路,时间到后闹2分钟,可按任意键取消本次闹钟。闹钟响时有2种音调,是用PIC的PWM实现的。
(5)按任意键可打开背光,1分钟后自动关闭背光。
(6)RA0~RA3为按键扫描输入,应接下拉电阻。
主程序
// FileName: Main.c
// MCU: MicroChip PIC16F877
// Tool: CCS-C compiler
// Author: KingEDA, MSN:kingeda@163.com, skype:kingeda, E-mail:kingeda@163.com
// Website: http://www.EDAKING.com
// Description:
// A timer program
// Ver 0.1: 2003-03-31, all cLOCk function with date display, 2 way alarm.
// Ver 0.2: 2003-05-05, (1) Alarm default is on,modify alarm1 time to 7:00:00,
// and alarm2 to 13:30:00.
// (2) Backlight will be enabLED when alarming.
// (3) Automatic adjust day(28,30,31).
// (4) Automatic MOV e cursor to next location when set item.
// PIN Connection:
// RC0~1 : 32768Hz Crystal
// RC2 : Buzzer
// RC3 : LCD Back Light,drive a PNP BJT
// RD0~RD7 : to LCD DB0~DB7
// RA0~RA3 : keypad col in
// RC4~RC7 : keypad line out
// 7 8 9 #
// 4 5 6 ↑
// 1 2 3 ↓
// 0 ← → *
// RE0 : LCD RS
// RE1 : LCD RW
// RE2 : LCD E
#include "my16f877.h"
#device ICD=true
//#fuses HS,NOWDT,NOPROTECT,PUT,BROWNOUT
#use delay(clock = 24000000)
//#use fast_io(C)
#use fast_io(E)
#define lcd_busy (lcd_read_addr()&0x80) == 0x80
#define time_start_addr 0x80+0x04
#define time_hourh_addr time_start_addr
#define time_hourl_addr time_start_addr+1
#define time_minuteh_addr time_start_addr+3
#define time_minutel_addr time_start_addr+4
#define time_secondh_addr time_start_addr+6
#define time_secondl_addr time_start_addr+7
#define key_0 0x11
#define key_1 0x21
#define key_2 0x22
#define key_3 0x24
#define key_4 0x41
#define key_5 0x42
#define key_6 0x44
#define key_7 0x81
#define key_8 0x82
#define key_9 0x84
#define key_left 0x12
#define key_right 0x14
#define key_up 0x48
#define key_down 0x28
#define key_mode 0x18
#define key_CANcel 0x88
char StrPower1[] = " * Power on * ";
char StrSetTime[] = " * Adjust time* ";
char StrSetDate[] = " * Adjust date* ";
char StrAlarm1[] = " * Set alarm 1* ";
char StrAlarm2[] = " * Set alarm 2* ";
unsigned char PORTC_MAP;
#bit BackLightEn = PORTC_MAP.3
unsigned char BackLightTimer;
int1 led;
#bit lcd_rs = PORTE.0
#bit lcd_rw = PORTE.1
#bit lcd_e = PORTE.2
#byte lcd_bus = PORTD
#byte lcd_dir = TRISD
#define PWM_on 0x0c
#define PWM_off 0x00
#define PWM_period 200
#define PWM_DC 100
unsigned char lcd_addr;
unsigned char KeyLine;
unsigned char KeyOld;
unsigned char KeyNew;
struct mTime {
unsigned char hourh; // hour,0~23
unsigned char hourl;
[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11] [12] [13] [14] [15] [16] [17] 下一页
本文关键字:程序 C语言 PIC单片机,单片机-工控设备 - PIC单片机