Electronic Systems
Comments
Content
sbit
sbit
sbit
sbit
sbit
sbit
LCD_RS
LCD_EN
LCD_D7
LCD_D6
LCD_D5
LCD_D4
at
at
at
at
at
at
RD7_bit;
RD6_bit;
RD0_bit;
RD1_bit;
RD2_bit;
RD3_bit;
// Pin direction
sbit LCD_RS_Direction
sbit LCD_EN_Direction
sbit LCD_D7_Direction
sbit LCD_D6_Direction
sbit LCD_D5_Direction
sbit LCD_D4_Direction
at
at
at
at
at
at
TRISD7_bit;
TRISD6_bit;
TRISD0_bit;
TRISD1_bit;
TRISD2_bit;
TRISD3_bit;
/*
sbit Chip_Select_Direction at TRISB2_bit;
sbit Chip_Select at RB2_bit;
unsigned int measurement, lastValue;
void Init() {
// Init LCD
LCD_Init();
LCD_Cmd(_LCD_CLEAR);
LCD_Cmd(_LCD_CURSOR_OFF);
//
measurement = 0;
ariable
// Initialize the measurement v
// Init SPI
SPI1_Init_Advanced(_SPI_MASTER_OSC_DIV64,
_SPI_DATA_SAMPLE_END,
_SPI_CLK_IDLE_LOW,
_SPI_LOW_2_HIGH);
// Initialize PIC as master
// Data sample at end
Chip_Select_Direction= 0;
Chip_Select= 1;
}//~
// ClearBit(TRISC,0)
// SetBit(PORTC,0)
unsigned int getADC(unsigned short channel) {
unsigned int tmp;
Chip_Select= 0;
SPI1_Write(0x06);
t segments
channel = channel << 6;
tmp = SPI1_Read(channel) & 0x0F;
tmp = tmp << 8;
tmp |= SPI1_Read(0);
Chip_Select= 1;
return tmp;
}//~
// Returns 0..4095
// Select MCP3204
// SPI communication using 8-bi
// Bits 7 & 6 define ADC input
// Get ADC value
void processValue(unsigned int pv, unsigned short channel) { // Writes measured
values to LCD
char i, lcdRow, lcdCol;
if (channel < 2) lcdRow=1;
else lcdRow=2;
if (channel%2 > 0 ) lcdCol=13;
else lcdCol=4;
// Converting the measured value into 4 characters
// and writing them to the LCD at the appropriate place
i = pv /1000 + 48;
LCD_Chr(lcdRow,lcdCol, i);
pv %= 1000;
i = pv /100 + 48;
LCD_Chr(lcdRow,lcdCol+1, i);
pv %= 100;
i = pv /10 + 48;
LCD_Chr(lcdRow,lcdCol+2, i);
pv %= 10;
i = pv + 48;
LCD_Chr(lcdRow,lcdCol+3, i);
}//~
// main procedure
void main() {
Init();
LCD_Out(1,1,"C0=
LCD_Out(2,1,"C2=
// Initialize SPI and LCD
C1=");
C3=");
while (1) {
measurement = getADC(0);
// Get ADC result from Channel
0
ProcessValue(measurement,0);
Delay_ms(100);
measurement = getADC(1);
// Writes measured value to LCD
// Wait 100ms
// Get ADC result from Channel
ProcessValue(measurement,1);
Delay_ms(100);
measurement = getADC(2);
// Writes measured value to LCD
// Wait 100ms
// Get ADC result from Channel
ProcessValue(measurement,2);
Delay_ms(100);
measurement = getADC(3);
// Writes measured value to LCD
// Wait 100ms
// Get ADC result from Channel
ProcessValue(measurement,3);
Delay_ms(100);
// Writes measured value to LCD
// Wait 100ms
1
2
3
}
}//~!
*/
// Set TEMP_RESOLUTION to the corresponding resolution of used DS18x20 sensor:
// 18S20: 9 (default setting; can be 9,10,11,or 12)
// 18B20: 12
const unsigned short TEMP_RESOLUTION = 9;
char *text = "000.0000";
unsigned temp;
void Display_Temperature(unsigned int temp2write) {
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
char temp_whole;
unsigned int temp_fraction;
// Check if temperature is negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
}
// Extract temp_whole
temp_whole = temp2write >> RES_SHIFT ;
// Convert temp_whole to characters
if (temp_whole/100)
text[0] = temp_whole/100 + 48;
else
text[0] = '0';
text[1] = (temp_whole/10)%10 + 48;
text[2] = temp_whole%10
+ 48;
// Extract tens digit
// Extract ones digit
// Extract temp_fraction and convert it to unsigned int
temp_fraction = temp2write << (4-RES_SHIFT);
temp_fraction &= 0x000F;
temp_fraction *= 625;
// Convert temp_fraction to characters
text[4] = temp_fraction/1000
+ 48;
text[5] = (temp_fraction/100)%10 + 48;
text[6] = (temp_fraction/10)%10 + 48;
text[7] = temp_fraction%10
+ 48;
//
//
//
//
Extract
Extract
Extract
Extract
thousands digit
hundreds digit
tens digit
ones digit
// Print temperature on LCD
Lcd_Out(2, 5, text);
}
void main()
{
TRISC = 0b11111001;
TRISA = 0x00;
//output
PORTA = 0xF3;
PORTC = 0b00000110;
Lcd_Init();
//
Lcd_Cmd(_LCD_CLEAR);
//
Lcd_Cmd(_LCD_CURSOR_OFF);
//
Lcd_Out(1, 1, " Temperature: ");
// Print degree character, 'C' for Centigrades
Lcd_Chr(2,13,223);
//
different char code for degree
//
r try typing 178 instead of 223
Initialize LCD
Clear LCD
Turn cursor off
Different LCD displays have
If you see greek alpha lette
Lcd_Chr(2,14,'C');
//--- Main loop
do {
//--- Perform temperature reading
Ow_Reset(&PORTC, 0);
Ow_Write(&PORTC, 0, 0xCC);
Ow_Write(&PORTC, 0, 0x44);
Delay_us(120);
// Onewire reset signal
// Issue command SKIP_ROM
// Issue command CONVERT_T
Ow_Reset(&PORTC, 0);
Ow_Write(&PORTC, 0, 0xCC);
Ow_Write(&PORTC, 0, 0xBE);
D
temp = Ow_Read(&PORTC, 0);
temp = (Ow_Read(&PORTC, 0) << 8) + temp;
//--- Format and display result on Lcd
Display_Temperature(temp);
Delay_ms(500);
} while (1);
}
// Issue command SKIP_ROM
// Issue command READ_SCRATCHPA
Sponsor Documents