PIC单片机多个端口上的不同位,怎样软件虚拟成一个端口来操作

2020-02-09 11:36发布

例如 使用PIC16F916
//LCD
#define RES  RA7
#define AO   RA6
#define RW   RC0
#define EP   RC1
#define DB0  RC2
#define DB1  RC3
#define DB2  RC4
#define DB3  RC5
#define DB4  RB3
#define DB5  RB4
#define DB6  RB5
#define DB7  RB6

union {
   struct {
       unsigned D0 :1;
       unsigned D1 :1;
       unsigned D2 :1;
       unsigned D3 :1;
       unsigned D4 :1;
       unsigned D5 :1;
       unsigned D6 :1;
       unsigned D7 :1;
     }ONEBIT;   
   unsigned char LCD_DAT;  
}LCD;
//这是输出
void LCD_DATA_OUT(unsigned char y)
{
   TRISC&=0xb3;//1100 0011
   TRISB&=0x87;//1000 0111
   LCD.LCD_DAT=y;
   DB0=LCD.ONEBIT.D0;  
   DB1=LCD.ONEBIT.D1;
   DB2=LCD.ONEBIT.D2;
   DB3=LCD.ONEBIT.D3;
   DB4=LCD.ONEBIT.D4;
   DB5=LCD.ONEBIT.D5;
   DB6=LCD.ONEBIT.D6;
   DB7=LCD.ONEBIT.D7;
}
//输入
unsigned char LCD_DATA_IN(void)
{
  TRISC|=0x3b; //0011 1100
  TRISB|=0x78;//0111 1000
  LCD.ONEBIT.D0=DB0;
  LCD.ONEBIT.D1=DB1;
  LCD.ONEBIT.D2=DB2;
  LCD.ONEBIT.D3=DB3;
  LCD.ONEBIT.D4=DB4;
  LCD.ONEBIT.D5=DB5;
  LCD.ONEBIT.D6=DB6;
  LCD.ONEBIT.D7=DB7;
  return  LCD.LCD_DAT;
  
}
unsigned char LCD_DATA_IN(void)
{
  TRISC|=0x3b; //0011 1100
  TRISB|=0x78;//0111 1000
  LCD.ONEBIT.D0=DB0;
  LCD.ONEBIT.D1=DB1;
  LCD.ONEBIT.D2=DB2;
  LCD.ONEBIT.D3=DB3;
  LCD.ONEBIT.D4=DB4;
  LCD.ONEBIT.D5=DB5;
  LCD.ONEBIT.D6=DB6;
  LCD.ONEBIT.D7=DB7;
  Delay1ms(1);
  return  LCD.LCD_DAT;
  
}
使用这种方法可以操作 ,但不是太稳定。
各位大侠有什么好的方法吗???
0条回答

一周热门 更多>