DS接MOSI,OE/GND接GND,SH_CP接SCLK,ST_CP接使能信号BIT0@P1,MR/VCC接POWER,如果不需要16位,改US16B,不使用H寄存器即可,还有SPI工作期间可以进入低功耗,也可以执行指令.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
|
#include <msp430.h>
void
main(
void
)
{
WDTCTL = WDTPW + WDTHOLD;
P1DIR |= BIT0 + BIT1;
P1OUT &= ~BIT0;
USICTL0 |= USIPE6 + USIPE5 + USIMST + USIOE;
USICTL1 |= USIIE;
USICKCTL = USIDIV_7 + USISSEL_2;
USICTL0 &= ~USISWRST;
while
(1)
{
P1OUT |= BIT0;
USISRH = 0xAA;
USISRL = 0xAA;
USICNT = 0x10 + USI16B;
// 16位数,级联可用.
while
((USICTL1 & USIIFG) != 0x01){
//此处可以干别的
//这里写入与SPI无关的代码,共8*16=128条单周期指令.
}
USICTL1 &= ~USIIFG;
P1OUT &= ~BIT0;
}
}
|
单片机74HC595模块驱动程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
//74HC595 LED控制
#include <reg51.h>
#include <intrins.h>
#define NOP() _nop_()
sbit MOSIO=P3^4;
sbit R_CLK=P3^5;
sbit S_CLK=P3^6;
void
delay(unsigned
int
i);
void
HC595SendData(unsigned
char
SendVal);
main( )
{unsigned
char
Led=0xfe;
HC595SendData(0xff);
while
(1)
{ HC595SendData(Led);
Led<<=1;
Led =Led|0x01;
if
(Led==0xff)Led=0xfe;
delay(200);
}
}
void
delay(unsigned
int
i)
{
unsigned
int
j;
for
(i;i>0;i--)
for
(j=300;j>0;j--);
}
void
HC595SendData(unsigned
char
SendVal)
{
unsigned
char
i;
for
(i=0;i<8;i++)
{
if
((SendVal<<i)&0x80)MOSIO=1;
else
MOSIO=0;
S_CLK=0;
NOP();
NOP();
S_CLK=1;
}
R_CLK=0;
NOP();
NOP();
R_CLK=1;
}
|
74HC595驱动静态数码管程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
|
#include<reg51.h>
#include<intrins.h>
#defineNOP()_nop_()
sbitMOSIO=P3^4;
sbitR_CLK=P3^5;
sbitS_CLK=P3^6;
voiddelay(unsignedinti);
voidHC595SendData(unsignedcharSendVal);
unsignedcharcodeLED7Code[]=
{~0x3F,~0x06,~0x5B,~0x4F,~0x66,~0x6D,~0x7D,~0x07,
~0x7F,~0x6F,~0x77,~0x7C,~0x39,~0x5E,~0x79,~0x71};
main()
{unsignedcharHC595SendVal;
unsignedintLedNumVal;
while
(1)
{ LedNumVal++;
HC595SendVal=LED7Code[LedNumVal%16]; HC595SendData(HC595SendVal);
delay(200);
}
}
voiddelay(unsignedinti)
{unsignedintj;
for
(i;i>0;i--)
for
(j=300;j>0;j--);
}
voidHC595SendData(unsignedcharSendVal)
{
unsignedchari;
for
(i=0;i<8;i++)
{
if
((SendVal<<i)&0x80)MOSIO=1;
elseMOSIO=0;
S_CLK=0;
NOP();
NOP();
S_CLK=1;
}
R_CLK=0;
NOP();
NOP();
R_CLK=1;
}
|
双595驱动点阵程序
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
|
#include<reg51.h>
#include<intrins.h>
#defineNOP()_nop_()
sbitMOSIO=P3^7;
sbitR_CLK=P3^5;
sbitS_CLK=P3^6;
sbiten573=P1^3;
sbitends=P1^2;
voidHC595SendData(unsignedintSendVal);
unsignedintVal;
unsignedcharcodetab[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsignedcharcodedigittab[18][8]={
{0x00,0x00,0x3e,0x41,0x41,0x41,0x3e,0x00},
//0
{0x00,0x00,0x00,0x00,0x21,0x7f,0x01,0x00},
//1
{0x00,0x00,0x27,0x45,0x45,0x45,0x39,0x00},
//2
{0x00,0x00,0x22,0x49,0x49,0x49,0x36,0x00},
//3
{0x00,0x00,0x0c,0x14,0x24,0x7f,0x04,0x00},
//4
{0x00,0x00,0x72,0x51,0x51,0x51,0x4e,0x00},
//5
{0x00,0x00,0x3e,0x49,0x49,0x49,0x26,0x00},
//6
{0x00,0x00,0x40,0x40,0x40,0x4f,0x70,0x00},
//7
{0x00,0x00,0x36,0x49,0x49,0x49,0x36,0x00},
//8
{0x00,0x00,0x32,0x49,0x49,0x49,0x3e,0x00},
//9
{0x00,0x00,0x7F,0x48,0x48,0x30,0x00,0x00},
//P
{0x00,0x00,0x7F,0x48,0x4C,0x73,0x00,0x00},
//R
{0x00,0x00,0x7F,0x49,0x49,0x49,0x00,0x00},
//E
{0x00,0x00,0x3E,0x41,0x41,0x62,0x00,0x00},
//C
{0x00,0x00,0x7F,0x08,0x08,0x7F,0x00,0x00},
//H
{0x00,0x00,0x00,0xFF,0xFF,0x00,0x00,0x00},
//I
{0x00,0x7F,0x10,0x08,0x04,0x7F,0x00,0x00},
//N
{0x7C,0x48,0x48,0xFF,0x48,0x48,0x7C,0x00}
};
unsignedinttimecount;
unsignedcharcnta;
unsignedcharcntb;
voidmain(
void
)
{
TMOD=0x01;
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
TR0=1;
ET0=1;
EA=1;
cntb=0;
ends=0;
en573=0;
while
(1)
{ }
}
voidt0(
void
)interrupt1using0
{
TH0=(65536-3000)/256;
TL0=(65536-3000)%256;
if
(cntb<18)/
{
//P1=0xFF;
//P2=tab[cnta];
P0=~digittab[cntb][cnta];
Val=tab[cnta]&0x00ff;
Val<<=8;
Val=Val+0x00ff;
HC595SendData(Val);
}
else
{
//P2=0xFF;
//P1=tab[cnta];
P0=~digittab[cntb-18][cnta];
Val=tab[cnta];
Val=Val+0xFF00;
HC595SendData(Val);
}
if
(++cnta>=8)cnta=0;
if
(++timecount>=333)
{timecount=0;
if
(++cntb>=36)cntb=0;
}
}
voidHC595SendData(unsignedintSendVal)
{unsignedchari;
for
(i=0;i<16;i++)
{
if
((SendVal<<i)&0x8000)MOSIO=1;
//setdatalinehigh0X8000
elseMOSIO=0;
//MOSIO=1
S_CLK=0;
NOP();
NOP();
S_CLK=1;
}
R_CLK=0;
//setdatalinelow
NOP();
NOP();
R_CLK=1;
}
|