74165芯片的简单使用

来源:互联网 发布:red herring软件下载 编辑:程序博客网 时间:2024/05/17 06:13
#include <iom8v.h>
#include 
<macros.h>


/**********************
* PD0~PD7 ----- D0~D7 *
* PC0   --------   PL *
* PC1   --------   CP *
* PC2   --------   CE *
* PC3   --------   DS *
* PC4   --------   Q7 *
* PB0~7 ------- LED0~7*
*********************
*/


#define PL_LOW (PORTC &= ~BIT(PC0))
#define PL_HIGH (PORTC |= BIT(PC0))
#define CP_HIGH (PORTC |= BIT(PC1))
#define CP_LOW (PORTC &= ~BIT(PC1))
#define CE_HIGH (PORTC |= BIT(PC2))
#define CE_LOW (PORTC &= ~BIT(PC2))
#define DS_HIGH (PORTC |= BIT(PC3))
#define DS_LOW (PORTC &= ~BIT(PC3))
#define PARALL_DATA PORTD
#define LED_DATA PORTB
#define CUR_SERIAL_DATA ((PINC & BIT(PC4)) ? BIT(0) : 0x00)

void Device_Init(void)
{
    Port_Init();
}

void Port_Init(void)
{
    DDRB 
= 0xFF;
    DDRD 
= 0xFF;
    DDRC 
= ~BIT(4);
    PORTB 
= 0xFF;
    PORTD 
= 0xFF;
    PORTC 
= 0x00;
}

void Delay_MS(UINT16 wTimer)
{
    UINT16 i 
= 0,j = 0;
    
for (i = 0;i < wTimer;i++)
    
{
        
for (j = 0;j < 200;j++)
        
{;}
    }

}


void Write_Data_To_165(UINT8 chData)        //向165芯片写入chData
{
    PL_LOW;                            
//将PL端置为低
    PARALL_DATA = chData;            //设置数据
}


UINT8 Read_A_Byte_From_165(
void)    //从165芯片读出一个字节
{
    UINT8 chCount 
= 0;        //控制计数
    UINT8 chResult = 0;        //存储结果
    PL_HIGH;                //PL端拉高
    CE_LOW;                    //CE端拉低
    DS_LOW;                    //DS端拉低
    for(chCount = 0; chCount < 8++chCount)    //依次读入八位数据
    {
        chResult 
<<= 1;                            //因为到上升沿时数据将会移位,所以移位之前的数据位就是原数据的最高位
        chResult |= CUR_SERIAL_DATA;
        CP_LOW;                                    
//给出CP时钟上升沿
        NOP();
        CP_HIGH;
    }

    
return chResult;                //返回结果
}



void main()
{
    UINT8 chCount 
= 0;
    UINT8 chResult 
= 0;
    Device_Init();
    
for(chCount = 0; chCount < 99; chCount++)        //依次向165芯片写入0~99,然后再显示到LED上以检验是否正确
    {
        Write_Data_To_165(chCount);
        chResult 
= Read_A_Byte_From_165();
        LED_DATA 
= ~chResult;
        Delay_MS(
10000);
    }

}


 
原创粉丝点击