MCU rebooting

Thread Starter

FroceMaster

Joined Jan 28, 2012
556
Hi
Have this setup and more.
But for easy sake i just do this.
When pressed on the button the MCU normal Counts ( TMR0 ) input,
But if i hold the switch just a half second or more the MCU reboots.
Why ?
 

Attachments

Thread Starter

FroceMaster

Joined Jan 28, 2012
556
He he, good one ;)

Ok, here it is.
C:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#include "lcd.h"
#include <string.h>

#pragma config FOSC = INTOSC      // Oscillator Selection Bits (XT Oscillator, Crystal/resonator connected between OSC1 and OSC2 pins)
#pragma config WDTE = OFF       // Watchdog Timer Enable (WDT disabled)
#pragma config PWRTE = ON       // Power-up Timer Enable (PWRT disabled)
#pragma config MCLRE = OFF      // MCLR Pin Function Select (MCLR/VPP pin function is digital input)
#pragma config CP = OFF         // Flash Program Memory Code Protection (Program memory code protection is disabled)
#pragma config BOREN = OFF      // Brown-out Reset Enable (Brown-out Reset enabled)
#pragma config CLKOUTEN = OFF   // Clock Out Enable (CLKOUT function is disabled. I/O or oscillator function on the CLKOUT pin)
#pragma config IESO = OFF       // Internal/External Switchover Mode (Internal/External Switchover Mode is enabled)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable (Fail-Safe Clock Monitor is enabled)

// CONFIG2
#pragma config WRT = OFF        // Flash Memory Self-Write Protection (Write protection off)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable (Stack Overflow or Underflow will cause a Reset)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (Vbor), low trip point selected.)
#pragma config LPBOR = OFF      // Low-Power Brown Out Reset (Low-Power BOR is disabled)
#pragma config LVP = ON         // Low-Voltage Programming Enable (Low-voltage programming enabled)

#define _XTAL_FREQ 8000000
#define FOSC 2000000L

#define set_hour RB4 //Buttons
#define set_minut RC2
#define set_week RC1
#define set_time RC3

#define SCS_BIT                0x01
#define INT_OSC_8MHZ         (0b01110000 | SCS_BIT)

#define Skip_ROM             0xCC
#define Convert_T             0x44
#define Read_scratchpad     0xBE

#define Port_18B20             RB5
#define Tx_18B20             TRISB5 = 0
#define Rx_18B20             TRISB5 = 1

#define WAIT1                1000
#define WAIT2                500

#define DS18B20_CONV_TIME    750
#define DS18B20_RESET_PULSE 480
#define DS18B20_WAIT_TIME     60
#define DS18B20_PULLUP_TIME 2

#define ONEWIRE_PRESENT     0
#define ONEWIRE_ABSENT         1

unsigned char lsb;
    unsigned char msb;
    unsigned char decimal;
    unsigned char buffer[11];
    unsigned int sign_flag =0;
    unsigned int integer =0;
    unsigned int integer1 =0;
    unsigned int integer2 =0;
    unsigned int integer3 =0;
    unsigned int integer4 =0;
    signed int min,max,minold,minhele,minfraction,maxhele,maxfraction,total,fraction,now;
    int tempmax,minnegativ,maxnegativ;
    char hele[10],deci[10];
bit sec,vis;
int xpulse,secund,minut,hour,x,today_rain,r_regn,rainTMR0,visning,f_gennemlob,negativ;
char test[10];
unsigned char raindays[31];
volatile unsigned char week_day;
unsigned char mintime[3],maxx[3];
unsigned char maxtime[3],minx[3];
char mintemp[3],maxtemp[3];

const char * const day_of_week_names[] = {"Mandag ",
                                          "Tirsdag",
                                          "Onsdag ",
                                          "Torsdag",
                                          "Fredag ",
                                          "Lordag ",
                                          "Sondag "
                                         };

static void interrupt isr(void)   // Here is interrupt function - the name is unimportant.
{
if(TMR2IF)
{
TMR2IF=0;
xpulse++;
if (xpulse==25)
{ xpulse=0;
sec=1;
//RA0=!RA0;
}
}
}
void setup (void)
{

IRCF0=0; //8 mhz
IRCF1=1;
IRCF2=1;
IRCF3=1;
TRISA=0; //set all port out
TRISA2=1; // set port 2 in
TRISA4=1; // set port 4 ind.
TRISA5=1; // set port 5 ind.
TRISB4=1; // port B 4 in
TRISB5=1; // port b 5 in // temp
TRISB6=0; // b6 ud
TRISB7=1; // port b7 in
TRISC0=0;
TRISC1=1;
TRISC2=1;
TRISC3=1;
TRISC4=0;
TRISC5=0;
TRISC6=0;
TRISC7=0;
OSCCON = INT_OSC_8MHZ;

CM1CON0 = 0;
    CM2CON0 = 0;
//Timer 0 is counter to rain. input RA2
TMR0CS=1; //use t0cki.
PSA=1; // no prescaller
TMR0SE=1; // high to low transiton

// TIMER 2.....
T2CKPS0=1;//Prescale 64
T2CKPS1=1;
T2OUTPS0=1; // postscale 10
T2OUTPS1=0;
T2OUTPS2=0;
T2OUTPS3=1;
PR2=0x7F; // 126

GIE = 0;  // Global interrupt disable just in case
//ANSELA = 0b00000001;  // Set PORT AN0 to analog input AN1 to AN7 digital I/O
ANSELA=0; // set alle digital.
ANSELB=0;
ANSELC=0;//turn off all analog functions
lcd_init();
//T1CKPS1=1;
//T1CKPS0=1;
//nT1SYNC=1;
}
void vis_maxmin(void) // viser max og min
{         
                lcd_clear();
                lcd_goto(0x00);
                lcd_puts(day_of_week_names[mintime[2]]);
                if (mintime[2]!=1 || mintime[2]!=3)
                {
                lcd_puts(" ");
                    }  
                if (mintime[0]<10)lcd_puts("0");
                utoa(minx, mintime[0], 10);
                lcd_puts(minx);
                lcd_puts(":");
                if (mintime[1]<10)lcd_puts("0");
                utoa(minx, mintime[1], 10);
                lcd_puts(minx);
                lcd_puts(" ");
                if (minnegativ!=0)
                {
                    minhele=(500-min)/10;
                    minfraction= (500-min)-(minhele*10);
                }
                if (minnegativ==0)
                {
                 minhele=(min-500)/10;
                minfraction= (min-500)-(minhele*10);
                }
                utoa(mintemp,minhele,10);
                if (minhele<10)lcd_puts(" ");
                lcd_puts(mintemp);
                lcd_puts(".");
                utoa(mintemp,minfraction,10);
                lcd_puts(mintemp);
                lcd_puts("ß");
  
                // max temp kl bob bob
                lcd_goto(0x40);
                lcd_puts(day_of_week_names[maxtime[2]]);
                if (maxtime[2]!=1 || maxtime[2]!=3)
                {
                lcd_puts(" ");
                    }  
                if (maxtime[0]<10)lcd_puts("0");
                utoa(minx, maxtime[0], 10);
                lcd_puts(minx);
                lcd_puts(":");
                if (maxtime[1]<10)lcd_puts("0");
                utoa(minx, maxtime[1], 10);
                lcd_puts(minx);
              
                lcd_puts(" ");
                if (maxnegativ!=0)
                {
                maxhele=(500-max)/10;
                maxfraction= (500-max)-(maxhele*10);
                }
               if (maxnegativ==0)
               {
                maxhele=(max-500)/10;
                maxfraction= (max-500)-(maxhele*10);
               }
             
                utoa(maxtemp,maxhele,10);
                if (maxhele<10)lcd_puts(" ");
                lcd_puts(maxtemp);
                lcd_puts(".");
                utoa(maxtemp,maxfraction,10);
                lcd_puts(maxtemp);
                lcd_puts("ß");
              
              
  
                         
                 
}


void vaelg_dage (void) // Vælger dagene
    {
int sel,y;
    sel=1;
        x=0;
        __delay_ms(100);
        while (x<40)
    {
    if (!set_hour)  // Ved hver tryk inden for 4 sec vælges næste dag
    {char regn[4];
            lcd_goto(0x40);
        lcd_puts("Regn dag -");
        lcd_goto(0x4a);
        if (sel<10)lcd_puts(" ");
        utoa(regn, sel, 10);
        lcd_puts(regn);
        lcd_goto(0x4c);
        lcd_puts(" :");
      
        y=raindays[sel]; 
        if (y<100)lcd_puts(" ");
        if (y<10)lcd_puts(" ");
        utoa(regn, y, 10);
      
        lcd_puts(regn);
        lcd_puts(" MM");
        sel++;
        x=0;
        if (sel==30)sel=1;
    __delay_ms(255);
    }
    __delay_ms(100);
        x++;
  
    }
}



void reset_temp (void) //sletter temp.
{
     lcd_clear();                            // sletter skærm
     lcd_goto(0x00);                        // linje 1
     lcd_puts("Nulstil Max-Min temp ?");    // skriver
     lcd_goto(0x45);                        // linje 2
     lcd_puts("JA / NEJ ");                    // spørger
     lcd_goto(0x00);                        // linje 1
    x=0;                                    // tæller 0
    while (x<100)                            // løkke til 100*100 = 10 sec
    {
     if (!set_time)                            // hvis tryk på nulstil
     {
       
         min=0;
         max=0;
         lcd_clear();                        // sletter skærm
         lcd_goto(0x04);                    // linje 1
         lcd_puts("Nulstillet !");            // skriver
         lcd_goto(0x00);                    // linje 1
         __delay_ms(1000);                    // pause 1 sec
        x=99;                                // afslutter løkken
        }
        if(!set_week)x=0;                    // hvis tryk igen, omstart løkke
      
        x++;                                 // lægger til løkken
        __delay_ms(100);                    // pause
}                // end while løkken
}    // SLUT RESET TEMP
void reset_regn (void) //sletter total regn.
{
lcd_clear();                        // sletter skærm
lcd_goto(0x00);                    // går til linje1
lcd_puts(" Nulstil total regn ?");    // skriver
lcd_goto(0x45);                    // går til linje 2
lcd_puts("JA / NEJ ");                // skriver
lcd_goto(0x00);                    // går til linje 1
    x=0;                            // tæller 0
while (x<100)                        // løkke 100 gange *100 = 10 sec.
    {
     if (!set_time)                    // hvis man trykker JA knappen
     {TMR0=0;
         r_regn=0;
         today_rain=0;
         lcd_clear();                // sletter skærm
         lcd_goto(0x04);            // går linje 1
         lcd_puts("Nulstillet !");    // skriver
         lcd_goto(0x00);            // linje 1
         __delay_ms(1000);            // Pause 1 sec
        x=99;                        // slutter løkken
        }
        if(!set_week)x=0;            // hvis der trykkes igen, starter løkken forfra
      
        x++;                         // tæller løkken op
        __delay_ms(100);            // 100 ms pause
}
    }         // SLUT NULSTIL REGN

unsigned char reset()
{
    Tx_18B20;
    Port_18B20 = 0;
    __delay_us(DS18B20_RESET_PULSE);
    Rx_18B20;
    __delay_us(DS18B20_WAIT_TIME);

    if (Port_18B20 == 1)
    {
        __delay_us(DS18B20_RESET_PULSE);
        return ONEWIRE_PRESENT;
    }

    __delay_us(DS18B20_RESET_PULSE);
    return ONEWIRE_ABSENT;
}

void write(char WRT)
{
    char i,Cmd;
    Cmd = WRT;
    Rx_18B20;
    for(i = 0; i < 8; i++)
    {
        if((Cmd & (1<<i))!= 0)
        {

            Tx_18B20;
            Port_18B20 = 0;
            __delay_us(DS18B20_PULLUP_TIME);
            Rx_18B20;
            __delay_us(DS18B20_WAIT_TIME);
        }
        else
        {

            Tx_18B20;
            Port_18B20 = 0;
            __delay_us(DS18B20_WAIT_TIME);
            Rx_18B20;
        }
    }
}


unsigned char read()
{
    char i,result = 0;
    Rx_18B20;
    for(i = 0; i < 8; i++)
    {
        Tx_18B20;
        Port_18B20 = 0;
        __delay_us(DS18B20_PULLUP_TIME);
        Rx_18B20;
        if(Port_18B20 != 0)
        {
            result |= 1<<i;//i
        }
        __delay_us(DS18B20_WAIT_TIME);
    }
    return result;
}

void laestemp (void)
{

                write(Skip_ROM);
                write(Convert_T);
                __delay_ms(DS18B20_CONV_TIME);

                reset();
                write(Skip_ROM);
                write(Read_scratchpad);
                lsb = read();
                msb = read();
                if (f_gennemlob==0) lcd_clear();
                unsigned int sign = 248 & msb;
                if(sign==0)
                    decimal = lsb & 15;

                if(sign!=0)
                {
                    decimal = lsb ^ 15;
                    decimal = decimal^255;

                }

                unsigned char places[4]= {0};
                if((decimal&8) == 8)
                {

                    places[0] =  places[0] + 5;

                }

                if((decimal&4)==4)
                {
                    places[0] = places[0] + 2;
                    places[1] = places[1] + 5;
                }

                if((decimal&2)==2)
                {
                    places[0] = places[0] + 1;
                    places[1] = places[1] + 2;
                    places[2] = places[2] + 5;
                }

                if((decimal&1)==1)
                {
                    places[1] = places[1] + 6;
                    if(places[1]>9)
                    {
                        places[1]= places[1]-10;
                        places[0]= places[0]+1;
                    }

                    places[2] = places[2] + 2;
                    places[3] = places[3] + 5;

                }


                if(sign!=0)
                {
                    integer1 =(places[0]*1000);
                    integer2 = (places[1]*100);
                    integer3 = (places[2]*10);
                    integer4 = (places[3]);
                    integer = integer1+integer2+integer3+integer4;
                    if(integer==0)
                    {
                        sign_flag=1;
                    }
                    integer = 10000-integer;

                    integer1 = integer/1000;
                    integer2 = (integer/100)-(10*integer1);
                    integer3 = (integer/10)-(100*integer1)-(10*integer2);
                    places[0]= integer1;
                    places[1]= integer2;
                    places[2]= integer3;
                    places[3]= integer4;

                }
                lsb = lsb >> 3;
                msb = msb << 5;
                lsb = msb | lsb;
                if(sign !=0)
                {
                    lsb =(lsb)^255;
                    buffer[0]='-';
                }
                else
                    buffer[0] = '+';

                lsb = lsb >>1;
                printf("sign %d\n", sign_flag);

                buffer[1] = ((lsb / 100)%10) + 48;
                buffer[2] = (((lsb / 10)%10) + 48);
                buffer[3] = (((lsb)%10))+ 48 + sign_flag;
                buffer[4] = '.';
                buffer[5] = ((places[0]%10)+48);
                buffer[6] = ((places[1]%10)+48);
                buffer[7] = ((places[2]%10)+48);
                buffer[8] = ((places[3]%10)+48);
                buffer[9] = ' ' ;//0xF8;
                buffer[10]= 'C';
                total=0;
                total=((lsb / 100)%10)* 100;
                total=total+(((lsb / 10)%10) * 10);
                total=total+(((lsb)% 10))+  sign_flag;
                fraction=0;
                fraction=((places[0]%10)+48)-48;
        ////////////// TEST TEST /////////////////
             /*  total=2;
                fraction=2;
                sign==1;*/
        ///////////// TEST TEST ////////////      
              

                lcd_goto(0x00);     
                lcd_puts("Ude temp : ");    // writing temp.              
                itoa(hele,total,10);
                if (total<10) lcd_puts(" ");
              
                if(sign !=0)
                {
                lcd_puts("-");
                                }
                else
                {
                    lcd_puts(" ");}
              
                //if (total<10) lcd_puts(" ");
                lcd_puts(hele);
                lcd_goto(0x0E);
                lcd_puts(".");
                itoa(deci,fraction,10);
                lcd_puts(deci);
                lcd_puts("ß C");
                if (sign!=0)
                {now=500-((total*10)+fraction); // add 50 grader pga intet negativ result
                negativ=1;
                }
                if (sign==0)
                {now=500+((total*10)+fraction); // add 50 grader
                negativ=0;
                }
                              
                // set min and max temperatur.
                if (min==0)
                {min=now;
                minnegativ=0;
                if (sign!=0)minnegativ=1;
                }        // just to get it to work
                if (max==0)
                {max=now;        // after reset of max-min .
                maxnegativ=0;
                if (sign!=0)maxnegativ=1;
                }
              
              
              
              
   if (now<=min)            // if degree is under last reading
            {               
              
                            // set new "min" temp.
                lcd_goto(0x40);
                if (sign !=0)
                {lcd_puts("-");
                minhele=(500-now)/10;
                minfraction= (500-now)-(minhele*10);//fraction;
                utoa(mintemp,minhele,10);
                lcd_puts(mintemp);
                lcd_puts(".");
                utoa(mintemp,minfraction,10);
                lcd_puts(mintemp);
                lcd_puts("ß");
                minnegativ=1;
                min=500-((minhele*10)+minfraction);
                }
                else
                {lcd_puts(" ");
              
                minhele=(now-500)/10;
                minfraction= (now-500)-(minhele*10);
                utoa(mintemp,minhele,10);
                lcd_puts(mintemp);
                lcd_puts(".");
                utoa(mintemp,minfraction,10);
                lcd_puts(mintemp);
                lcd_puts("ß");
                minnegativ=0;
                min=(minhele*10)+minfraction+500;
                }
              
                mintime[0]=hour;
                mintime[1]=minut;
                mintime[2]=week_day;
                }
            
              
    if (now>min)                // if degree bigger than "min"
            {    lcd_goto(0x40);
                if (minnegativ==1)
                {lcd_puts("-");
                minhele=(500-min)/10;
                minfraction= (500-min)-(minhele*10);
                      
                utoa(mintemp,minhele,10);
                lcd_puts(mintemp);
                lcd_puts(".");
                utoa(mintemp,minfraction,10);
                lcd_puts(mintemp);
                lcd_puts("ß");
                }
                else
                {lcd_puts(" ");
                minhele=(min-500)/10;
                minfraction= (min-500)-(minhele*10);
                utoa(mintemp,minhele,10);
                lcd_puts(mintemp);
                lcd_puts(".");
                utoa(mintemp,minfraction,10);
                lcd_puts(mintemp);
                lcd_puts("ß");
                }
    }

    lcd_goto(0x46);
    lcd_puts(":MinMax:");    // just text
  
         
  
    if (now>=max)            // if degree is bigger than "Max"
{  
      
                lcd_goto(0x4E);
                if (sign !=0)
                {
                lcd_puts("-");
                maxhele=(500-now)/10;
                maxfraction= (500-now)-(maxhele*10);
                utoa(maxtemp,maxhele,10);
                lcd_puts(maxtemp);
                lcd_puts(".");
                utoa(maxtemp,minfraction,10);
                lcd_puts(maxtemp);
                lcd_puts("ß C");
                maxnegativ=1;
                max=500-((maxhele*10)+maxfraction);
                }
                else
                {lcd_puts(" ");
              
                maxhele=(now-500)/10;
                maxfraction= (now-500)-(maxhele*10);
                utoa(maxtemp,maxhele,10);
                lcd_puts(maxtemp);
                lcd_puts(".");
                utoa(maxtemp,maxfraction,10);
                lcd_puts(maxtemp);
                lcd_puts("ß C");
                maxnegativ=0;
                max=(maxhele*10)+maxfraction+500;
                }
                maxtime[0]=hour;
                maxtime[1]=minut;
                maxtime[2]=week_day;
                }
    // make time date function
    if (now<max)                // if degree bigger than "min"
            {    lcd_goto(0x4E);
                if (maxnegativ==1)
                {lcd_puts("-");
                 maxhele=(500-max)/10;
                maxfraction= (500-max)-(maxhele*10);
                utoa(maxtemp,maxhele,10);
                lcd_puts(maxtemp);
                lcd_puts(".");
                utoa(maxtemp,maxfraction,10);
                lcd_puts(maxtemp);
                lcd_puts("ß C");
                }
                else
                {lcd_puts(" ");
                maxhele=(max-500)/10;
                maxfraction= (max-500)-(maxhele*10);
                utoa(maxtemp,maxhele,10);
                lcd_puts(maxtemp);
                lcd_puts(".");
                utoa(maxtemp,maxfraction,10);
                lcd_puts(maxtemp);
                lcd_puts("ß C");
                }
 
            }
}

void vis_klokken(void)
{
char timestr[3];
    lcd_goto(0x00);                    // går til første linje
    lcd_puts("Klokken er: ");        // skriver
    utoa(timestr, hour, 10);        // laver om til streng
    if (hour<10)lcd_puts("0");        // hvis timer er under 10 skriv 0
        lcd_puts(timestr);            // skriver timer
        lcd_puts(":");                // skriver
        utoa(timestr, minut, 10);    // laver om til streng
    if (minut<10)lcd_puts("0");     // hvis minut er under 10 skriv 0
        lcd_puts(timestr);            // skriver minut
        lcd_puts(":");                // skriver
        utoa(timestr, secund, 10);        // laver til streng
    if (secund<10)lcd_puts("0");        // hvis sec er under 10 skriv 0
        lcd_puts(timestr);            // skriver
        lcd_goto(0x40);                // går til anden linje
    lcd_puts("Dagen er  : ");        // skriver
     for (unsigned char i=0;i<7;i++) lcd_putch(day_of_week_names[week_day][i]);
lcd_goto(0x00);                        // går til første linje
}    // slut "sub vis klokken"

void start_ur (void) // Starter uret.
{
    RA0=0;
GIE = 1;      // Global interrupt enable
TMR2ON=1;     // timer1 on
}     // slut "sub "start ur"

void sluk_ur (void) // Slukker uret
{
GIE = 0;         // Global interrupt disable as we are setting the time
TMR2ON=0;       // slukker timer til uret.
secund=0;            // sætter sec = 0
RA0=1;         // tænder lys

}// slut "sub sluk ur "

void stil_ur (void) //Indstiller uret
{
    sluk_ur();         // Stopper tiden
    lcd_clear();
    vis_klokken();     // Viser uret
    secund=0;
   do                  // Læser på knapperne
   {
    if (!set_hour)     // Stiller timer fremad
   {
       hour++;                    // Lægger en time til
        if (hour==24) hour=0;     // hvis timer = 24 så 0.
        vis_klokken();            // viser vi har tælt en op
        __delay_ms(255);         // venter lidt.
        __delay_ms(255);
               }
    if (!set_minut) // Stiller minutter frem
   {
        minut++;                // lægger 1 minut til.
         if (minut==60) minut=0;// hvis minut = 60 så 0.
           vis_klokken();            // viser vi har talt op.
         __delay_ms(255);         // venter lidt.
         __delay_ms(255);
               }  
    if (!set_week)  // Stiller ugedagen
   {
          week_day++;             // lægger 1 dag til.
         if (week_day==7) week_day=0; // hvis dag 7 så dag 0.
         vis_klokken();            // viser vi har talt op.
         __delay_ms(255);         // venter lidt.
               }  
       
    } while (!set_time); // Når knappen slippes igen
  
start_ur();  //Starter uret
//     vis_regn();
}// end stil uret

void midnat(void)    //thing to do at midnight. shift of date.
{int d;
week_day++;
if (week_day==7) week_day=0;
r_regn=r_regn+today_rain;
for(d=30;d>1;d--)
{
raindays[d]=raindays[d-1];
}

raindays[1]=today_rain;
today_rain=0;

}        // slut midnat


void uret(void)
      {
  
           minut++;      // og plusser et minut
    if (minut>=60)  // hvis minut = 60
    {    minut=0;    // minut = 0
        hour++;        // og plusser en time.
    if (hour>=24)    // hvis time er 24
    {    hour=0;        // time = 0
        midnat();    // kører sub midnat.
    }    // slut "if hour"
    }    // slut "if minut"
    }    // slut "sub uret"  

void fyldarray (void)
{int d;
     for(d=0;d<30;d++)
     {
         raindays[d]=0;
        }
    }     // fyld data

void vis_regn(void)
    {
        //lcd_clear();
    char regn[4], idag[4];     // defines
//lcd_clear();                // slet skærm
lcd_goto(0x00);                // går til 0
lcd_puts("Regn Total  : ");    // skriver
if (TMR0<100)lcd_puts(" ");    // højrestiller korrekt
if (TMR0<10)lcd_puts(" ");    // højrestiller igen
utoa(regn, TMR0, 10);        // laver tekststreng
lcd_puts(regn);                // skriver regnen
lcd_puts(" MM");            // i MM
lcd_goto(0x40);                // går til linje 2
lcd_puts("Regn ");            // skriver
for (unsigned char i=0;i<7;i++) lcd_putch(day_of_week_names[week_day][i]);
lcd_puts(": ");            // skriver :

today_rain=TMR0-r_regn;        // r_regn er resttal som skal bruges.

if (today_rain<100)lcd_puts(" ");    // Højrestiller
if (today_rain<10)lcd_puts(" ");    // højrestiller
utoa(idag, today_rain, 10);            // til tekststreng
lcd_puts(idag);            // skriver
lcd_puts(" MM");            // husker MM

}    //slut vis regn
void main (void)
{setup();         //kører setup
   lcd_clear();
       lcd_goto(0x00);
       lcd_puts("1234567890abcdefghij");
       lcd_goto(0x40);
       lcd_puts("abcdefghij1234567890");
       fyldarray();
      __delay_ms(1000);
      lcd_clear();
 
   sec=0;
   PEIE=1;
   GIE=1;
   TMR0=0;        // timer 0 til 0
   xpulse=0;
   TMR2IE=1; // tænder interrupt timer2
   TMR2ON=1; // timer 2 on.
   secund=-1;
   min=0;
   max=0;
lcd_goto(0x00);

   while (1)
   {
       if (sec==1)
       {
           secund++;   // add 1 sec.
           visning++;  // add til visning
         
           if (secund==60) // hvis det er talt til 60
               {secund =0; // sætter sekund 0
           uret();  // kører sub uret.
         
           }
           if (visning==5)  // hvis talt til 5,
           {
               visning=0;   // sætter tæller til 0
            
               if (vis==0)  // hvis visning = regn
               {vis=1;   // visning = temp
               f_gennemlob=0;
               }
               else
               {vis=0;      // sæt vis = regn
                lcd_clear();  // sletter skærmm
               }
             
             
           }
       sec=0;
           }
     
       if (vis==0)
       {
           vis_regn();  // viser regnen
       }
       else
       {
           if (!reset() == ONEWIRE_PRESENT)
            {
           laestemp();  // viser temperatur
            f_gennemlob=1;
            }
       }
     
if (!set_time) // indstiller uret.
       {
           stil_ur();
     
       }
if(!set_week)            // hvis tryk på set week  viser max min,
                {  vis_maxmin();
                int x;
                x=0;                // tæller til 0          
                    while (x<100)    // løkke indtil 100*100=10 SEC
                        {
                    if (!set_week) x=0;        // hvis der trykkes igen, nulstiller vi tæller
                        __delay_ms(100);    // i 10 sec.
                        x++;                // tæller op
                        }    //END WHILE tryk på vis max min
                }        // END IF set_week
      
 
//SET MINUT  NULSTILLER DIVERSE.      
if (!set_minut)        // hvis tryk på set minut  KLAR TIL NULSTILLING AF DIVERSE.
        {
            sluk_ur();        // slukker uret.
            RA0=1;            // tænder led for at vise vi sletter
              //vis_regn();        // vis regn sub.
               x=0;            // tæller til 0          
while (x<50)                // løkke indtil 50*100=5 SEC
{
if (!set_minut) x=0;        // hvis der trykkes igen, nulstiller vi tæller
__delay_ms(100);            // pause
x++;                        // tæller op
if (!set_week)                // hvis der trykkes på nulstil.
{
reset_regn();                // viser reset sub
}
}
vis_maxmin();                    // viser temperatur sub
x=0;                        // tæller på 0
while (x<50)                // løkke indtil 50*100
{
if (!set_minut) x=0;        // hvis der trykkes nulstiller vi temp.
__delay_ms(100);            // pauser
x++;                        // tæller op
if (!set_week)                // hvis der trykkes på nulstil
{
reset_temp();                // viser rest temp sub
}
}



start_ur();                    // starter ur igen
RA0=0;                        // slukker LED igen.
        }                    // end set minut
     
     
// SET HOUR  SKIFTER MELLLEM DAGE.
if (!set_hour)
{vis_regn();
    vaelg_dage();

}
       // more if.

   }
}
 
Last edited by a moderator:
Top