Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // ***********************************************************************
- //
- // Demo program for education, subject CAPS (9/2014)
- // Petr Olivka, dept. of computer science, FEI, VSB-TU Ostrava
- // email: petr.olivka@vsb.cz
- //
- // Example for AVR-KIT usage:
- // LCD module
- //
- // ***********************************************************************
- #include \"../avrkit/avrkit.h\"
- #include <stdio.h>
- #include <string.h>
- #include <avr io.h="">
- #include \"lcd-lib.h\"
- #define CMD_CLEAR 0x01
- #define CMD_GOTO_HOME 0x02
- struct msh {
- unsigned char min; //00-59
- unsigned char sec; //00-59
- unsigned char hun; //00-99
- } mymsh;
- struct msh2 {
- unsigned char min; //00-59
- unsigned char sec; //00-59
- unsigned char hun; //00-99
- } mymsh2;
- unsigned char getChar(unsigned char c) {
- return c + \'0\';
- }
- void set (){
- mymsh.min = 15;
- mymsh.sec = 58;
- mymsh.hun = 87;
- }
- void set2 (){
- mymsh2.min = 8;
- mymsh2.sec = 16;
- mymsh2.hun = 42;
- }
- //DODELAT TOHLE TIM PRINTEFEM POTOM!!!!!
- void show(struct msh *t, char line, char separator) {
- unsigned char cursor1;
- if (line==\'0\') {
- cursor1 = 0x00;
- }
- else {
- cursor1 = 0x40;
- }
- //consider printf
- //lcd_send_cmd(0x01);
- //delay_ms(5);
- char buf[11];
- 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 );
- char len = strlen( buf );
- // display string on LCD
- for ( int l = 0; l < len; l++ ) {
- lcd_send_cmd( 0x80 | cursor1 + l);
- delay_ms(5);
- lcd_send_data( buf[ l ] );
- delay_ms(5);
- }
- }
- void show2(struct msh2 *t, char line, char separator) {
- unsigned char cursor1;
- if (line==\'0\') {
- cursor1 = 0x00;
- }
- else {
- cursor1 = 0x40;
- }
- //consider printf
- //lcd_send_cmd(0x01);
- //delay_ms(5);
- char buf[11];
- 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 );
- char len = strlen( buf );
- // display string on LCD
- for ( int l = 0; l < len; l++ ) {
- lcd_send_cmd( 0x80 | cursor1 + l);
- delay_ms(5);
- lcd_send_data( buf[ l ] );
- delay_ms(5);
- }
- }
- void zero(struct msh *t) {
- mymsh.min = 0;
- mymsh.sec = 0;
- mymsh.hun = 0;
- }
- void zero2(struct msh2 *t) {
- mymsh2.min = 0;
- mymsh2.sec = 0;
- mymsh2.hun = 0;
- }
- void add001(struct msh *t) {
- mymsh.hun = mymsh.hun + 1;
- }
- void add0012(struct msh2 *t) {
- mymsh2.hun = mymsh2.hun + 1;
- }
- void cursor(char onoff){
- if (onoff==\'0\')
- {
- lcd_send_cmd(0x0C);
- }
- else if(onoff==\'1\') { //asi nejde
- lcd_send_cmd(0x04);
- }
- }
- void checkInc (){
- if (mymsh.hun == 100) {
- mymsh.hun = 0;
- mymsh.sec += 1;
- }
- if (mymsh.sec == 60) {
- mymsh.sec = 0;
- mymsh.hun = 0;
- mymsh.min += 1;
- }
- if (mymsh.min == 60) {
- mymsh.min = 0;
- mymsh.sec = 0;
- mymsh.hun = 0;
- }
- }
- void checkInc2 (){
- if (mymsh2.hun == 100) {
- mymsh2.hun = 0;
- mymsh2.sec += 1;
- }
- if (mymsh2.sec == 60) {
- mymsh2.sec = 0;
- mymsh2.hun = 0;
- mymsh2.min += 1;
- }
- if (mymsh2.min == 60) {
- mymsh2.min = 0;
- mymsh2.sec = 0;
- mymsh2.hun = 0;
- }
- }
- void specialchar(char line){
- char mychar[ 8 ] = {0b00000000,
- 0b00000000,
- 0b00011111,
- 0b00010101,
- 0b00010101,
- 0b00011111,
- 0b00000000,
- 0b00000000};
- unsigned char cursor1;
- if (line==\'0\') {
- cursor1 = 0x00;
- }
- else {
- cursor1 = 0x40;
- }
- for ( int i = 0; i < 8; i++ )
- {
- lcd_send_cmd( 0x40 | ( i ));
- delay_ms(5);
- lcd_send_data( mychar[ i ] );
- delay_ms(5);
- }
- //smazat
- lcd_send_cmd( 0x80 | 0x00 + 15);
- lcd_send_data(\' \');
- lcd_send_cmd( 0x80 | 0x40 + 15 );
- lcd_send_data(\' \');
- //nacist
- lcd_send_cmd( 0x80 | cursor1 + 15 );
- delay_ms(5);
- lcd_send_data(0);
- delay_ms(5);
- }
- int main(void)
- {
- avrkit_init();
- lcd_init();
- set();
- set2 ();
- //show(&mymsh, \'1\',\'/\'); //radek 0/1, separator
- //zero(&mymsh);
- //add001(&mymsh);
- //show(&mymsh, \'1\',\'/\'); //radek 0/1, separator
- cursor(\'0\');
- //delay_ms(200);
- //cursor(\'1\');
- //3. program,.. loop and test
- /*
- for (int i=0; i<200; i++) {
- add001(&mymsh);
- checkInc (); //change incrementation
- delay_ms(5);
- show(&mymsh, \'1\',\'/\'); //radek 0/1, separator
- delay_ms(100);
- }
- */
- show(&mymsh, \'0\',\'/\');
- //inicializace tlacitka
- DDRD |= (1 << DDD7);
- PORTD |= (1 << PORTD7);
- DDRB |= 255;
- //stopky druhy pokus
- char readchar=\' \';
- char action = \' \';
- char secondwatch=\'1\';
- int counter=1;
- //contrast
- DDRA |= 7;
- PORTA = 4;
- while (1) {
- //1
- if (read_butt()==0xE0){action=\'1\';} //pind4 start 1
- if (read_butt()==0xD0){action=\'0\';} //pind5 stop 1
- if (read_butt()==0x80){action=\'3\';} //pind4+5+6 reset 1
- if (action==\'1\'){
- add001(&mymsh);
- checkInc (); //change incrementation
- show(&mymsh, \'0\',\'/\'); //radek 0/1, separator
- }
- if (action==\'3\'){
- zero(&mymsh);
- show(&mymsh, \'0\',\'/\'); //radek 0/1, separator
- //blink 5x a pak znova start
- for (int i=0;i<5;i++){
- PORTD &= ~(1 << PORTD7);
- delay_ms(100);
- PORTD |= (1 << PORTD7);
- delay_ms(100);
- }
- action=\'1\';
- }
- if (action==\'0\') {
- //changecontrast using pind5 D0 a pind6 B0
- DDRA |= 7;
- int contrast = 4;
- while (1) {
- if (read_butt()==0xD0){
- contrast--;
- if (contrast==0) contrast=1;
- }
- if (read_butt()==0xB0){
- contrast++;
- if (contrast==8) contrast=7;
- }
- PORTA = contrast; //nastavit kontrast
- if (read_butt()==0xE0){action=\'1\'; break;}
- }
- //PORTA = 4; //vychozi kontrast
- //PORTA = 7; //minimalni kontrast
- //PORTA = 1; //max kontrast
- }
- //2
- if (read_butt()==0xB0){secondwatch=\'0\';} //pind6 stop 2
- if (secondwatch==\'1\') {
- add0012(&mymsh2);
- checkInc2 (); //change incrementation
- show2(&mymsh2, \'1\',\':\'); //radek 0/1, separator
- }
- //funny animation
- if (counter%4==0){
- specialchar(\'0\');
- }
- else{
- specialchar(\'1\');
- }
- counter++;
- }
- }</avr></string.h></stdio.h>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement