4.png (22.44 KB, 下載次數(shù): 117)
下載附件
2019-10-12 12:13 上傳
#define _XTAL_FREQ 4000000 //晶振定義
#include<pic.h> //頭文件包含
//__CONFIG(0x3b31);//芯片配置字定義
// CONFIG1
#pragma config FOSC = XT // Oscillator Selection bits (XT oscillator: Crystal/resonator on RA6/OSC2/CLKOUT and RA7/OSC1/CLKIN)
#pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = ON // RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown Out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal/External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#pragma config LVP = ON // Low Voltage Programming Enable bit (RB3/PGM pin has PGM function, low voltage programming enabled)
// CONFIG2
#pragma config BOR4V = BOR40V // Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
#pragma config WRT = OFF // Flash Program Memory Self Write Enable bits (Write protection off)
//宏定義
#define uchar unsigned char
#define uint unsigned int
#define RS RC0 //命令數(shù)據(jù)選擇
#define RW RC1 //讀寫選擇
#define EN RC2 //使能
uint count=0;
uchar miao,fen,shi;
/*******************延時函數(shù)部分******************
大約延時1ms
************************************************/
void delayms(uint ms)
{
uchar i,j;
for(j=ms;j>0;j--)
for(i=110;i>0;i--);
}
/*******************1602發(fā)送主函數(shù)******************
************************************************/
void LCD_WRITE(uchar data,uchar com)
{
RS=com;//1表示數(shù)據(jù);0表示命令
PORTD=data;//將數(shù)據(jù)寫入
delayms(5);//延時
EN=1;//使能
delayms(5);//延時
EN=0;//關閉
}
/*******************1602顯示主函數(shù)******************
//指定位置顯示字符
//輸入:列地址X(0~15),行地址y(0~1)
// 字符串指針*p,要顯示的字符個數(shù)num
************************************************/
void disp_1602(uchar x,uchar y,uchar *p,uchar num)
{
uchar i;
for(i=0;i<num;i++)
{
if(0==y)
x|=0x80;//第一行地址為0x80
else
x|=0xc0;//第二行地址為0xC0
LCD_WRITE(x,0); //寫地址命令
LCD_WRITE(*p,1);//寫數(shù)據(jù)
x++;//地址加
p++;//顯示字符地址加
}
}
/*******************初始化函數(shù)部分******************
************************************************/
void LCD_init(void)
{
// ADCON1=0x07;//定義RE為數(shù)據(jù)口
TRISC0=0;
TRISC1=0;
TRISC2=0;//RE口為輸出
RC0=0;
RC1=0;
RC2=0;//初始化輸出低電平
TRISD=0;//D口為輸出
PORTD=0;//輸出0
LCD_WRITE(0x38,0);//設置8位總線,雙行顯示
LCD_WRITE(0x01,0);//清屏
LCD_WRITE(0x0c,0);//開顯示,關光標
LCD_WRITE(0x06,0);//光標右移
LCD_WRITE(0x80,0);//第一行顯示位置
}
/*******************主程序函數(shù)******************
************************************************/
void main(void)
{
LCD_init();//端口初始化
T1CKPS0=1;
T1CKPS1=1; //前置分頻器8分頻
TMR1CS=0; //TMR1工作于定時器方式
TMR1L=(65536-12500)%256; //定時12500個時鐘
TMR1H=(65536-12500)/256;
GIE=1; //總中斷允許
PEIE=1; //外圍功能模塊中斷允許
TMR1IE=1; //TMR1中斷允許
TMR1ON=1; //啟動TMR1
while(1)
{
disp_1602(0,0,"Time:",5);//第一行顯示temp:
delayms(20);
LCD_WRITE(0x80+5,0); //寫地址命令
LCD_WRITE(shi/10+0x30,1);//寫數(shù)據(jù)
LCD_WRITE(shi%10+0x30,1);//寫數(shù)據(jù)
LCD_WRITE(':',1);//寫數(shù)據(jù)
LCD_WRITE(fen/10+0x30,1);//寫數(shù)據(jù)
LCD_WRITE(fen%10+0x30,1);//寫數(shù)據(jù)
LCD_WRITE(':',1);//寫數(shù)據(jù)
LCD_WRITE(miao/10+0x30,1);//寫數(shù)據(jù)
LCD_WRITE(miao%10+0x30,1);//寫數(shù)據(jù)
}
}
//*********************中斷服務程序******************
void interrupt isr(void)
{
if(TMR1IE&&TMR1IF) //判斷是否為TMR1中斷
{
TMR1L=(65536-12500)%256;
TMR1H=(65536-12500)/256;
TMR1IF=0; //清TMR1中斷標志位(必須用軟件清零)
count++;
if(count==10)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
shi=0;
}
}
}
}
}
|