![]() |
Contents |
| J4 (the serial connector on the TEA board): | standard 9-pin serial RS232 connector: | |||||||||||||
|
|
| The connector to the ICD-programmer | J5 (the programmer connector on the TEA board) | |||||||||||
|
|
|
| 1: ground | 2: 3.3 V |
| The connector of a Nokia GSM | Pinout of the phone connector |
![]() |
|
|
|
|
|
|
| OSC1/CLKIN | 14 | Oscillator HS 20Mhz | |
| OSC2/CLKOUT | 15 | ||
| MCLR/VPP | 2 | master clear | programmer (J5) |
| RA0 | 3 | read | microphone1 |
| RA1 | 4 | read | microphone2 |
| RA2 | 5 | read | photodiode1 |
| RA3 | 6 | read | photodiode2 |
| RA4/T0CKI | 7 | (timer0 clock input) | |
| RA5/SS | 8 | (synchronous serial slave select) | J3 |
| RB0 | 36 | select 0 | accelerometer (SN74HC153 -> ADXL202) |
| RB1 | 37 | select 1 | |
| RB2 | 38 | J1 | |
| RB3 | 39 | J1 | |
| RB4 | 41 | read/write | temperature (DS1820) |
| RB5 | 42 | read | touch |
| RB6 | 43 | serial programming clock | programmer (J5) |
| RB7 | 44 | serial programming data | programmer (J5) |
| RC0/T1OSO/T1CK1 | 16 | (timer1 oscillator i/o) | |
| RC1 | 18 | read | accelerometer (SN74HC153 -> ADXL202) |
| RC2 | 19 | read | |
| RC3/SCK/SCL | 20 | (synchronous serial clock i/o for SPI or I2C) | |
| RC4/SDI/SDA | 25 | (SPI Data in) (data I/O for I2C) | |
| RC5/SDO | 26 | (SPI Data out) | |
| RC6/TX | 27 | transmit | RS232 (MAXIM220E) |
| RC7/RX | 29 | receive | |
| RD0/PSP0 | 21 | (parallel slave port) | J8 |
| RD1/PSP1 | 22 | (parallel slave port) | J8 |
| RD2/PSP2 | 23 | (parallel slave port) | J8 |
| RD3/PSP3 | 24 | (parallel slave port) | J8 |
| RD4/PSP4 | 30 | (parallel slave port) | J8 |
| RD5/PSP5 | 31 | (parallel slave port) | J8 |
| RD6/PSP6 | 32 | (parallel slave port) | J8 |
| RD7/PSP7 | 33 | (parallel slave port) | J8 |
| RE0/RD | 9 | J3 | |
| RE1/WR | 10 | read | touch |
| RE2/CS | 11 | J3 | |
| VSS | 13 | ground | |
| VDD | 12 | positive | |
| VSS | 34 | ground | |
| NC | 1 |
|
|
| NC | 17 | ||
| NC | 28 | ||
| NC | 40 | ||
| To select... | set pin B0 | set pin B1 |
| read X-axis | 0 | 1 |
| read Y-axis | 1 | 1 |
Then, the value can be read from pin C1 or C2 (you choose). However,
that's not it, yet: the values are given through duty cycles. This means
that the values should be calculated by dividing the pulse-width by the
period, eg. for:
| ______
__ _______ ________
_____ _______
____| |______| |_| |____| |_______| |_____| |__
aaaaaaaaaa
|
the fourth incoming value would be calculated by (a/b).
example program in C:
| if (read_X)
output_low(PIN_B0); else // if read_Y output_high(PIN_B1); output_high(PIN_B1); while( input(PIN_C1) ); // wait until low
|
| command: | value: (hex format) |
| skip ROM (for a single bus system) | 0xCC |
| convert temperature (*) | 0x44 |
| Read DS1820's Scratchpad | 0xBE |
(*): note that this takes 500 ms !
You'll need to initialize the DS1820 every time you want to issue a command. This means forcing the pin to the sensor to a low state (=reset pulse) for 480 up to 960 µsecs, after which the DS1820 will send back a presence pulse lasting 60-240 µsecs. Thus, to just get the temperature, you need to do the following:
| void _1w_init(int sensor) {
_1w_pin_hi(sensor); _1w_pin_low(sensor); delay_us(550); // ok. output_float(PIN_B4); delay_us(15); // wait for presence pulse delay_us(285); // wait until presence pulse is over } int _1w_in_byte(int sensor) { int n, i_byte, temp, mask; mask = 0xff & (~(0x01<<sensor)); for (n=0; n<8; n++) { PORTB=0x00; TRISB=mask; TRISB=0xff; #asm CLRWDT NOP NOP #endasm temp=PORTB; if (temp & ~mask) { i_byte=(i_byte>>1) | 0x80; // least sig bit first } else { i_byte=i_byte >> 1; } delay_us(60); } return(i_byte); } void _1w_out_byte(int d, int sensor) { int n, mask; mask = 0xff & (~(0x01<<sensor)); for(n=0; n<8; n++) { if (d&0x01) { PORTB=0; TRISB=mask; // momentary low TRISB=0xff; delay_us(60); } else { PORTB=0; TRISB=mask; delay_us(60); TRISB=0xff; } d=d>>1; } } void _1w_pin_hi(int sensor) { TRISB = 0xff; } void _1w_pin_low(int sensor) { PORTB = 0x00; TRISB = 0xff & (~(0x01 << sensor)); } void _1w_strong_pull_up(int sensor) { PORTB = 0x01 << sensor; TRISB = 0xff & (~(0x01 << sensor)); delay_ms(500); TRISB = 0xff; } |
The read Scratchpad gives 9 (nine!) bytes. The last byte is a
CRC value, which can be used to check if the transmission of the first
8 bytes was flawless. To calculate the temperature, we need only the first
two bytes.
| Byte | Description |
| 0 | Temperature Least Significat Byte (LSB) |
| 1 | Temperature MSB |
| 2 | Temperature High (TH) since last power-up |
| 3 | Temperature Low (TL) since last power-up |
| 4 | <reserved> 0b11111111 |
| 5 | <reserved> 0b11111111 |
| 6 | count remain for obtaining higher resolution |
| 7 | count per °C for obtaining higher resolution |
| 8 | CRC check |
But even then, we have to do some minor calculations on the first two
values before we get the temperature itself: the MSB byte is either 0b00000000
(for positive temperature) or 0b11111111 (for negative temperature). The
first LSB byte is the actual temperature (absolute) value, multiplied by
2 for positive values, and twos complement multiplied by 2 for negative
values. Thus to know the exact temperature, we need to divide the second
byte by 2. Resolution is therefore 1/2°C. So the program to get a normal
temperature value in °C should look like this:
| // get the 9 bytes from DS1820
int i; for (i=0; i<9; i++) { received[i] = inByte(PIN_B4); } // calculate temperature if (received[1] == 1) { // if positive, divide by two value = received[0]/2.0; } else { // else 2's comp and divide by two received[1] = ((received[1] ^ 0xFF))+1; value = received[0]/2.0; } |
It is also possible to enhance the values's resolution, to do the CRC check, or to do something with the TL and TH values. Check the DS1820 datasheet/notes for those.
| int value;
setup_adc_ports(ALL_ANALOG); setup_adc(ADC_CLOCK_INTERNAL); set_adc_channel(0); value = read_adc(); |
*remark: here we just take an 8-bit value, although the AD conversion really outputs a 10-bit value. Change int to long int to cope with this.
For questions, remarks, etc. contact the author at kristof@starlab.net,
or kofi@starlab.net (08/2001: obsolete: use the comp.lancs address)