[古董贴][范例]使用595配合165级联模块扩展端口(硬件SPI)
前面看到很多大侠讨论单片机扩展端口的方法,我这里也插一嘴,将我们习惯使用的一种利用595和165配对扩展输入输出端口的方法拿出来和大家交流。其实,思路很简单,595和165在时钟极性上非常相似,都是上升沿采样/置数,同时也都需要一个控制信号的上升沿来锁存并行数据,很自然的,我们就想到利用硬件SPI的主机模式配合对SS引脚的控制来实现一次数据交换同时读取和置数的操作,并且利用595和165的可级联特性,特别适合端口扩展。
以下是扩展模块的原理图
http://cache.amobbs.com/bbs_upload782111/files_6/armok01126915.GIF
以下是驱动该结构的头文件(测试通过 外部16M,1/4分频)
#ifndef _USE_VIRTUAL_PORT_H_
#define _USE_VIRTUAL_PORT_H_
/***********************************************************
*函数说明:硬件SPI虚拟端口底层驱动函数库 *
*版本: v1.00 *
*作者: 傻孩子 *
*创建日期:2006年7月30日 *
* -------------------------------------------------------- *
*[支持库] *
*支持库名称:RD_MacroAndConst.h *
*需要版本:v0.04 &abv *
*函数库说明:系统常用宏定义库 *
* -------------------------------------------------------- *
*[版本更新] *
* -------------------------------------------------------- *
*[版本历史] *
* v1.00 支持595和165成队及联,支持硬件SPI传输模式, *
* 支持通过宏定义设定虚拟端口数量;虚拟端口同*
* 时支持读取和写两种模式,并且兼容AVR普通端 *
* 口的操作方式,支持虚拟端口位操作。 *
* -------------------------------------------------------- *
*[使用说明] *
* 1、调用该头文件之前,可以通过宏VP_PORT_COUNT *
* 来设定虚拟端口的数目,这里的数目8个引脚算 *
* 一个端口。 *
* 2、通过宏VP_SS来设定SS脚,这里需要位操作支持。 *
* 3、将宏VIRTUALPORT_INSERT_SPI_STC_ISR_CODE插 *
* 入到SPI通讯完成中断处理程序中。 *
* 4、虚拟端口从PORTH和PINH开始,每次需要刷新端 *
* 口时只需要调用宏REFRESH_VIRTUAL_PORT,特别*
* 说明,在中断处理程序里面调用该宏时要打开全*
* 局中断SEI(); *
***********************************************************/
# include <RD_MacroAndConst.h>
/********************
* 系 统 宏 定 义*
********************/
/*------------------*
* 常 数 宏 定 义*
*------------------*/
#ifndef VP_PORT_COUNT
#define VP_PORT_COUNT 2
#endif
#if VP_PORT_COUNT-1 >= 0
# define PORTH (*((volatile unsigned char *)&VP_PORTx))
# define PINH (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-2 >= 0
# define PORTI (*((volatile unsigned char *)&VP_PORTx))
# define PINI (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-3 >= 0
# define PORTJ (*((volatile unsigned char *)&VP_PORTx))
# define PINJ (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-4 >= 0
# define PORTK (*((volatile unsigned char *)&VP_PORTx))
# define PINK (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-5 >= 0
# define PORTL (*((volatile unsigned char *)&VP_PORTx))
# define PINL (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-6 >= 0
# define PORTM (*((volatile unsigned char *)&VP_PORTx))
# define PINM (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-7 >= 0
# define PORTN (*((volatile unsigned char *)&VP_PORTx))
# define PINN (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-8 >= 0
# define PORTO (*((volatile unsigned char *)&VP_PORTx))
# define PINO (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-9 >= 0
# define PORTP (*((volatile unsigned char *)&VP_PORTx))
# define PINP (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-10 >= 0
# define PORTQ (*((volatile unsigned char *)&VP_PORTx))
# define PINQ (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-11 >= 0
# define PORTR (*((volatile unsigned char *)&VP_PORTx))
# define PINR (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-12 >= 0
# define PORTS (*((volatile unsigned char *)&VP_PORTx))
# define PINS (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-13 >= 0
# define PORTT (*((volatile unsigned char *)&VP_PORTx))
# define PINT (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-14 >= 0
# define PORTU (*((volatile unsigned char *)&VP_PORTx))
# define PINU (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-15 >= 0
# define PORTV (*((volatile unsigned char *)&VP_PORTx))
# define PINV (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-16 >= 0
# define PORTW (*((volatile unsigned char *)&VP_PORTx))
# define PINW (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-17 >= 0
# define PORTX (*((volatile unsigned char *)&VP_PORTx))
# define PINX (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-18 >= 0
# define PORTY (*((volatile unsigned char *)&VP_PORTx))
# define PINY (*((volatile unsigned char *)&VP_PINx))
#endif
#if VP_PORT_COUNT-19 >= 0
# define PORTZ (*((volatile unsigned char *)&VP_PORTx))
# define PINZ (*((volatile unsigned char *)&VP_PINx))
#endif
#ifdef PORTH
# define PORTH_BIT SET_BIT8_FORMAT(PORTH)
#endif
#ifdef PORTI
# define PORTI_BIT SET_BIT8_FORMAT(PORTI)
#endif
#ifdef PORTJ
# define PORTJ_BIT SET_BIT8_FORMAT(PORTJ)
#endif
#ifdef PORTK
# define PORTK_BIT SET_BIT8_FORMAT(PORTK)
#endif
#ifdef PORTL
# define PORTL_BIT SET_BIT8_FORMAT(PORTL)
#endif
#ifdef PORTM
# define PORTM_BIT SET_BIT8_FORMAT(PORTM)
#endif
#ifdef PORTN
# define PORTN_BIT SET_BIT8_FORMAT(PORTN)
#endif
#ifdef PORTO
# define PORTO_BIT SET_BIT8_FORMAT(PORTO)
#endif
#ifdef PORTP
# define PORTP_BIT SET_BIT8_FORMAT(PORTP)
#endif
#ifdef PORTQ
# define PORTQ_BIT SET_BIT8_FORMAT(PORTQ)
#endif
#ifdef PORTR
# define PORTR_BIT SET_BIT8_FORMAT(PORTR)
#endif
#ifdef PORTS
# define PORTS_BIT SET_BIT8_FORMAT(PORTS)
#endif
#ifdef PORTT
# define PORTT_BIT SET_BIT8_FORMAT(PORTT)
#endif
#ifdef PORTU
# define PORTU_BIT SET_BIT8_FORMAT(PORTU)
#endif
#ifdef PORTV
# define PORTV_BIT SET_BIT8_FORMAT(PORTV)
#endif
#ifdef PORTW
# define PORTW_BIT SET_BIT8_FORMAT(PORTW)
#endif
#ifdef PORTX
# define PORTX_BIT SET_BIT8_FORMAT(PORTX)
#endif
#ifdef PORTY
# define PORTY_BIT SET_BIT8_FORMAT(PORTY)
#endif
#ifdef PORTZ
# define PORTZ_BIT SET_BIT8_FORMAT(PORTZ)
#endif
#ifdef PINH
# define PINH_BIT SET_BIT8_FORMAT(PINH)
#endif
#ifdef PINI
# define PINI_BIT SET_BIT8_FORMAT(PINI)
#endif
#ifdef PINJ
# define PINJ_BIT SET_BIT8_FORMAT(PINJ)
#endif
#ifdef PINK
# define PINK_BIT SET_BIT8_FORMAT(PINK)
#endif
#ifdef PINL
# define PINL_BIT SET_BIT8_FORMAT(PINL)
#endif
#ifdef PINM
# define PINM_BIT SET_BIT8_FORMAT(PINM)
#endif
#ifdef PINN
# define PINN_BIT SET_BIT8_FORMAT(PINN)
#endif
#ifdef PINO
# define PINO_BIT SET_BIT8_FORMAT(PINO)
#endif
#ifdef PINP
# define PINP_BIT SET_BIT8_FORMAT(PINP)
#endif
#ifdef PINQ
# define PINQ_BIT SET_BIT8_FORMAT(PINQ)
#endif
#ifdef PINR
# define PINR_BIT SET_BIT8_FORMAT(PINR)
#endif
#ifdef PINS
# define PINS_BIT SET_BIT8_FORMAT(PINS)
#endif
#ifdef PINT
# define PINT_BIT SET_BIT8_FORMAT(PINT)
#endif
#ifdef PINU
# define PINU_BIT SET_BIT8_FORMAT(PINU)
#endif
#ifdef PINV
# define PINV_BIT SET_BIT8_FORMAT(PINV)
#endif
#ifdef PINW
# define PINW_BIT SET_BIT8_FORMAT(PINW)
#endif
#ifdef PINX
# define PINX_BIT SET_BIT8_FORMAT(PINX)
#endif
#ifdef PINY
# define PINY_BIT SET_BIT8_FORMAT(PINY)
#endif
#ifdef PINZ
# define PINZ_BIT SET_BIT8_FORMAT(PINZ)
#endif
#ifdef PORTH
# define _PH0 PORTH_BIT.BIT0
# define _PH1 PORTH_BIT.BIT1
# define _PH2 PORTH_BIT.BIT2
# define _PH3 PORTH_BIT.BIT3
# define _PH4 PORTH_BIT.BIT4
# define _PH5 PORTH_BIT.BIT5
# define _PH6 PORTH_BIT.BIT6
# define _PH7 PORTH_BIT.BIT7
#endif
#ifdef PORTI
# define _PI0 PORTI_BIT.BIT0
# define _PI1 PORTI_BIT.BIT1
# define _PI2 PORTI_BIT.BIT2
# define _PI3 PORTI_BIT.BIT3
# define _PI4 PORTI_BIT.BIT4
# define _PI5 PORTI_BIT.BIT5
# define _PI6 PORTI_BIT.BIT6
# define _PI7 PORTI_BIT.BIT7
#endif
#ifdef PORTJ
# define _PJ0 PORTJ_BIT.BIT0
# define _PJ1 PORTJ_BIT.BIT1
# define _PJ2 PORTJ_BIT.BIT2
# define _PJ3 PORTJ_BIT.BIT3
# define _PJ4 PORTJ_BIT.BIT4
# define _PJ5 PORTJ_BIT.BIT5
# define _PJ6 PORTJ_BIT.BIT6
# define _PJ7 PORTJ_BIT.BIT7
#endif
#ifdef PORTK
# define _PK0 PORTK_BIT.BIT0
# define _PK1 PORTK_BIT.BIT1
# define _PK2 PORTK_BIT.BIT2
# define _PK3 PORTK_BIT.BIT3
# define _PK4 PORTK_BIT.BIT4
# define _PK5 PORTK_BIT.BIT5
# define _PK6 PORTK_BIT.BIT6
# define _PK7 PORTK_BIT.BIT7
#endif
#ifdef PORTL
# define _PL0 PORTL_BIT.BIT0
# define _PL1 PORTL_BIT.BIT1
# define _PL2 PORTL_BIT.BIT2
# define _PL3 PORTL_BIT.BIT3
# define _PL4 PORTL_BIT.BIT4
# define _PL5 PORTL_BIT.BIT5
# define _PL6 PORTL_BIT.BIT6
# define _PL7 PORTL_BIT.BIT7
#endif
#ifdef PORTM
# define _PM0 PORTM_BIT.BIT0
# define _PM1 PORTM_BIT.BIT1
# define _PM2 PORTM_BIT.BIT2
# define _PM3 PORTM_BIT.BIT3
# define _PM4 PORTM_BIT.BIT4
# define _PM5 PORTM_BIT.BIT5
# define _PM6 PORTM_BIT.BIT6
# define _PM7 PORTM_BIT.BIT7
#endif
#ifdef PORTN
# define _PN0 PORTN_BIT.BIT0
# define _PN1 PORTN_BIT.BIT1
# define _PN2 PORTN_BIT.BIT2
# define _PN3 PORTN_BIT.BIT3
# define _PN4 PORTN_BIT.BIT4
# define _PN5 PORTN_BIT.BIT5
# define _PN6 PORTN_BIT.BIT6
# define _PN7 PORTN_BIT.BIT7
#endif
#ifdef PORTO
# define _PO0 PORTO_BIT.BIT0
# define _PO1 PORTO_BIT.BIT1
# define _PO2 PORTO_BIT.BIT2
# define _PO3 PORTO_BIT.BIT3
# define _PO4 PORTO_BIT.BIT4
# define _PO5 PORTO_BIT.BIT5
# define _PO6 PORTO_BIT.BIT6
# define _PO7 PORTO_BIT.BIT7
#endif
#ifdef PORTP
# define _PP0 PORTP_BIT.BIT0
# define _PP1 PORTP_BIT.BIT1
# define _PP2 PORTP_BIT.BIT2
# define _PP3 PORTP_BIT.BIT3
# define _PP4 PORTP_BIT.BIT4
# define _PP5 PORTP_BIT.BIT5
# define _PP6 PORTP_BIT.BIT6
# define _PP7 PORTP_BIT.BIT7
#endif
#ifdef PORTQ
# define _PQ0 PORTQ_BIT.BIT0
# define _PQ1 PORTQ_BIT.BIT1
# define _PQ2 PORTQ_BIT.BIT2
# define _PQ3 PORTQ_BIT.BIT3
# define _PQ4 PORTQ_BIT.BIT4
# define _PQ5 PORTQ_BIT.BIT5
# define _PQ6 PORTQ_BIT.BIT6
# define _PQ7 PORTQ_BIT.BIT7
#endif
#ifdef PORTR
# define _PR0 PORTR_BIT.BIT0
# define _PR1 PORTR_BIT.BIT1
# define _PR2 PORTR_BIT.BIT2
# define _PR3 PORTR_BIT.BIT3
# define _PR4 PORTR_BIT.BIT4
# define _PR5 PORTR_BIT.BIT5
# define _PR6 PORTR_BIT.BIT6
# define _PR7 PORTR_BIT.BIT7
#endif
#ifdef PORTS
# define _PS0 PORTS_BIT.BIT0
# define _PS1 PORTS_BIT.BIT1
# define _PS2 PORTS_BIT.BIT2
# define _PS3 PORTS_BIT.BIT3
# define _PS4 PORTS_BIT.BIT4
# define _PS5 PORTS_BIT.BIT5
# define _PS6 PORTS_BIT.BIT6
# define _PS7 PORTS_BIT.BIT7
#endif
#ifdef PORTT
# define _PT0 PORTT_BIT.BIT0
# define _PT1 PORTT_BIT.BIT1
# define _PT2 PORTT_BIT.BIT2
# define _PT3 PORTT_BIT.BIT3
# define _PT4 PORTT_BIT.BIT4
# define _PT5 PORTT_BIT.BIT5
# define _PT6 PORTT_BIT.BIT6
# define _PT7 PORTT_BIT.BIT7
#endif
#ifdef PORTU
# define _PU0 PORTU_BIT.BIT0
# define _PU1 PORTU_BIT.BIT1
# define _PU2 PORTU_BIT.BIT2
# define _PU3 PORTU_BIT.BIT3
# define _PU4 PORTU_BIT.BIT4
# define _PU5 PORTU_BIT.BIT5
# define _PU6 PORTU_BIT.BIT6
# define _PU7 PORTU_BIT.BIT7
#endif
#ifdef PORTV
# define _PV0 PORTV_BIT.BIT0
# define _PV1 PORTV_BIT.BIT1
# define _PV2 PORTV_BIT.BIT2
# define _PV3 PORTV_BIT.BIT3
# define _PV4 PORTV_BIT.BIT4
# define _PV5 PORTV_BIT.BIT5
# define _PV6 PORTV_BIT.BIT6
# define _PV7 PORTV_BIT.BIT7
#endif
#ifdef PORTW
# define _PW0 PORTW_BIT.BIT0
# define _PW1 PORTW_BIT.BIT1
# define _PW2 PORTW_BIT.BIT2
# define _PW3 PORTW_BIT.BIT3
# define _PW4 PORTW_BIT.BIT4
# define _PW5 PORTW_BIT.BIT5
# define _PW6 PORTW_BIT.BIT6
# define _PW7 PORTW_BIT.BIT7
#endif
#ifdef PORTX
# define _PX0 PORTX_BIT.BIT0
# define _PX1 PORTX_BIT.BIT1
# define _PX2 PORTX_BIT.BIT2
# define _PX3 PORTX_BIT.BIT3
# define _PX4 PORTX_BIT.BIT4
# define _PX5 PORTX_BIT.BIT5
# define _PX6 PORTX_BIT.BIT6
# define _PX7 PORTX_BIT.BIT7
#endif
#ifdef PORTY
# define _PY0 PORTY_BIT.BIT0
# define _PY1 PORTY_BIT.BIT1
# define _PY2 PORTY_BIT.BIT2
# define _PY3 PORTY_BIT.BIT3
# define _PY4 PORTY_BIT.BIT4
# define _PY5 PORTY_BIT.BIT5
# define _PY6 PORTY_BIT.BIT6
# define _PY7 PORTY_BIT.BIT7
#endif
#ifdef PORTZ
# define _PZ0 PORTZ_BIT.BIT0
# define _PZ1 PORTZ_BIT.BIT1
# define _PZ2 PORTZ_BIT.BIT2
# define _PZ3 PORTZ_BIT.BIT3
# define _PZ4 PORTZ_BIT.BIT4
# define _PZ5 PORTZ_BIT.BIT5
# define _PZ6 PORTZ_BIT.BIT6
# define _PZ7 PORTZ_BIT.BIT7
#endif
#ifdef PINH
# define Read_PH0 PINH_BIT.BIT0
# define Read_PH1 PINH_BIT.BIT1
# define Read_PH2 PINH_BIT.BIT2
# define Read_PH3 PINH_BIT.BIT3
# define Read_PH4 PINH_BIT.BIT4
# define Read_PH5 PINH_BIT.BIT5
# define Read_PH6 PINH_BIT.BIT6
# define Read_PH7 PINH_BIT.BIT7
#endif
#ifdef PINI
# define Read_PI0 PINI_BIT.BIT0
# define Read_PI1 PINI_BIT.BIT1
# define Read_PI2 PINI_BIT.BIT2
# define Read_PI3 PINI_BIT.BIT3
# define Read_PI4 PINI_BIT.BIT4
# define Read_PI5 PINI_BIT.BIT5
# define Read_PI6 PINI_BIT.BIT6
# define Read_PI7 PINI_BIT.BIT7
#endif
#ifdef PINJ
# define Read_PJ0 PINJ_BIT.BIT0
# define Read_PJ1 PINJ_BIT.BIT1
# define Read_PJ2 PINJ_BIT.BIT2
# define Read_PJ3 PINJ_BIT.BIT3
# define Read_PJ4 PINJ_BIT.BIT4
# define Read_PJ5 PINJ_BIT.BIT5
# define Read_PJ6 PINJ_BIT.BIT6
# define Read_PJ7 PINJ_BIT.BIT7
#endif
#ifdef PINK
# define Read_PK0 PINK_BIT.BIT0
# define Read_PK1 PINK_BIT.BIT1
# define Read_PK2 PINK_BIT.BIT2
# define Read_PK3 PINK_BIT.BIT3
# define Read_PK4 PINK_BIT.BIT4
# define Read_PK5 PINK_BIT.BIT5
# define Read_PK6 PINK_BIT.BIT6
# define Read_PK7 PINK_BIT.BIT7
#endif
#ifdef PINL
# define Read_PL0 PINL_BIT.BIT0
# define Read_PL1 PINL_BIT.BIT1
# define Read_PL2 PINL_BIT.BIT2
# define Read_PL3 PINL_BIT.BIT3
# define Read_PL4 PINL_BIT.BIT4
# define Read_PL5 PINL_BIT.BIT5
# define Read_PL6 PINL_BIT.BIT6
# define Read_PL7 PINL_BIT.BIT7
#endif
#ifdef PINM
# define Read_PM0 PINM_BIT.BIT0
# define Read_PM1 PINM_BIT.BIT1
# define Read_PM2 PINM_BIT.BIT2
# define Read_PM3 PINM_BIT.BIT3
# define Read_PM4 PINM_BIT.BIT4
# define Read_PM5 PINM_BIT.BIT5
# define Read_PM6 PINM_BIT.BIT6
# define Read_PM7 PINM_BIT.BIT7
#endif
#ifdef PINN
# define Read_PN0 PINN_BIT.BIT0
# define Read_PN1 PINN_BIT.BIT1
# define Read_PN2 PINN_BIT.BIT2
# define Read_PN3 PINN_BIT.BIT3
# define Read_PN4 PINN_BIT.BIT4
# define Read_PN5 PINN_BIT.BIT5
# define Read_PN6 PINN_BIT.BIT6
# define Read_PN7 PINN_BIT.BIT7
#endif
#ifdef PINO
# define Read_PO0 PINO_BIT.BIT0
# define Read_PO1 PINO_BIT.BIT1
# define Read_PO2 PINO_BIT.BIT2
# define Read_PO3 PINO_BIT.BIT3
# define Read_PO4 PINO_BIT.BIT4
# define Read_PO5 PINO_BIT.BIT5
# define Read_PO6 PINO_BIT.BIT6
# define Read_PO7 PINO_BIT.BIT7
#endif
#ifdef PINP
# define Read_PP0 PINP_BIT.BIT0
# define Read_PP1 PINP_BIT.BIT1
# define Read_PP2 PINP_BIT.BIT2
# define Read_PP3 PINP_BIT.BIT3
# define Read_PP4 PINP_BIT.BIT4
# define Read_PP5 PINP_BIT.BIT5
# define Read_PP6 PINP_BIT.BIT6
# define Read_PP7 PINP_BIT.BIT7
#endif
#ifdef PINQ
# define Read_PQ0 PINQ_BIT.BIT0
# define Read_PQ1 PINQ_BIT.BIT1
# define Read_PQ2 PINQ_BIT.BIT2
# define Read_PQ3 PINQ_BIT.BIT3
# define Read_PQ4 PINQ_BIT.BIT4
# define Read_PQ5 PINQ_BIT.BIT5
# define Read_PQ6 PINQ_BIT.BIT6
# define Read_PQ7 PINQ_BIT.BIT7
#endif
#ifdef PINR
# define Read_PR0 PINR_BIT.BIT0
# define Read_PR1 PINR_BIT.BIT1
# define Read_PR2 PINR_BIT.BIT2
# define Read_PR3 PINR_BIT.BIT3
# define Read_PR4 PINR_BIT.BIT4
# define Read_PR5 PINR_BIT.BIT5
# define Read_PR6 PINR_BIT.BIT6
# define Read_PR7 PINR_BIT.BIT7
#endif
#ifdef PINS
# define Read_PS0 PINS_BIT.BIT0
# define Read_PS1 PINS_BIT.BIT1
# define Read_PS2 PINS_BIT.BIT2
# define Read_PS3 PINS_BIT.BIT3
# define Read_PS4 PINS_BIT.BIT4
# define Read_PS5 PINS_BIT.BIT5
# define Read_PS6 PINS_BIT.BIT6
# define Read_PS7 PINS_BIT.BIT7
#endif
#ifdef PINT
# define Read_PT0 PINT_BIT.BIT0
# define Read_PT1 PINT_BIT.BIT1
# define Read_PT2 PINT_BIT.BIT2
# define Read_PT3 PINT_BIT.BIT3
# define Read_PT4 PINT_BIT.BIT4
# define Read_PT5 PINT_BIT.BIT5
# define Read_PT6 PINT_BIT.BIT6
# define Read_PT7 PINT_BIT.BIT7
#endif
#ifdef PINU
# define Read_PU0 PINU_BIT.BIT0
# define Read_PU1 PINU_BIT.BIT1
# define Read_PU2 PINU_BIT.BIT2
# define Read_PU3 PINU_BIT.BIT3
# define Read_PU4 PINU_BIT.BIT4
# define Read_PU5 PINU_BIT.BIT5
# define Read_PU6 PINU_BIT.BIT6
# define Read_PU7 PINU_BIT.BIT7
#endif
#ifdef PINV
# define Read_PV0 PINV_BIT.BIT0
# define Read_PV1 PINV_BIT.BIT1
# define Read_PV2 PINV_BIT.BIT2
# define Read_PV3 PINV_BIT.BIT3
# define Read_PV4 PINV_BIT.BIT4
# define Read_PV5 PINV_BIT.BIT5
# define Read_PV6 PINV_BIT.BIT6
# define Read_PV7 PINV_BIT.BIT7
#endif
#ifdef PINW
# define Read_PW0 PINW_BIT.BIT0
# define Read_PW1 PINW_BIT.BIT1
# define Read_PW2 PINW_BIT.BIT2
# define Read_PW3 PINW_BIT.BIT3
# define Read_PW4 PINW_BIT.BIT4
# define Read_PW5 PINW_BIT.BIT5
# define Read_PW6 PINW_BIT.BIT6
# define Read_PW7 PINW_BIT.BIT7
#endif
#ifdef PINX
# define Read_PX0 PINX_BIT.BIT0
# define Read_PX1 PINX_BIT.BIT1
# define Read_PX2 PINX_BIT.BIT2
# define Read_PX3 PINX_BIT.BIT3
# define Read_PX4 PINX_BIT.BIT4
# define Read_PX5 PINX_BIT.BIT5
# define Read_PX6 PINX_BIT.BIT6
# define Read_PX7 PINX_BIT.BIT7
#endif
#ifdef PINY
# define Read_PY0 PINY_BIT.BIT0
# define Read_PY1 PINY_BIT.BIT1
# define Read_PY2 PINY_BIT.BIT2
# define Read_PY3 PINY_BIT.BIT3
# define Read_PY4 PINY_BIT.BIT4
# define Read_PY5 PINY_BIT.BIT5
# define Read_PY6 PINY_BIT.BIT6
# define Read_PY7 PINY_BIT.BIT7
#endif
#ifdef PINZ
# define Read_PZ0 PINZ_BIT.BIT0
# define Read_PZ1 PINZ_BIT.BIT1
# define Read_PZ2 PINZ_BIT.BIT2
# define Read_PZ3 PINZ_BIT.BIT3
# define Read_PZ4 PINZ_BIT.BIT4
# define Read_PZ5 PINZ_BIT.BIT5
# define Read_PZ6 PINZ_BIT.BIT6
# define Read_PZ7 PINZ_BIT.BIT7
#endif
/*------------------*
* 动 作 宏 定 义*
*------------------*/
# define VIRTUALPORT_INSERT_SPI_STC_ISR_CODEVP_PINx = SPDR; VP_IfStartRefresh = True;
# define REFRESH_VIRTUAL_PORT refreshVirtualPort();
/********************
* 结构体宏定义 *
********************/
/********************
* 函 数 声 明 区*
********************/
BOOL refreshVirtualPort(void);
/********************
* 全局变量声明区*
********************/
volatile unsigned char VP_PORTx;
volatile unsigned char VP_PINx;
BOOL VP_IfStartRefresh = False; //虚拟端口刷新标志
unsigned char VP_PortCounter = 0; //虚拟端口计数器
/***********************************************************
*函数说明:虚拟端口刷新函数 *
***********************************************************/
BOOL refreshVirtualPort(void)
{
static BOOL IfStartRefresh = False; //刷新忙信号
if (IfStartRefresh)
{
return False;
}
IfStartRefresh = True;
VP_SS = Low;
VP_SS = High; //锁存并行数据
for (VP_PortCounter = VP_PORT_COUNT;VP_PortCounter>0;VP_PortCounter--)
{
VP_IfStartRefresh = False; //设置等待完成信号
SPDR = VP_PORTx;
while(VP_IfStartRefresh == False);
}
VP_SS = Low;
VP_SS = High; //锁存串行数据
IfStartRefresh = False;
return True;
}
#endif 应用实例
比方说,我要扩展2个端口
使用M8
……
/*--------------虚拟端口模块---------------*/
# define VP_PORT_COUNT2
# define VP_SS _PB0
# include <RD_UseVirtualPort.h>
/***********************************************************
*函数说明:硬件SPI初始化函数 *
***********************************************************/
void SPIInit(void)
{
SPCR = 0xDC;
SPSR = 0x01;
}
/***********************************************************
*函数说明:硬件SPI中断处理程序 *
***********************************************************/
void SPIStcIsr(void)
{
VIRTUALPORT_INSERT_SPI_STC_ISR_CODE
}
/***********************************************************
*函数说明:定时器0初始化函数 *
***********************************************************/
void Timer0Init(void)
{
TCCR0 = 0x00;
ASSR= 0x00;
TCNT0 = 0x06;
OCR0= 0xFA;
TCCR0 = 0x04;
}
/***********************************************************
*函数说明:定时器0中断处理程序 *
***********************************************************/
void Timer0OvfIsr(void)
{
TCNT0 = 0x06;
REFRESH_VIRTUAL_PORT //一毫秒刷新一次
}
因为我们只扩展两个端口
所以,这两个端口分别是
PORTH PORTIPINH PINI
当然,也支持位操作哦^_^ 不错 ,595及165以前用过 ,感觉还可以 傻孩子 要出书了。现在要用到165,这么好的范例帖子帮顶。 顶,好东西,可以用得上 这两个端口分别是
PORTH PORTIPINH PINI
是不是也要用DDR控制方向呢?
另外1楼给出的电路图是扩展4组IO端口吧,可是为什么要用2片165,2片595呢!
新手的问题比较菜,见笑了! 不需要DDR控制方向哈,因为虚拟出来的PORTx和PINx在物理上是分开的,分别对应595和165。
另外,该贴子介绍的方法已经有了改进版本,不需要额外编写时序来驱动了。只需要在165的S/P信号上加一个非门,就可以直接用硬件SPI直接驱动——其时钟极性与595兼容。在我的书中,我从万用板开始,由硬到软一步一步(多图)介绍了这种端口扩展方式。 都明白了,谢谢楼主。 楼主:下面这头文件在哪里呢。
RD_MacroAndConst.h 楼主在吗,或者知道的朋友帮忙贴下上来吧 这个头文件在专栏楼顶的贴子里面 真的不错,哪天我才有这样的境界啊。看来是我没有用心读书~~~595很多人都用过啊 楼主牛人,不知道你的书里面有没有将很多个165和595联级混合使用的范例.我以前见过这样的电路,但自己没写过!
我是菜鸟,望指教! mark 顶一下! 楼主厉害,长见识了 dingyiging! 非常不错的资料,正在用到了,谢谢楼主~ 实践证明一切。 牛人!!!!!!!!!!!!!!!!!!!!!!! MARK........ mark mark mark 标记,[古董贴][范例]使用595配合165级联模块扩展端口(硬件SPI) 本帖最后由 click_lijie 于 2014-7-4 15:26 编辑
顶。提醒一下,165和595 都是高位在前的,设计程序注意一下。还有165的锁存是低电平有效,595的锁存是上升沿有效。若想利用SPI的片选信号对它们同时有效(要不然165可能会有一位不正确----最高位),最好在165的锁存加一个非门(我用三极管反向),在SPI的自动片选下降沿就完成锁存.
页:
[1]