#include #include #include #define _XTAL_FREQ 1843200 #define Baudrate 1200 //bps #define OneBitDelay (1000000/Baudrate) #define DataBitCount 8 // no parity, no flow control #define UART_RX PORTB,RB1 // UART RX pin #define UART_TX PORTA,RA3 // UART TX pin #define UART_RX_DIR TRISB,RB1 // UART RX pin direction register #define UART_TX_DIR TRISA,RA3 // UART TX pin direction register #define SCL TRISAbits.TRISA1 #define SDA TRISAbits.TRISA0 #define LED RA2 #define SCL_IN PORTA,RA1 #define SDA_IN PORTA,RA0 #define TMR0_2 (TMR0 & 1<<2) #define LM75AD 0b10010000 #pragma config "FOSC=XT" #pragma config "WDTE=OFF" #pragma config "PWRTE=ON" #pragma config "MCLRE=OFF" #pragma config "CP=OFF" #pragma config "CPD=OFF" #pragma config "BOREN=OFF" #pragma config "LVP=OFF" unsigned char count; unsigned char tempmsb; unsigned char templsb; unsigned char tempBCD1; unsigned char tempBCD2; unsigned char loop; unsigned char LEDFLAG; 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(20); } void i2c_start(void) { SDA = 1; i2c_dly(); SCL = 1; i2c_dly(); SDA = 0; PORTA=0b11111100; i2c_dly(); SCL = 0; PORTA=0b11111100; i2c_dly(); } void i2c_stop(void) { SDA = 0; // i2c stop bit sequence PORTA=0b11111100; 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; PORTA=0b11111100; SCL = 1; i2c_dly(); d <<= 1; SCL = 0; PORTA=0b11111100; i2c_dly(); } SDA = 1; i2c_dly(); SCL = 1; i2c_dly(); b = SDA_IN; // possible ACK bit SCL = 0; PORTA=0b11111100; return b; } unsigned char i2c_rx(char ack) { unsigned char x, d=0; SDA = 1; for(x=0; x<8; x++) { d <<= 1; do { SCL = 1; } while(SCL_IN==0); // wait for any SCL clock stretching i2c_dly(); if(SDA_IN) d |= 1; SCL = 0; PORTA=0b11111100; } if(ack) SDA = 0; else SDA = 1; SCL = 1; i2c_dly(); // send (N)ACK bit SCL = 0; PORTA=0b11111100; 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=234; 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; PORTA = 0b11100000; CMCON = 0b00000111; VRCON = 0b00000000; TRISA = 0b11110011; TRISB = 0b11111111; 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 = 232; // preset for timer register T0IE = 1; PSA = 0; PEIE = 1; GIE = 1; TXENABLE = 0; tempmsb=0; templsb=0; count=0; Tick=200; Long= 6; LEDFLAG=0; SEC=0; loop=200; //T0IE=1; //GIE=1; InitSoftUART(); while(1) { PORTA,LED = 0; if (SEC==30) { SEC=0; i2c_dly; PORTA,LED = 1; //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); } } } // } // if (DATARDY==1) // { // } //} //}