?? ch452.lst
字號(hào):
1: /********************************************************************************************
2: CH452驅(qū)動(dòng)程序
3: 與單片機(jī)接口方式:4線
4: ********************************************************************************************/
5: #include <pic.h>
6:
7: typedef unsigned int uint;
8: typedef unsigned char uchar;
9: unsigned int Led_seg[]={0xFC,0x60,0xDA,0xF2,0x66,0xB6,0xBE,0xE0,0xFE,0xF6,
10: 0xEE,0x3E,0x9C,0x7A,0x9E,0x8E,0x02,0x01}; //*0,1,2,3,4,5,6,7,8,9,A,b,C,d,E,F,-,.的字符碼*/
11: unsigned int Bitmap[]={0xc00,0xd00,0xe00,0xf00,0x800,0x900,0xa00,0xb00}; //顯示用位碼列表
12: //管腳定義
13: #define CH452_DCLK RD0 // 串行數(shù)據(jù)時(shí)鐘上升沿激活
14: #define CH452_DIN RD1 // 串行數(shù)據(jù)輸出,接CH452的數(shù)據(jù)輸入
15: #define CH452_LOAD RD2 // 串行命令加載,上升沿激活
16: #define CH452_DOUT RB0 // INT0,鍵盤中斷和鍵值數(shù)據(jù)輸入,接CH452的數(shù)據(jù)輸出
17: // 4線接口的位操作,與單片機(jī)有關(guān)
18: #define CH452_DCLK_SET (CH452_DCLK=1)
19: #define CH452_DCLK_CLR (CH452_DCLK=0)
20: #define CH452_DIN_SET (CH452_DIN=1)
21: #define CH452_DIN_CLR (CH452_DIN=0)
22: #define CH452_LOAD_SET (CH452_LOAD=1)
23: #define CH452_LOAD_CLR (CH452_LOAD=0)
24: // 與單片機(jī)有關(guān),與中斷連接方式有關(guān)
25: #define DISABLE_INTERRUPT (INTE=0)
26: #define ENABLE_INTERRUPT (INTE=1)
27: #define CLEAR_INTER_FLAG (INTF=0)
28:
29: //命令碼
30: #define CH452_NOP 0x0000 // 空操作
31: #define CH452_RESET 0x0201 // 復(fù)位
32: #define CH452_LEVEL 0x0100 // 加載光柱值
33: #define CH452_CLR_BIT 0x0180 // 段位清0
34: #define CH452_SET_BIT 0x01C0 // 段位置1
35: #define CH452_SLEEP 0x0202 // 進(jìn)入睡眠狀態(tài)
36: #define CH452_LEFTMOV 0x0300 //設(shè)置移動(dòng)方式-左移
37: #define CH452_LEFTCYC 0x0301 //設(shè)置移動(dòng)方式-左循
38: #define CH452_RIGHTMOV 0x0302 //設(shè)置移動(dòng)方式-右移
39: #define CH452_RIGHTCYC 0x0303 //設(shè)置移動(dòng)方式-右循
40: #define CH452_SELF_BCD 0x0380 //自定義BCD碼
41: #define CH452_SYSOFF 0x0400 //關(guān)顯示、鍵盤(設(shè)置系統(tǒng)參數(shù))
42: #define CH452_SYSON1 0x0401 //開顯示 (設(shè)置系統(tǒng)參數(shù))
43: #define CH452_SYSON2 0x0403 //開顯示、鍵盤 (設(shè)置系統(tǒng)參數(shù))
44: #define CH452_SYSON2W 0x0423 //開顯示、鍵盤, 真正2線接口 (設(shè)置系統(tǒng)參數(shù))
45: #define CH452_DSP 0x0500 //設(shè)置默認(rèn)顯示方式 (設(shè)置顯示參數(shù))
46: #define CH452_BCD 0x0580 //設(shè)置BCD譯碼方式
47: #define CH452_TWINKLE 0x0600 //設(shè)置閃爍控制
48: #define CH452_GET_KEY 0x0700 // 獲取按鍵
49:
50:
51: void InitCh452();
52: void WrietCh452(uint da);
53: void DelayUs();
54: void Bit_Display_Num(uint bite,uint buf);
55: void Display_Reset();
56: void Flash_SingleBit(uint bitnumber);
57: uint ReadCh452();
58: void Flash_AllBit();
59: void display(uint keyda);
60: void IntKey();
61: //系統(tǒng)初始化函數(shù)*/
62: void initial()
63: {
64: INTCON=0x00;
65: ADCON1=0x07;
66: PIE1=0;
67: PIE2=0;
68: }
69:
70: /********************************************************************************************
71: 初始化
72: ********************************************************************************************/
73: void InitCh452()
74: {
75: WrietCh452(CH452_SYSON2); //打開顯示
76: WrietCh452(CH452_DSP); //設(shè)置默認(rèn)方式
77: ENABLE_INTERRUPT;
78: //Display_Reset();
79: }
80:
81: /********************************************************************************************
82: 向CH452發(fā)送指令和數(shù)據(jù)
83: da;指令和數(shù)據(jù)
84: ********************************************************************************************/
85: void WrietCh452(uint da)
86: {
87: uint iq0;
88: DISABLE_INTERRUPT; //關(guān)閉鍵盤中斷
89: CH452_LOAD_CLR; //復(fù)位LOAD信號(hào)
90: for(iq0=0;iq0<12;iq0++)
91: {
92: if((da&0x0001)==1)
93: CH452_DIN_SET;
94: else
95: CH452_DIN_CLR;
96: CH452_DCLK_CLR;
97: da >>=1;
98: CH452_DCLK_SET; //CH452上升沿讀取數(shù)據(jù)
99: }
100: CH452_LOAD_SET; //加載數(shù)據(jù),LOAD上升沿
101: DelayUs();
102: ENABLE_INTERRUPT; //打開鍵盤中斷
103: }
104: /*--------------------------------------------------------------------------------------------
105: 單個(gè)位上顯示數(shù)據(jù)
106: ---------------------------------------------------------------------------------------------*/
107: void Bit_Display_Num(uint bite,uint buf)
108: {
109: WrietCh452(Bitmap[bite]+Led_seg[buf]);
110: }
111: /****************************************************************
112: 顯示復(fù)位
113: ****************************************************************/
114: void Display_Reset()
115: {
116:
117: WrietCh452(0x201);
118: }
119: /*--------------------------------------------------------------------------------------------
120: 單個(gè)位的閃爍操作
121: ---------------------------------------------------------------------------------------------*/
122: void Flash_SingleBit(uint bitnumber)
123: {
124: WrietCh452(CH452_TWINKLE+(0x01<<bitnumber));
125: }
126: /*--------------------------------------------------------------------------------------------
127: 全閃操作
128: ---------------------------------------------------------------------------------------------*/
129: void Flash_AllBit()
130: {
131: WrietCh452(CH452_TWINKLE+0xff);
132: }
133: /***************************************************************************************
134: 從CH452讀取按鍵值
135: 返回:讀取的按鍵值
136: ***************************************************************************************/
137: uint ReadCh452()
138: {
139: uchar q0;
140: uint cmd,keycode; //定義命令字,和數(shù)據(jù)存儲(chǔ)器
141: DISABLE_INTERRUPT; //禁止鍵盤中斷,防止傳輸過程中被CH452中斷而進(jìn)入中斷服務(wù)程序中再次傳輸
142: cmd=0x07; //輸入讀451命令字
143: CH452_LOAD_CLR;
144: for(q0=0;q0<4;q0++)
145: {
146: if ((cmd&0x1)==1)
147: CH452_DIN_SET;
148: else
149: CH452_DIN_CLR; //輸出位數(shù)據(jù)
150: CH452_DCLK_CLR;
151: cmd>>=1; //向右移一位
152: CH452_DCLK_SET; //產(chǎn)生時(shí)鐘上升沿鎖通知CH451輸入位數(shù)據(jù)
153: }
154: CH452_LOAD_SET; //產(chǎn)生加載上升沿通知CH451處理命令數(shù)據(jù)
155: keycode=0; //清除按鍵碼
156: for(q0=0;q0<7;q0++)
157: {
158: keycode<<=1; //數(shù)據(jù)移入keycode,高位在前,低位在后
159: keycode|=CH452_DOUT; //從高到低讀入451的數(shù)據(jù)
160: CH452_DCLK_CLR; //產(chǎn)生時(shí)鐘下降沿通知CH451輸出下一位
161: CH452_DCLK_SET;
162: }
163: CLEAR_INTER_FLAG; //清中斷標(biāo)志
164: ENABLE_INTERRUPT;
165: return(keycode); //返回鍵值
166: }
167: void display(uint keyda)
168: {
169: Bit_Display_Num(0,keyda);
170: }
171: /***************************************************************************************
172: 按鍵中斷函數(shù)
173: ***************************************************************************************/
174: void interrupt IntKey()
175: {
176: uint key;
177: key=ReadCh452();
178:
179: display(key);
180: }
181: /***************************************************************************************
182: 延時(shí)
183: ***************************************************************************************/
184: void DelayUs()
185: {
186: asm("nop");
187: asm("nop");
188: asm("nop");
189:
190: }
191:
192: void main()
193: {
194: uint i;
195: TRISD=0x00;
196: CLEAR_INTER_FLAG;
197: initial();
198: InitCh452();
199: GIE=1;
200: for(i=0;i<8;i++)
201: {
202: Bit_Display_Num(i,i);
203: }
204:
205: }
206:
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -