picsimlab 关于pic16f877A 无中断按键使用

该博客介绍了一个使用PIC16F877A微控制器实现的简单控制系统,通过板上按键RB3和RB4进行数字加减操作,数值范围限制在1到9之间。程序中定义了按键处理函数Key()和延时函数,用于更新LCD1602显示屏上的显示内容。LCD1602初始化和字符显示功能由lcd1602.c和lcd1602.h文件提供支持。整个系统利用了配置位设置和C语言编程,实现了基本的人机交互界面。
摘要由CSDN通过智能技术生成

这次通过板上自带按键  RB3  和 RB4  完成

按下RB3  数字加一

按下RB4  数字减一

最大为9

最小为1

主函数 main.c


// PIC16F877A Configuration Bit Settings

// 'C' source line config statements

// CONFIG
#pragma config FOSC = HS        // Oscillator Selection bits (HS oscillator)
#pragma config WDTE = OFF       // Watchdog Timer Enable bit (WDT disabled)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bit (BOR enabled)
#pragma config LVP = ON         // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3/PGM pin has PGM function; low-voltage programming enabled)
#pragma config CPD = OFF        // Data EEPROM Memory Code Protection bit (Data EEPROM code protection off)
#pragma config WRT = OFF        // Flash Program Memory Write Enable bits (Write protection off; all program memory may be written to by EECON control)
#pragma config CP = OFF         // Flash Program Memory Code Protection bit (Code protection off)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>

//__CONFIG(0xFF32);//??xc8?????????????

#include "lcd1602.h"
#define uchar unsigned char
#define uint  unsigned int
#define    K1   RB3
#define    K2   RB4

unsigned char Tempmin = '0'; //  最小问题
unsigned char Tempmax = '1'; //  最大问题
uchar kflag = 0;

uchar ge=1;//????


void delay_1ms(void)
{ 
    unsigned int n;
    for(n=0;n<50;n++)
    { 
    NOP();
    }
}

void delay_ms(unsigned int time)

{
    for(;time>0;time--)
    {
        delay_1ms();
    }
}

void Key()
{
    if(K1&&K2)  // 按键没有按下
    {
       kflag = 1;
    }
    else  // 按键按下
    {
       if(kflag == 1)
       {
          kflag = 0;
          if(K1 == 0)
          {
              if(++Tempmax >= '9')
                   Tempmax  = '9';
              
          }
          if(K2 == 0)
          {   
              if(Tempmax > Tempmin+1)
              {
                  --Tempmax;
              }
          }
          
       }
    }

}
void main()
{
    TRISB1=1;
    LCD1602_GPIO_Init(); //????1602?????
    LCD1602_init(); //1602?????


  while(1)
    { 
       
          DisplayListChar(0,0,"SPEED = ");
      //    Tempvalue =  ReadTemperature(); 
       //   Lcd_Display(7,1,Tempvalue);  //  显示温度值
         DisplayOneChar(8,0,Tempmax);    //  最高温度
          
          Key();                       //  按键程序    
  }
    
}

lcd1602.c

//???pmlab x??????????????gb2312??
/*
lcd1602.c?lcd1602.h?????????????
DisplayListChar(unsigned charx, unsigned char y,unsigned char* str)???????
DisplayOneChar(unsigned charx, unsigned char y,unsigned char str)??????
x???y???ch?????'a'?str??????"i am a string"?
????????????lcd1602.h???#define??????
??lcd1602.c???void LCD1602_GPIO_Init(void)???????
*/
#include <pic.h>           //??PIC16F87XA???????
#include "lcd1602.h"    

//1602???????
//---------------------------------------
void delay(void)
{
	int i;
	for (i = 0;i < 200;i++);
}

//---------------------------------------
//???1602?????
//---------------------------------------
void LCD1602_Write_Instruction(unsigned char combuf)
{ 
     Lcd_Date=combuf;     //??????RD???DB
     RS=0;                //???????
//     RW=0;                //?????
     E=0;  
     delay();             //?????
     E=1;                 //E?????????1602??
     asm("NOP");          //????????????????1us
     E=0;                 //???????E???
} 

//---------------------------------------
//???1602?????
//---------------------------------------
void LCD1602_Write_data(unsigned char databuf)
{ 
     Lcd_Date=databuf;    //??????RD???DB
     RS=1;                //???????
   //  RW=0;                //?????
     E=0;  
     delay();             //?????
     E=1;                 //E?????????1602??
     asm("NOP");          //????????????????1us
     E=0;                 //???????E???
} 

 
//---------------------------------------
//???1602???????
//---------------------------------------
void LCD1602_init(void)
{ 
     LCD1602_Write_Instruction(0x38);  //8?????????????5*7????
     LCD1602_Write_Instruction(0x08);  //?????????
     LCD1602_Write_Instruction(0x01);  //??
     LCD1602_Write_Instruction(0x06);  //???????????????????
     LCD1602_Write_Instruction(0x0C);  //?????????
} 

/****************??????????????*************/

void DisplayOneChar(unsigned char X,unsigned char Y,const unsigned char DData)
{
    Y&=1;
    X&=15;
    if(Y)X|=0x40;               //?y?1???????????+0X40
    X|=0x80;                    //???????+0X80
    LCD1602_Write_Instruction(X);
    LCD1602_Write_data(DData);
}

/***********??????????????***********/

void DisplayListChar(unsigned char X,unsigned char Y,const unsigned char *DData)
{
    unsigned char ListLength=0;
    Y&=0x01;
    X&=0x0f;
    while(X<16 && DData[ListLength])
    {
        DisplayOneChar(X,Y,DData[ListLength]);
        ListLength++;
        X++;
    }
}


void LCD1602_GPIO_Init(void)
{
    ADCON1=0X07;//??A?????????????????IO
    TRISE=0B11111001;      //RA5 RA2 RA3??
    TRISD=0B00000000;      //RD7-RD0??
    PORTA=0B00000000;      //???RA7-RA0???
    PORTD=0B00000000;      //???RD7-RD0???
}

lcd1602.h

//???pmlab x??????????????gb2312??
/*
lcd1602.c?lcd1602.h?????????????
DisplayListChar(unsigned charx, unsigned char y,unsigned char* str)???????
DisplayOneChar(unsigned charx, unsigned char y,unsigned char str)??????
x???y???ch?????'a'?str??????"i am a string"?
????????????lcd1602.h???#define??????
??lcd1602.c???void LCD1602_GPIO_Init(void)???????
*/
#ifndef  __LCD1602_H
#define  __LCD1602_H

//---------------------------------------
//1602????I/O??
#define E   RE1            //1602???E???RA3??
//#define RW  RW          //1602???RW???RA2??
#define RS  RE2           //1602???RS???RA5??
#define Lcd_Date PORTD     //LCD1602????


void delay(void);
void LCD1602_Write_Instruction(unsigned char combuf);
void LCD1602_Write_data(unsigned char databuf);
void LCD1602_init(void);
void DisplayOneChar(unsigned char X,unsigned char Y,const unsigned char DData);
void DisplayListChar(unsigned char X,unsigned char Y,const unsigned char *DData);
void LCD1602_GPIO_Init(void);


#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值