#include #include #include #include __CONFIG(FOSC_INTRCIO & WDTE_OFF & MCLRE_ON); #define _XTAL_FREQ 4000000 #define DEBUG 1 #define LCD 1 #define LCD_DATA PORTC #define LCD_DATA_TRIS TRISC #define LCD_EN_TRIS TRISC4 #define LCD_EN RC4 #define LCD_RS_TRIS TRISC5 #define LCD_RS RC5 #define LCD_STROBE() ((LCD_EN = 1),(__delay_us(2)),(LCD_EN = 0)) #define ACTLED RC7 #define OW_TRIS TRISA0 #define OW_PORT RA0 #define AD_PORT RA2 void lcdinit(void); void lcdwrite(unsigned char c); void lcdclear(void); void lcdtxt(unsigned char * s); void lcdtxtline1(unsigned char * s); void lcdtxtline2(unsigned char * s); void lcdchr(unsigned char c); void lcdgoto(unsigned char pos); void serialinit(void); void serialtxt(unsigned char * s); void serialchr(unsigned char c); int owInit(void); int owReset(void); void owWriteByte(unsigned int data); void owWriteBit(unsigned int b); int owReadByte(void); int owReadBit(void); void owMatchRom(unsigned int * id); void memWrite(unsigned int addr, unsigned int data); int memRead(unsigned int addr); void owPrintID(void); void owPrintTemp(unsigned int * id); const unsigned int tA1[8] = {0x28,0x61,0xBC,0x93,0x03,0x00,0x00,0x09}; const unsigned int tA2[8] = {0x28,0xD7,0xC8,0x93,0x03,0x00,0x00,0xCE}; unsigned int tID[8]; unsigned int owFound = 0; void adc_init(void); int adc_sample(void); void main() { //Disable analog functions ANSEL = 0x00; ANSELH = 0x00; GIE = 1; //Global Interrupt Enable RABIE = 1; //Port A/B Interrupt Enable #if LCD //Initialize LCD lcdinit(); lcdclear(); #if DEBUG lcdtxtline1("SerialLCD v0.1"); lcdtxtline2("Initializing..."); __delay_ms(500); #endif #endif //Initialize Serial //lcdtxtline2("Init: COMM"); //serialinit(); //lcdtxt(" OK"); //ADC Init #if DEBUG && LCD lcdclear(); lcdtxtline2("ADC Init..."); #endif adc_init(); #if DEBUG && LCD __delay_ms(1000); #endif //Initialize 1-Wire owInit(); lcdclear(); int owShow = 0; while(1==1) { /*if(owFound && !owShow) { lcdgoto(0x00); owPrintID(); owShow = 1; }*/ if(owFound) { lcdgoto(0x00); lcdtxt("Buiten: "); owPrintTemp(tA1); lcdgoto(0x40); lcdtxt("Binnen: "); owPrintTemp(tA2); } int data = adc_sample(); lcdgoto(0x00 + 12); char cText[4]; sprintf(cText,"%4d",data); lcdtxt(cText); //26 - 16 = 10 = bereik //1024 / 10 = 102,4 = 102 int range = (data / 102) + 16; lcdgoto(0x40 + 12); char cText2[2]; sprintf(cText2,"%2d",range); lcdtxt(cText2); ACTLED = 0; __delay_ms(100); ACTLED = 1; __delay_ms(100); } } void adc_init(void) { TRISA2 = 1; //Set RA2 to input mode ANS2 = 1; //Set RA2 to analog input ADFM = 1; //Right justify ADON = 1; //ADC Enable CHS0 = 0; CHS1 = 1; //Analog 2 CHS2 = 0; CHS3 = 0; ADCS0 = 1; ADCS1 = 0; ADCS2 = 0; } int adc_sample(void) { GO = 1; while(GO) { //wait } int data = (ADRESH << 8) + ADRESL; return data; } void owPrintTemp(unsigned int * id) { //Issue 'ConvertT' owMatchRom(id); owWriteByte(0x44); //Wait 2seconds for converstion __delay_ms(2000); //Request read out owMatchRom(id); owWriteByte(0xBE); int scratch[9]; for(int i=0;i<8;i++) { scratch[i]=owReadByte(); } int Temp= (scratch[1]<<8) + scratch[0]; Temp=Temp>>4; //Drop tens,hundreds,thousands char cText[16]; sprintf(cText,"%3d",Temp); lcdtxt(cText); owReset(); __delay_ms(10); } void owPrintID(void) { char txt[16]; sprintf(txt,"%2X%2X%2X%2X%2X%2X%2X%2X", tID[0],tID[1],tID[2],tID[3], tID[4],tID[5],tID[6],tID[7]); lcdtxt(txt); } int owInit(void) { if(owReset()) { #if LCD lcdgoto(0x40 + 7); lcdtxt("1-Wire found"); lcdclear(); lcdgoto(0x00); #endif //After 1-wire has been reset //issue 0x33, read 64bit address, single device on bus owWriteByte(0x33); //Read command //Now read 64bits //64bit / 8bit = 8bytes for(int i = 0;i<9;i++) { int b = owReadByte(); tID[i]=b; } #if LCD lcdgoto(0x40); owPrintID(); #if DEBUG __delay_ms(3000); #endif #endif owFound = 1; __delay_ms(250); } } void owMatchRom(unsigned int * id) { owReset(); owWriteByte(0x55); //Match ROM for(int i = 0;i<8;i++) { owWriteByte(id[i]); } } int owReadByte(void) { int loop, result = 0; for(loop = 0; loop < 8; loop++) { result >>= 1; if(owReadBit()) result |= 0x80; } return result; } int owReadBit(void) { unsigned int iReadState = 0; OW_TRIS = 0; //Set as output OW_PORT = 0; //Drive low __delay_us(4); OW_TRIS = 1; //Release, set as input __delay_us(8); iReadState = OW_PORT; return iReadState; } void owWriteByte(unsigned int data) { int loop; for(loop = 0; loop < 8; loop++) { owWriteBit(data &0x01); data >>= 1; } } void owWriteBit(unsigned int b) { OW_TRIS = 0; //Set as output OW_PORT = 0; //Drive low if(b==1) { __delay_us(3); OW_TRIS = 1; //Release, set as input __delay_us(62); } else { __delay_us(57); OW_TRIS = 1; //Release, set as input __delay_us(7); } } int owReset(void) { char state = 0; OW_TRIS = 0; //Set as output OW_PORT = 0; //Drive Low __delay_us(480); OW_TRIS = 1; //Release, Set back as input __delay_us(70); state = !OW_PORT; __delay_us(410); return state; } void serialinit(void) { SPBRG = 207; BRGH = 0; BRG16 = 0; TXEN = 1; //Enables Transmitter circuitery SYNC = 0; //Enable async mode SPEN = 1; // } void serialtxt(unsigned char * s) { while(*s) serialchr(*s++); } void serialchr(unsigned char c) { while(!TXIF){} TXREG = c; } void lcdinit(void) { LCD_DATA_TRIS = 0x00; //Set port as output LCD_DATA = 0x00; //PORTC Clear LCD_EN_TRIS = 0; //Set pin as output LCD_RS_TRIS = 0; //Set pin as output LCD_EN = 0; LCD_RS = 0; __delay_ms(20); //Wait after power-on LCD_DATA = 0x02; //See datasheet CG046 LCD_STROBE(); //send 2 times 0010 __delay_ms(5); LCD_STROBE(); __delay_ms(1); LCD_DATA = 0x0C; //2-line, 5x10 LCD_STROBE(); __delay_ms(1); LCD_DATA = 0x00; //0000 1111 0x0F Display on, cursor on, blink on LCD_STROBE(); //0000 1110 0x0E Display on, cursor on, blink off LCD_DATA = 0x0C; //0000 1100 0x0C Display on, cursor off, blink off LCD_STROBE(); //Blink On __delay_ms(1); LCD_DATA = 0x00; //0000 0001 LCD_STROBE(); //Clear LCD_DATA = 0x01; LCD_STROBE(); __delay_ms(2); LCD_DATA = 0x00; //Increment, no shift LCD_STROBE(); //0000 0110 LCD_DATA = 0x06; LCD_STROBE(); __delay_ms(2); } void lcdcmd(unsigned char c) { __delay_us(40); LCD_RS = 0; //Command mode LCD_DATA = ( ( c >> 4 ) & 0x0F ); LCD_STROBE(); LCD_DATA = ( c & 0x0F ); LCD_STROBE(); __delay_ms(2); } void lcdchr(unsigned char c) { __delay_us(40); LCD_DATA = ( ( c >> 4) & 0x0F ); LCD_RS = 1; LCD_STROBE(); LCD_DATA = ( c & 0x0F ); LCD_RS = 1; LCD_STROBE(); } void lcdtxt(unsigned char * s) { while(*s) lcdchr(*s++); } void lcdtxtline1(unsigned char * s) { lcdgoto(0x00); lcdtxt(" "); lcdgoto(0x00); lcdtxt(s); } void lcdtxtline2(unsigned char * s) { lcdgoto(0x40);//second line lcdtxt(" "); lcdgoto(0x40);//second line lcdtxt(s); } void lcdclear(void) { LCD_RS = 0; lcdcmd(0x01); } void lcdgoto(unsigned char pos) { LCD_RS = 0; lcdcmd(0x80+pos); }