Advertisement
Guest User

Untitled

a guest
Dec 28th, 2012
25
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.48 KB | None | 0 0
  1. /*
  2.  * SevensegmentClock1wire.c
  3.  *
  4.  * Created: 29. 12. 2012. 1:35:23
  5.  *  Author: Sale
  6.  */
  7.  
  8. #ifndef F_CPU
  9. #define F_CPU 1000000UL // 10 MHz
  10. #endif
  11.  
  12. #include <avr/io.h>
  13. #include <util/delay.h>
  14. #include <avr/interrupt.h>
  15. #include "1wire.h"
  16.  
  17. const char SSEG[13] = { 0x3F,   0x21,   0x5B,   0x73,   0x65,   0x76,   0x7E,   0x23,   0x7F,   0x77,   0x00,       0x47,       0x1F};
  18.  
  19. //Konstante koje oznacavaju mesta (pinove) buffera tj. koji pin im daje clock ivicu
  20. #ifndef BUFFERPINSANDTEMPSENS
  21. #define BUFFERPINSANDTEMPSENS
  22. #define BUFFSATIHIGH    PD5
  23. #define BUFFSATILOW     PD4
  24. #define BUFFMINHIGH     PD1
  25. #define BUFFMINLOW      PD0
  26. #define DS18B20PIN      PB7
  27. #define SECPIN          PD6
  28. #define HOURBUTTON      PD3
  29. #define MINBUTTON       PD2
  30. #endif
  31.  
  32.  
  33. //Upisivanje vrednosti u zeljeni bafer
  34. void SetBufferValue(int buffer, char bufferValue)
  35. {
  36.     PORTD   |= (1<<BUFFSATIHIGH) | (1<<BUFFSATILOW) | (1<<BUFFMINHIGH) | (1<<BUFFMINLOW);   //Stavi logicke 1 na CLK od bafera (podaci se upisuju kad dodje HIGH-to-LOW tranzicija)
  37.     PORTB = bufferValue;
  38.     PORTD |= (1<<buffer);   //buffer clock to HIGH
  39.     _delay_us(50);
  40.     PORTD &= ~(1<<buffer);  //HIGH-to-LOW transition
  41.     _delay_us(50);
  42.     PORTD |= (1<<buffer);   //buffer clock to HIGH again
  43.     _delay_us(50);
  44. }
  45.  
  46. void startUP() {
  47.     cli();  //Disable Interrupts
  48.  
  49.     TIMSK   |= (1<<OCIE0A) | (1<<TOIE0);        //Enable OCIE0A: Timer/Counter0 Output Compare Match A Interrupt Enable
  50.  
  51.     DDRB    = 0xFF;
  52.     PORTB   = 0x00;
  53.     DDRD    = (0<<MINBUTTON) & (0<<HOURBUTTON);
  54.     DDRD    |= (1<<BUFFSATIHIGH) | (1<<BUFFSATILOW) | (1<<BUFFMINHIGH) | (1<<BUFFMINLOW);   //Postavi pinove na kojima su baferi kao izlazne
  55.     PORTD   |= (1<<BUFFSATIHIGH) | (1<<BUFFSATILOW) | (1<<BUFFMINHIGH) | (1<<BUFFMINLOW);   //Stavi logicke 1 na CLK od bafera (podaci se upisuju kad dodje HIGH-to-LOW tranzicija)
  56.  
  57.     //Postavi sve sedmosegmentne displeje na neku pocetnu vrednost
  58.     SetBufferValue(BUFFSATIHIGH, SSEG[10]);
  59.     SetBufferValue(BUFFSATILOW, SSEG[10]);
  60.     SetBufferValue(BUFFMINHIGH, SSEG[11]);
  61.     SetBufferValue(BUFFMINLOW, SSEG[12]);
  62.  
  63.  
  64.     CLKPR = (1<<CLKPCE);    //Ukljuci prescale originalnog takta
  65.     CLKPR |= (1<<CLKPS0); // Divide by 1
  66.  
  67.  
  68.     TCCR0A  |= (1 << WGM01);                // Configure Timer 0 for CTC mode
  69.     TCCR0B  |= (1 << CS01) | (1 << CS00);   //Prescaling the timer
  70.     OCR0A   = 0xF9;                         // Set CTC compare value to 0.5Hz AVR clock, with pre-scaler of 1024
  71.     TCNT0   = 0x06;                         //Podesi brojac odakle da pocne da broji
  72.  
  73.  
  74.  
  75.     sei();  //Enable Interrupts
  76. }
  77.  
  78. void DisplayTemp() {
  79.  
  80. }
  81.  
  82. int main(void)
  83. {
  84.     int sseg_high=0, sseg_low=0;
  85.    startUP();
  86.     one_wire_init();
  87.  
  88.     while(1){
  89.         if (one_wire_reset())
  90.         {
  91.             one_wire_write(ONE_WIRE_SKIP_ROM);  // Skip addressing, since we only have one device on the wire
  92.             one_wire_write(0x44);               // Start conversion
  93.  
  94.             _delay_ms(750);                     // the maximum tCONV, when dealing with 12-bit resolution
  95.             // (check the datasheet for your sensor)
  96.             // 9 bit = 100ms, 10 bit = 190ms, 11 bit = 375ms
  97.  
  98.             if (one_wire_reset())
  99.             {
  100.                 one_wire_write(ONE_WIRE_SKIP_ROM);  // Skip addressing, since we only have one device on the wire
  101.                 one_wire_write(0xBE);               // Read stuff
  102.  
  103.                 int16_t temp = one_wire_read();     // read the LSB
  104.                 temp |= one_wire_read() << 8;       // read the MSB
  105.  
  106.                 // and here you can do something with the temperature
  107.  
  108.                 while(temp >=10) {
  109.                     temp-=10;
  110.                     sseg_high++;
  111.                 }
  112.                 sseg_low = temp;
  113.  
  114.                 SetBufferValue(BUFFSATIHIGH, SSEG[sseg_high]);
  115.                 SetBufferValue(BUFFSATILOW, SSEG[sseg_low]);
  116.             }
  117.             // else the chip went away, wth?
  118.         }
  119.         // else we didn't get a presence pulse
  120.  
  121.         return 0;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement