Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "stdio.h"
- #include "stdlib.h"
- #include "display.h"
- #include "addons.h"
- #include<msp430x14x.h>
- #define DALLAS BIT7
- #define OUT_DALLAS (P1DIR |= DALLAS)
- #define INP_DALLAS (P1DIR &= ~DALLAS)
- #define SET_DALLAS (P1OUT |= DALLAS)
- #define CLR_DALLAS (P1OUT &= ~DALLAS)
- #define SKIP_ROM 0xCC
- #define CONVERT_T 0x44
- #define READ_SCRATCHPAD 0xBE
- #define POS 0x03
- #define DEBUG 1
- /* Inicjalizacja przed wysłaniem komendy ROM */
- unsigned char resetPulse() {
- P1DIR |= BIT7; //tryb wyjściowy
- P1OUT &= ~BIT7; // magistrale w poziom niski
- __Delayx1us(480); // 480 us na odebranie RESET PULSE
- P1DIR |= BIT7; // port Dallas ustawiany w tryb wyjściowy
- P1OUT |= BIT7; // ustawiamy magistrale w poziom wysoki
- __Delayx1us(50); // odczekaj 30 us
- P1DIR &= ~BIT7; // port Dallas ustawiany w tryb wejściowy
- if (! (P1IN & BIT7)) // odebrano sygnał‚ presence(0)
- ;
- else // nie odebrano sygnału presence
- return 1;
- __Delayx1us(470); // odczekaj 470µs
- if(P1IN & BIT7)
- return 0; // inicjalizacja wykonana poprawnie
- else
- return 2; // inicjalizacja nie powiodla sie
- }
- /* wysyla pojedynczy bit */
- void send(unsigned char bit) {
- P1DIR |= BIT7; // magistrala w trybie wyjściowym
- P1OUT &= ~BIT7; // zero na wyjście
- __Delayx1us(5);
- if (bit==1) { // wysylana 1
- P1OUT |= BIT7; // jedynka na wyjście
- }
- __Delayx1us(80);
- P1OUT |= BIT7;
- }
- /* Odbiera z układu pojedynczy bit */
- unsigned char read() {
- P1DIR |= BIT7;
- P1OUT &= ~BIT7;
- __Delayx1us(2); // odczekanie 2us
- P1OUT |= BIT7;
- __Delayx1us(2); // odczekanie 2us
- P1DIR &= ~BIT7; // port ustawiany na tryb wejściowy
- if (P1IN & BIT7) // odebrano jedynke
- return 1;
- else return 0; // odebrano zero
- }
- /* Wysyła cały bajt do układu */
- void send_byte(unsigned char wartosc) {
- unsigned char i;
- unsigned char pom;
- for(i=0; i!=8; i++) {
- pom = wartosc>>i;
- pom &= 0x01;
- send(pom);
- }
- __Delayx1us(100); //odczekanie 100us
- }
- unsigned char read_byte(void) {
- unsigned char i;
- unsigned char wartosc =0;
- for(i=0; i!=8; i++) {
- if (read()) wartosc |= (0x01<<i);
- __Delayx1us(15);
- }
- return wartosc;
- }
- void pullUpDQ() {
- P1DIR |= BIT7;
- P1OUT |= BIT7;
- }
- int main() {
- WDTCTL = WDTPW | WDTHOLD;
- IE1 |= WDTIE; // aktywuje przerwania NMI
- _EINT();
- InitOsc2(8, 1); // MCLK=8MHz, ACLK=1MHz
- InitLCD();
- InitPorts2();
- STATUS_LED_OFF;
- CHAR_DD('-', POS);
- printString("--,----");
- SEND_CHAR(0x00);
- SEND_CHAR('C');
- unsigned char t, spr=0, msb=0, lsb=0, prevMSB=0xFF, prevLSB=0xFF, calk;
- unsigned short ulam;
- while (1) {
- spr = resetPulse(); //inicjalizacja
- if (spr == 0) {
- send_byte(SKIP_ROM); // send SKIP ROM command
- send_byte(CONVERT_T); // send CONVERT TEMPERATURE command
- pullUpDQ();
- __Delayx1ms(500);
- __Delayx1ms(250);
- spr = resetPulse();
- if (spr != 0) {
- SEND_CMD(CUR_HOME);
- printString(" [2] Failed ("); SEND_CHAR(spr+'0'); SEND_CHAR(')');
- __Delayx1ms(500);
- __Delayx1ms(500);
- __Delayx1ms(500);
- SEND_CMD(CUR_HOME);
- printString(" ");
- CHAR_DD('-', POS);
- printString("--,----");
- SEND_CHAR(0x00);
- SEND_CHAR('C');
- continue;
- }
- send_byte(SKIP_ROM);
- send_byte(READ_SCRATCHPAD);
- lsb = read_byte();
- msb = read_byte();
- read_byte(); read_byte(); read_byte(); read_byte(); read_byte(); read_byte(); read_byte();
- if ((prevLSB != lsb) || (prevMSB != msb)) {
- prevLSB = lsb;
- prevMSB = msb;
- calk = lsb >> 4;
- calk += (msb & 0x07) << 4;
- if (msb & 0x80) {
- calk ^= 0x7F;
- ulam = (lsb & 0x08) ? 0 : 5000;
- ulam += (lsb & 0x04) ? 0 : 2500;
- ulam += (lsb & 0x02) ? 0 : 1250;
- ulam += (lsb & 0x01) ? 0 : 625;
- }
- else {
- ulam = (lsb & 0x08) ? 5000 : 0;
- ulam += (lsb & 0x04) ? 2500 : 0;
- ulam += (lsb & 0x02) ? 1250 : 0;
- ulam += (lsb & 0x01) ? 625 : 0;
- }
- if (calk < 10) {
- CHAR_DD(' ', POS); SEND_CHAR((msb & 0x80) && (calk || ulam) ? '-' : ' ');
- SEND_CHAR(calk+'0');
- }
- else {
- if (calk < 100) {
- CHAR_DD(((msb & 0x80) ? '-' : ' '), POS);
- SEND_CHAR(calk/10+'0');
- SEND_CHAR(calk%10+'0');
- }
- else {
- t = calk/100;
- CHAR_DD(t+'0', POS);
- calk -= 100 * t;
- SEND_CHAR(calk/10+'0');
- SEND_CHAR(calk%10+'0');
- }
- }
- t = ulam / 1000;
- CHAR_DD(t+'0', POS+4);
- ulam -= t * 1000;
- t = ulam / 100;
- SEND_CHAR(t+'0');
- ulam -= t * 100;
- SEND_CHAR(ulam/10+'0');
- SEND_CHAR(ulam%10+'0');
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement