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
91
92
93
94
95
96
97
98
99
100
101
102
| #include <xbee_config.h>
#include <types.h>
#define BUFLEN_C 1
#define BUFLEN_D 3
uint8_t txbuf; //transit buffer
uint8_t rxbuf[BUFLEN_D]; //receive buffer
uint8_t dummybuf[BUFLEN_D]; //Buffer with dummy values
bool_t Flag = 0; //Real time clock flag: will be changed to real time later
void ADC_SPI(void);
void delay(int num);
void delay(int num)
{
int j;
int k;
for(j=0;j<=num;j++){
while(k<256){
k++;
}
k=0;
}
}
void ADC_SPI(void)
{
uint32_t value = 0;
int i;
printf("\nCommence Setup");
dummybuf[0] = 0xFF;
dummybuf[1] = 0xFF;
dummybuf[2] = 0xFF;
rxbuf[0] = 0xFF;
rxbuf[1] = 0xFF;
rxbuf[2] = 0xFF;
txbuf = (uint8_t)32; //0x20 set comm reg to write to high filter reg
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
txbuf = (uint8_t)64; //0x40 set high fil reg so 24bits and no filter
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
txbuf = (uint8_t)48; //0x30 set comm reg to write to low filter reg
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
txbuf = (uint8_t)0; //0x00 set low fil reg so there is no filter
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
txbuf = (uint8_t)16; //0x10 set comm reg to write to mode reg
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
txbuf = (uint8_t)0; //0x00 set mode reg for normal mode no gain
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
printf("\nFinish Setup\nWaiting for DRDY to go LOW...");
for(;;){
if ((gpio_get(XPIN_7))==0){
printf("\nDRDY is LOW!");
txbuf= (uint8_t)88; //0x58 set comm reg to read from data reg and at Ain 1/6
(void)spi_transfer(0,txbuf,NULL,BUFLEN_C);
delay(100);
(void)spi_transfer(0,dummybuf,rxbuf, BUFLEN_D); //read
delay(100);
printf("\n");
for (i = 0; i < BUFLEN_D; i++) {
printf("%d", rxbuf[i]);
}
return;
}
}
}
#if defined(RTC_ENABLE_PERIODIC_TASK)
void rtc_periodic_task(void)
{
Flag = 1;
}
#endif
void main(void)
{
int count = 1;
sys_hw_init();
sys_xbee_init();
sys_app_banner();
printf("\nStarting");
for (;;) {
if(Flag ==1 ){
printf("\nADC Selected %d times",count);
count++;
ADC_SPI();
printf("\nDone");
Flag = 0;
}
delay_ticks(HZ);
sys_watchdog_reset();
sys_xbee_tick();
}
} |