Hi
have this code, and it works partly, but it allways sends the Hex 80. around every 2-3 second.
HC-05 and PIC 16F690
If i send a command to PIC, it return what i ask for, so connections is working,
have this code, and it works partly, but it allways sends the Hex 80. around every 2-3 second.
HC-05 and PIC 16F690
If i send a command to PIC, it return what i ask for, so connections is working,
Code:
/*
*/
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <xc.h>
// CONFIG
#pragma config FOSC = INTRCIO // Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA4/OSC2/CLKOUT pin, I/O function on RA5/OSC1/CLKIN)
#pragma config WDTE = ON // Watchdog Timer Enable bit (WDT enabled)
#pragma config PWRTE = OFF // Power-up Timer Enable bit (PWRT disabled)
#pragma config MCLRE = OFF // MCLR Pin Function Select bit (MCLR pin function is digital input, MCLR internally tied to VDD)
#pragma config CP = OFF // Code Protection bit (Program memory code protection is disabled)
#pragma config CPD = OFF // Data Code Protection bit (Data memory code protection is disabled)
#pragma config BOREN = ON // Brown-out Reset Selection bits (BOR enabled)
#pragma config IESO = ON // Internal External Switchover bit (Internal External Switchover mode is enabled)
#pragma config FCMEN = ON // Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is enabled)
#define _XTAL_FREQ 8000000
#define FOSC 8000000L
#define Skip_ROM 0xCC
#define Convert_T 0x44
#define Read_scratchpad 0xBE
#define Port1_18B20 RB4
#define Tx1_18B20 TRISB4 = 0
#define Rx1_18B20 TRISB4 = 1
#define Port2_18B20 RC6
#define Tx2_18B20 TRISC6 = 0
#define Rx2_18B20 TRISC6 = 1
#define Port3_18B20 RC7
#define Tx3_18B20 TRISC7 = 0
#define Rx3_18B20 TRISC7 = 1
#define WAIT1 1000
#define WAIT2 500
#define DS18B20_CONV_TIME 750//750
#define DS18B20_RESET_PULSE 480//480
#define DS18B20_WAIT_TIME 60//60
#define DS18B20_PULLUP_TIME 2//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;
unsigned int run,readwhattemp;
char negativ;
signed int total,fraction,now,temp1,temp2,temp3,tempmin1,tempmin2,tempmin3,tempmax1,tempmax2,tempmax3,oktemp1,get_times_run;
signed int tempmin1_part1,tempmin1_part2,tempmax1_part1,tempmax1_part2,tempmin2_part1,tempmin2_part2,tempmax2_part1,tempmax2_part2;
signed int tempmin3_part1,tempmin3_part2,tempmax3_part1,tempmax3_part2,tempmin1OK,tempmin2OK,tempmin3OK,tempmax1OK,tempmax2OK,tempmax3OK;
__bit data,recieving,readytorecieve;
uint8_t UART_Buffer = 0; // is a 8bit integer / 1.byte
void __interrupt() isr (void)
{
if (RCIF==1)
{
CREN=0;
UART_Buffer=RCREG;
data=1;
CREN=1;
}
}
void setup (void)
{
IRCF0=1;
IRCF1=1;
IRCF2=1; // 8 mhz
SCS=1; // internal clock
TRISA0=1; // input data
TRISA1=1; // data
TRISA2=1;
TRISA3=1; // reset
TRISA4=1;
TRISA5=1;
TRISB4=1;//input Temp1
TRISB5=1;//Input RX
TRISB6=1;
TRISB7=0; // out TX
TRISC0=0; // Relay on
TRISC1=1;
TRISC2=1;
TRISC3=1;
TRISC4=1;
TRISC5=1;
TRISC6=1; //Temp 2
TRISC7=1; //Temp 3
ANSEL=0;
ANSELH=0;
}
void EUSART_Write(uint8_t txData)
{
while(0 == PIR1bits.TXIF)
{
}
TXREG = txData; // Write the data byte to the USART.
}
void EUSART_Initialize(void)
{
// TRISC6 = 0; // As stated in the datasheet
// TRISC7 = 1; // As stated in the datasheet
// ANSELC=0; // digital
// 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;
BAUDCTL = 0x08;
// SPEN enabled; RX9 8-bit; CREN disabled; ADDEN disabled; SREN disabled;
RCSTA = 0x80;
// TX9 8-bit; TX9D 0; SENDB sync_break_complete; TXEN enabled; SYNC asynchronous; BRGH hi_speed; CSRC slave;
TXSTA = 0x24;
// SP1BRGL 25;
SPBRG = 207;//0x08;//19;
// SP1BRGH 0;
SPBRGH = 0x00;
CREN=1;
RCIE=1;
PEIE=1;
GIE=1;
}
unsigned char reset()
{
if (readwhattemp==1)
{Tx1_18B20;
Port1_18B20 = 0;
__delay_us(DS18B20_RESET_PULSE);
Rx1_18B20;
__delay_us(DS18B20_WAIT_TIME);
if (Port1_18B20 == 1)
{
__delay_us(DS18B20_RESET_PULSE);
return ONEWIRE_PRESENT;
}
__delay_us(DS18B20_RESET_PULSE);
return ONEWIRE_ABSENT;
}
if (readwhattemp==2)
{Tx2_18B20;
Port2_18B20 = 0;
__delay_us(DS18B20_RESET_PULSE);
Rx2_18B20;
__delay_us(DS18B20_WAIT_TIME);
if (Port2_18B20 == 1)
{
__delay_us(DS18B20_RESET_PULSE);
return ONEWIRE_PRESENT;
}
__delay_us(DS18B20_RESET_PULSE);
return ONEWIRE_ABSENT;
}
if (readwhattemp==3)
{Tx3_18B20;
Port3_18B20 = 0;
__delay_us(DS18B20_RESET_PULSE);
Rx3_18B20;
__delay_us(DS18B20_WAIT_TIME);
if (Port3_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;
if(readwhattemp==1)
{
Rx1_18B20;
for(i = 0; i < 8; i++)
{
if((Cmd & (1<<i))!= 0)
{
Tx1_18B20;
Port1_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx1_18B20;
__delay_us(DS18B20_WAIT_TIME);
}
else
{
Tx1_18B20;
Port1_18B20 = 0;
__delay_us(DS18B20_WAIT_TIME);
Rx1_18B20;
}
}
}
if (readwhattemp==2)
{
Rx2_18B20;
for(i = 0; i < 8; i++)
{
if((Cmd & (1<<i))!= 0)
{
Tx2_18B20;
Port2_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx2_18B20;
__delay_us(DS18B20_WAIT_TIME);
}
else
{
Tx2_18B20;
Port2_18B20 = 0;
__delay_us(DS18B20_WAIT_TIME);
Rx2_18B20;
}
}
}
if (readwhattemp==3)
{
Rx3_18B20;
for(i = 0; i < 8; i++)
{
if((Cmd & (1<<i))!= 0)
{
Tx3_18B20;
Port3_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx3_18B20;
__delay_us(DS18B20_WAIT_TIME);
}
else
{
Tx3_18B20;
Port3_18B20 = 0;
__delay_us(DS18B20_WAIT_TIME);
Rx3_18B20;
}
}
}
}
unsigned char read()
{
char i,result = 0;
if (readwhattemp==1)
{
Rx1_18B20;
for(i = 0; i < 8; i++)
{
Tx1_18B20;
Port1_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx1_18B20;
if(Port1_18B20 != 0)
{
result |= 1<<i;//i
}
__delay_us(DS18B20_WAIT_TIME);
}
} // end readwahttemp 1
if (readwhattemp==2)
{
Rx2_18B20;
for(i = 0; i < 8; i++)
{
Tx2_18B20;
Port2_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx2_18B20;
if(Port2_18B20 != 0)
{
result |= 1<<i;//i
}
__delay_us(DS18B20_WAIT_TIME);
}
} // end readwahttemp 3
if (readwhattemp==3)
{
Rx3_18B20;
for(i = 0; i < 8; i++)
{
Tx3_18B20;
Port3_18B20 = 0;
__delay_us(DS18B20_PULLUP_TIME);
Rx3_18B20;
if(Port3_18B20 != 0)
{
result |= 1<<i;//i
}
__delay_us(DS18B20_WAIT_TIME);
}
} // end readwahttemp 3
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;
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;
}
switch (readwhattemp)
{ case 1:
temp1=now;
readwhattemp=2;
break;
case 2:
temp2=now;
readwhattemp=3;
break;
case 3:
temp3=now;
readwhattemp=1;
break;
default:
break;
}
}
void tjekifoktemp(void)
{tempmin1OK=0; // set all to 0.
tempmin2OK=0;
tempmin3OK=0;
tempmax1OK=0;
tempmax2OK=0;
tempmax3OK=0;
if (temp1>tempmin1) tempmin1OK=1;
if (temp1<tempmax1) tempmax1OK=1;
if (temp2>tempmin2) tempmin2OK=1;
if (temp2<tempmax2) tempmax2OK=1;
if (temp3>tempmin3) tempmin3OK=1;
if (temp3<tempmax3) tempmax3OK=1;
if (tempmin1OK==1 && tempmin2OK==1 && tempmin3OK==1)
{//do nothing all is bigger degree than min
}
else
{
RC0=1;
}
if (tempmax1OK==1 && tempmax2OK==1 && tempmax3OK==1)
{ // all is ok
}
else // turn off heater, cause over max degree.
{ RC0=0;
}
}
void main(void)
{
setup();
EUSART_Initialize();
readwhattemp=1;
temp1=0;
temp2=0;
temp3=0;
TXEN=1;
SYNC=0;
SPEN=1;
while(1)
{
if (data==1)
{
data=0;
readytorecieve=0; // 0= can reci, 1= not this time
if ((UART_Buffer==0x01) && (recieving==0)) // send temp 1-3, incl min&max
{
EUSART_Write(temp1/256); // gives hole number
EUSART_Write(temp1-(256*(temp1/256)));
EUSART_Write(temp2/256); // gives hole number
EUSART_Write(temp2-(256*(temp2/256)));
EUSART_Write(temp3/256); // gives hole number
EUSART_Write(temp3-(256*(temp3/256)));
EUSART_Write(tempmin1/256); // gives hole number
EUSART_Write(tempmin1-(256*(tempmin1/256)));
EUSART_Write(tempmax1/256); // gives hole number
EUSART_Write(tempmax1-(256*(tempmax1/256)));
EUSART_Write(tempmin2/256); // gives hole number
EUSART_Write(tempmin2-(256*(tempmin2/256)));
EUSART_Write(tempmax2/256); // gives hole number
EUSART_Write(tempmax2-(256*(tempmax2/256)));
EUSART_Write(tempmin3/256); // gives hole number
EUSART_Write(tempmin3-(256*(tempmin3/256)));
EUSART_Write(tempmax3/256); // gives hole number
EUSART_Write(tempmax3-(256*(tempmax3/256)));
}
if ((UART_Buffer==0x02) && (recieving==0)) // Recieve data
{ // set something active, so i know i recieve data.
recieving=1;
readytorecieve=1; // to pass next statement
get_times_run=0;
}
if ((UART_Buffer==0x03) && (recieving==0)) // check if data is ok,
{
}
if ((recieving==1)&& (readytorecieve==0))// recieving data. send one at a time, more easy
{ //
//recieving=0; // maybe send 6 more, to seperate into 2,
if (get_times_run==0) tempmin1_part1=UART_Buffer;
if (get_times_run==1) tempmin1_part2=UART_Buffer;
if (get_times_run==2) tempmax1_part1=UART_Buffer;
if (get_times_run==3) tempmax1_part2=UART_Buffer;
if (get_times_run==4) tempmin2_part1=UART_Buffer;
if (get_times_run==5) tempmin2_part2=UART_Buffer;
if (get_times_run==6) tempmax2_part1=UART_Buffer;
if (get_times_run==7) tempmax2_part2=UART_Buffer;
if (get_times_run==8) tempmin3_part1=UART_Buffer;
if (get_times_run==9) tempmin3_part2=UART_Buffer;
if (get_times_run==10) tempmax3_part1=UART_Buffer;
if (get_times_run==11)
{tempmax3_part2=UART_Buffer;
recieving=0;
// add to hole
tempmin1=(tempmin1_part1*256)+tempmin1_part2;
tempmax1=(tempmax1_part1*256)+tempmax1_part2;
tempmin2=(tempmin2_part1*256)+tempmin2_part2;
tempmax2=(tempmax2_part1*256)+tempmax2_part2;
tempmin3=(tempmin3_part1*256)+tempmin3_part2;
tempmax3=(tempmax3_part1*256)+tempmax3_part2;
}
get_times_run++;
// slit data into 6.,
}
} // end interrupt data
// check temp
// dont want to read temp all the time, so will add a delay, like run 100 times with no reading, then read 1, 100, read 2, 100, read 3 ect.
run++;
if (run==100)
{ run=0;
{// laestemp();//read temp-
// tjekifoktemp(); // if i need to turn on/off relay
}
}
}
}