📄 readswitch._c
字号:
#include"include.h"
#define CLK 7
#define SH_LD 5
#define SER_SL_HI() DDRB |= (1<<SH_LD)
#define SER_SL_LO() DDRB &= ~(1<<SH_LD) //SH/LD 接PB5
#define SER_CK_HI() DDRB |= (1<<CLK)
#define SER_CK_LO() DDRB &= ~(1<<CLK) //SCK 接 PB7
#define CLK_EN_HI() DDRB |= (1<<4)
#define CLK_EN_LO() DDRB &= (1<<4)
#define HC165Switch_Port() (PINB&(1<<6)) //输入 QH 接PB6
//------------------------------------------------------------------------------
//读AVR控制器地址码和扩展口的按键
void ReadHC165Switch(void)
{
static unsigned char *Read_buffer;
struct ReadHC165Switch *Readwitch;
unsigned char i,j,ret = 0xff;
Read_buffer = Read74HC165Switch;
CLK_EN_LO();
//数据锁存,在shift/load输入一脉冲
SER_CK_LO();
SER_SL_HI();
asm("nop");
SER_SL_LO();
asm("nop");
SER_SL_HI();
asm("nop");
asm("nop");
//读数据
for(i=0;i<sizeof(struct ReadHC165Switch);i++)
{
for(j=0;j<8;j++)
{
SER_CK_HI();
if(HC165Switch_Port()==0)
{
ret <<= 1;
ret &= ~0x01;
}
else
{
ret <<= 1;
ret |= 0x01;
}
asm("nop");
asm("nop");
SER_CK_LO();
asm("nop");
asm("nop");
}
*Read_buffer++ = ret;
}
Readwitch = (struct ReadHC165Switch *)Read74HC165Switch;
}
//------------------------------------------------------------------------------
//扫描按键 PA,PC端口
void ReadSwitch(void)
{
unsigned char i;
ReadSwitchPINA = 0;
ReadSwitchPINC = 0;
for(i=0; i<40; i++);
if(!(PINA & 0x80)) ReadSwitchPINA |= 0x80;
if(!(PINA & 0x40)) ReadSwitchPINA |= 0x40;
if(!(PINA & 0x20)) ReadSwitchPINA |= 0x20;
if(!(PINA & 0x10)) ReadSwitchPINA |= 0x10;
if(!(PINA & 0x08)) ReadSwitchPINA |= 0x08;
if(!(PINA & 0x04)) ReadSwitchPINA |= 0x04;
if(!(PINA & 0x02)) ReadSwitchPINA |= 0x02;
if(!(PINA & 0x01)) ReadSwitchPINA |= 0x01;
for(i=0; i<40; i++);
if(!(PINC & 0x80)) ReadSwitchPINC |= 0x80;
if(!(PINC & 0x40)) ReadSwitchPINC |= 0x40;
if(!(PINC & 0x20)) ReadSwitchPINC |= 0x20;
if(!(PINC & 0x10)) ReadSwitchPINC |= 0x10;
if(!(PINC & 0x08)) ReadSwitchPINC |= 0x08;
if(!(PINC & 0x04)) ReadSwitchPINC |= 0x04;
if(!(PINC & 0x02)) ReadSwitchPINC |= 0x02;
if(!(PINC & 0x01)) ReadSwitchPINC |= 0x01;
}
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
void communication_ReadSwitch(void)
{
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -