/*
 * File:   newmain.c
 * Author: User
 *
 * Created on 19 ?????? 2024 ?., 14:20
 */

#include <htc.h>

#pragma config WDTE=OFF, MCLRE=OFF, FOSC=INTRCIO, CP=OFF

#define _XTAL_FREQ 4000000
#define DIO GPIO4 //pin7
#define CLK GPIO5 //pin6
#define DIOio TRISIO4 //SDA input or output 
#define sr GPIO2		//sensor  pin2
#define sr_in TRISIO2=1		//sensor is input
#define sr_out TRISIO2=0		//sensor is output
//prototypes
void display( unsigned char addr, unsigned char fig );
void sendData(unsigned char dataB);
void startBit(void);
void stopBit(void);
void clock(void);
unsigned char sensor_rst();
unsigned char reply ();
void cmnd_w(unsigned char cmnd);
unsigned char addr=0x78;  //0b1111000
const unsigned char  num[] = {0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f}; //0,1,2,3...9

void main(void){
    unsigned char i;
    GPIO = 0b00000000; 
    TRISIO = 0b00001100; // 
    
    ANSEL = 0; // 
    CMCON = 7; //
	
    OPTION_REG = 0;	//pullup
   
    CLK=1;
    DIO=1;
     for(i=0;i<4;++i){
        display(i,0x00);     //blanks
    }
  
    
	while(1){
    int tempL, tempH, tempS;
    unsigned int temp;
	unsigned char minus;//     
    int tDec;
    unsigned char d0,d1,d2,d3;	//
        //read sensor
	sensor_rst();	//sensor init
	cmnd_w(0xCC);	//skip ROM command
	cmnd_w(0xBE);	//read pad command
	tempL=reply();	//LSB of temp
	tempH=reply();	//MSB of temp
	sensor_rst();
	sensor_rst();
	cmnd_w(0xCC);	//skip ROM command
	cmnd_w(0x44);	//start conversion command
    
    tempS = tempL + (tempH << 8);	//calculate temp

    if(tempS<0){
        minus=1; 
        temp=65536-tempS;   //convert to unsigned integer
    }else{
        minus=0;
        temp=(unsigned int)tempS;
    }
    
    d0=((temp & 15)*10)/16;
    tDec=temp/16;
    d1=tDec %10;
    d2=(tDec/10) %10;
    if(tDec>99){d3=(tDec/100) %10;
    display(0,num [d3]);
    display(1,num [d2]);
	display(2,num [d1]+ 0x80);
    display(3,num [d0]); 
    }
 	if(minus){
    display(0,0b01000000);
    if(d2==0){
    display(1,num [d1]+ 0x80);
    display(2,num [d0]);
    display(3,0b01100011);
    }
    display(1,num [d2]);
    display(2,num [d1]+ 0x80);
	display(3,num [d0]);
    } 
    else{
     if(d2==0){display(0,0x00);}
     display(0,num [d2]); 
     display(1,num [d1]+ 0x80);
     display(2,num [d0]);
     display(3,0b01100011);
    } 	
        __delay_ms(1000);
     
}
}

unsigned char reply (){	//reply from sensor
	unsigned char ret=0,i;
	
        for(i=0;i<8;i++){	//read 8 bits
            sr_out; sr=0; __delay_us(2); sr_in; __delay_us(6);
            if(sr){ret += 1 << i;} __delay_us(80);	//output high=bit is high
        }
	sr_in;
    
	return ret;
}

void cmnd_w(unsigned char cmnd){	//send command temperature sensor
	unsigned char i;
	
        for(i=0;i<8;i++){	//8 bits 
            if(cmnd & (1 << i)){sr_out; sr=0; __delay_us(2); sr_in; __delay_us(80);}
            else{sr_out; sr=0; __delay_us(80); sr_in; __delay_us(2);}	//hold output low if bit is low 
        }
        sr_in;
       
    }    
	


unsigned char sensor_rst(){		//reset the temperature
	
    
        sr_out;
        sr=0; __delay_us(600);
        sr_in; __delay_us(100);
        __delay_us(600);
        return sr;	//return 0 for sensor present
	
}

void sendData(unsigned char dataB)
{
    for(unsigned char b=0;b<8;b++){
       DIO=(dataB >> b) % 2;
       clock();
    }
    DIOio=1;   //make DIO input
    clock();
    __delay_us(5);
    DIOio=0;   //make DIO output

}

void clock(void)
{
   __delay_us(4);
   CLK=1;
   __delay_us(5);
   CLK=0;
   __delay_us(4);
}

void startBit(void)
{
    DIO=0;
    __delay_us(5);
    CLK=0;
}

void stopBit(void)
{
    CLK=1;
    __delay_us(5);
    DIO=1;
}

    //includes brightness and point setting.
void display( unsigned char addr, unsigned char fig ){
    unsigned char mode=0b01000100;// control=0b10001100;  //0b10001010
    unsigned char brightness=7; //brightness can be selected 0 to 7
    unsigned char control=136+brightness;
   // fig=fig+0x80;    //0x80 is point 
    addr +=192;
    startBit();
    sendData(mode);            // command1-data 
    stopBit();
    startBit();
    sendData(addr);            // command2-address
    sendData(fig);
    stopBit();
    startBit();
    sendData(control);             // command3-control
    stopBit();
}
