Advertisement
koudelak

Untitled

Oct 21st, 2014
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 8.02 KB | None | 0 0
  1. // ***********************************************************************
  2. //
  3. // Demo program for education, subject CAPS (9/2014)
  4. // Petr Olivka, dept. of computer science, FEI, VSB-TU Ostrava
  5. // email: petr.olivka@vsb.cz
  6. //
  7. // Example for AVR-KIT usage:
  8. // LCD module
  9. //
  10. // ***********************************************************************
  11.  
  12. #include \"../avrkit/avrkit.h\"
  13.  
  14. #include <stdio.h>
  15. #include <string.h>
  16. #include <avr io.h="">
  17.  
  18. #include \"lcd-lib.h\"
  19.  
  20. #define CMD_CLEAR       0x01
  21. #define CMD_GOTO_HOME   0x02
  22.  
  23. struct msh {
  24.  unsigned char min; //00-59
  25.  unsigned char sec; //00-59
  26.  unsigned char hun; //00-99
  27. } mymsh;
  28.  
  29. struct msh2 {
  30.     unsigned char min; //00-59
  31.     unsigned char sec; //00-59
  32.     unsigned char hun;  //00-99
  33. } mymsh2;
  34.  
  35. unsigned char getChar(unsigned char c) {
  36.     return c + \'0\';
  37. }
  38.  
  39. void set (){
  40.    mymsh.min = 15;
  41.    mymsh.sec = 58;
  42.    mymsh.hun = 87;
  43. }
  44.  
  45. void set2 (){
  46.    mymsh2.min = 8;
  47.    mymsh2.sec = 16;
  48.    mymsh2.hun = 42;
  49. }
  50.  
  51. //DODELAT TOHLE TIM PRINTEFEM POTOM!!!!!
  52. void show(struct msh *t, char line, char separator) {
  53.    
  54.    unsigned char cursor1;
  55.    
  56.    if (line==\'0\') {
  57.        cursor1 = 0x00;
  58.    }
  59.    else {
  60.        cursor1 = 0x40;
  61.    }
  62.    
  63.    
  64.    //consider printf
  65.        //lcd_send_cmd(0x01);
  66.        //delay_ms(5);
  67.        
  68.        
  69.            char buf[11];
  70.            sprintf( buf, \"%d%d%c%d%d%c%d%d\", mymsh.min / 10, mymsh.min % 10,separator, mymsh.sec / 10, mymsh.sec % 10,separator, mymsh.hun / 10, mymsh.hun % 10 );
  71.            char len = strlen( buf );
  72.            
  73.            // display string on LCD
  74.            for ( int l = 0; l < len; l++ ) {
  75.                lcd_send_cmd( 0x80 | cursor1 + l);
  76.                delay_ms(5);
  77.                lcd_send_data( buf[ l ] );  
  78.                delay_ms(5);
  79.            }
  80. }
  81.  
  82. void show2(struct msh2 *t, char line, char separator) {
  83.    
  84.    unsigned char cursor1;
  85.    
  86.    if (line==\'0\') {
  87.        cursor1 = 0x00;
  88.    }
  89.    else {
  90.        cursor1 = 0x40;
  91.    }
  92.    
  93.    
  94.    //consider printf
  95.    //lcd_send_cmd(0x01);
  96.    //delay_ms(5);
  97.    
  98.    
  99.    char buf[11];
  100.    sprintf( buf, \"%d%d%c%d%d%c%d%d\", mymsh2.min / 10, mymsh2.min % 10,separator, mymsh2.sec / 10, mymsh2.sec % 10,separator, mymsh2.hun / 10, mymsh2.hun % 10 );
  101.    char len = strlen( buf );
  102.    
  103.    // display string on LCD
  104.    for ( int l = 0; l < len; l++ ) {
  105.        lcd_send_cmd( 0x80 | cursor1 + l);
  106.        delay_ms(5);
  107.        lcd_send_data( buf[ l ] );
  108.        delay_ms(5);
  109.    }
  110. }
  111.  
  112. void zero(struct msh *t) {
  113.    mymsh.min = 0;
  114.    mymsh.sec = 0;
  115.    mymsh.hun = 0;  
  116. }
  117.  
  118. void zero2(struct msh2 *t) {
  119.    mymsh2.min = 0;
  120.    mymsh2.sec = 0;
  121.    mymsh2.hun = 0;
  122. }
  123.  
  124. void add001(struct msh *t) {
  125.    mymsh.hun = mymsh.hun + 1;
  126. }
  127.  
  128. void add0012(struct msh2 *t) {
  129.    mymsh2.hun = mymsh2.hun + 1;
  130. }
  131.  
  132. void cursor(char onoff){
  133.    if (onoff==\'0\')
  134.    {
  135.        lcd_send_cmd(0x0C);
  136.    }
  137.    else if(onoff==\'1\') { //asi nejde
  138.        lcd_send_cmd(0x04);
  139.    }
  140. }
  141.  
  142. void checkInc (){
  143.        if (mymsh.hun == 100) {
  144.            mymsh.hun = 0;
  145.            mymsh.sec += 1;
  146.        }
  147.        if (mymsh.sec == 60) {
  148.            mymsh.sec = 0;
  149.            mymsh.hun = 0;
  150.            mymsh.min += 1;
  151.        }
  152.        if (mymsh.min == 60) {
  153.            mymsh.min = 0;
  154.            mymsh.sec = 0;
  155.            mymsh.hun = 0;
  156.        }
  157.            
  158. }
  159.  
  160. void checkInc2 (){
  161.    if (mymsh2.hun == 100) {
  162.        mymsh2.hun = 0;
  163.        mymsh2.sec += 1;
  164.    }
  165.    if (mymsh2.sec == 60) {
  166.        mymsh2.sec = 0;
  167.        mymsh2.hun = 0;
  168.        mymsh2.min += 1;
  169.    }
  170.    if (mymsh2.min == 60) {
  171.        mymsh2.min = 0;
  172.        mymsh2.sec = 0;
  173.        mymsh2.hun = 0;
  174.    }
  175.    
  176. }
  177.  
  178. void specialchar(char line){
  179.    char mychar[ 8 ] = {0b00000000,
  180.                        0b00000000,
  181.                        0b00011111,
  182.                        0b00010101,
  183.                        0b00010101,
  184.                        0b00011111,
  185.                        0b00000000,
  186.                        0b00000000};
  187.    
  188.    unsigned char cursor1;
  189.    
  190.    if (line==\'0\') {
  191.        cursor1 = 0x00;
  192.    }
  193.    else {
  194.        cursor1 = 0x40;
  195.    }
  196.    
  197.      for ( int i = 0; i < 8; i++ )
  198.      {
  199.          lcd_send_cmd( 0x40 | ( i ));
  200.          delay_ms(5);
  201.          lcd_send_data( mychar[ i ] );
  202.          delay_ms(5);
  203.      }
  204.      
  205.      //smazat
  206.      lcd_send_cmd( 0x80 | 0x00 + 15);
  207.      lcd_send_data(\' \');  
  208.      lcd_send_cmd( 0x80 | 0x40 + 15 );
  209.      lcd_send_data(\' \');
  210.      
  211.      //nacist
  212.      lcd_send_cmd( 0x80 | cursor1 + 15 );
  213.      delay_ms(5);
  214.      lcd_send_data(0);
  215.      delay_ms(5);      
  216. }
  217.  
  218. int main(void)
  219. {
  220.  
  221.    
  222.  
  223.        avrkit_init();
  224.        lcd_init();
  225.        
  226.        set();
  227.        set2 ();
  228.        //show(&mymsh, \'1\',\'/\'); //radek 0/1, separator
  229.        //zero(&mymsh);
  230.        //add001(&mymsh);
  231.        //show(&mymsh, \'1\',\'/\'); //radek 0/1, separator
  232.        cursor(\'0\');
  233.        //delay_ms(200);
  234.        //cursor(\'1\');
  235.        
  236.        
  237.        //3. program,.. loop and test
  238.        /*
  239.        for (int i=0; i<200; i++) {
  240.            add001(&mymsh);
  241.            checkInc (); //change incrementation
  242.            delay_ms(5);        
  243.            show(&mymsh, \'1\',\'/\'); //radek 0/1, separator
  244.            delay_ms(100);  
  245.        }
  246.        */
  247.        
  248.    show(&mymsh, \'0\',\'/\');  
  249.        
  250.     //inicializace tlacitka
  251.     DDRD |= (1 << DDD7);
  252.     PORTD |= (1 << PORTD7);
  253.     DDRB |= 255;        
  254.      
  255.     //stopky druhy pokus
  256.     char readchar=\' \';
  257.     char action = \' \';
  258.     char secondwatch=\'1\';
  259.      
  260.     int counter=1;
  261.     //contrast
  262.     DDRA |= 7;
  263.     PORTA = 4;
  264.      
  265.     while (1) {
  266.         //1
  267.         if (read_butt()==0xE0){action=\'1\';} //pind4 start 1
  268.         if (read_butt()==0xD0){action=\'0\';} //pind5 stop 1
  269.         if (read_butt()==0x80){action=\'3\';} //pind4+5+6 reset 1
  270.              
  271.        if (action==\'1\'){
  272.                add001(&mymsh);
  273.                checkInc (); //change incrementation    
  274.                show(&mymsh, \'0\',\'/\'); //radek 0/1, separator  
  275.        }
  276.        if (action==\'3\'){
  277.                zero(&mymsh);
  278.                show(&mymsh, \'0\',\'/\'); //radek 0/1, separator  
  279.                //blink 5x a pak znova start
  280.                for (int i=0;i<5;i++){
  281.                    PORTD &= ~(1 << PORTD7);
  282.                    delay_ms(100);
  283.                    PORTD  |= (1 << PORTD7);
  284.                    delay_ms(100);
  285.                }
  286.                action=\'1\';
  287.        }
  288.        
  289.        if (action==\'0\') {
  290.            
  291.            //changecontrast using pind5 D0 a pind6 B0
  292.            DDRA |= 7;
  293.            int contrast = 4;
  294.            while (1) {
  295.                if (read_butt()==0xD0){
  296.                    contrast--;
  297.                    if (contrast==0) contrast=1;
  298.                }
  299.                if (read_butt()==0xB0){
  300.                    contrast++;
  301.                    if (contrast==8) contrast=7;
  302.                }              
  303.                PORTA = contrast; //nastavit kontrast
  304.                if (read_butt()==0xE0){action=\'1\'; break;}
  305.            }
  306.            //PORTA = 4; //vychozi kontrast
  307.            //PORTA = 7; //minimalni kontrast
  308.            //PORTA = 1; //max kontrast
  309.            
  310.        }
  311.        
  312.        //2
  313.        if (read_butt()==0xB0){secondwatch=\'0\';} //pind6 stop 2
  314.        
  315.        if (secondwatch==\'1\') {
  316.            add0012(&mymsh2);
  317.            checkInc2 (); //change incrementation
  318.            show2(&mymsh2, \'1\',\':\'); //radek 0/1, separator
  319.        }
  320.                
  321.                
  322.        //funny animation
  323.        if (counter%4==0){
  324.            specialchar(\'0\');
  325.        }
  326.        else{
  327.            specialchar(\'1\');
  328.        }
  329.                
  330.        counter++;          
  331.     }
  332.  
  333.        
  334.        
  335. }</avr></string.h></stdio.h>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement