#include<pic.h>
#define uchar unsigned char
#define uint unsigned int
bit flag_flash=0;
uchar cc_t1=0;
#define RSPIN RC6
#define RWPIN RC5
#define EPIN RC4
const uchar tab1[]={"bbbbbbbbbb"};
const uchar tab2[]={"aaaaaaaaaa"};
//----------------------------------------------------------------------------------
//延时子程序
//----------------------------------------------------------------------------------
void delay(uint dat)
{
uint k;
for(k=0;k<dat;k++);
}
//--------------------------------------------------------------------------------
void init()
{
//----------AD口设置---------------------------------------------------------------
ADCON0=0X40; //转换速度FOSC/8,选择模拟通道0(RA0/AN0),关闭AD功能
ADCON1=0X07; //右对齐 全不设为D口
ADON=0;
//-----------比较器配置---------------------------------------------------------------
CMCON=0x07; //比较器关闭,RA0-RA3为IO
CVRCON=0x00; //比较器电源关闭
//---------------------端口配置------------------------------------------------------
TRISA=0B00000000;
PORTA=0XFF;
TRISB=0B00000000;
PORTB=0XFF;
OPTION_REG=OPTION_REG&0X7F; //B口上拉允许
// OPTION_REG=OPTION_REG|0X80; //B口上拉禁止
TRISC=0B00000000; //串口用时 RC7/RC6必须是输入
PORTC=0XFF;
TRISD=0B00000000;
PORTD=0XFF;
TRISE=0B00000000;
PORTE=0X07;
//----------------中断使能设置---------------------------------------------------------
INTCON=0B11000000; //开全局中断及外设中断
OPTION_REG=0B00000111; //分频器给T0 256分频
//-------------------T0定时器--------------------------------------------------------
T0IE=1;
TMR0=0;
//-------------------T1定时器-------------------------------------------------------
TMR1IE=0;
T1CON=0B00000000; //1:1分频
TMR1H=(65536-50000)/256;
TMR1L=(65536-50000)%256;
TMR1ON=0;
//--------------------T2定时器------------------------------------------------------
TMR2IE=0;
T2CON=0B00000000; // 预分频1:1 后分频1:1
TMR2=0;
TMR2ON=0;
//----------------------PWM 捕捉设置--------------------------------------------------
PR2=0XFF;
CCP1CON=0B00000000; //关模块
CCPR1L=0X0F;
CCP2CON=0B00000000; //关模块
CCPR1L=0X0F;
//----------------------串口设置-------------------------------------------------------
TXSTA=0X24; //使能串口发送,选择高速波特率 //0010 0100
// RCSTA=0X90; //使能串口工作,连续接收 //1001 0000
RCSTA=0X10; //禁止串口工作,连续接收 //0001 0000
RCIE=0; //禁止使能接收中断 //0000 0001
SPBRG=71; //设置波特率为9600BPS
}
//-----------AD转换------------------------------------------
/*void AD_ZH(void)
{
GO_DONE=1; //启动转换
while(GO_DONE); //等待转换完成
AD_HIGH=ADRESH;
AD_LOW=ADRESL;
adval=AD_HIGH;
adval=adval<<8|AD_LOW;
}*/
//-------------------------中断------只有一个入口--------------------------------------
void interrupt svr()
{
if(T0IE&&T0IF)
{
T0IF=0;
TMR0=0;
cc_t1++;
if(cc_t1==10)
{
cc_t1=0;
flag_flash=1;
}
}
/* if(RCIE&&RCIF)
{
RCIF=0;
receive=RCREG;
flag_write=1;
RC5=1;
TXREG=receive;
while(!TRMT);
RC5=0;
} */
}
//-------------------------------------------------------------------------------
//功能:向液晶显示控制器写送指令.
//-------------------------------------------------------------------------------
void write_com(uchar ch) //向液晶显示控制器写送指令
{
uchar temp;
RSPIN=0;
RWPIN=1;
do
{
TRISB=0XFF;
EPIN=1;
delay(10);
temp=PORTB;
EPIN=0;
}
while(temp&0x80);
TRISB=0X00;
RWPIN=0;
PORTB=ch;
delay(10);
EPIN=1;
delay(10);
EPIN=0;
}
//-------------------------------------------------------------------------------
//功能:向液晶显示控制器写数据.
//-------------------------------------------------------------------------------
void write_dat(uchar ch) //向液晶显示控制器写数据
{
uchar temp;
RSPIN=0;
RWPIN=1;
do
{
TRISB=0XFF;
EPIN=1;
delay(10);
temp=PORTB;
EPIN=0;
}
while(temp&0x80);
TRISB=0X00;
RSPIN=1;
RWPIN=0;
PORTB=ch;
delay(10);
EPIN=1;
delay(10);
EPIN=0;
}
/********************************************************************
* 名称 : L1602_init()
* 功能 : 1602初始化,请参考1602的资料
***********************************************************************/
void L1602_init(void)
{
RSPIN=0;
RWPIN=0;
EPIN=0;
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x08);
delay(5);
write_com(0x01);
delay(5);
write_com(0x06);
delay(5);
write_com(0x0c);
delay(5);
}
/***********************************************************************/
void main()
{
uchar i=0;
init();
L1602_init();
while(1)
{
write_com(0x80);
for(i=0;i<10;i++)
{
write_dat(tab1[i]);
}
write_com(0x80+0x40);
for(i=0;i<10;i++)
{
write_dat(tab2[i]);
}
delay(500000);
delay(500000);
delay(500000);
delay(500000);
//-------------------------------------------------------------
if(flag_flash==1)
{
flag_flash=0;
RD1=!RD1;
}
}
} |