|
MSP430全系列单片机 74HC595/74LS595 SPI总线驱动程序
[复制链接]
- /********************************************************
- *74HC595.c for MSP430各个系列单片机
- * 可移植性好,只需修改宏定义和端口的初始化
- * by 追梦 2011-3-19
- ********************************************************/
- #define SHCLK_1 P1OUT |=BIT2 //P1.2 移位时钟信号
- #define SHCLK_0 P1OUT &=~BIT2
- #define STCLK_1 P1OUT |=BIT1 //P1.1 锁存输入信号
- #define STCLK_0 P1OUT &=~BIT1
- #define DataLine_1 P1OUT |= BIT0 //P1.0 数据输入
- #define DataLine_0 P1OUT &=~BIT0
- //共“阳”极数码管的码表
- unsigned char LED7CA[] ={~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,
- ~0x07,~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};
- //共“阴”极数码管的码表
- unsigned char LED7CC[] ={0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,
- 0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
- //初始化74HC595的端口
- void HC595_Init()
- {
- P1SEL = 0;
- P1DIR |= BIT0+BIT1+BIT2; //使用P1.0 P1.1 P1.3
- }
- /********************************************************
- 名 称:HC595_SendData()
- 功 能:向74HC595 的SPI总线发送一字节数据
- 参 数:val:数码管的段码
- ********************************************************/
- void HC595_SendData(unsigned char val)
- {
- unsigned int i;
- for(i=0;i<8;i++)
- { //从最高位开始各个位依次发送
- if((val<<i)&0x80)
- {
- DataLine_1;
- }
- else DataLine_0;
- SHCLK_0;
- _NOP();
- SHCLK_1;
- }
- STCLK_0;
- _NOP();
- STCLK_1; //锁存一个字节,数码管显示
- }
-
- //测试代码
- #include "msp430x44x.h"
- #include "MSP430_74HC595.c"
- void main( void )
- {
- unsigned char i,led;
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- FLL_CTL0 |= XCAP18PF;
-
- HC595_Init();
- STCLK_0;
- led=0xfe;
-
- while(1)
- {
-
-
- HC595_SendData(led);
- __delay_cycles(200000);//0.2s
- led <<= 1;
- led |= BIT0;
- if(led==0xff)led=0xfe;
-
- /*
- for(i=0;i<16;i++)
- {
- HC595_SendData(LED7CA[i]);
- __delay_cycles(500000);
- } */
- }
- }
复制代码 MSP430全系列单片机 74HC165/74LS165驱动程序- /********************************************************
- *HC74165.c 74HC165 for MSP430各系列单片机
- *74HC165:8位并入串出移位寄存器,可以接8个按键
- *by 追梦 2011-3-19
- *P1.0 QH输出;P1.1 SHIFT/LOAD;P1.2 CLOCK & CLOCK INHIBIT
- ********************************************************/
- #define QH BIT0
- #define SHIFT BIT1
- #define CLOCK BIT2
- #define HC165SEL P1SEL
- #define HC165DIR P1DIR
- #define HC165OUT P1OUT
- #define HC165IN P1IN
- //74HC165与单片机的连接端口初始设置
- void HC74165_Init()
- {
- HC165SEL &= ~(QH+SHIFT+CLOCK);
- HC165DIR |= SHIFT + CLOCK;
- HC165DIR &= ~QH;
- }
- /********************************************************
- *名 称:GetHC74165()
- *功 能:锁存74HC165的8位输入,并逐位读出
- *出 口:读取的8位状态数据,放在整型数据的低八位
- ********************************************************/
- unsigned int GetHC74165(void)
- {
- unsigned char i;
- unsigned int indata=0;
- HC165OUT &= ~SHIFT;
- _NOP();
- HC165OUT |= SHIFT;
- _NOP();
- _NOP();
- for(i=0;i<8;i++)
- {
- indata <<= 1;
- if(HC165IN & QH) indata += 1;
- HC165OUT |= CLOCK;
- _NOP();
- HC165OUT &= ~CLOCK;
- }
- return indata;
- }
-
- //测试代码
- #include "msp430x44x.h"
- #include "HC74165.c"
- void main( void )
- {
- unsigned int key;
- // Stop watchdog timer to prevent time out reset
- WDTCTL = WDTPW + WDTHOLD;
- FLL_CTL0 |= XCAP18PF;
-
- HC74165_Init();
- P2DIR = 0xff;
- P2OUT = 0xff;
-
- while(1)
- {
- if((key=GetHC74165())!=0xff)
- P2OUT = key;
- }
-
- }
复制代码
|
|