#include #include #include #define _XTAL_FREQ 6000000 #define Baudrate 1200 //bps #define OneBitDelay (1000000/Baudrate) #define DataBitCount 8 // no parity, no flow control #define UART_RX GPIO,GP3 // UART RX pin #define UART_TX GPIO,GP1 // UART TX pin #define SCL GPIO,GP0 #define SDA TRISIO2 #define SCL_IN GPIO,GP0 #define SDA_IN GPIO,GP2 #define TMR0_2 (TMR0 & 1<<2) #define LM75AD 0b10010000 #pragma config "FOSC=EC" #pragma config "WDTE=OFF" #pragma config "PWRTE=ON" #pragma config "MCLRE=OFF" #pragma config "CP=OFF" #pragma config "CPD=OFF" #pragma config "BOREN=OFF" unsigned char count; unsigned char tempmsb; unsigned char templsb; unsigned char tempBCD1; unsigned char tempBCD2; unsigned char loop; unsigned char SEC; unsigned char DATARDY; unsigned char Tick,Long; unsigned char DataValue; unsigned char TXENABLE; unsigned char i; unsigned char shift; void i2c_dly(void) { _delay(50); } void i2c_start(void) { SDA = 1; i2c_dly(); SCL = 1; i2c_dly(); SDA = 0; GPIO = 0b11011; i2c_dly(); SCL = 0; i2c_dly(); } void i2c_stop(void) { SDA = 0; GPIO = 0b11010; // i2c stop bit sequence i2c_dly(); SCL = 1; i2c_dly(); SDA = 1; i2c_dly(); } bit i2c_tx(unsigned char d) { char x; static bit b; for(x=8; x; x--) { if(d&0x80) SDA = 1; else SDA = 0; i2c_dly(); GPIO = 0b11010; i2c_dly(); SCL = 1; i2c_dly(); d <<= 1; SCL = 0; i2c_dly(); } SDA = 1; i2c_dly(); SCL = 1; i2c_dly(); b = SDA_IN; // possible ACK bit i2c_dly(); SCL = 0; i2c_dly(); return b; } unsigned char i2c_rx(char ack) { unsigned char x, d=0; SDA = 1; i2c_dly(); for(x=0; x<8; x++) { d <<= 1; SCL = 1; i2c_dly(); if(SDA_IN) d |= 1; i2c_dly(); SCL = 0; i2c_dly(); } if(ack) SDA = 0; else SDA = 1; i2c_dly(); // send (N)ACK bit SCL = 1; i2c_dly(); SCL = 0; SDA = 1; return (d) ; } void InitSoftUART(void) // Initialize UART pins to proper values { UART_TX = 1; // TX pin is high in idle state } void interrupt isr(void) { asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); asm("nop"); if(T0IF) { TMR0=205; T0IF = 0; GIE= 1; loop--; } if (loop==0) { loop=200; Long--; if (Long==0) { SEC++; Long=6; } } if (TXENABLE==1) { if (i==0) { UART_TX = 0; } else if (i==9) { UART_TX = 1; i=0; TXENABLE=0; } if(i>0 & i<9) { shift = i-1; if( ((DataValue>>shift) &0x1) == 0x1 ) { UART_TX = 1; } else //if Bit is low { UART_TX = 0; } } i++; } } void main() { unsigned char ch = 0; i=0; ADCON0=0; ANSEL=0; CMCON0=7; VRCON = 0b00000000; TRISIO = 0b10100; GPIO = 0b10011; T0CS = 0; // bit 5 TMR0 Clock Source Select bit...0 = Internal Clock (CLKO) 1 = Transition on T0CKI pin T0SE = 0; // bit 4 TMR0 Source Edge Select bit 0 = low/high 1 = high/low PSA = 0; // bit 3 Prescaler Assignment bit...0 = Prescaler is assigned to the Timer0 PS2 = 0; // bits 2-0 PS2:PS0: Prescaler Rate Select bits PS1 = 1; PS0 = 1; TMR0 = 178; // preset for timer register T0IE = 1; PSA = 0; PEIE = 1; GIE = 1; TXENABLE = 0; tempmsb=0; templsb=0; count=0; Tick=200; Long= 6; SEC=0; loop=200; //T0IE=1; //GIE=1; InitSoftUART(); while(1) { if (SEC==30) { SEC=0; i2c_dly; //Set Pointer to the Temperature Register i2c_start(); i2c_tx(0b10010000); i2c_tx(0b00000000); i2c_start(); i2c_tx(0b10010001); tempmsb=i2c_rx(1); templsb=i2c_rx(0); i2c_stop(); tempmsb=tempmsb; tempBCD1=(tempmsb/10) + 48; tempBCD2=(tempmsb % 10) + 48; DataValue = 'T'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'e'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'm'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'p'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'e'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'r'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'a'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 't'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'u'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'r'; i=0; TXENABLE=1; __delay_ms(50); DataValue = 'e'; i=0; TXENABLE=1; __delay_ms(50); DataValue = ' '; i=0; TXENABLE=1; __delay_ms(50); DataValue = '='; i=0; TXENABLE=1; __delay_ms(50); DataValue = ' '; i=0; TXENABLE=1; __delay_ms(50); DataValue = tempBCD1; i=0; TXENABLE=1; __delay_ms(50); DataValue = tempBCD2; i=0; TXENABLE=1; __delay_ms(50); DataValue = '\r'; i=0; TXENABLE=1; __delay_ms(50); DataValue = '\n'; i=0; TXENABLE=1; __delay_ms(50); __delay_ms(300); } } }