📄 spi_master.lst
字号:
RC51 COMPILER V03.03.35, SPI_MASTER 07/26/05 15:25:17 PAGE 1
QCW(0x0000EF32)
WIN32 RC51 COMPILER V03.03.35, COMPILATION OF MODULE SPI_MASTER
OBJECT MODULE PLACED IN c:\allmyfiles\msc products\cd-rom\cd next version\exampl
-e programs\msc120x examples\spi_master\spi_master.obj
COMPILER INVOKED BY: RC51.EXE C:\ALLMYFILES\MSC PRODUCTS\CD-ROM\CD NEXT VERSION\
-EXAMPLE PROGRAMS\MSC120X EXAMPLES\SPI_MASTER\SPI_MASTER.C OBJECT(C:
-\ALLMYFILES\MSC PRODUCTS\CD-ROM\CD NEXT VERSION\EXAMPLE PROGRAMS\MS
-C120X EXAMPLES\SPI_MASTER\SPI_MASTER.OBJ) PIN(C:\RIDE\INC) PIN(C:\R
-IDE\INC\51\TI\) NOAM PR(C:\ALLMYFILES\MSC PRODUCTS\CD-ROM\CD NEXT V
-ERSION\EXAMPLE PROGRAMS\MSC120X EXAMPLES\SPI_MASTER\SPI_MASTER.LST)
- CD SB OE(1) HUGE FP(NOFLOAT) PW(80) NOIS UNSIGNEDCHAR ET(CHAR)
stmt level source
1 //****************************************************************
-****
2 // File name: spi_master.c
3 //
4 // Copyright 2003 Texas Instruments Inc as an unpublished work.
- 5 // Created By: Saripalli Ramesh
6 //
7 // Version 1.1 (update by RG on 03/01/2004)
8 //
9 // Compiler Version (Raisonance V6.10.14)
10 //
11 // Module Description:
12 // SPI Master & Slave Program
13 //
14 //
15 //****************************************************************
-****
16 #include "legal.c" //Texas Instruments, Inc. copyright an
-d liability
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include "reg1200.h"
20 #define SET 1
21 #define CLEAR 0
22 #define USEC_val (8-1)
23 #define debug 1
24 #define RX_BEG ((unsigned char data*) 0x60) // Start address of t
-he RX buffer
25 #define RX_END ((unsigned char data*) 0x70) // (End-1) address
-of the RX buffer
26 #define TX_BEG ((unsigned char data*) 0x70) // Start address of t
-he TX buffer
27 #define TX_END ((unsigned char data*) 0x80) // (End-1) address of
- the TX buffer
28 #define autobaud() ((void (code *) (void)) 0xFBFA) () // MSC120
-0
29
30 extern char spim_send_recv_byte(char);
31 extern unsigned long unipolar(void);
32 void stop_spi(void);
33 void init_spi_slave(void);
34 void init_spi_master(void);
35
36 sbit SCLK=P3^6;
37 sbit SDA=P1^2;
38 sbit SSN=P1^1;
39 sbit ADC_CS = P1^0;
40 char data *rxhead, *rxtail, *txhead, *txtail;
41 bit rxbuf_full, txbuf_full, rxbuf_empty, txbuf_empty;
42
43 void reset_pointers(void) {
44 1 rxhead = RX_BEG;
45 1 rxtail=RX_BEG;
46 1 txhead=TX_BEG;
47 1 txtail=TX_BEG;
48 1 rxbuf_full=0;
49 1 txbuf_full=0;
50 1 rxbuf_empty=1;
51 1 txbuf_empty=1;
52 1 }
53
54 //void interrupt_isr(void) interrupt 6 {
55 //char k;
56 //}
57
58 void int2_isr(void) interrupt 8 {
59 1 stop_spi();
60 1 }
WARNING C233 IN LINE 60 OF spi_master.c : using absolute registers together wit
-h interrupts can cause register corruptions (mixup of register bank
-s)
61
62 void int3_isr(void) interrupt 9 {
63 1 init_spi_slave();
64 1 EX2=SET; // Interrupt2 is enabled which is active high, edg
-e sensitive
65 1
66 1 }
67
68 // This routine is to write data to the Rx buffer at the head loca
-tion.
69 // The routine writes the data if there is space available in the
-buffer, if not the data is trashed.
70 void rxbuf_write(char in_data) {
71 1 if(rxbuf_full==1) { // RX Buffer full so the received data
-is trashed
72 2 if(debug) printf("Data overflow than the buffer\n");
73 2 }
WARNING C043 IN LINE 72 OF spi_master.c : Condition always true !
74 1 else { // Space available in the RX buffer.
75 2 *rxhead=in_data; // Write the input data to the rxhead poin
-ter
76 2 rxhead++; // Increment the rx head pointer
77 2 rxbuf_empty=0; // Irrepectively of the rxbuf_empty value s
-et it to zero
78 2 if(rxhead==RX_END) { rxhead=RX_BEG;} // If the rxhead is equal t
-o the end then set it back to the begining
79 2 if(rxhead==rxtail) { rxbuf_full=1;} // If the head and tail are
- equal then it means that the buffer is full
80 2 }
81 1 }
82
83 // This subroutine is to read data from the RX buffer from the tai
-l location.
84 // If the buffer is empty then the routine waits till a new byte i
-s received.
85 char rxbuf_read(void) {
86 1 char tmp_data;
87 1 while(rxbuf_empty==1) { } // RX buffer is empty so we wait til
-l we receive a byte
88 1 EAI=0; // Disable the interrupt so that the read is not
-interrupted.
89 1 tmp_data=*rxtail; // Store the data in the tmp_d
-ata before passing it
90 1 rxtail++; // Increment the RX tail pointe
-r
91 1 rxbuf_full=0; // Since one byte is consumed the rx buffer
-is not full, irrespective of what its previous value
92 1 if(rxtail==RX_END) { rxtail=RX_BEG; } // If tail reaches end it i
-s moved to the begning
93 1 if(rxtail==rxhead) { rxbuf_empty=1; } // If tail and head are the
- same then the rx buf is empty.
94 1 EAI=1; // Enable the interrupts
95 1 return(tmp_data);
96 1 }
97
98 // This routine is to write data to the Tx buffer at the head loca
-tion.
99 // The routine waits till the tx buffer is not full.
100 void txbuf_write(char in_data) {
101 1 while(txbuf_full==1) { } // Wait untill the Tx Buffer is not
-full.
102 1 EAI=0; // Disable the interrupt so that the write is not
- interrupted.
103 1 *txhead=in_data; // Write the input data to the txhead poin
-ter
104 1 txhead++; // Increment the tx head pointer
105 1 txbuf_empty=0; // Irrepectively of the txbuf_empty value s
-et it to zero
106 1 if(txhead==TX_END) { txhead=TX_BEG;} // If the txhead is equal to
- the end then set it back to the begining
107 1 if(txhead==txtail) { txbuf_full=1;} // If the head and tail are
-equal then it means that the buffer is full
108 1 EAI=1; // Enable the interrupts
109 1 }
110
111 // This subroutine is to read data from the TX buffer from the tai
-l location.
112 // If the buffer is empty then it passes FFh.
113 char txbuf_read(void) {
114 1 char tmp_data=0xFF;
115 1 if(txbuf_empty==1) { // Tx buffer is empty so we cannot send
- a valid data
116 2 if(debug) printf("Data underflow on Tx buffer\n");
117 2 }
WARNING C043 IN LINE 116 OF spi_master.c : Condition always true !
118 1 else { // When there is data in the TX buffer
119 2 tmp_data=*txtail; // Store the data in the tmp_da
-ta before passing it
120 2 txtail++; // Increment the Tx tail pointe
-r
121 2 txbuf_full=0; // Since one byte is consumed the tx buffer
-is not full, irrespective of what its previous value
122 2 if(txtail==TX_END) { txtail=TX_BEG; } // If tail reaches end it
-is moved to the begning
123 2 if(txtail==txhead) { txbuf_empty=1; } // If tail and head are th
-e same then the tx buf is empty.
124 2 }
125 1 return(tmp_data);
126 1 }
127
128 void init_spi_slave(void) {
129 1 P1DDRL=0xD0; // P12(SOUT)=output and P13(SIN)=Input.
130 1 P3DDRH=0x30; // P36(SCLK)=Output
131 1 PDCON&=0xFE; // Enable SPI/I2C module
132 1 SPICON=0x00; // SPI mode, CPOL=0, SCK idle low.
133 1 reset_pointers();
134 1 SPIDATA=0x00;
135 1 AIE=0x04;
136 1 AI=CLEAR;
137 1 EAI=SET;
138 1 }
139
140 void stop_spi(void) {
141 1 P1DDRL=0x00; // P12(SOUT)=output and P13(SIN)=Input.
142 1 P3DDRH=0x00; // P36(SCLK)=Output
143 1 PDCON|=0x01; // Disable SPI/I2C module
144 1 P1=0xFF;
145 1 P3=0xFF;
146 1 }
147
148 void init_spi_master(void) {
149 1 SSN=SET; // Disable the slave
150 1 P1DDRL=0xD4; // P11(SSN)=output,P12(SOUT)=output and P13(SIN)
-=Input.
151 1 P3DDRH=0x10; // P36(SCLK)=Output
152 1 PDCON&=0xFE; // Enable SPI/I2C module
153 1 SPICON=0x00; // SPI mode, CPOL=0, SCK idle low.
154 1 SCLK=CLEAR;
155 1 reset_pointers();
156 1 }
157
158 // This routine takes in a byte and sends it on SPI bus. The recei
-ved data is read and is written to the RX buffer.
159 void spim_send_recv_data ( char tx_data ) {
160 1 char k;
161 1 k=tx_data;
162 1 rxbuf_write(spim_send_recv_byte(k));
163 1 }
164
165 #define LedOn 0
166 #define LedOff 1
167 sbit RedLed = P3^4;
168 sbit YelLed = P3^5;
169
170 void adc_spi_dacout (void)
171 {
172 1 unsigned short int data_value;
173 1 unsigned int adc16, value0, value1, value2;
174 1 char i;
175 1 data_value=unipolar();
176 1 ADC_CS = CLEAR;
177 1 SSN = SET; // Sync pluse for the DAC's SPI
178 1 SSN = CLEAR;
179 1 // Note: To avoid SPI overflow, sufficient delay between
180 1 // every 3 SPIDATA write is needed.
181 1 value0 = spim_send_recv_byte(0x00); // Normal DAC Mode
182 1 value1 = spim_send_recv_byte((char)(data_value>>8 & 0x000ff)); //
- Send the Hi-Byte of the ADC result
183 1 value2 = spim_send_recv_byte((char)(data_value & 0x000ff)); //
-Send the middle-Byte of the ADC result
184 1 ADC_CS = SET;
185 1 reset_pointers();
186 1 adc16 = value0 << 14 | value1 << 6 | value2 >> 2;
187 1 printf("V=%6.4f\ti=%d\r",adc16*2.5/65536, i);
188 1 IDAC = i++;
189 1 if (i==0){
190 2 YelLed = RedLed;
191 2 RedLed = !RedLed;
192 2 }
193 1 }
194
195
196 void main(void) {
197 1
198 1 int i, sel;
199 1 char c;
200 1 // AutoBaud routine
201 1 CKCON = 0x10; // 0 MOVX cycle stretch and clk/4
202 1 TCON=0x00;
203 1 autobaud();
204 1 // End of Autobaud
205 1 printf("\x1b[2J");
206 1 printf("\t\t\t\tMSC1200 SPI Master/Slave \n");
207 1 printf("1)Master 2)Slave\n");
208 1 scanf("%d", &sel);
209 1 if(sel==1) {
210 2 printf("SPI Master \n");
211 2 init_spi_master();
212 2 PDCON &= 0xB7; // Turn on IDAC, ADC-Vref
213 2 // ADC settings
214 2 ACLK = 1; // ACLK = 8MHz/(1+1)= 4MHz
215 2 DECIMATION = 150; // Data Rate = ACLK/64/Decimation ~ 416Hz
216 2 ADMUX = 0x08; // +ve connected to AIN0, -ve connected to AIN
-COM
217 2 ADCON0 = 0x30; // BOD off, Internal Vref @ 2.5V, Buff off, P
-GA 1
218 2 ADCON1 = 0x41; // Unipolar, Auto filter setting, self cal
-ibration for offset and gain
219 2 while(1) { adc_spi_dacout(); }
220 2
221 2 }
222 1 else {
223 2 printf("SPI Slave \n");
224 2 EX3=SET; // Interrupt3 is enabled which is active low, ed
-ge sensitive
225 2 EA=SET; // The external interrupts are enabled.
- 226 2 }
227 1
228 1 while(1) { }
229 1 }
WARNING C092 IN LINE 198 OF spi_master.c : 'i' is declared but not used
WARNING C092 IN LINE 199 OF spi_master.c : 'c' is declared but not used
230
231
232
233
RC51 COMPILER V03.03.35, SPI_MASTER 07/26/05 15:25:17 PAGE 2
ASSEMBLY LISTING OF GENERATED OBJECT CODE
; FUNCTION reset_pointers (BEGIN)
; SOURCE LINE # 51
0000 120000 R LCALL SH_NINR?C
; SOURCE LINE # 44
0003 7460 MOV A,#060H
0005 900000 R MOV DPTR,#rxhead
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -