Share Pastebin
Guest
Public paste!

carlhako

By: a guest | Mar 23rd, 2010 | Syntax: C++ | Size: 11.09 KB | Hits: 157 | Expires: Never
Copy text to clipboard
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. #define F_CPU 14745600
  5.  
  6. #include <avr/io.h>
  7. #include <avr/interrupt.h>
  8. #include "../libnerdkits/delay.h"
  9.  
  10. // these 2 for serial
  11. #include <avr/pgmspace.h>
  12. #include "../libnerdkits/uart.h"
  13.  
  14. #include "max7219.c"
  15. #include "ds1302.c"
  16.  
  17. #define CE PD4 // PIN connected to CE
  18. #define IO PD3 // PIN connected to PD3
  19. #define SCLK PD2 // PIN connected to PD2
  20.  
  21. //volatile unsigned char b10;
  22. //volatile unsigned char bpm;                  
  23. volatile int16_t the_time;
  24. volatile char sec_s;
  25. volatile char sec_t;
  26. volatile char min_s;
  27. volatile char min_t;
  28. volatile char hr_s;
  29. volatile char hr_t;
  30.  
  31.  
  32. void realtimeclock_setup() {
  33.   TCCR0B = (1<<CS01) | (1<<CS00);
  34.   TIMSK0 = (1<<TOIE0);
  35. }
  36.  
  37. SIGNAL(SIG_OVERFLOW0) { // interupt to run 100 times per second.
  38.         the_time++;
  39. }
  40.  
  41. void display_current_time(char on) { // read time from ds1302 then update max7219.
  42.         min_s = read_minutes(1);
  43.         min_t = read_minutes(10);
  44.         hr_s = read_hours(1);
  45.         hr_t = read_hours(10);
  46.         if (on) { // if set to on, turn on colon between hr/min
  47.                 min_t |= (1<<7);
  48.                 hr_s |= (1<<7);
  49.         }
  50.         display(1,hr_t); // pass info to max chip to update display
  51.         display(2,hr_s);
  52.         display(3,min_t);
  53.         display(4,min_s);
  54. }
  55.  
  56. int main() {
  57.         volatile uint8_t tmp; // int all vars here
  58.         volatile int tmp2;
  59.         volatile uint8_t button1;
  60.         volatile uint8_t button2;
  61.         volatile uint8_t button3;
  62.         volatile uint8_t mode[3];
  63.         volatile int8_t allow_adjust;
  64.         volatile int16_t counter;
  65.         volatile char blink_sep;
  66.        
  67.         //mode[0] = 0;
  68.         mode[2] = 0;
  69.        
  70.        
  71.         // set pins to be used as buttons
  72.         DDRD &= ~(1<<PD7); // set pin as input
  73.         PORTD |= (1<<PD7); // enable pull up resistor
  74.         DDRD &= ~(1<<PD6); // set pin as input
  75.         PORTD |= (1<<PD6); // enable pull up resistor
  76.         DDRD &= ~(1<<PD5); // set pin as input
  77.         PORTD |= (1<<PD5); // enable pull up resistor
  78.  
  79.         //init uart and create stream
  80.         uart_init();
  81.         FILE uart_stream = FDEV_SETUP_STREAM(uart_putchar, uart_getchar, _FDEV_SETUP_RW);
  82.         stdin = stdout = &uart_stream;
  83.  
  84.         rtc_init(); // init ds1302 rtc chip
  85.         realtimeclock_setup(); // setup interupt
  86.         sei(); // enable interupts
  87.         max7219_init(); // init max7219 chip
  88.          
  89.          
  90.          
  91.          
  92.          
  93.          
  94.         //check bit stored in ram of ds1302, if its missing assume clock has been reset.
  95.        
  96.        
  97.         min_s = read_minutes(1);
  98.         min_s = min_s + (read_minutes(10) * 10);
  99.         write_minutes(min_s);
  100.         write_seconds(1);
  101.  
  102.         //turn on trickle charger
  103.         trickle();
  104.  
  105.         while(1) {
  106.                 button1 = (PIND & (1<<PD7)) >> PD7; // update button status
  107.                 //printf_P(PSTR("1 : %u "), button1);
  108.  
  109.                 while (!button1) { // while button is pressed
  110.                         button1 = (PIND & (1<<PD7)); // update button status
  111.                        
  112.                         if ((!tmp) && (!mode[2])) {
  113.                                 mode[0]++;
  114.                                 tmp = 1;
  115.                         }      
  116.        
  117.                         button1 = (PIND & (1<<PD7)); // update button status
  118.                         if (button1) { // filter glitches, if its off wait 2ms then reread the button
  119.                                 delay_ms(2); // its probably the cheap switches im using but as they approach being on they would switch on and off very fast a few times
  120.                                 button1 = (PIND & (1<<PD7)); // update button status
  121.                         }
  122.        
  123.                 }
  124.        
  125.                 button2 = (PIND & (1<<PD6)) >> PD6; // update button status
  126.                 while (!button2) { // while button is pressed
  127.                        
  128.                         if ((!tmp) && (!mode[2])) {
  129.                                 if (mode[0] == 0) mode[0] = 3;
  130.                                 mode[0]--;
  131.                                 tmp = 1;
  132.                         }      
  133.        
  134.                         button2 = (PIND & (1<<PD6)); // update button status
  135.                         if (button2) { // filter glitches, if its off wait 2ms then reread the button
  136.                                 delay_ms(2); // its probably the cheap switches im using but as they approach being on they would switch on and off very fast a few times
  137.                                 button2 = (PIND & (1<<PD6)); // update button status
  138.                         }
  139.                 }
  140.        
  141.                 button3 = (PIND & (1<<PD5));
  142.                 while (!button3) { // detect if change mode button has been pressed
  143.                         button3 = (PIND & (1<<PD5));
  144.                         if (mode[0] == 2) {
  145.                                 tmp2 = 1;
  146.                                 mode[2] = 1;
  147.                         }
  148.                         if (button3) { // filter glitches, if its off wait 2ms then reread the button
  149.                                 delay_ms(2); // its probably the cheap switches im using but as they approach being on they would switch on and off very fast a few times
  150.                                 button3 = (PIND & (1<<PD5)); // update button status
  151.                         }
  152.                 }
  153.  
  154.                 tmp = 0;
  155.        
  156.                 switch(mode[0]) {
  157.                         case 0: // check and update display with time
  158.                        
  159.                        
  160.                                
  161.                                 if (the_time > 100) { // every second run the below
  162.                                         blink_sep++;
  163.                                         the_time = 0;
  164.                                         display_current_time(1);
  165.                                 }
  166.                                 if (blink_sep == 5) {
  167.                                 }
  168.                                 break;
  169.  
  170.        
  171.                         case 1: // show temp
  172.                                 display(1,3);
  173.                                 display(2,5);
  174.                                 display(3,37); // '
  175.                                 display(4,13); // c
  176.                                 break;
  177.                        
  178.                         case 2:
  179.                                 if (!mode[2]) {
  180.                                         display(1,29); // s
  181.                                         display(2,16); // e
  182.                                         display(3,30); // t
  183.                                         display(4,100); //
  184.                                 }
  185.                                
  186.                                 char b_pressed = 0;
  187.                        
  188.                                 while (mode[2]) { // set selected
  189.                                         button1 = (PIND & (1<<PD7)); // update button status
  190.                                         button2 = (PIND & (1<<PD6)); // update button status
  191.                                         button3 = (PIND & (1<<PD5)); // update button status
  192.                                         if ((!button1) && (!b_pressed)) {
  193.                                                 b_pressed = 1; // let rest of code a button has been pressed, avoids a few things happening twice
  194.                                                 delay_ms(2);
  195.                                                 mode[2]++;
  196.                                         }
  197.                                         if ((!button2) && (!b_pressed)) {
  198.                                                 b_pressed = 1;
  199.                                                 delay_ms(2);
  200.                                                 mode[2]--;
  201.                                         }
  202.                                         if (mode[2] > 3) mode[2] = 1;
  203.                                         if (mode[2] < 1) mode[2] = 3;
  204.                                        
  205.                                         if ((button1) && (button2)) {
  206.                                                 b_pressed = 0;
  207.                                         }
  208.                                        
  209.                                         while (!button3) {
  210.                                                 b_pressed = 2; // set var for code below
  211.                                                
  212.                                                 button3 = (PIND & (1<<PD5)); // update button status
  213.                                                 if (button3) {
  214.                                                         delay_ms(2);
  215.                                                         button3 = (PIND & (1<<PD5)); // update button status
  216.                                                 }
  217.                                         }
  218.                                        
  219.                                         if ((!button1) || (!button2) || (tmp2)) { // if a change mode button was pressed update display
  220.                                                 tmp2 = 0;
  221.                                                 switch(mode[2]) {
  222.                                                         case 1:
  223.                                                                 display(1,29); // s
  224.                                                                 display(2,16); // e
  225.                                                                 display(3,30); // t
  226.                                                                 display(4,30); // t
  227.                                                                 break;
  228.                                                                
  229.                                                         case 2:
  230.                                                                 display(1,29); // s
  231.                                                                 display(2,16); // e
  232.                                                                 display(3,30); // t
  233.                                                                 display(4,1); //  1
  234.                                                                 break;
  235.                                                        
  236.                                                         case 3:
  237.                                                                 display(1,29); // s
  238.                                                                 display(2,16); // e
  239.                                                                 display(3,30); // t
  240.                                                                 display(4,2); //  2
  241.                                                                 break;
  242.                                                 }
  243.                                         }
  244.  
  245.                                         if (b_pressed == 2) {
  246.                                                 tmp = 1;
  247.                                                 if (mode[2] == 1) { // if on sett and button 3 is pressed, enter set time mode
  248.                                                         counter = 0;
  249.                                                         the_time = 0;
  250.                                                         char on = 1;
  251.                                                         char tmp2;
  252.                                                         display_current_time(1);
  253.                                                         while(tmp) { // enter set time mode
  254.                                                        
  255.                                                                 button1 = (PIND & (1<<PD7)); // update button status
  256.                                                                 button2 = (PIND & (1<<PD6)); // update button status
  257.                                                                 button3 = (PIND & (1<<PD5)); // update button status
  258.                                                        
  259.                                                                 if (the_time > 300) { // make min or hr flash to show which one is being set
  260.                                                                         if (!on) { // this if statment will show min/hr selection by flashing
  261.                                                                                 if (mode[2] == 1) {
  262.                                                                                         display(3,100); // 100 = blank
  263.                                                                                         display(4,100);
  264.                                                                                 } else {
  265.                                                                                         display(1,100);
  266.                                                                                         display(2,100);
  267.                                                                                 }
  268.                                                                                 on = 1;
  269.                                                                         } else {
  270.                                                                                 if (mode[2] == 1) {
  271.                                                                                         display(4,min_s);
  272.                                                                                         display(3,min_t);
  273.                                                                                 } else {
  274.                                                                                         display(2,hr_s);
  275.                                                                                         display(1,hr_t);
  276.                                                                                 }
  277.                                                                                 on = 0;
  278.                                                                         }
  279.                                                                         the_time = 0;
  280.                                                                 }
  281.                                                                
  282.                                                                 // if button 1 pressed cycle up
  283.  
  284.  
  285.                                                                 while ((!button1) || (!button2)) { // while button up/down is pressed inc/dec
  286.                                                                         if (!counter) {
  287.                                                                                 counter = 1; // to make this if statment true only once
  288.                                                                                 if (mode[2] == 1) { // modify minutes
  289.                                                                                         tmp2 = read_minutes(1);
  290.                                                                                         tmp2 = tmp2 + (read_minutes(10) * 10);
  291.                                                                                         if (!button1) tmp2++; // adjust the time depending on button pressed
  292.                                                                                         else tmp2--;
  293.                                                                                         if (tmp2 > 59) tmp2 = 0; // cycle mins
  294.                                                                                         if (tmp2 < 0) tmp2 = 59; // cycle mins
  295.                                                                                         display(4,(tmp2 % 10));// update display
  296.                                                                                         display(3,(tmp2 / 10));
  297.                                                                                 } else { // modify hours
  298.                                                                                         tmp2 = read_hours(1);
  299.                                                                                         tmp2 = tmp2 + (read_hours(10) * 10);
  300.                                                                                         if (!button1) tmp2++; // adjust the time depending on button pressed
  301.                                                                                         else tmp2--;
  302.                                                                                         if (tmp2 > 23) tmp2 = 0; // cycle mins
  303.                                                                                         if (tmp2 < 0) tmp2 = 23; // cycle mins
  304.                                                                                         display(2,(tmp2 % 10));// update display
  305.                                                                                         display(1,(tmp2 / 10));
  306.                                                                                 }
  307.                                                                                 the_time = 0;
  308.                                                                         }
  309.                                                                        
  310.                                                                         if (the_time > 500) {
  311.                                                                                 if (mode[2] == 1) { // modify minutes
  312.                                                                                         the_time = 450;
  313.                                                                                         if (!button1) tmp2++; // adjust the time depending on button pressed
  314.                                                                                         else tmp2--;
  315.                                                                                         if (tmp2 > 59) tmp2 = 0; // cycle mins
  316.                                                                                         if (tmp2 < 0) tmp2 = 59; // cycle mins
  317.                                                                                         display(4,(tmp2 % 10));// update display
  318.                                                                                         display(3,(tmp2 / 10));
  319.                                                                                 } else{
  320.                                                                                         if (!button1) tmp2++; // adjust the time depending on button pressed
  321.                                                                                         else tmp2--;
  322.                                                                                         if (tmp2 > 23) tmp2 = 0; // cycle mins
  323.                                                                                         if (tmp2 < 0) tmp2 = 23; // cycle mins
  324.                                                                                         display(2,(tmp2 % 10));// update display
  325.                                                                                         display(1,(tmp2 / 10));
  326.                                                                                 }
  327.                                                                         }
  328.                                                                         button1 = (PIND & (1<<PD7)); // update button status
  329.                                                                         button2 = (PIND & (1<<PD6)); // update button status
  330.                                                                         if ((button1) || (button2)) { // filter glitches, if its off wait 2ms then reread the button
  331.                                                                                 delay_ms(2); // its probably the cheap switches im using but as they approach being on they would switch on and off very fast a few times
  332.                                                                                 button1 = (PIND & (1<<PD7)); // update button status
  333.                                                                                 button2 = (PIND & (1<<PD6)); // update button status
  334.                                                                         }
  335.                                                                 }
  336.                                                                 if (counter) {
  337.                                                                         if (mode[2] == 1) {
  338.                                                                                 write_minutes(tmp2); // update clock
  339.                                                                                 min_s = tmp2 % 10;
  340.                                                                                 min_t = tmp2 / 10;
  341.                                                                         } else {
  342.                                                                                 write_hours(tmp2);
  343.                                                                                 hr_s = tmp2 % 10;
  344.                                                                                 hr_t = tmp2 / 10;
  345.                                                                         }
  346.                                                                         tmp2 = 0;
  347.                                                                         counter = 0;
  348.                                                                 }
  349.                                                                
  350.                                                                 // if button 3 pressed change from min-hr and then back to time
  351.                                                                 while (!button3) {
  352.                                                                         if ((!tmp2) && (mode[2] == 1))  {
  353.                                                                                 tmp2 = 1;
  354.                                                                                 mode[2]++;
  355.                                                                         }
  356.                                                                         if ((!tmp2) && (mode[2] == 2)) { // after setting hours go back to displaying the time
  357.                                                                                 tmp = 0;
  358.                                                                                 tmp2 = 1;
  359.                                                                                 mode[2] = 0;
  360.                                                                                 mode[0] = 0;
  361.                                                                                 the_time = 100; // so time is updated straight away
  362.                                                                         }
  363.                                                                         button3 = (PIND & (1<<PD5)); // update button status
  364.                                                                         if (button3) { // filter glitches, if its off wait 2ms then reread the button
  365.                                                                                 delay_ms(2); // its probably the cheap switches im using but as they approach being on they would switch on and off very fast a few times
  366.                                                                                 button3 = (PIND & (1<<PD5)); // update button status
  367.                                                                         }
  368.                                                                 }
  369.                                                                 tmp2 = 0;
  370.                                                         } // end of while(tmp)
  371.                                                 }
  372.                                         }
  373.                                        
  374.                                        
  375.                                 }
  376.                                 break;
  377.                                
  378.                                
  379.                         default:
  380.                                 mode[0] = 0;
  381.                                 the_time = 100; // so clock will sync straight away
  382.                 }
  383.         } // end of while(1);
  384.   return 0;
  385. } // end of main()