Advertisement
Guest User

Untitled

a guest
Jan 24th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.99 KB | None | 0 0
  1.  
  2.  
  3. #include "stdio.h"
  4. #include "stdlib.h"
  5. #include "display.h"
  6. #include "addons.h"
  7. #include<msp430x14x.h>
  8.  
  9.  
  10. #define DALLAS BIT7
  11. #define OUT_DALLAS (P1DIR |= DALLAS)
  12. #define INP_DALLAS (P1DIR &= ~DALLAS)
  13. #define SET_DALLAS (P1OUT |= DALLAS)
  14. #define CLR_DALLAS (P1OUT &= ~DALLAS)
  15.  
  16. #define SKIP_ROM 0xCC
  17. #define CONVERT_T 0x44
  18. #define READ_SCRATCHPAD 0xBE
  19.  
  20. #define POS 0x03
  21. #define DEBUG 1
  22.  
  23. /* Inicjalizacja przed wysłaniem komendy ROM */
  24. unsigned char resetPulse() {
  25. P1DIR |= BIT7; //tryb wyjściowy
  26. P1OUT &= ~BIT7; // magistrale w poziom niski
  27.  
  28. __Delayx1us(480); // 480 us na odebranie RESET PULSE
  29.  
  30. P1DIR |= BIT7; // port Dallas ustawiany w tryb wyjściowy
  31. P1OUT |= BIT7; // ustawiamy magistrale w poziom wysoki
  32.  
  33. __Delayx1us(50); // odczekaj 30 us
  34.  
  35. P1DIR &= ~BIT7; // port Dallas ustawiany w tryb wejściowy
  36.  
  37. if (! (P1IN & BIT7)) // odebrano sygnał‚ presence(0)
  38. ;
  39. else // nie odebrano sygnału presence
  40. return 1;
  41.  
  42. __Delayx1us(470); // odczekaj 470µs
  43.  
  44. if(P1IN & BIT7)
  45. return 0; // inicjalizacja wykonana poprawnie
  46. else
  47. return 2; // inicjalizacja nie powiodla sie
  48. }
  49.  
  50. /* wysyla pojedynczy bit */
  51. void send(unsigned char bit) {
  52. P1DIR |= BIT7; // magistrala w trybie wyjściowym
  53. P1OUT &= ~BIT7; // zero na wyjście
  54. __Delayx1us(5);
  55. if (bit==1) { // wysylana 1
  56. P1OUT |= BIT7; // jedynka na wyjście
  57. }
  58. __Delayx1us(80);
  59.  
  60. P1OUT |= BIT7;
  61. }
  62.  
  63. /* Odbiera z układu pojedynczy bit */
  64. unsigned char read() {
  65. P1DIR |= BIT7;
  66. P1OUT &= ~BIT7;
  67. __Delayx1us(2); // odczekanie 2us
  68. P1OUT |= BIT7;
  69. __Delayx1us(2); // odczekanie 2us
  70. P1DIR &= ~BIT7; // port ustawiany na tryb wejściowy
  71. if (P1IN & BIT7) // odebrano jedynke
  72. return 1;
  73. else return 0; // odebrano zero
  74. }
  75.  
  76. /* Wysyła cały bajt do układu */
  77. void send_byte(unsigned char wartosc) {
  78. unsigned char i;
  79. unsigned char pom;
  80.  
  81. for(i=0; i!=8; i++) {
  82. pom = wartosc>>i;
  83. pom &= 0x01;
  84. send(pom);
  85. }
  86. __Delayx1us(100); //odczekanie 100us
  87. }
  88.  
  89.  
  90. unsigned char read_byte(void) {
  91. unsigned char i;
  92. unsigned char wartosc =0;
  93.  
  94. for(i=0; i!=8; i++) {
  95. if (read()) wartosc |= (0x01<<i);
  96. __Delayx1us(15);
  97. }
  98. return wartosc;
  99. }
  100.  
  101.  
  102. void pullUpDQ() {
  103. P1DIR |= BIT7;
  104. P1OUT |= BIT7;
  105. }
  106.  
  107. int main() {
  108. WDTCTL = WDTPW | WDTHOLD;
  109. IE1 |= WDTIE; // aktywuje przerwania NMI
  110. _EINT();
  111. InitOsc2(8, 1); // MCLK=8MHz, ACLK=1MHz
  112. InitLCD();
  113. InitPorts2();
  114.  
  115. STATUS_LED_OFF;
  116.  
  117. CHAR_DD('-', POS);
  118. printString("--,----");
  119. SEND_CHAR(0x00);
  120. SEND_CHAR('C');
  121. unsigned char t, spr=0, msb=0, lsb=0, prevMSB=0xFF, prevLSB=0xFF, calk;
  122. unsigned short ulam;
  123. while (1) {
  124. spr = resetPulse(); //inicjalizacja
  125. if (spr == 0) {
  126.  
  127. send_byte(SKIP_ROM); // send SKIP ROM command
  128. send_byte(CONVERT_T); // send CONVERT TEMPERATURE command
  129. pullUpDQ();
  130. __Delayx1ms(500);
  131. __Delayx1ms(250);
  132. spr = resetPulse();
  133. if (spr != 0) {
  134. SEND_CMD(CUR_HOME);
  135. printString(" [2] Failed ("); SEND_CHAR(spr+'0'); SEND_CHAR(')');
  136. __Delayx1ms(500);
  137. __Delayx1ms(500);
  138. __Delayx1ms(500);
  139. SEND_CMD(CUR_HOME);
  140. printString(" ");
  141. CHAR_DD('-', POS);
  142. printString("--,----");
  143. SEND_CHAR(0x00);
  144. SEND_CHAR('C');
  145. continue;
  146. }
  147. send_byte(SKIP_ROM);
  148. send_byte(READ_SCRATCHPAD);
  149.  
  150.  
  151. lsb = read_byte();
  152. msb = read_byte();
  153. read_byte(); read_byte(); read_byte(); read_byte(); read_byte(); read_byte(); read_byte();
  154.  
  155. if ((prevLSB != lsb) || (prevMSB != msb)) {
  156.  
  157. prevLSB = lsb;
  158. prevMSB = msb;
  159.  
  160. calk = lsb >> 4;
  161. calk += (msb & 0x07) << 4;
  162.  
  163.  
  164. if (msb & 0x80) {
  165. calk ^= 0x7F;
  166.  
  167. ulam = (lsb & 0x08) ? 0 : 5000;
  168. ulam += (lsb & 0x04) ? 0 : 2500;
  169. ulam += (lsb & 0x02) ? 0 : 1250;
  170. ulam += (lsb & 0x01) ? 0 : 625;
  171. }
  172. else {
  173. ulam = (lsb & 0x08) ? 5000 : 0;
  174. ulam += (lsb & 0x04) ? 2500 : 0;
  175. ulam += (lsb & 0x02) ? 1250 : 0;
  176. ulam += (lsb & 0x01) ? 625 : 0;
  177. }
  178.  
  179. if (calk < 10) {
  180. CHAR_DD(' ', POS); SEND_CHAR((msb & 0x80) && (calk || ulam) ? '-' : ' ');
  181. SEND_CHAR(calk+'0');
  182. }
  183. else {
  184. if (calk < 100) {
  185. CHAR_DD(((msb & 0x80) ? '-' : ' '), POS);
  186. SEND_CHAR(calk/10+'0');
  187. SEND_CHAR(calk%10+'0');
  188. }
  189. else {
  190.  
  191. t = calk/100;
  192. CHAR_DD(t+'0', POS);
  193. calk -= 100 * t;
  194. SEND_CHAR(calk/10+'0');
  195. SEND_CHAR(calk%10+'0');
  196. }
  197. }
  198.  
  199. t = ulam / 1000;
  200. CHAR_DD(t+'0', POS+4);
  201. ulam -= t * 1000;
  202. t = ulam / 100;
  203. SEND_CHAR(t+'0');
  204. ulam -= t * 100;
  205. SEND_CHAR(ulam/10+'0');
  206. SEND_CHAR(ulam%10+'0');
  207.  
  208.  
  209. }
  210. }
  211.  
  212. }
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement