代码:
#include <mega16.h>
// Declare your global variables here
#define hc165_clk PORTB.0
#define hc165_lp PORTB.1
#define hc165_out PINB.2
unsigned char read_hc165(void)
{
unsigned char data=0,i,temp=0x80;
hc165_lp=0;
hc165_lp=1;
for(i=0;i<=7;i++)
{
if(hc165_out)
data|=temp;
hc165_clk=1;
temp=temp>>1;
hc165_clk=0;
}
return data;
}
void main(void)
{
// Declare your local variables here
PORTA=0XFF;
DDRA=0XFF;
PORTB=0X06;
DDRB=0X03;
while (1)
{
// Place your code here
PORTA=~read_hc165();
}
}
仿真效果图: