Delay are wrong in 16F18877

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
Hi,
Have my running code, but it seems that delay_ms is off,.
Delays will last around 4 times more that i select,

C:
if (!KN2)  //(knap2==1)  // Set time/date
         {
             inaktiv=0; //
        
               int p;
                p=0;
             while (p<10)    // repeat until 100*10 = 1 SEC ???
             {
                __delay_ms(100);
                p++;
             }
                 if (!KN2) // if stilled pressed after 1 sec.
                 {
                     back_light=1;  // turn on backlight on LCD
                
                     sluk_ur(); // kill the time, and get ready to set it,.
//some more codes, not neassasary here.
}
Here is my config, and setup.
C:
#pragma config FEXTOSC = LP //XT     // External Oscillator mode selection bits (XT (crystal oscillator) above 500kHz, below 4MHz; PFM set to medium power)
#pragma config RSTOSC = HFINT1//32 // Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)


#define _XTAL_FREQ 4000000
#define FOSC 1000000L

#define back_light RB0
#define led1_on RE0
#define KN1 RC3
#define KN2 RC2
#define KN3 RC4
#define KN4 RC5

void setup_init(void)
{
        // NOSC HFINTOSC; NDIV 4;
    OSCCON1 = 0x62;
    //O

    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;

    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x00;

    // HFFRQ 4_MHz;
    OSCFRQ = 0x02; //

    // HFTUN 0;
    OSCTUNE = 0x00;

 
      // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC enabled; CRCMD CRC enabled; NVMMD NVM enabled;
    PMD0 = 0x00;
    // TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; NCOMD DDS(NCO) enabled; TMR6MD TMR6 enabled;
    PMD1 = 0x00;
    // ZCDMD ZCD enabled; DACMD DAC enabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
    PMD2 = 0x00;
    // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; CCP5MD CCP5 enabled; PWM6MD PWM6 enabled; PWM7MD PWM7 enabled;
    PMD3 = 0x00;
    // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled; MSSP1MD MSSP1 enabled; UART1MD EUSART enabled; MSSP2MD MSSP2 enabled;
    PMD4 = 0x00;
    // DSMMD DSM enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; SMT2MD SMT2 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
    PMD5 = 0x00;
      /*
       * WPUx registers
    */
    COSC2=1;
    COSC1=1;
    COSC0=0;
      /**
    LATx registers
    */
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    /*TRISx registers  */
    TRISE0=0; // LED
    TRISE1=0;
    TRISE2=0;
   // TRISE3=1; // reset,
    TRISA0=0;
    TRISA1=0;
    TRISA2=0;
    TRISA3=0;
    TRISA4=0;
    TRISA5=0;
    TRISA6=1; //
    TRISA7=1; //
    TRISB0=0; // bk light
    TRISB1=0;
    TRISB2=0;
    TRISB3=0; // RS
    TRISB4=0;
    TRISB5=0;
    TRISB6=0;
    TRISB7=0;
    TRISC0=0;
    TRISC1=0;
    TRISC2=1; // KN2
    TRISC3=1; // KN1
    TRISC4=1; // KN3
    TRISC5=1; // KN4
    TRISC6=0; // TX
    TRISC7=1; // RX
    TRISD0=0; // DATA
    TRISD1=0; //
    TRISD2=0; //
    TRISD3=0; // Data
    TRISD4=0;
    TRISD5=0;
    TRISD6=0;
    TRISD7=0; // Enable

    /**
    ANSELx registers
    */
    ANSELD = 0x00;
    ANSELC = 0x00;
    ANSELB = 0x00;
    ANSELE = 0x00;
    ANSELA = 0x00;

    /**/

    WPUD = 0x00;
    WPUE = 0x00;
    WPUB = 0x00;
    WPUA = 0x00;
    WPUC = 0x00;

    /**
    ODx registers
    */
    ODCONE = 0x00;
    ODCONA = 0x00;
    ODCONB = 0x00;
    ODCONC = 0x00;
    ODCOND = 0x00;

    /**
    SLRCONx registers
    */
    SLRCONA = 0xFF;
    SLRCONB = 0xFF;
    SLRCONC = 0xFF;
    SLRCOND = 0xFF;
    SLRCONE = 0x07;

      RC6PPS = 0x10;
 
       //timer 1 setup
T1CS0=0; // 0110 = SOCS
T1CS1=1;
T1CS2=1;
T1CS3=0;
T1CKPS0=0; // no prescale
T1CKPS1=0;
nT1SYNC=1; // no sync

lcd_init();  //lcd's init
lcd_goto(0);


}
Mod edit: 'C' code tags
 
Last edited by a moderator:

jpanhalt

Joined Jan 18, 2008
11,087
Your OSCCON1 setting of 0x62 sets the HF internal oscillator with a divide ratio of 4:
upload_2019-1-19_7-14-0.png

Then you set the frequency to 4 MHz (OSCFRQ = 2). Have you accounted for that division ratio?
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
I know, and now i have changed it to 1, (0000), But now BT is not function anymore.
Have tried many settings, but can not get both BT and delays to function at the same time

The funny thing is that when all settings says i run at 4 MHz, the Baudrate for BT is calculated after 16mhz.

I now changed the system to run at 16mhz, CDIV 1:1 and now the Delay's are correct,.
But BT is not function,

i am lost.
;(
 

jpanhalt

Joined Jan 18, 2008
11,087
I have experience neither with that chip nor BT, but I am familiar with other 8-bit chips and serial protocols. Since external communications all depend on the oscillator frequency (e.g., for setting baud rates), I chose an oscillator frequency appropriate for my voltage, timers, and possibly other internal needs. In the last few years, that frequency has been at least 8 MHz. I then calculate the register values needed for communication.

In my experience with older chips, you have great leeway in selecting those communication rates, but on occasion, I have needed to change my oscillator frequency to reduce baud error at higher rates, for example. It's a little iterative, but you don't have anything invested, except calculations. And fortunately, there are often online calculators for doing those.

I assume your DS18b20 uses serial communication , like I2C or SPI. So, it's only a matter of setting those controlling registers to function correctly with your new oscillator frequency.
 

spinnaker

Joined Oct 29, 2009
7,830
I know, and now i have changed it to 1, (0000), But now BT is not function anymore.
Have tried many settings, but can not get both BT and delays to function at the same time

The funny thing is that when all settings says i run at 4 MHz, the Baudrate for BT is calculated after 16mhz.

I now changed the system to run at 16mhz, CDIV 1:1 and now the Delay's are correct,.
But BT is not function,

i am lost.
;(

What "BT"??? Do you mean HC05????

Check your settings for you Pic. You might be able to run the chip off of the external oscillator but async peripheral off of the internal oscillator. If you have MCC available for that chip then use it.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
Have fixed the Hc-06 BT and delays seems to be right. 100×100ms delay gives me 10 seconds on stop watch.

But the ds18b20 is making fun
Have copy codes from other project where temp sensor works.
Have connected my signal analyser and got what i mean is a working signal.
Reading is allways 0.
 

Attachments

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
Here is code
C:
#include <xc.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lcd.c"

#pragma config FEXTOSC = LP //XT     // External Oscillator mode selection bits (XT (crystal oscillator) above 500kHz, below 4MHz; PFM set to medium power)
#pragma config RSTOSC = HFINT32 //1// Power-up default value for COSC bits (HFINTOSC with OSCFRQ= 32 MHz and CDIV = 1:1)
#pragma config CLKOUTEN = OFF   // Clock Out Enable bit (CLKOUT function is disabled; i/o or oscillator function on OSC2)
#pragma config CSWEN = ON      // Clock Switch Enable bit (The NOSC and NDIV bits cannot be changed by user software)
#pragma config FCMEN = ON       // Fail-Safe Clock Monitor Enable bit (FSCM timer enabled)

#pragma config MCLRE = ON       // Master Clear Enable bit (MCLR pin is Master Clear function)
#pragma config PWRTE = OFF      // Power-up Timer Enable bit (PWRT disabled)
#pragma config LPBOREN = OFF    // Low-Power BOR enable bit (ULPBOR disabled)
#pragma config BOREN = ON       // Brown-out reset enable bits (Brown-out Reset Enabled, SBOREN bit is ignored)
#pragma config BORV = LO        // Brown-out Reset Voltage Selection (Brown-out Reset Voltage (VBOR) set to 1.9V on LF, and 2.45V on F Devices)
#pragma config ZCD = OFF        // Zero-cross detect disable (Zero-cross detect circuit is disabled at POR.)
#pragma config PPS1WAY = ON     // Peripheral Pin Select one-way control (The PPSLOCK bit can be cleared and set only once in software)
#pragma config STVREN = ON      // Stack Overflow/Underflow Reset Enable bit (Stack Overflow or Underflow will cause a reset)

#pragma config WDTCPS = WDTCPS_31// WDT Period Select bits (Divider ratio 1:65536; software control of WDTPS)
#pragma config WDTE = OFF        // WDT operating mode (WDT enabled regardless of sleep; SWDTEN ignored)
#pragma config WDTCWS = WDTCWS_7// WDT Window Select bits (window always open (100%); software control; keyed access not required)
#pragma config WDTCCS = SC      // WDT input clock selector (Software Control)

#pragma config WRT = OFF        // UserNVM self-write protection bits (Write protection off)
#pragma config SCANE = available// Scanner Enable bit (Scanner module is available for use)
#pragma config LVP = ON         // Low Voltage Programming Enable bit (Low Voltage programming enabled. MCLR/Vpp pin function is MCLR.)

#pragma config CP = OFF         // UserNVM Program memory code protection bit (Program Memory code protection disabled)
#pragma config CPD = OFF        // DataNVM code protection bit (Data EEPROM code protection disabled)


#define _XTAL_FREQ 8000000
#define FOSC 8000000L

#define back_light RB0
#define led1_on RE0
#define KN1 RC3
#define KN2 RC2
#define KN3 RC4
#define KN4 RC5

#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 int secund,hour,minut,rain,get_times_run;
unsigned int rainmonth,rainyear,shifttick,inaktiv,firstrun,dayhold,show_rain,rains_run,shifttemp;
unsigned int rain_year[20],display3on;
unsigned int rainyear_part1,rainyear_part2,calyear,calmonth,oldcalmonth,caldate,calday;
unsigned int temphour_part1,temphour_part2,tempoldmontsmin_part1,tempoldmontsmin_part2,tempoldmontsmax_part1,tempoldmontsmax_part2;
unsigned int oldrainyears_part1,oldrainyears_part2;
unsigned int tempnow_part1,tempnow_part2,shift10sec,daypart,rainpart,monthpart,yp;
unsigned int tempdaymin,tempdaymax,temphourmin,temphourmax,temptick;
unsigned int tempdaymin_part1,tempdaymin_part2,tempdaymax_part1,tempdaymax_part2;
unsigned int temphours_T[20];
unsigned char timestr[6],minutstr[6],secstr[6],rainstr[6],datestr[6];
bit sec,rainincome,showrain,leapyear,dailymonth,LCDonoff,knap4trykket,data,ReadytoTime;

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;//0-32000
    char hele[10],deci[10]; //0-255
   char xpulse,x,negativ,maxxpulse; //0-255
char test[10]; //0-255
unsigned char raindays[20];  // 0 -255
unsigned int rainoldyears[40]; // 0 - 65000
unsigned char rainoldmonth[20]; // 0-255
unsigned char rain_OM[18],rain_OY[18];// 0-255
unsigned char temphours[40]; //0-255
unsigned int tempoldmonthsmin[80],tempoldmonthsmax[80];// 0-65536
volatile unsigned char week_day; //0-255
unsigned char mintime[3],maxx[3];// time where min temp 0-255
unsigned char maxtime[3],minx[3];// time where max temp 0-255
char mintemp[3],maxtemp[3]; //0-255 or -128-127
const char * const day_of_week_names[] = {"Mandag ",
                                          "Tirsdag",
                                          "Onsdag ",
                                          "Torsdag",
                                          "Fredag ",
                                          "Lordag ",
                                          "Sondag "
                                         };

unsigned int run;
char mintest[6];

bit data;


//--------------------------------
// Functions Declarations
uint8_t UART_Buffer = 0; // is a 8bit interger / 1.byte

//--------------------------------

void interrupt ISR (void)
{
if (RCIF==1)
   {
   CREN=0;
   UART_Buffer=RCREG;
   data=1;
   CREN=1;
  }

if(TMR1IF)             // Was this a timer overflow?
    {
     TMR1IF=0;        // Clear interrupt flag, ready for next
    sec=1;             // adds 2 sec.  to main loop
    }
}
void setup_init(void)
{
        // NOSC HFINTOSC; NDIV 4;
    OSCCON1 = 0x60;//62
    //O
  
    // CSWHOLD may proceed; SOSCPWR Low power;
    OSCCON3 = 0x00;
  
    // MFOEN disabled; LFOEN disabled; ADOEN disabled; SOSCEN disabled; EXTOEN disabled; HFOEN disabled;
    OSCEN = 0x00;
  
    // HFFRQ 4_MHz;
    OSCFRQ = 0x03;//0x02; // 4 mhz
  
    // HFTUN 0;
    OSCTUNE = 0x20;//center freq
  
    
      // CLKRMD CLKR enabled; SYSCMD SYSCLK enabled; SCANMD SCANNER enabled; FVRMD FVR enabled; IOCMD IOC enabled; CRCMD CRC enabled; NVMMD NVM enabled;
    PMD0 = 0x00;
    // TMR0MD TMR0 enabled; TMR1MD TMR1 enabled; TMR4MD TMR4 enabled; TMR5MD TMR5 enabled; TMR2MD TMR2 enabled; TMR3MD TMR3 enabled; NCOMD DDS(NCO) enabled; TMR6MD TMR6 enabled;
    PMD1 = 0x00;
    // ZCDMD ZCD enabled; DACMD DAC enabled; CMP1MD CMP1 enabled; ADCMD ADC enabled; CMP2MD CMP2 enabled;
    PMD2 = 0x00;
    // CCP2MD CCP2 enabled; CCP1MD CCP1 enabled; CCP4MD CCP4 enabled; CCP3MD CCP3 enabled; CCP5MD CCP5 enabled; PWM6MD PWM6 enabled; PWM7MD PWM7 enabled;
    PMD3 = 0x00;
    // CWG3MD CWG3 enabled; CWG2MD CWG2 enabled; CWG1MD CWG1 enabled; MSSP1MD MSSP1 enabled; UART1MD EUSART enabled; MSSP2MD MSSP2 enabled;
    PMD4 = 0x00;
    // DSMMD DSM enabled; CLC3MD CLC3 enabled; CLC4MD CLC4 enabled; SMT1MD SMT1 enabled; SMT2MD SMT2 enabled; CLC1MD CLC1 enabled; CLC2MD CLC2 enabled;
    PMD5 = 0x00;
      /*
       * WPUx registers
    */
 
    CDIV3=0;
    CDIV2=0;
    CDIV1=0;
    CDIV0=0;
      /**
    LATx registers
    */
    LATE = 0x00;
    LATD = 0x00;
    LATA = 0x00;
    LATB = 0x00;
    LATC = 0x00;
    /*TRISx registers  */
    TRISE0=0; // LED
    TRISE1=0;
    TRISE2=0;
   // TRISE3=1; // reset,
    TRISA0=0;
    TRISA1=0;
    TRISA2=0;
    TRISA3=0;
    TRISA4=1; // rain in
    TRISA5=0;
    TRISA6=1; //
    TRISA7=1; //
    TRISB0=0; // bk light
    TRISB1=0;
    TRISB2=0;
    TRISB3=0; // RS
    TRISB4=0;
    TRISB5=1; // temp in
    TRISB6=0;
    TRISB7=0;
    TRISC0=0;
    TRISC1=0;
    TRISC2=1; // KN2
    TRISC3=1; // KN1
    TRISC4=1; // KN3
    TRISC5=1; // KN4
    TRISC6=0; // TX
    TRISC7=1; // RX
    TRISD0=0; // DATA
    TRISD1=0; // DATA
    TRISD2=0; // DATA
    TRISD3=0; // Data
    TRISD4=0;
    TRISD5=0;
    TRISD6=0;
    TRISD7=0; // Enable
  
    /**
    ANSELx registers
    */
    ANSELD = 0x00;
    ANSELC = 0x00;
    ANSELB = 0x00;
    ANSELE = 0x00;
    ANSELA = 0x00;
  
    /**/
   
    WPUD = 0x00;
    WPUE = 0x00;
    WPUB = 0x00;
    WPUA = 0x00;
    WPUC = 0x00;

    /**
    ODx registers
    */
    ODCONE = 0x00;
    ODCONA = 0x00;
    ODCONB = 0x00;
    ODCONC = 0x00;
    ODCOND = 0x00;

    /**
    SLRCONx registers
    */
    SLRCONA = 0xFF;
    SLRCONB = 0x00;
    SLRCONC = 0xFF;
    SLRCOND = 0xFF;
    SLRCONE = 0x07;
 
      RC6PPS = 0x10;
    
       //timer 1 setup
T1CS0=0; // 0110 = SOCS
T1CS1=1;
T1CS2=1;
T1CS3=0;
T1CKPS0=0; // no prescale
T1CKPS1=0;
nT1SYNC=1; // no sync
  
lcd_init();  //lcd's init
lcd_goto(0);


}
/////////////   TEMPERATURE READING //////////////////////

  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();
             
                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;
            
                lcd_goto(0x4d);     
                //lcd3_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(0x50);
                lcd_puts(".");
                itoa(deci,fraction,10);
                lcd_puts(deci);
                if (sign!=0)
                {
        now=500-((total*10)+fraction); // add 50 degree, will not have negative numbers
                negativ=1;
                }
                if (sign==0)
                {now=500+((total*10)+fraction); // add 50 degree
                negativ=0;
                }
                              
                        
              
               }
} // end else max temp overload.
//////////////////TEMPERATURE READING ENDS  //////////////////////////////////

  void start_ur (void) // Start clock.
{
   RE0=0; // kill LED.
   IOCAF4=0; // RA4 clear interrupts
   IOCIE=1; // enable IOC
   TMR1IF=0; // clear interrupts  timer1
   TMR1IE=1;  // enable interrupr for timer1
   GIE=1; // enable global interrupr
 
}     // slut "sub "start ur"
      
void sluk_ur (void) // cut off clock.
{
   IOCIE=0; // disable IOC
   TMR1IE=0;  // disable interrupr for timer1
   GIE=0; // disable global interrupr
   secund=0;            // sets sec = 0
   RE0=1;         // turn on led

}// slut "sub sluk ur "
   
void vis_timedate (void) // show time and date
     {
   
     lcd_goto(0x03);
     lcd_puts("TIME AND DATE");
     lcd_goto(0x40);
     lcd_puts("Time is =    :  :");
     lcd_goto(0x4b);
     if (hour<10)lcd_puts("0");
     utoa(rainstr,hour,10);
     lcd_puts(rainstr);
     lcd_goto(0x4e);
     if (minut<10)lcd_puts("0");
     utoa(rainstr,minut, 10);
     lcd_puts(rainstr);
     lcd_goto(0x51);
     if (secund<10)lcd_puts("0");
     utoa(rainstr,secund,10);
     lcd_puts(rainstr);
     lcd_goto(0x14);
     lcd_puts("Date is =   -  -");
     lcd_goto(0x1e);
     if (caldate<10)lcd_puts("0");
     utoa(datestr,caldate,10);
     lcd_puts(datestr);
     lcd_goto(0x21);
     if (calmonth<10)lcd_puts("0");
     utoa(rainstr,calmonth,10);
     lcd_puts(rainstr);
     lcd_goto(0x24);
     utoa(rainstr,calyear,10);
     lcd_puts(rainstr);
     lcd_goto(0x54);
     lcd_puts("Day is ");
     for (unsigned char i=0;i<7;i++) lcd_putch(day_of_week_names[calday][i]);
   
     }
 
void stil_ur (void) //Setup clock
{
// new test
//first read on keypad, until key 3 is pressed,
    // until key3 only sets hour.
    // when 3 is pressed shift to next, ex minut. Maybee sets cursor
 
    int x,sets;
    lcd_clear();
    vis_timedate();     // time date show
    lcd_goto(0x4c);
    lcd_write(0x0d); //test cursor
    sets=0;
    x=0;
    do
    {
        if ((KN4==0) && (sets==0)) // button 4. counts hour up
        {
            hour++;                    // adds an hour
        if (hour==24) hour=0;     // if 24 then 0.
            lcd_write(0x0C); // kill kursor
        vis_timedate();            // update LCD
      
      
        if (x<5)
        {__delay_ms(255);         // wait a bit.
        __delay_ms(100);
        x++;
        }
        else // try to run faster
        {__delay_ms(100);
        }
        lcd_goto(0x4c);
        lcd_write(0x0D);// turn on cursor,
      
        }
        //////////////////////////////////////////////////////////
      
        if ((KN4==0) && (sets==1)) // button 4. counts minut up
        {
            minut++;                    // adds a minut
        if (minut==60) minut=0;     // if = 60 then 0.
        lcd_write(0x0C); // kill kursor
        vis_timedate();            // update LCD
      
      
        if (x<5)
        {__delay_ms(255);         // wait
        __delay_ms(100);
        x++;
        }
        else // try to run faster
        {__delay_ms(100);
        }
        lcd_goto(0x4f);
        lcd_write(0x0D);// turn on cursor
        }
     ///////////////////////////////////////////////////////////////////////
         if ((KN4==0) && (sets==2)) // button 4. counts day up
        {
            caldate++;                    // Adds a day
        if (caldate==32) caldate=1;     // if day = 32 then 1.
            lcd_write(0x0C); // kill kursor
        vis_timedate();            // update LCD
      
      
        if (x<5)
        {__delay_ms(255);         // waits.
        __delay_ms(100);
        x++;
        }
        else // try to run faster
        {__delay_ms(100);
        }
        lcd_goto(0x1f);
        lcd_write(0x0D);// turn on cursor
        }
    /////////////////////////////////////////////////////////////  
        if ((KN4==0) && (sets==3)) // button 4. counts month up
        {
            calmonth++;                    // Adds one month
        if (calmonth==13) calmonth=1;     // if month = 13 then 1.
            lcd_write(0x0C); // kill kursor
        vis_timedate();            // Update LCD
     
      
        if (x<5)
        {__delay_ms(255);         // waits
        __delay_ms(100);
        x++;
        }
        else // try to run faster
        {__delay_ms(100);
        }
         lcd_goto(0x22);
        lcd_write(0x0D);// turn on cursor}
      
        }
      
    /////////////////////////////////////////////////////////////  
        if ((KN4==0) && (sets==4)) // button 4. counts year up
        {
            calyear++;                    // adds year
            lcd_write(0x0C); // kill kursor
        vis_timedate();            // update LCD
     
      
        if (x<5)
        {__delay_ms(255);         // waits
        __delay_ms(100);
        x++;
        }
        else // try to run faster
        {__delay_ms(100);
        }
         lcd_goto(0x27);
        lcd_write(0x0D);// turn on cursor
        }
 
    /////////////////////////////////////////////////////////////  
        if ((KN3==0) && (sets==4)) // button 3. counts year down
        {
            calyear--;                    // remove year
            if (calyear==2018) calyear=2019; // go back to 2019
            lcd_write(0x0C); // kill kursor
        vis_timedate();            // update LCD
     
      
        if (x<5)
        {__delay_ms(255);         // waits
        __delay_ms(100);
        x++;
        }
        else // try to run faster
        {__delay_ms(100);
        }
         lcd_goto(0x27);
        lcd_write(0x0D);// turn on cursor
        }
    /////////////////////////////////////////////////////////////       
         if ((KN4==0) && (sets==5)) // button 4. counts weekdays
        {
            calday++;                    // adds another day
        if (calday==7) calday=0;     // if day = 7 then 0.
            lcd_write(0x0C); // kill kursor
        vis_timedate();            // Update LCD
      
      
        __delay_ms(255);         // waits
        __delay_ms(100);
      
      
        lcd_goto(0x5b);
        lcd_write(0x0D);// turn on cursor
        }
 
      
    /////////////////////////////////////////////////////////////       
        if (KN4==1) x=0; // released butt
        if ((!KN2) && (dayhold==0))  // change input
   {
            sets++;
         __delay_ms(255);         // waits.
         if (sets==6) sets=0; // if day sets. go back to hour.
         if (sets==0) lcd_goto(0x4c); // line 2 hour
         if (sets==1) lcd_goto(0x4f); //line 2, minut
         if (sets==2) lcd_goto(0x1f); // line 3 day.
         if (sets==3) lcd_goto(0x22); // line 3 month.
         if (sets==4) lcd_goto(0x27); // line 3 year.
         if (sets==5) lcd_goto(0x5b); // line 4 weekday
               }  
      
      
        if (KN2)  // if knap2 is low then set pressed to 0;;
    {
        dayhold=0; // to prevent running of things,
    }
    }
    while (KN1); // until kanp 1  pressed
    lcd_write(0x0C); // kill cursor
 

    eeprom_write(164,caldate);
           eeprom_write(165,calmonth);
           eeprom_write(166,calyear-2000);
           eeprom_write(167,calday);
}// end stil uret


 
void show_lcd3 (void)  // show temperatur on LCD 3 - just the text
     {
             lcd_clear();
             lcd_goto(0x04);
             lcd_puts("Temperaturen");
             lcd_goto(0x40);
             lcd_puts("Ude temp =       ");
          
             lcd_goto(0x14);
             lcd_puts("Min");
             lcd_goto(0x1d);
          
             lcd_goto(0x54);
             lcd_puts("Max");
         
     }
        
       void klokken (void)  // calc the time
        {
           
             if (secund>=60)  // if secund is 60
             {
             secund=0;
             minut++;      // og plusser et minut  add 1 minut
        
                   if (minut>=60)  // hvis minut = 60  // maybe an hour also
            {    minut=0;    // minut = 0
                hour++;        // og plusser en time. // add hour
              //  hourly_update();
                // run SUB to save hourly rain and temperatur.
            }
             if (hour==24) // if midnight
             {
                 hour=0;
                 temptick=0; // to stop from reading temp right now
             //    midnat(); // save to EEPROM
             
             // datoskift();       // change of date.
          
             }
           
             }  // end sub secund
     }  // end sub klokken
   
      
void EUSART_Write(uint8_t txData)
{
    while(0 == PIR3bits.TXIF)
    {
    }

    TX1REG = txData;    // Write the data byte to the USART.
}
void EUSART_Initialize(void)
{
    // Set the EUSART module to the options selected in the user interface.

    // ABDOVF no_overflow; SCKP Non-Inverted; BRG16 16bit_generator; WUE disabled; ABDEN disabled;
    BAUD1CON = 0x08;

    // SPEN enabled; RX9 8-bit; CREN disabled; ADDEN disabled; SREN disabled;
    RC1STA = 0x80;

    // TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
    TX1STA = 0x24;

    // SP1BRGL ;
    SP1BRGL = 0xCF;//19;

    // SP1BRGH 0;
    SP1BRGH = 0x00;
  
    CREN=1;
    RCIE=1;
    PEIE=1;
    GIE=1;

}



void main(void)
{
setup_init();
EUSART_Initialize();

RB0=1;  // turn on bk light
lcd_clear();
lcd_goto(0x03);
    lcd_puts("STARTER OP");
    lcd_goto(0x43);
    lcd_puts("VENT VENLIGST!");
  T1RD16=0; // turn on timer1 16 bit read write
   TMR1ON=1; // turn on timer1
   T1GE=0; // gate allways count
   TMR1IE=1;  // enable interrupr for timer1
   inaktiv=0;
    shift10sec=0; 
    daypart=0;
    temptick=0;

    while(1)
  {
         if (!KN2)  //(knap2==1)  // set time and date
         {
             inaktiv=0;
           
               int p;
                p=0;  
             while (p<15)    // repeat 1.5 second
             {
                __delay_ms(100);
                p++;
             }
                 if (!KN2) // if stilled pressed after 1,5 sec.
                 {
                     back_light=1;
                   
                     sluk_ur(); //  kill time
                     dayhold=1;
                     stil_ur(); // set time
                     start_ur();// start clock again
                   
             }
       
         } // end set time/date
           
           
       
         if (rainincome==1) // if rain RA4 active.
         {
             rain++;
             eeprom_write(0,rain);
             rainmonth++;
             rainyear++;
             rainincome=0;
           
         } // end rain
      
      
      
         if (sec==1) // if secund tick.
         {
             secund+=2;
             sec=0; 
             klokken();
       
       
       
            shifttick+=2; // count second to shift of LCD4
             inaktiv+=2; // second to inaktiv, kill backlight
            
             ////////////////////////////
           if (shifttick==4) // shift of LCD from Rain to DATE AND TIME and VS.
         {
             if (showrain) // show rain on LCD
                {
                    lcd_clear();
                    vis_regn(); // show rain
                    showrain=(!showrain);
                    shifttick=0;
                }
             else // shows temperature
                {   lcd_clear();
                    show_lcd3(); // all the text for temperature
                 if (!reset() == ONEWIRE_PRESENT)  //read temperature
                {
                      
                        laestemp();  // show temperature
                  
                       }
                   showrain=(!showrain);
                    shifttick=0;
                }   
            } // end shifttick
           
////////////////////////////          
             
////////////////////////////                 
             
         }  // end second tick
       
      
        
      
        if ((inaktiv<=1) && (back_light==0)) // inaktiv and light out
                    {back_light=1;
                     }
         if (inaktiv==30) back_light=0;
       
   
      if (data==1)
      {
       if ((UART_Buffer==8) && (ReadytoTime==0))
          {
           EUSART_Write(hour);
           EUSART_Write(minut);
           EUSART_Write(caldate);
           EUSART_Write(calmonth);
           EUSART_Write(calyear-2000);
           EUSART_Write(calday);
            __delay_ms(10);
            data=0;
      }
    
       if (ReadytoTime==1)  // recieve time and date
      {
           if (get_times_run==0) hour=UART_Buffer;
           if (get_times_run==1) minut=UART_Buffer;
           if (get_times_run==2) caldate=UART_Buffer;
           if (get_times_run==3) calmonth=UART_Buffer;
           if (get_times_run==4) calyear=2000+UART_Buffer;
           if (get_times_run==5) calday=UART_Buffer;
           if (get_times_run==5) ReadytoTime=0;
         
                     get_times_run++;
       data=0;
     
      }
       if ((UART_Buffer==0x07) && (ReadytoTime==0))// We send 7 to PIC ready to send time/date
       {
         data=0;    
       ReadytoTime=1; // ready to recieve time
       get_times_run=0;
       }
          
   
    
    }
}


//--------------------------------
// Functions Definitions
Mod edit: 'C' code tags
 
Last edited by a moderator:

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
I assume my sensor is working correct, cause I have tried it on an older working configuration. Sensor = OK
My delay I think Is correct, by testing a delay routine, 100*100 ms delay = 10 sec, by stopwatch = OK
Pin has been swapped to RE1, for testing, gives me same result = NOT OK.
The PIC is sending reset/read command, according to the LogicAnalyser, = OK.
PIC is just not handeling the incoming data as anything that 0....
The code is copied from the working configurations, and should therefor be ok,
PIC input is checked several times, and is set as DIGITAL I/O, Voltages on input pin is 5v. so no need to read 0 ??

It's driving me nuts.
 
I assume my sensor is working correct, cause I have tried it on an older working configuration. Sensor = OK
My delay I think Is correct, by testing a delay routine, 100*100 ms delay = 10 sec, by stopwatch = OK
Pin has been swapped to RE1, for testing, gives me same result = NOT OK.
The PIC is sending reset/read command, according to the LogicAnalyser, = OK.
PIC is just not handeling the incoming data as anything that 0....
The code is copied from the working configurations, and should therefor be ok,
PIC input is checked several times, and is set as DIGITAL I/O, Voltages on input pin is 5v. so no need to read 0 ??

It's driving me nuts.
I have not gone through your code very carefully, but....

C:
  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;
}
Shouldn't it be if (Port_18B20 == 0) not if (Port_18B20 == 1) as you have it coded.
 

Thread Starter

FroceMaster

Joined Jan 28, 2012
699
Yes it will make sence . But the code is working in another microchip. 16F1519.
And in an other project with 16F18877.but without Uart/BT. So i think it's Something in setup.
 
Top