打印
[其他产品]

picsimlab 关于pic16f877A 无中断按键使用

[复制链接]
523|0
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
磨砂|  楼主 | 2022-6-13 15:31 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式

这次通过板上自带按键  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



使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

96

主题

4179

帖子

2

粉丝