turn the dc motor using 16F877A

Status
Not open for further replies.

too fast

New Member
Hi there..now im doing a project using one wire DS18S20 and display the temperature on 4 digit 7-segment. I would like to add 5v dc motor as cooling fan. the fan will start running when temperature reach 60 C and above.
**broken link removed**

Anybody can help me in programming in order to run the dc motor (constant speed)..below is my source code to display temperature. Since port C and D have been used, i would like to use port A.

Define LOADER_USED 1
DEFINE OSC 4
@ Device pic16F877A, xt_OSC, BOD_OFF, PWRT_ON, WDT_OFF, PROTECT_OFF 'WRT_On

Segments Var PORTC
Digits Var PORTD

i Var Byte
n Var Byte
Value Var Word

' Allocate variables
command var byte ' Storage for command
J var byte ' Storage for loop counter
temp var word ' Storage for temperature
T VAR WORD
DQ var PORTd.4 ' Alias DS1820 data pin
DQ_DIR var TRISd.4 ' Alias DS1820 data direction pin
read_done var byte
Lastdata var word


TRISc = $00 ' Set segment pins to output
TRISd = $f0 ' Set digit pins to output
TrisB = $00
Portb = $00
portd = $ff

OPTION_REG = $50 ' Set TMR0 configuration 1:64
INTCON = $A0 ' Enable TMR0 interrupts
'On Interrupt Goto display

'ADCON1 = 7 ' Set PORTA and PORTE to digital
' Lcdout $fe, 1, "Temp in degrees C" ' Display sign-on message
'debug ====


DEFINE HSER_RCSTA 90h
DEFINE HSER_TXSTA 24h
DEFINE HSER_SPBRG 12 ' 19200 Bauds ,4 m
'debug ====

' Mainloop to read the temperature and display on LCD


mainloop:
'goto display
'Goto mainloop

Gosub init1820 ' Init the DS1820

command = $cc ' Issue Skip ROM command
Gosub write1820

command = $44 ' Start temperature conversion
Gosub write1820

'Pause 2000 ' Wait 2 seconds for conversion to complete

Gosub init1820 ' Do another init

command = $cc ' Issue Skip ROM command
Gosub write1820

command = $be ' Read the temperature
Gosub write1820
Gosub read1820


'Display the decimal temperature
'HSEROUT ["Temp = ",dec (temp>>1),".", dec (temp.0*5)," C",13,10]
'T = ((temp>> 1)*100)+((TEMP.0*5)*10)
'HSEROUT ["Temp = ",dec T ,13,10]
value = ((temp>> 1)*100)+((TEMP.0*5)*10)
GOTO DISPLAY
Goto mainloop ' Do it forever


' Initialize DS1820 and check for presence
init1820:
Low DQ ' Set the data pin low to init
Pauseus 500 ' Wait > 480us
DQ_DIR = 1 ' Release data pin (set to input for high)

Pauseus 100 ' Wait > 60us
If DQ = 1 Then
'Lcdout $fe, 1, "DS1820 not present"
PORTC = $39
LOW DIGITS.0
PAUSE 150
PORTC = $0
Pause 150
Goto mainloop ' Try again
Endif
Pauseus 400 ' Wait for end of presence pulse
Return


' Write "command" byte to the DS1820
write1820:
For i = 1 to 8 ' 8 bits to a byte
If command.0 = 0 Then
Gosub write0 ' Write a 0 bit
Else
Gosub write1 ' Write a 1 bit
Endif
command = command >> 1 ' Shift to next bit
Next i
Return

' Write a 0 bit to the DS1820
write0:
Low DQ
Pauseus 60 ' Low for > 60us for 0
DQ_DIR = 1 ' Release data pin (set to input for high)
Return

' Write a 1 bit to the DS1820
write1:
Low DQ ' Low for < 15us for 1
@ nop ' Delay 1us at 4MHz
DQ_DIR = 1 ' Release data pin (set to input for high)
Pauseus 60 ' Use up rest of time slot
Return


' Read temperature from the DS1820
read1820:
For i = 1 to 16 ' 16 bits to a word
temp = temp >> 1 ' Shift down bits
Gosub readbit ' Get the bit to the top of temp
Next i
Return

' Read a bit from the DS1820
readbit:
temp.15 = 1 ' Preset read bit to 1
Low DQ ' Start the time slot
@ nop ' Delay 1us at 4MHz
DQ_DIR = 1 ' Release data pin (set to input for high)
If DQ = 0 Then
temp.15 = 0 ' Set bit to 0
Endif
Pauseus 60 ' Wait out rest of time slot
Return
'================== 7 seg ment display ==============================
display:
'value = ((temp>> 1)*100)+((TEMP.0*5)*10)

PORTC = $39 'Show C on display
LOW DIGITS.0
PAUSEUS 1500
PORTC = $0

'value = 125
For i = 1 To 4 ' Loop through 4 digits

n = Value Dig i ' Get digit to display

' Gosub display1 ' Display the value
Digits = $ff

Lookup n, [$3F, $06, $5B, $4F, $66, $6D, $7D, $07, $7F, $6F,_
$77, $7C, $39, $5E, $79, $71, $0FF], Segments

If i = 2 then segments = segments + 128 'Digit 2 show dot

Digits = ~Dcd i

NEXT I
goto mainloop
 
Last edited:
please any one...

