libyantiger
Member level 5
hi
after spend year studing assembly and 3 month studying c langauge "general c not c for mcu"
i thought i have the magic wand of going to "embeded systems design"
i thought it is time to go for c language that easy to learn and quick to wirite....time for making some serious project and no more led blinking
however it is not as easy as i thought i find program that reallly challanges my skills and it brought me down to ground
lets first write what the program inventor said about it
"Temperature measurement is one of the most common tasks performed by the
microcontroller. A DS1820 sensor is used for measurement here. It is capable of measuring
temperature in the range of -55 °C to 125 °C with 0.5 °C accuracy. For the purpose of
transferring data to the microcontroller, a special type of serial communication called 1-wire is used.
Due to a simple and wide use of these sensors, commands used to run and control them are in
the form of functions stored in the One_Wire library. There are three functions in total:
Ow_Reset is used for reseting sensor;
Ow_Read is used for receiving data from sensor; and
Ow_Write is used for sending commands to sensor.
This example implies the advantage in using libraries with ready-to-use functions. Concretely,
you donÂ’t have to study documentation provided by the manufacturer in order to use this sensor. It is sufficient to copy some of these functions in the program. If you want to know how any of them is declared, just right click on it and select the Help option.
----------------------------------------
here are my question
to me function Display_Temperature(temp); is basless ......from where it comes? what it does ? i cant see those curley at the end of the program
that really does show the contents of this function or what it does ?
*text is pointer ....pointer is supposed to point to a memory location not to just normal float value ?
char *text = "000.0000"; ???????????????????
the interger varible int temp2write is being manipulated with some number to detect if it zero or not ( assembly is best...go to status register if u want so)
i cant get this ?
// check if temperature is negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
temp_whole = temp2write >> RES_SHIFT ; we can shift a byte right ...to shift a byte to another byte right is beyond my kin
>> is the shift sign .....res_shift is constant how a varible can be shifted right to a constsant i just mind torture experience??
also it is silly to defind a = b-1 where b =1 ....is not it straight forward just to say that a =0?
this madness happen in this line
const unsigned short TEMP_RESOLUTION = 9;
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
help me my mind is blown and i feel i have to burn all books that i have read ....no matter how you kill your slef in reading it is always
frustrating
after spend year studing assembly and 3 month studying c langauge "general c not c for mcu"
i thought i have the magic wand of going to "embeded systems design"
i thought it is time to go for c language that easy to learn and quick to wirite....time for making some serious project and no more led blinking
however it is not as easy as i thought i find program that reallly challanges my skills and it brought me down to ground
lets first write what the program inventor said about it
"Temperature measurement is one of the most common tasks performed by the
microcontroller. A DS1820 sensor is used for measurement here. It is capable of measuring
temperature in the range of -55 °C to 125 °C with 0.5 °C accuracy. For the purpose of
transferring data to the microcontroller, a special type of serial communication called 1-wire is used.
Due to a simple and wide use of these sensors, commands used to run and control them are in
the form of functions stored in the One_Wire library. There are three functions in total:
Ow_Reset is used for reseting sensor;
Ow_Read is used for receiving data from sensor; and
Ow_Write is used for sending commands to sensor.
This example implies the advantage in using libraries with ready-to-use functions. Concretely,
you donÂ’t have to study documentation provided by the manufacturer in order to use this sensor. It is sufficient to copy some of these functions in the program. If you want to know how any of them is declared, just right click on it and select the Help option.
Code:
[COLOR=#808080][I]/*Header******************************************************/[/I][/COLOR]
here is the code
sbit LCD_EN at RB5_bit[COLOR=#339933];[/COLOR]
sbit LCD_D4 at RB0_bit[COLOR=#339933];[/COLOR]
sbit LCD_D5 at RB1_bit[COLOR=#339933];[/COLOR]
sbit LCD_D6 at RB2_bit[COLOR=#339933];[/COLOR]
sbit LCD_D7 at RB3_bit[COLOR=#339933];[/COLOR]
sbit LCD_RS_Direction at TRISB4_bit[COLOR=#339933];[/COLOR]
sbit LCD_EN_Direction at TRISB5_bit[COLOR=#339933];[/COLOR]
sbit LCD_D4_Direction at TRISB0_bit[COLOR=#339933];[/COLOR]
sbit LCD_D5_Direction at TRISB1_bit[COLOR=#339933];[/COLOR]
sbit LCD_D6_Direction at TRISB2_bit[COLOR=#339933];[/COLOR]
sbit LCD_D7_Direction at TRISB3_bit[COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// End LCD module connections[/I][/COLOR]
[COLOR=#993333]const[/COLOR] [COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]short[/COLOR] TEMP_RESOLUTION [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]9[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#993333]char[/COLOR] [COLOR=#339933]*[/COLOR]text [COLOR=#339933]=[/COLOR] [COLOR=#ff0000]"000.0000"[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#993333]unsigned[/COLOR] temp[COLOR=#339933];[/COLOR]
[COLOR=#993333]void[/COLOR] Display_Temperature[COLOR=#009900]([/COLOR][COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]int[/COLOR] temp2write[COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
[COLOR=#993333]const[/COLOR] [COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]short[/COLOR] RES_SHIFT [COLOR=#339933]=[/COLOR] TEMP_RESOLUTION [COLOR=#339933]-[/COLOR] [COLOR=#0000dd]8[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#993333]char[/COLOR] temp_whole[COLOR=#339933];[/COLOR]
[COLOR=#993333]unsigned[/COLOR] [COLOR=#993333]int[/COLOR] temp_fraction[COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// check if temperature is negative[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR]temp2write [COLOR=#339933]&[/COLOR] [COLOR=#208080]0x8000[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]0[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#ff0000]'-'[/COLOR][COLOR=#339933];[/COLOR]
temp2write [COLOR=#339933]=[/COLOR] ~temp2write [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]1[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#009900]}[/COLOR]
[COLOR=#666666][I]// extract temp_whole[/I][/COLOR]
temp_whole [COLOR=#339933]=[/COLOR] temp2write [COLOR=#339933]>>[/COLOR] RES_SHIFT [COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// convert temp_whole to characters[/I][/COLOR]
[COLOR=#b1b100]if[/COLOR] [COLOR=#009900]([/COLOR]temp_whole[COLOR=#339933]/[/COLOR][COLOR=#0000dd]100[/COLOR][COLOR=#009900])[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]0[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_whole[COLOR=#339933]/[/COLOR][COLOR=#0000dd]100[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#b1b100]else[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]0[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#ff0000]'0'[/COLOR][COLOR=#339933];[/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]1[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]temp_whole[COLOR=#339933]/[/COLOR][COLOR=#0000dd]10[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract tens digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_whole[COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract ones digit[/I][/COLOR]
[COLOR=#666666][I]// extract temp_fraction and convert it to unsigned int[/I][/COLOR]
temp_fraction [COLOR=#339933]=[/COLOR] temp2write [COLOR=#339933]<<[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#0000dd]4[/COLOR][COLOR=#339933]-[/COLOR]RES_SHIFT[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
temp_fraction [COLOR=#339933]&=[/COLOR] [COLOR=#208080]0x000F[/COLOR][COLOR=#339933];[/COLOR]
temp_fraction [COLOR=#339933]*=[/COLOR] [COLOR=#0000dd]625[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// convert temp_fraction to characters[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]4[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_fraction[COLOR=#339933]/[/COLOR][COLOR=#0000dd]1000[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract thousands digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]5[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]temp_fraction[COLOR=#339933]/[/COLOR][COLOR=#0000dd]100[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract hundreds digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]6[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]temp_fraction[COLOR=#339933]/[/COLOR][COLOR=#0000dd]10[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract tens digit[/I][/COLOR]
text[COLOR=#009900][[/COLOR][COLOR=#0000dd]7[/COLOR][COLOR=#009900]][/COLOR] [COLOR=#339933]=[/COLOR] temp_fraction[COLOR=#339933]%[/COLOR][COLOR=#800080]10[/COLOR] [COLOR=#339933]+[/COLOR] [COLOR=#0000dd]48[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Extract ones digit[/I][/COLOR]
[COLOR=#666666][I]// Display temperature on LCD[/I][/COLOR]
Lcd_Out[COLOR=#009900]([/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000dd]5[/COLOR][COLOR=#339933],[/COLOR] text[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#009900]}[/COLOR]
[COLOR=#993333]void[/COLOR] main[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR] [COLOR=#009900]{[/COLOR]
ANSEL [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Configure AN pins as digital I/O[/I][/COLOR]
ANSELH [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR]
C1ON_bit [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Disable comparators[/I][/COLOR]
C2ON_bit [COLOR=#339933]=[/COLOR] [COLOR=#0000dd]0[/COLOR][COLOR=#339933];[/COLOR]
Lcd_Init[COLOR=#009900]([/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Initialize LCD[/I][/COLOR]
Lcd_Cmd[COLOR=#009900]([/COLOR]_LCD_CLEAR[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Clear LCD[/I][/COLOR]
Lcd_Cmd[COLOR=#009900]([/COLOR]_LCD_CURSOR_OFF[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Turn the cursor off[/I][/COLOR]
Lcd_Out[COLOR=#009900]([/COLOR][COLOR=#0000dd]1[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#0000dd]1[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#ff0000]" Temperature: "[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#666666][I]// Print degree character, 'C' for Centigrades[/I][/COLOR]
Lcd_Chr[COLOR=#009900]([/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR][COLOR=#0000dd]13[/COLOR][COLOR=#339933],[/COLOR][COLOR=#0000dd]223[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// different LCD displays have different[/I][/COLOR]
[COLOR=#993333]char[/COLOR] code [COLOR=#b1b100]for[/COLOR] degree
[COLOR=#666666][I]// if you see greek alpha letter try typing 178 instead of 223[/I][/COLOR]
Lcd_Chr[COLOR=#009900]([/COLOR][COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR][COLOR=#0000dd]14[/COLOR][COLOR=#339933],[/COLOR][COLOR=#ff0000]'C'[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#666666][I]//--- main loop[/I][/COLOR]
[COLOR=#b1b100]do[/COLOR] [COLOR=#009900]{[/COLOR]
[COLOR=#666666][I]//--- perform temperature reading[/I][/COLOR]
Ow_Reset[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Onewire reset signal[/I][/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0xCC[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command SKIP_ROM[/I][/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0x44[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command CONVERT_T[/I][/COLOR]
Delay_us[COLOR=#009900]([/COLOR][COLOR=#0000dd]120[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
Ow_Reset[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0xCC[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command SKIP_ROM[/I][/COLOR]
Ow_Write[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#339933],[/COLOR] [COLOR=#208080]0xBE[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR] [COLOR=#666666][I]// Issue command READ_SCRATCHPAD[/I][/COLOR]
temp [COLOR=#339933]=[/COLOR] Ow_Read[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
temp [COLOR=#339933]=[/COLOR] [COLOR=#009900]([/COLOR]Ow_Read[COLOR=#009900]([/COLOR][COLOR=#339933]&[/COLOR]PORTE[COLOR=#339933],[/COLOR] [COLOR=#0000dd]2[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]<<[/COLOR] [COLOR=#0000dd]8[/COLOR][COLOR=#009900])[/COLOR] [COLOR=#339933]+[/COLOR] temp[COLOR=#339933];[/COLOR]
[COLOR=#666666][I]//--- Format and display result on Lcd[/I][/COLOR]
Display_Temperature[COLOR=#009900]([/COLOR]temp[COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
Delay_ms[COLOR=#009900]([/COLOR][COLOR=#0000dd]500[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
[COLOR=#009900]}[/COLOR] [COLOR=#b1b100]while[/COLOR] [COLOR=#009900]([/COLOR][COLOR=#0000dd]1[/COLOR][COLOR=#009900])[/COLOR][COLOR=#339933];[/COLOR]
here are my question
to me function Display_Temperature(temp); is basless ......from where it comes? what it does ? i cant see those curley at the end of the program
that really does show the contents of this function or what it does ?
*text is pointer ....pointer is supposed to point to a memory location not to just normal float value ?
char *text = "000.0000"; ???????????????????
the interger varible int temp2write is being manipulated with some number to detect if it zero or not ( assembly is best...go to status register if u want so)
i cant get this ?
// check if temperature is negative
if (temp2write & 0x8000) {
text[0] = '-';
temp2write = ~temp2write + 1;
temp_whole = temp2write >> RES_SHIFT ; we can shift a byte right ...to shift a byte to another byte right is beyond my kin
>> is the shift sign .....res_shift is constant how a varible can be shifted right to a constsant i just mind torture experience??
also it is silly to defind a = b-1 where b =1 ....is not it straight forward just to say that a =0?
this madness happen in this line
const unsigned short TEMP_RESOLUTION = 9;
const unsigned short RES_SHIFT = TEMP_RESOLUTION - 8;
help me my mind is blown and i feel i have to burn all books that i have read ....no matter how you kill your slef in reading it is always
frustrating
Last edited by a moderator: