⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 74hc164_test.c

📁 LPC2103,74HC164
💻 C
字号:
;/****************************************Copyright (c)**************************************************
;**                               深圳市优龙科技有限公司
;**
;**                               http://www.ucdragon.com
;**
;**------------------------------------------------------------------------------------------------------
;********************************************************************************************************/
#include  "..\inc\YL_LPC2103.h"
#include  "..\inc\config.h"

U8 const  HC164_string[]  = "\nPlease enter two numbers(0-f) to turn on the leds\n";//输入两个数字,0到f

/*************************************************************************
* Function Name: UART0_GetChar
* Parameters:
* Return: char
*
* Description: Receive a character from Uart by polling LSR register
*
*************************************************************************/
char UART0_GetChar ( void )
{
  U8  Rcv_Data;
  while((U0LSR&0x01)==0);
  Rcv_Data=U0RBR;
  return(Rcv_Data);
}
/*************************************************************************
* Function Name: UART0_PutChar
* Parameters:
* 	       char ch
* Return: void
*
*
* Description: Send character by polling LSR register
*
*************************************************************************/
void UART0_PutChar ( U8  data)
{
  U0THR = data;		             // 发送数据
  while( (U0LSR&0x40)==0 );	     // 等待数据发送完毕
}
/*************************************************************************
* Function Name: UART0_PutString
* Parameters:
* 	       char *Buf
* Return: int :
*
*
* Description: Send a string by using polling method
*
*************************************************************************/
void  UART0_PutString(char *str)
{
	while(*str !='\0')
	{
		UART0_PutChar(*str++);
	}
}
/*************************************************************************
 * Function Name: UART_Init
 * Parameters: Which_Uart DevNum
 * 			
 * Return: int
 *             	0: sucess
 *		1: fail
 * Description: Initialize Uart, configure baut rate, frame format and FIFO
 *
 *************************************************************************/
void UART0_Init ()		//串口0配置
{
  U0LCR = 0x83;		        // DLAB = 1,可设置波特率
  U0DLL = 0x11;
  U0DLM = 0x00;
  U0LCR = 0x03;
}
/*************************************************************************
* Function Name: main
* Parameters:
* 	
* Return: void
*
*
* Description:
*
*************************************************************************/
void main( void )
{
  U8 no,Display_Bit, Display_Data_Hight, Display_Data_Low;	//循环参数
  PINSEL0 = 0x00000005;		                                // 设置I/O连接到UART0,	P0.4,P0.5,P0.6,P0.7,P0.8为输出,用来控制LED.
  PINSEL1 = 0x00000000;
  UART0_Init();

  for(;;)
  {
    UART0_PutString("\nPlease enter two numbers(0-f) to turn on the leds\n");
    Display_Data_Hight = UART0_GetChar();		        //拿取第一个显示数据
    Display_Data_Low = UART0_GetChar();	                        //拿取第二个显示数据

    UART0_PutChar( Display_Data_Hight );		        //返回要显示的数据
    UART0_PutChar( Display_Data_Low );

    if ( Display_Data_Hight == 'a' )  Display_Data_Hight = 0xa;
    if ( Display_Data_Hight == 'b' )  Display_Data_Hight = 0xb;
    if ( Display_Data_Hight == 'c' )  Display_Data_Hight = 0xc;
    if ( Display_Data_Hight == 'd' )  Display_Data_Hight = 0xd;
    if ( Display_Data_Hight == 'e' )  Display_Data_Hight = 0xe;
    if ( Display_Data_Hight == 'f' )  Display_Data_Hight = 0xf;

    if ( Display_Data_Hight == 'A' )  Display_Data_Hight = 0xa;
    if ( Display_Data_Hight == 'B' )  Display_Data_Hight = 0xb;
    if ( Display_Data_Hight == 'C' )  Display_Data_Hight = 0xc;
    if ( Display_Data_Hight == 'D' )  Display_Data_Hight = 0xd;
    if ( Display_Data_Hight == 'E' )  Display_Data_Hight = 0xe;
    if ( Display_Data_Hight == 'F' )  Display_Data_Hight = 0xf;
    else    Display_Data_Hight = Display_Data_Hight;

    if ( Display_Data_Low == 'a' )  Display_Data_Low = 0xa;
    if ( Display_Data_Low == 'b' )  Display_Data_Low = 0xb;
    if ( Display_Data_Low == 'c' )  Display_Data_Low = 0xc;
    if ( Display_Data_Low == 'd' )  Display_Data_Low = 0xd;
    if ( Display_Data_Low == 'e' )  Display_Data_Low = 0xe;
    if ( Display_Data_Low == 'f' )  Display_Data_Low = 0xf;
    if ( Display_Data_Low == 'A' )  Display_Data_Low = 0xa;
    if ( Display_Data_Low == 'B' )  Display_Data_Low = 0xb;
    if ( Display_Data_Low == 'C' )  Display_Data_Low = 0xc;
    if ( Display_Data_Low == 'D' )  Display_Data_Low = 0xd;
    if ( Display_Data_Low == 'E' )  Display_Data_Low = 0xe;
    if ( Display_Data_Low == 'F' )  Display_Data_Low = 0xf;
    else  Display_Data_Low = Display_Data_Low;

    IODIR = HC164_Data | HC164_Clk;		  //设置移位寄存器的时钟线和数据线为输出

    for ( no = 4; no > 0; no-- )
      {
        Display_Bit = Display_Data_Hight & ( 0x1 << ( no - 1 ) );   //取最高位,再取第二位,依此类推
        if ( Display_Bit == 0 ) IOCLR = HC164_Data;		    //判断所取的位是否为0,并发送该位给74HC164
        else    IOSET = HC164_Data;

        IOSET = HC164_Clk;					     //拉高时钟线,上升沿有效
        IOCLR = HC164_Clk;					     //拉底时钟线,完成一个位的输入,经过8次(参数no)后完成一个字节的输入
      }
    for ( no = 4; no > 0; no-- )
      {
        Display_Bit = Display_Data_Low & ( 0x1 << ( no - 1 ) );	      //取最高位,再取第二位,依此类推
        if ( Display_Bit == 0 )  IOCLR = HC164_Data;		      //判断所取的位是否为0,并发送该位给74HC164
        else  IOSET = HC164_Data;

        IOSET = HC164_Clk;					      //拉高时钟线,上升沿有效
        IOCLR = HC164_Clk;					      //拉底时钟线,完成一个位的输入,经过8次(参数no)后完成一个字节的输入
    }
   }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -