日韩一区二区三区精品,欧美疯狂xxxxbbbb牲交,热99re久久免费视精品频,人妻互换 综合,欧美激情肉欲高潮视频

歷史上的今天

今天是:2024年08月27日(星期二)

正在發(fā)生

2020年08月27日 | PIC16F887 串口 LCD1602 按鍵

發(fā)布者:TranquilVibes 來源: eefocus關鍵字:PIC16F887  串口  LCD1602  按鍵 手機看文章 掃描二維碼
隨時隨地手機看文章

#include

#include

#include

#include

#define uchar unsigned char

#define uint unsigned int


/* 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 digital input, MCLR internally tied to VDD) */

#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 = OFF      /* Brown Out Reset Selection bits (BOR disabled) */

#pragma config IESO = OFF       /* Internal External Switchover bit (Internal/External Switchover mode is disabled) */

#pragma config FCMEN = OFF      /* Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled) */

#pragma config LVP = OFF        /* Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming) */


/* 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) */



void delaya_US( unsigned char n );



void LCD_CSH( void );



void LCD_WRITE_4( unsigned char R1, unsigned char FLAG );



void LCD_WRITE( unsigned char R1, unsigned char FLAG );



unsigned char LCD_READ( void );



void LCD_BUSY( void );



void delaya( unsigned int n );



/* LCD顯示相關 */

#define DATA 1                                       /* LCD寫數(shù)據(jù)時為1 */

#define COM 0                                       /* LCD寫命令時為0 */

#define LINE1 0b10000000

#define LINE2 0b11000000


#define LCD_E RD6

#define LCD_RW RD5

#define LCD_RS RD4


/* ======延時(n×10)us */

void delaya_US( unsigned char n )

{

unsigned char j;

for ( j = 0; j < n; j++ )

{

NOP(); NOP();

}

}



/* LCD初始化 */

void LCD_CSH( void )

{

TRISD = 0x00;  /* RD方向輸出 */


delaya( 20 );


LCD_WRITE_4( 0b0011, COM );

delaya( 5 );

LCD_WRITE_4( 0b0011, COM );

delaya_US( 10 );

LCD_WRITE_4( 0b0011, COM );

delaya_US( 10 );


LCD_WRITE_4( 0b0010, COM );


LCD_BUSY();

LCD_WRITE( 0b00101000, COM );

LCD_WRITE( 0b00001100, COM );

LCD_WRITE( 0b00000001, COM );

delaya( 2 );

LCD_WRITE( 0b00000110, COM );

}



/* LCD寫4位命令 */

void LCD_WRITE_4( unsigned char R1, unsigned char FLAG )

{

LCD_RW = 0; NOP();

LCD_RS = FLAG;

PORTD &= 0xF0;

LCD_E = 1; NOP();

PORTD |= R1;

NOP(); NOP();

LCD_E = 0; NOP();

LCD_RS = 0; NOP();

PORTD &= 0xF0;

}



/* LCD寫命令 */

void LCD_WRITE( unsigned char R1, unsigned char FLAG )

{

unsigned char R2;


LCD_BUSY();

LCD_RW = 0; NOP();

LCD_RS = FLAG;

R2 = (R1 >> 4) & 0x0F;

PORTD &= 0xF0; NOP();

LCD_E = 1;

PORTD |= R2;

NOP(); NOP();

LCD_E = 0;


R2 = R1 & 0x0F;

PORTD &= 0xF0; NOP();

LCD_E = 1; NOP();

PORTD |= R2;

NOP(); NOP();

LCD_E = 0; NOP();

LCD_RS = 0; NOP();

PORTD &= 0xF0;

}



/* 讀LCD狀態(tài) */

unsigned char LCD_READ( void )

{

unsigned char R1;

TRISD = 0x0F;

LCD_RS = 0; NOP();

LCD_RW = 1;

NOP(); NOP();

LCD_E = 1;

NOP(); NOP();

NOP(); NOP();

R1 = 0;

R1 = (PORTD << 4) & 0xF0;

LCD_E = 0;

NOP(); NOP();

NOP(); NOP();

LCD_E = 1;

NOP(); NOP();

R1 |= (PORTD & 0x0F);

LCD_E = 0; NOP();

LCD_RW = 0;

TRISD = 0x00;

return(R1);

}



/* 檢測LCD是否忙 */

void LCD_BUSY( void )

{

unsigned char R1;

do

{

R1 = LCD_READ();

}

while ( (R1 & 0x80) == 0x80 );

}



/* ======延時(n)ms */

void delaya( unsigned int n )

{

unsigned int j;

unsigned char k;

for ( j = 0; j < n; j++ )

for ( k = 246; k > 0; k-- )

NOP();

}



/*

 * ***********************************************************************

 * 顯示屏字符串寫入函數(shù)

 * ***********************************************************************

 */

void LCD_write_str( unsigned char x, unsigned char y, char *s )

{

if ( y == 0 )

{

LCD_WRITE( 0x80 + x, COM );

}else {

LCD_WRITE( 0xC0 + x, COM );

}


while ( *s )

{

LCD_WRITE( *s, DATA );

s++;

}

}



#define K1 RC0

#define K2 RC1

int Keyscan( void )

{

TRISC |= 0x03;                   /*按鍵是輸入 */

if ( K1 == 0 )

{

delaya( 5 );

if ( K1 == 0 )

{

while ( K1 == 0 )

;       /* 等待按鍵松開 */

return(1);

}

}

if ( K2 == 0 )

{

delaya( 5 );

if ( K2 == 0 )

{

while ( K2 == 0 )

;  /* 等待按鍵松開 */

return(2);

}

}

return(0);

}



/*********************************************************************************

 * 【函 數(shù) 名】: void send_cmd(unsigned char dat)

 * 【功    能】: 串口發(fā)送數(shù)據(jù)命令

 **********************************************************************************/

void send( unsigned char dat )

{

TXREG = dat;

while ( TRMT != 1 )

;  /* 等待發(fā)送完成 */

}



/*********************************************************************************

 * 【函 數(shù) 名】: Print_Str(unsigned char *s)

 * 【功    能】: 串口發(fā)送 一個字符串

 **********************************************************************************/

void Print_Str( unsigned char *s )

{

while ( *s != '