using C# for temperature controlC#, ASM, 8051, LM35, ADC0804...
15 Replies - 5747 Views - Last Post: 12 June 2010 - 09:04 AM
#1
using C# for temperature control
Posted 09 June 2010 - 03:28 AM
nice to meet you!
I'm having problems with serialport in C#. I have project about communication between the PC and 8051 (exactly: auto change temperature). I need display the current temperature on Textbox (Windowform in C#).now i can dispaly current temperature but not exactly! Example: I sent data from 8051 to PC (10)but data to be display on Textbox (29). I dont know why!
please help me! Thanks so much!
Replies To: using C# for temperature control
#2
Re: using C# for temperature control
Posted 09 June 2010 - 04:09 AM
If you put breakpoint in the code where the the input is received, what is the input ther, 10 or 29?
Also what happens if you send 11, do you get 30 or something else?
Hope we can help
#3
Re: using C# for temperature control
Posted 09 June 2010 - 06:28 AM
i tested, with 8051 im using assembly,
-mov a,#10 mov sbuf,a ...=> on textbox display (29) -mov a,#13 => textbox: 6b -mov a,#15 => textbox: 3f -mov a,#17 => textbox: 08 -mov a,#20 => textbox: 08
i dont know why!i think problems from my code assembly for 8051.
; ____________________________________________________________________________________
; temperature control
; LCD: P1.0-P1.7, RS:P3.5 ,RW:3.6 ,E:3.7
; BF: P1.7
; ADC: P2
; WR:P3.1
; RD:P3.2
; INTR:P3.0
; DIEU KHIEN: UP,MODE,DOWN
;________________________________________________KHAI BAO______________________________________
INCLUDE 8051.MC
RS BIT P2.5
E BIT P2.7
RW BIT p2.6
BF BIT p0.7
LCDPORT EQU p0
LCDDATA DATA 30h
TOADO DATA 31h
XOA_HIENTHI DATA 32h
VE_DAUDONG DATA 33h
LCD162 DATA 34h
DICH_CT_RIGHT DATA 35h
ONHT_OFFCT DATA 36h
CT_LEFT DATA 37h
CT_RIGHT DATA 38h
HT_LEFT DATA 39h
HT_RIGHT DATA 3ah
ROW1 DATA 3BH
ROW2 DATA 3CH
NHOA DATA 3DH
;+++++++++++++
TMIN DATA 3eh
TMAX DATA 3fh
TCUR DATA 40h
TSET DATA 41h
RD_ADC BIT p3.7
WR_ADC BIT p3.6
INTR_ADC BIT p3.2
DATA_LCD EQU p1
;+++++++++++++
MODE BIT p2.4
UP BIT p2.3
DOWN BIT p2.2
;
QUAT BIT P3.3
BONGDEN BIT P3.4
;___________________________________________________________________________________________
;_____________________________________________MAIN__________________________________________
ORG 00H
JMP MAIN
ORG 0030H
MAIN:
MOV XOA_HIENTHI,#01h
MOV VE_DAUDONG,#02h
MOV LCD162,#38h
MOV DICH_CT_RIGHT,#06h
MOV ONHT_OFFCT,#0ch
MOV CT_LEFT,#10h
MOV CT_RIGHT,#14h
MOV HT_LEFT,#18h
MOV HT_RIGHT,#1ch
MOV ROW1,#80h
MOV ROW2,#0c0h
MOV TMIN,#18
MOV TMAX,#40
MOV TSET,#32
CLR QUAT
CLR BONGDEN
; LCALL NGAT
; MOV R1,P1
; ___________________________________________________________________________
MOV LCDDATA,LCD162
LCALL KHOITAO_LCD
MOV LCDDATA,DICH_CT_RIGHT
LCALL KHOITAO_LCD
MOV LCDDATA,ONHT_OFFCT
LCALL KHOITAO_LCD
MOV LCDDATA,XOA_HIENTHI
LCALL KHOITAO_LCD
;__________________________________________________________________________
HANG1:
MOV DPTR,#400h
LOOP:
CLR A
MOVC A,@a+dptr
MOV LCDDATA,A
LCALL WAIT_100ns
LCALL SENT_DATA
INC DPTR
LCALL WAIT_500ms
CJNE A,#"#",LOOP
HANG2:
MOV LCDDATA,ROW2
CALL KHOITAO_LCD
MOV DPTR,#450h
LOOP1:
CLR A
MOVC A,@a+dptr
MOV LCDDATA,A
LCALL WAIT_100ns
LCALL SENT_DATA
INC DPTR
LCALL WAIT_500ms
CJNE A,#"#",LOOP1
LCALL delay500ms
LCALL delay500ms
LCALL delay500ms
MOV LCDDATA,XOA_HIENTHI
LCALL KHOITAO_LCD
; DAI NHIET DO VA NHIET DO HIEN TAI, NHIET DO THIET LAP.
HANG11:
MOV DPTR,#500h
LOOP3:
CLR A
MOVC A,@a+dptr
MOV LCDDATA,A
LCALL WAIT_100ns
LCALL SENT_DATA
INC DPTR
CJNE A,#"#",LOOP3
HANG22:
MOV LCDDATA,ROW2
LCALL KHOITAO_LCD
MOV DPTR,#550h
LOOP4:
CLR A
MOVC A,@a+dptr
MOV LCDDATA,A
LCALL WAIT_100ns
LCALL SENT_DATA
INC DPTR
CJNE A,#"#",LOOP4
;_______________________________
LCALL TOADO_TMIN
MOV R7,TMIN
LCALL HEXTOBCD
LCALL HIENTHI
LCALL TOADO_TMAX
MOV R7,TMAX
LCALL HEXTOBCD
LCALL HIENTHI
LCALL TOADO_SET
MOV R7,TSET
LCALL HEXTOBCD
LCALL HIENTHI
START:
JNB MODE,SETUP
LCALL TOADO_CUR
LCALL READ_ADC
; LCALL HIENTHI
LCALL TRUYEN
LCALL HIENTHI
JMP START
;___________________________________
SETUP:
SETUP_TMIN:
LCALL WAIT_100ns
JNB MODE,$
LCALL WAIT_100ns
LOOP_SETUP_TMIN:
LCALL TOADO_TMIN
MOV LCDDATA,CT_LEFT
LCALL KHOITAO_LCD
MOV LCDDATA,#"*"
LCALL SENT_DATA
JNB UP,INC_TMIN
JNB DOWN,DEC_TMIN
JNB MODE,SETUP_TMAX
JMP LOOP_SETUP_TMIN
INC_TMIN:
LCALL delay500ms
INC TMIN
MOV R7,TMIN
LCALL HEXTOBCD
LCALL HIENTHI
JMP LOOP_SETUP_TMIN
DEC_TMIN:
LCALL delay500ms
DEC TMIN
MOV R7,TMIN
LCALL HEXTOBCD
LCALL HIENTHI
JMP LOOP_SETUP_TMIN
;____________________________
SETUP_TMAX:
LCALL TOADO_TMIN
MOV LCDDATA,CT_LEFT
LCALL KHOITAO_LCD
MOV LCDDATA,#" "
LCALL SENT_DATA
LCALL WAIT_100ns
JNB MODE,$
LCALL WAIT_100ns
LOOP_SETUP_TMAX:
LCALL TOADO_TMAX
MOV LCDDATA,CT_LEFT
LCALL KHOITAO_LCD
MOV LCDDATA,#"*"
LCALL SENT_DATA
JNB UP,INC_TMAX
JNB DOWN,DEC_TMAX
JNB MODE,SETUP_TSET
JMP LOOP_SETUP_TMAX
INC_TMAX:
LCALL delay500ms
INC TMAX
MOV R7,TMAX
LCALL HEXTOBCD
LCALL HIENTHI
JMP LOOP_SETUP_TMAX
DEC_TMAX:
LCALL delay500ms
DEC TMAX
MOV R7,TMAX
LCALL HEXTOBCD
LCALL HIENTHI
JMP LOOP_SETUP_TMAX
;_________________________
SETUP_TSET:
LCALL TOADO_TMAX
MOV LCDDATA,CT_LEFT
LCALL KHOITAO_LCD
MOV LCDDATA,#" "
LCALL SENT_DATA
LCALL WAIT_100ns
JNB MODE,$
LCALL WAIT_100ns
LOOP_SETUP_TSET:
LCALL TOADO_SET
MOV LCDDATA,CT_LEFT
LCALL KHOITAO_LCD
MOV LCDDATA,#"*"
LCALL SENT_DATA
JNB UP,INC_TSET
JNB DOWN,DEC_TSET
JNB MODE,JMPSTART
JMP LOOP_SETUP_TSET
INC_TSET:
LCALL delay500ms
INC TSET
MOV R7,TSET
LCALL HEXTOBCD
LCALL HIENTHI
JMP LOOP_SETUP_TSET
DEC_TSET:
LCALL delay500ms
DEC TSET
MOV R7,TSET
LCALL HEXTOBCD
LCALL HIENTHI
JMP LOOP_SETUP_TSET
;____Nhay_______
JMPSTART:
LCALL TOADO_SET
MOV LCDDATA,CT_LEFT
LCALL KHOITAO_LCD
MOV LCDDATA,#" "
LCALL SENT_DATA
LCALL delay500ms
LJMP START
;**************************************************************
;ADC
READ_ADC:
SETB RD_ADC
CLR WR_ADC
NOP
SETB WR_ADC
JB INTR_ADC,$
SETB INTR_ADC
CLR RD_ADC
NOP
DAOBIT:
MOV C,p1.7
MOV A.0,C
MOV C,p1.6
MOV A.1,C
MOV C,p1.5
MOV A.2,C
MOV C,p1.4
MOV a.3,C
MOV C,p1.3
MOV A.4,C
MOV C,p1.2
MOV A.5,C
MOV C,p1.1
MOV a.6,C
MOV C,p1.0
MOV A.7,C
MOV R7,A
;_____________________ DIEU KHIEN DEN VA QUAT _____________________
CONTROL:
CLR C
MOV A,TSET
SUBB A,TCUR
JB CY,MO_QUAT
CLR QUAT
SETB BONGDEN
MO_QUAT:
SETB QUAT
CLR BONGDEN
CLR CY
RET
;________________________________________CAC CHUONG TRINH CON________________
HEXTOBCD:
MOV A,R7
MOV B,#10
DIV AB
MOV R6,A
MOV R7,B
RET
BCDTOASCII:
MOV A,R6 ; A = 29H, packed BCD
MOV R2,A ; sao lu+u A va`o R2
ANL A,#0FH ; che nibble cao (A = 09)
ORL A,#30H ; chuye^?n tha`nh ma~ ASCII, A = 39H (`9')
MOV R6,A ; lu+u ke^'t qua? va`o R6 (R6 = 39H ASCII char)
MOV A,R2 ; la^'y la.i gia' tri. A ban dda^`u
ANL A,#0F0H ; che nibble tha^'p (A = 20)
RR A ; quay pha?i 4 la^`n
RR A ;
RR A ;
RR A ; - > A = 02
ORL A,#30H ; chuye^? tha`nh ma~ ASCII
MOV R2,A ; lu+u va`o R2
RET
CHUYENSANGTHAPPHAN:
MOV A,R1
MOV B,#10
DIV AB
MOV R2,A
MOV A,B
DIV AB
MOV R3,A
MOV R4,B
RET
HIENTHI1:
MOV A,R2
MOVC A,@A+DPTR
MOV LCDDATA,A
CALL SENT_DATA
MOV A,R3
MOVC A,@A+DPTR
MOV LCDDATA,A
CALL SENT_DATA
MOV A,R4
MOVC A,@A+DPTR
MOV LCDDATA,A
CALL SENT_DATA
RET
HIENTHI: ; TRONG DOAN NAY CHUYEN SANG MA ASCII XONG MOI HIEN THI DC
MOV A,R6
ANL A,#0fh ; CHE NIBLE CAO
ORL A,#30h ; CHUYEN SANG MA ASCII
MOV LCDDATA,A ; CHUYEN VAO LCDDATA
ACALL SENT_DATA
MOV A,R7
ANL A,#0fh
ORL A,#30h
MOV LCDDATA,A
ACALL SENT_DATA
RET
KHOITAO_LCD:
ACALL TEST_BF
CLR RS
CLR RW
MOV LCDPORT,LCDDATA
CLR E
SETB E
RET
SENT_DATA:
ACALL TEST_BF
SETB RS
CLR RW
MOV LCDPORT,LCDDATA
SETB E
CLR E
RET
TEST_BF:
CLR RS
SETB RW
SETB BF
RETEST:
CLR E
SETB E
JB BF,RETEST
RET
TOADO_TMIN:
MOV LCDDATA,ROW1
CALL KHOITAO_LCD
MOV TOADO,#07
GOTO_TMIN:
MOV LCDDATA,CT_RIGHT
CALL KHOITAO_LCD
DJNZ TOADO,GOTO_TMIN
RET
TOADO_TMAX:
MOV LCDDATA,ROW1
CALL KHOITAO_LCD
MOV TOADO,#12
GOTO_TMAX:
MOV LCDDATA,CT_RIGHT
CALL KHOITAO_LCD
DJNZ TOADO,GOTO_TMAX
RET
TOADO_CUR:
MOV LCDDATA,ROW2
CALL KHOITAO_LCD
MOV TOADO,#04
GOTO_CUR:
MOV LCDDATA,CT_RIGHT
CALL KHOITAO_LCD
DJNZ TOADO,GOTO_CUR
RET
TOADO_SET:
MOV LCDDATA,ROW2
CALL KHOITAO_LCD
MOV TOADO,#13
GOTO_SET:
MOV LCDDATA,CT_RIGHT
CALL KHOITAO_LCD
DJNZ TOADO,GOTO_SET
RET
;****************************************************************
WAIT_100ns:
MOV R1,#100
DJNZ R1,$
RET
WAIT_500MS:
MOV R1,#150
LOOPWAIT:
MOV R3,#200
DJNZ R3,$
DJNZ R1,LOOPWAIT
RET
DELAY500MS:
MOV R4,#10
MOV TMOD,#10H ;timer1 che do 1
LOOP_TIMER1_2S:
MOV TH1,#HIGH(15535)
MOV TL1,#LOW(15535)
SETB TR1
JNB TF1,$
CLR TF1
DJNZ R4,LOOP_TIMER1_2S
CLR TR1
RET
NGAT:
MOV SCON,#50H
MOV TMOD,#20H
MOV TH1,#0FDH
CLR TI
SETB TR1
RET
TRUYEN:
CALL NGAT
MOV A,#20
MOV SBUF,A
NOP
NOP
CLR TI
RET
;TRUYEN:
; CALL NGAT
; MOV R1,P1
; MOV A,R1
; LCALL BCDTOASCII ; KHONG PHAI MOV A,@R1
; MOV SBUF,A
; NOP
; NOP
; CLR TI
;RET
NHAN:
CALL NGAT
MOV A,SBUF
MOV DATA_LCD,A
NOP
NOP
CLR RI
RET
ORG 400h
DB " PROJECT 4 #"
ORG 450h
DB " PC AND MCU 8051 #"
ORG 500h
DB "RANGE: * #"
ORG 550h
DB "CUR: SET: #"
END
this is my code for 8051.
I will happy for your help!
THANKS SO MUCH!
This post has been edited by JackOfAllTrades: 09 June 2010 - 07:15 AM
Reason for edit:: Added code tags.
#4
Re: using C# for temperature control
Posted 09 June 2010 - 09:47 AM
For example, one would return a value that was really the electrical voltage across the sensor.
The datasheet supplied a formula you had to apply to that response to calculate the temperature.
#5
Re: using C# for temperature control
Posted 09 June 2010 - 10:02 AM
I tested on real curcuit. I think problems here, convert Hex to BCD or Decimal to ASCII...anything other.
I cant find them. Can you help me?please!
Thanks alot!!!
#6
Re: using C# for temperature control
Posted 09 June 2010 - 10:18 AM
#7
Re: using C# for temperature control
Posted 09 June 2010 - 11:33 AM
#8
Re: using C# for temperature control
Posted 09 June 2010 - 11:40 AM
You need to figure out what kind of data your chip is providing (read the data sheet and instructions) before you do more.
In the mean time, you should read the MSDN for your need.
http://msdn.microsof...y/bb311038.aspx
#9
Re: using C# for temperature control
Posted 10 June 2010 - 09:31 AM
This post has been edited by kiwi_FPGA: 10 June 2010 - 08:00 PM
#10
Re: using C# for temperature control
Posted 10 June 2010 - 09:40 AM
If the correct answer was 36, and you are getting 3366, then it looks like you are adding each digit twice.
Put a breakpoint at the start of the method and walk through it step by step with the F10 function key.
Watch the Locals and Autos pallets. These will show you the value of each variable, and will update when you move to the next line.
Actually watching your code execute line by line will let you visualize what your code is really doing.
This is a vital step in debugging. It is something we all do.
Once you watch your code, you will see where you are adding each character a second time.
#11
Re: using C# for temperature control
Posted 10 June 2010 - 11:50 AM
tlhIn, on 10 June 2010 - 08:40 AM, said:
If the correct answer was 36, and you are getting 3366, then it looks like you are adding each digit twice.
Put a breakpoint at the start of the method and walk through it step by step with the F10 function key.
Watch the Locals and Autos pallets. These will show you the value of each variable, and will update when you move to the next line.
Actually watching your code execute line by line will let you visualize what your code is really doing.
This is a vital step in debugging. It is something we all do.
Once you watch your code, you will see where you are adding each character a second time.
yes dear! thanks you very much!
#12
Re: using C# for temperature control
Posted 11 June 2010 - 02:03 AM
I would like to see your finished code for reading out the temp.
#13
Re: using C# for temperature control
Posted 11 June 2010 - 03:41 AM
{ text });
}
else
{
//this.txt_nhietdo.Text = hextodecimal(converttohex(text));
tx = converttohex(text);
// tn = hextodecimal(tx);
this.txt_nhietdo.Text =tx ;
}
}
private string converttohex(string ascii)
{
string hex = "";
foreach (char c in ascii)
{
int tmp = c;
hex += string.Format("{0:x2}", (uint)System.Convert.ToUInt32(tmp.ToString()));
}
return hex;
}
private int hextodecimal(string hex)
{
int dec;
string moi = converttohex(hex);
foreach(char k in moi)
{
int tp = k;
dec = int.Parse(moi, System.Globalization.NumberStyles.HexNumber);
}
return dec;
}']
yes dear this is my code!thanks you!
This post has been edited by kiwi_FPGA: 11 June 2010 - 03:44 AM
#14
Re: using C# for temperature control
Posted 11 June 2010 - 06:10 AM
And this is an convertion what was the code for reading it out?
#15
Re: using C# for temperature control
Posted 11 June 2010 - 06:48 AM
|
|

New Topic/Question
Reply



MultiQuote






|