Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * SevensegmentClock1wire.c
- *
- * Created: 29. 12. 2012. 1:35:23
- * Author: Sale
- */
- #ifndef F_CPU
- #define F_CPU 1000000UL // 10 MHz
- #endif
- #include <avr/io.h>
- #include <util/delay.h>
- #include <avr/interrupt.h>
- #include "1wire.h"
- const char SSEG[13] = { 0x3F, 0x21, 0x5B, 0x73, 0x65, 0x76, 0x7E, 0x23, 0x7F, 0x77, 0x00, 0x47, 0x1F};
- //Konstante koje oznacavaju mesta (pinove) buffera tj. koji pin im daje clock ivicu
- #ifndef BUFFERPINSANDTEMPSENS
- #define BUFFERPINSANDTEMPSENS
- #define BUFFSATIHIGH PD5
- #define BUFFSATILOW PD4
- #define BUFFMINHIGH PD1
- #define BUFFMINLOW PD0
- #define DS18B20PIN PB7
- #define SECPIN PD6
- #define HOURBUTTON PD3
- #define MINBUTTON PD2
- #endif
- //Upisivanje vrednosti u zeljeni bafer
- void SetBufferValue(int buffer, char bufferValue)
- {
- 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)
- PORTB = bufferValue;
- PORTD |= (1<<buffer); //buffer clock to HIGH
- _delay_us(50);
- PORTD &= ~(1<<buffer); //HIGH-to-LOW transition
- _delay_us(50);
- PORTD |= (1<<buffer); //buffer clock to HIGH again
- _delay_us(50);
- }
- void startUP() {
- cli(); //Disable Interrupts
- TIMSK |= (1<<OCIE0A) | (1<<TOIE0); //Enable OCIE0A: Timer/Counter0 Output Compare Match A Interrupt Enable
- DDRB = 0xFF;
- PORTB = 0x00;
- DDRD = (0<<MINBUTTON) & (0<<HOURBUTTON);
- DDRD |= (1<<BUFFSATIHIGH) | (1<<BUFFSATILOW) | (1<<BUFFMINHIGH) | (1<<BUFFMINLOW); //Postavi pinove na kojima su baferi kao izlazne
- 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)
- //Postavi sve sedmosegmentne displeje na neku pocetnu vrednost
- SetBufferValue(BUFFSATIHIGH, SSEG[10]);
- SetBufferValue(BUFFSATILOW, SSEG[10]);
- SetBufferValue(BUFFMINHIGH, SSEG[11]);
- SetBufferValue(BUFFMINLOW, SSEG[12]);
- CLKPR = (1<<CLKPCE); //Ukljuci prescale originalnog takta
- CLKPR |= (1<<CLKPS0); // Divide by 1
- TCCR0A |= (1 << WGM01); // Configure Timer 0 for CTC mode
- TCCR0B |= (1 << CS01) | (1 << CS00); //Prescaling the timer
- OCR0A = 0xF9; // Set CTC compare value to 0.5Hz AVR clock, with pre-scaler of 1024
- TCNT0 = 0x06; //Podesi brojac odakle da pocne da broji
- sei(); //Enable Interrupts
- }
- void DisplayTemp() {
- }
- int main(void)
- {
- int sseg_high=0, sseg_low=0;
- startUP();
- one_wire_init();
- while(1){
- if (one_wire_reset())
- {
- one_wire_write(ONE_WIRE_SKIP_ROM); // Skip addressing, since we only have one device on the wire
- one_wire_write(0x44); // Start conversion
- _delay_ms(750); // the maximum tCONV, when dealing with 12-bit resolution
- // (check the datasheet for your sensor)
- // 9 bit = 100ms, 10 bit = 190ms, 11 bit = 375ms
- if (one_wire_reset())
- {
- one_wire_write(ONE_WIRE_SKIP_ROM); // Skip addressing, since we only have one device on the wire
- one_wire_write(0xBE); // Read stuff
- int16_t temp = one_wire_read(); // read the LSB
- temp |= one_wire_read() << 8; // read the MSB
- // and here you can do something with the temperature
- while(temp >=10) {
- temp-=10;
- sseg_high++;
- }
- sseg_low = temp;
- SetBufferValue(BUFFSATIHIGH, SSEG[sseg_high]);
- SetBufferValue(BUFFSATILOW, SSEG[sseg_low]);
- }
- // else the chip went away, wth?
- }
- // else we didn't get a presence pulse
- return 0;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement