#include <pic.h>
__CONFIG(XT & PWRTEN & LVPDIS & WDTDIS & BOREN);//D???????×?
#define uchar unsigned char
#define uint unsigned int
#define ulint unsigned long int
#define LCD_DATA_PORT PORTD //?¨ò?LCDμ?D0-D7?ù?ú???ú
#define LCD_DATA_POUT TRISD=0x00 //?¨ò?LCDêy?Y???ú?aê?3?
#define LCD_DATA_PIN TRISD=0xFF //?¨ò?LCDêy?Y???ú?aê?è?(?ú?áLCD×′ì?ê±ó?μ?)
#define LCD_Control_OUT TRISA&=0x00;
#define LCD_EN_1 RA3=1
#define LCD_EN_0 RA3=0
#define LCD_RW_1 RA2=1
#define LCD_RW_0 RA2=0
#define LCD_RS_1 RA1=1
#define LCD_RS_0 RA1=0
uchar s=0;
uchar m=0;
uchar h=0;
uchar count=0;
uchar String_1[]={"BEIJING TIME:"};
uchar String_2[]={"0123456789"};
void count_func(void);
void display_second(uchar s1);
void display_min(uchar s2);
void display_hour(uchar s3);
void timer1_init(void);
void nus_delay(uint nus);
void nms_delay(uint nms);
void mcu_io_init(void);
void lcd_init(void);
void LCD_en_write(void);
void wait_LCD_Ready(void);
void LCD_write_Order(uchar COMM);
void set_LCD_xy(uchar x, uchar y );
void LCD_write_Data(uchar DAT);
void LCD_write_string(uchar *string);
void timer_1(void);
void main(void)
{
mcu_io_init();
lcd_init();
set_LCD_xy(0,0);
LCD_write_string(String_1);
timer_1();
T0IE=1 ;//Tiemr0中断允许
GIE=1 ;//总中断允许
while(1)
{
display_second(s);
}
}
void timer_1(void)
{
PSA=0 ;//Timer0 使用预分频器
PS0=0 ;//Timer0选择分频率为1:128
PS1=1 ;
PS2=1 ;
T0CS=0 ;//内部时钟(即系统时钟)
TMR0=0xD5 ;
}
void interrupt timer_ie(void)
{
static unsigned int count_1=0;
if(T0IF)//Timer0中断服务子程序
{
T0IF=0 ;//清定时器中断标志
TMR0=0xD5 ;//重设定时器初值
if(count_1++>50)
{
count_1=0;
s++;
if(s>59)
s=0;
}
}
}
void display_second(uchar s1)
{
uchar i,j,temp;
i=s1/10;
j=s1%10;
set_LCD_xy(1,14);
LCD_write_Data(String_2);
nus_delay(5);
set_LCD_xy(1,15);
LCD_write_Data(String_2[j]);
nus_delay(5);
}
void mcu_io_init(void)
{
ADCON1=0x07;
PSPMODE=0;
LCD_Control_OUT;
LCD_EN_1;
LCD_DATA_PORT=0xff;
LCD_DATA_POUT; //LCDêy?Y?úéè?aê?3?
LCD_EN_1;
LCD_RW_1;
LCD_RS_1;
LCD_Control_OUT;
}
void lcd_init(void)
{
nms_delay(15);
LCD_write_Order(0x01);
nms_delay(5);
LCD_write_Order(0x38);
nms_delay(5);
LCD_write_Order(0x0e);
nms_delay(5);
LCD_write_Order(0x06);
nms_delay(5);
}
void LCD_en_write(void)
{
LCD_EN_1;
asm("NOP");
asm("NOP");
asm("NOP");
asm("NOP");
LCD_EN_0;
}
void wait_LCD_Ready(void)
{
uchar temp1;
LCD_DATA_PORT=0XFF;
LCD_DATA_PIN; //D7éè?aê?è?£?×?±??D??LCD?|±ê??
//1?a?| 0?a???D
LCD_RS_0;
LCD_RW_1; //?á
LCD_EN_1;
nus_delay(2);
while((LCD_DATA_PORT & 0X80));
LCD_EN_0;
LCD_RS_1;
LCD_DATA_POUT;
}
void LCD_write_Order(uchar COMM)
{
wait_LCD_Ready(); //μè′yLCD???D
LCD_RW_0; //D′
LCD_RS_0;
LCD_DATA_PORT=COMM;
LCD_en_write();
nus_delay(2);
LCD_RW_1;
LCD_RS_1;
}
void set_LCD_xy(uchar x, uchar y )
{
uchar DIS_address;
if (x == 0)
DIS_address = 0x80 + y; //μúò?DDXáD
else
DIS_address = 0xc0 + y;
LCD_write_Order( DIS_address); //μú?tDDXáD
}
void LCD_write_Data(uchar DAT)
{
wait_LCD_Ready();
LCD_RW_0; //D′
LCD_RS_1;
LCD_DATA_PORT=DAT;
LCD_en_write();
nus_delay(2);
LCD_RW_1;
LCD_RS_1;
}
void LCD_write_string(uchar *string)
{
while(*string)
{
LCD_write_Data(*string);
string++;
}
}
void nus_delay(uint nus)
{
int j=10;
while(nus--)
{
while(j--);
j=10;
}
}
void nms_delay(uint nms)
{
while(nms--)
nus_delay(1000);
}
一周热门 更多>