hi,
I have used 'Pommies' code tidy.
Its helpful when you post code to Select it and click the '#' Code symbol on the menu bar, this keeps the formatting and makes it easy to read.


Code:
Define	LOADER_USED 1
DEFINE	OSC	4
@	Device	pic16F877A, xt_OSC, BOD_OFF, PWRT_ON, WDT_OFF, PROTECT_OFF 'WRT_On

Segments Var	PORTC
Digits	Var	PORTD

i	Var	Byte
n	Var	Byte
Value	Var	Word

'	Allocate variables
command	var	byte ' Storage for command
J	var	byte ' Storage for loop counter
temp	var	word ' Storage for temperature
T	VAR	WORD
DQ	var	PORTd.4 ' Alias DS1820 data pin
DQ_DIR	var	TRISd.4 ' Alias DS1820 data direction pin
read_done var	byte
Lastdata var	word


TRISc	=	$00 ' Set segment pins to output
TRISd	=	$f0 ' Set digit pins to output
TrisB	=	$00
Portb	=	$00
portd	=	$ff

OPTION_REG =	$50 ' Set TMR0 configuration 1:64
INTCON	=	$A0 ' Enable TMR0 interrupts
'On	Interrupt Goto display

'ADCON1	=	7 ' Set PORTA and PORTE to digital
'	Lcdout	$fe, 1, "Temp in degrees C" ' Display sign-on message
'debug	====


DEFINE	HSER_RCSTA 90h
DEFINE	HSER_TXSTA 24h
DEFINE	HSER_SPBRG 12 ' 19200 Bauds ,4 m
'debug	====

'	Mainloop to read the temperature and display on LCD


mainloop: 
'goto	display
'Goto	mainloop 

Gosub	init1820 ' Init the DS1820

command	=	$cc ' Issue Skip ROM command
Gosub	write1820 

command	=	$44 ' Start temperature conversion
Gosub	write1820 

'Pause	2000	' Wait 2 seconds for conversion to complete

Gosub	init1820 ' Do another init

command	=	$cc ' Issue Skip ROM command
Gosub	write1820 

command	=	$be ' Read the temperature
Gosub	write1820 
Gosub	read1820 


'Display the	decimal temperature
'HSEROUT ["Temp	= ",dec (temp>>1),".", dec (temp.0*5)," C",13,10]
'T	=	((temp>> 1)*100)+((TEMP.0*5)*10)
'HSEROUT ["Temp	= ",dec T ,13,10]
value	=	((temp>> 1)*100)+((TEMP.0*5)*10)
	goto	DISPLAY
	goto	mainloop ' Do it forever


'	Initialize DS1820 and check for presence
init1820: 
Low	DQ	' Set the data pin low to init
Pauseus	500	' Wait > 480us
DQ_DIR	=	1 ' Release data pin (set to input for high)

Pauseus	100	' Wait > 60us
	if	DQ = 1 Then
'Lcdout	$fe,	1, "DS1820 not present"
PORTC	=	$39
LOW	DIGITS.0 
PAUSE	150
PORTC	=	$0
Pause	150
	goto	mainloop ' Try again
	endif
Pauseus	400	' Wait for end of presence pulse
	return


'	Write	"command" byte to the DS1820
write1820: 
For	i	= 1 to 8 ' 8 bits to a byte
	if	command.0 = 0 Then
Gosub	write0	' Write a 0 bit
	else
Gosub	write1	' Write a 1 bit
	endif
command	=	command >> 1 ' Shift to next bit
Next	i
	return

'	Write	a 0 bit to the DS1820
write0:
Low	DQ
Pauseus	60	' Low for > 60us for 0
DQ_DIR	=	1 ' Release data pin (set to input for high)
	return

'	Write	a 1 bit to the DS1820
write1:
Low	DQ	' Low for < 15us for 1
@	nop	' Delay 1us at 4MHz
DQ_DIR	=	1 ' Release data pin (set to input for high)
Pauseus	60	' Use up rest of time slot
	return


'	Read	temperature from the DS1820
read1820: 
For	i	= 1 to 16 ' 16 bits to a word
temp	=	temp >> 1 ' Shift down bits
Gosub	readbit	' Get the bit to the top of temp
Next	i
	return

'	Read	a bit from the DS1820
readbit: 
temp.15	=	1 ' Preset read bit to 1
Low	DQ	' Start the time slot
@	nop	' Delay 1us at 4MHz
DQ_DIR	=	1 ' Release data pin (set to input for high)
	if	DQ = 0 Then
temp.15	=	0 ' Set bit to 0
	endif
Pauseus	60	' Wait out rest of time slot
	return
'================== 7 seg ment display ==============================
display: 
'value	=	((temp>> 1)*100)+((TEMP.0*5)*10)

PORTC	=	$39 'Show C on display
LOW	DIGITS.0 
PAUSEUS	1500
PORTC	=	$0

'value	=	125
For	i	= 1 To 4 ' Loop through 4 digits

n	=	Value Dig i ' Get digit to display

'	Gosub	display1 ' Display the value
Digits	=	$ff

Lookup	n,	[$3F, $06, $5B, $4F, $66, $6D, $7D, $07, $7F, $6F,_
$77,	$7C,	$39, $5E, $79, $71, $0FF], Segments

	if	i = 2 then segments = segments + 128 'Digit 2 show dot

Digits	=	~Dcd i

NEXT	I
	goto	mainloop
 
can i borrow your code too fast?!,.. please,.. coz i have the same project,... thank you,...
 
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…