 |
:: استاذ و مشرف قسم الالكترونيات ::
تاريخ التسجيل: May 2007
المشاركات: 6,894
|
|
نشاط [ F.Abdelaziz ]
قوة السمعة:332
|
|
30-08-2012, 03:01 PM
المشاركة 3
|
|
البرنامج :
كود:
/*
Project: Using TC74 with PIC microcontroller
for temperature measurement
MCU: PIC18F2550 on-board StartUSB for PIC
Clock 48.0 MHz using HS + PLL
MCLR Enabled
*/
// Define LCD module connections.
sbit LCD_RS at RC6_bit;
sbit LCD_EN at RC7_bit;
sbit LCD_D4 at RB4_bit;
sbit LCD_D5 at RB5_bit;
sbit LCD_D6 at RB6_bit;
sbit LCD_D7 at RB7_bit;
sbit LCD_RS_Direction at TRISC6_bit;
sbit LCD_EN_Direction at TRISC7_bit;
sbit LCD_D4_Direction at TRISB4_bit;
sbit LCD_D5_Direction at TRISB5_bit;
sbit LCD_D6_Direction at TRISB6_bit;
sbit LCD_D7_Direction at TRISB7_bit;
// End LCD module connection definition
unsigned char Temp;
unsigned short num;
const int TC74A0 = 0x90;
void check_device(unsigned short dev_address){
I2C1_Start();
if (I2C1_Wr(dev_address)){
Lcd_Out(1,1,"Device not found");
}
else Lcd_Out(1,1,"TC74 device");
I2C1_Stop();
}
unsigned short Read_Temp(){
unsigned short result;
I2C1_Start(); // Issue start signal
I2C1_Wr(TC74A0); // Address + Write bit
I2C1_Wr(0x00); // Read Temp
I2C1_Repeated_Start(); // Issue start signal
I2C1_Wr(TC74A0+1); // Address + Read bit
result = I2C1_Rd(0u);
return result;
}
char temperature[] = " 000 C";
void main() {
CMCON = 0x07; // Disable comparators
ADCON1 = 0x0F; // Disable Analog functions
TRISA = 0x00;
TRISC = 0x00;
TRISB = 0x00;
I2C1_Init(100000); // Initiate I2C
Lcd_Init(); // Initialize LCD
Lcd_Cmd(_LCD_CLEAR); // CLEAR display
Lcd_Cmd(_LCD_CURSOR_OFF); // Cursor off
Lcd_Out(1,1,"Testing TC74");
Lcd_Out(2,1,"Thermal sensor");
Delay_ms(1000);
Lcd_Cmd(_LCD_CLEAR);
do {
check_device(TC74A0);
num = Read_Temp();
// Check for negative temperature
if (num > 127) {
temperature[0] = '-';
num = ~num +1;
}
else temperature[0] = '+';
temperature[1] = num/100 + 48;
temperature[2] = (num/10)%10 + 48;
temperature[3] = num%10 + 48;
temperature[5] = 223;
// eliminate 0s at beginning
if (temperature[1] == '0') {
temperature[1] = ' ';
if (temperature[2] == '0') temperature[2] = ' ';
}
Lcd_Out(2,4,temperature);
Delay_ms(500);
} while(1);
}
|