| 1 | /*= ex4c.c =====================================================================
|
| 2 | *
|
| 3 | * Copyright (C) 2004 Nordic Semiconductor
|
| 4 | *
|
| 5 | * This file is distributed in the hope that it will be useful, but WITHOUT
|
| 6 | * WARRANTY OF ANY KIND.
|
| 7 | *
|
| 8 | * Author(s): Ole Saether
|
| 9 | *
|
| 10 | * DESCRIPTION:
|
| 11 | *
|
| 12 | * This example shows how to read the LM35 temperature sensor on the EVBOARD and
|
| 13 | * output the value in hexadecimal form to the serial port.
|
| 14 | *
|
| 15 | * COMPILER:
|
| 16 | *
|
| 17 | * This program has been tested with Keil V7.07a.
|
| 18 | *
|
| 19 | * $Revision: 2 $
|
| 20 | *
|
| 21 | *==============================================================================
|
| 22 | */
|
| 23 | #include <Nordic\reg9e5.h>
|
| 24 |
|
| 25 | const char hex_tab[] = "0123456789ABCDEF";
|
| 26 |
|
| 27 | void Delay100us(volatile unsigned char n)
|
| 28 | {
|
| 29 | unsigned char i;
|
| 30 | while(n--)
|
| 31 | for(i=0;i<35;i++)
|
| 32 | ;
|
| 33 | }
|
| 34 |
|
| 35 | unsigned char SpiReadWrite(unsigned char b)
|
| 36 | {
|
| 37 | EXIF &= ~0x20; // Clear SPI interrupt
|
| 38 | SPI_DATA = b; // Move byte to send to SPI data register
|
| 39 | while((EXIF & 0x20) == 0x00) // Wait until SPI hs finished transmitting
|
| 40 | ;
|
| 41 | return SPI_DATA;
|
| 42 | }
|
| 43 |
|
| 44 | void InitADC(void)
|
| 45 | {
|
| 46 | // Configure and turn on ADC
|
| 47 | RACSN = 0;
|
| 48 | SpiReadWrite(WAC); // Write to ADC config
|
| 49 | SpiReadWrite(0x35); // Select AIN3, PWR_UP = 1, VFSEL=0;
|
| 50 | SpiReadWrite(0x0b); // Set RES_CTRL = 3, Right justified
|
| 51 | RACSN = 1;
|
| 52 | }
|
| 53 |
|
| 54 | void Init(void)
|
| 55 | {
|
| 56 | unsigned char cklf;
|
| 57 |
|
| 58 | TH1 = 243; // 19200@16MHz (when T1M=1 and SMOD=1)
|
| 59 | CKCON |= 0x10; // T1M=1 (/4 timer clock)
|
| 60 | PCON = 0x80; // SMOD=1 (double baud rate)
|
| 61 | SCON = 0x52; // Serial mode1, enable receiver
|
| 62 | TMOD = 0x20; // Timer1 8bit auto reload
|
| 63 | TR1 = 1; // Start timer1
|
| 64 | P0_ALT |= 0x06; // Select alternate functions on pins P0.1 and P0.2
|
| 65 | P0_DIR |= 0x02; // P0.1 (RxD) is input
|
| 66 |
|
| 67 | SPICLK = 0; // Max SPI clock
|
| 68 | SPI_CTRL = 0x02; // Connect internal SPI controller to Radio
|
| 69 |
|
| 70 | // Switch to 16MHz clock:
|
| 71 | RACSN = 0;
|
| 72 | SpiReadWrite(RRC | 0x09);
|
| 73 | cklf = SpiReadWrite(0) | 0x04;
|
| 74 | RACSN = 1;
|
| 75 | RACSN = 0;
|
| 76 | SpiReadWrite(WRC | 0x09);
|
| 77 | SpiReadWrite(cklf);
|
| 78 | RACSN = 1;
|
| 79 | InitADC();
|
| 80 | }
|
| 81 |
|
| 82 | void PutChar(char c)
|
| 83 | {
|
| 84 | while(!TI)
|
| 85 | ;
|
| 86 | TI = 0;
|
| 87 | SBUF = c;
|
| 88 | }
|
| 89 |
|
| 90 | void PutString(const char *s)
|
| 91 | {
|
| 92 | while(*s != 0)
|
| 93 | PutChar(*s++);
|
| 94 | }
|
| 95 |
|
| 96 | void HexOutNib(unsigned char n)
|
| 97 | {
|
| 98 | PutChar(hex_tab[n & 0x0f]);
|
| 99 | }
|
| 100 |
|
| 101 | void HexOutByte(unsigned char b)
|
| 102 | {
|
| 103 | HexOutNib(b >> 4);
|
| 104 | HexOutNib(b & 0x0f);
|
| 105 | }
|
| 106 |
|
| 107 | void main(void)
|
| 108 | {
|
| 109 | unsigned char adc0, adc1, i;
|
| 110 | Init();
|
| 111 | for (;;)
|
| 112 | {
|
| 113 | // Start ADC conversion
|
| 114 | RACSN = 0;
|
| 115 | SpiReadWrite(SAV | 0x03);
|
| 116 | RACSN = 1;
|
| 117 |
|
| 118 | // Wait until ADC conversion completes
|
| 119 | while(EOC == 0)
|
| 120 | ;
|
| 121 | // Read Out ADC Value
|
| 122 | RACSN = 0;
|
| 123 | SpiReadWrite(RAD);
|
| 124 | adc0 = SpiReadWrite(NOP);
|
| 125 | adc1 = SpiReadWrite(NOP);
|
| 126 | RACSN = 1;
|
| 127 | HexOutByte(adc1);
|
| 128 | HexOutByte(adc0);
|
| 129 | PutString("\r\n");
|
| 130 | // Wait a while
|
| 131 | for (i=0;i<10;i++)
|
| 132 | Delay100us(255);
|
| 133 | }
|
| 134 | }
|