Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

need help
Goto page 1, 2  Next
 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
javanismechimol

Bascom Member



Joined: 15 Apr 2014
Posts: 9

PostPosted: Fri May 09, 2014 10:32 am    Post subject: need help Reply with quote

anyone can help me, i have trouble with my bascom coding.
why all my actuators (fan, lamp, heater, blower) is on when play. i use sht11 and dht11 for controlling temperature and humidity....
--this my coding--
# sorry too long

$regfile = "m16def.dat"
$crystal = 8000000
$baud = 9600
$hwstack = 32
$swstack = 8
$framesize = 24


'>>>>>>>>>>>>>>>>>> add this HERE for software JTAG disable
'DISABLE_JTAG:
IN R24, MCUCR
!SET
BLD R24, 7
!OUT MCUCR, R24
!OUT MCUCR, R24


Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = Porta.2 , Db5 = Porta.3 , Db6 = Porta.4 , Db7 = Porta.5 , E = Porta.1 , Rs = Porta.0
Cls
Cursor Off Noblink
'Routine to read the SHT11 Humidity sensor chip
'By Stuart Leslie
'Contact stu@4sightinc.com with any questions
'Uses BascomAVR
'a .01 uf capacitor across VCC and Ground on the SHT11 really cleans up the data
'a pullup is required on "data" pin as shown in the data sheet

Dim Ctr As Byte
Dim Dataword As Word
Dim Command As Byte
Dim Dis As String * 20

Dim Calc As Single
Dim Calc2 As Single
Dim Rhlinear As Single
Dim Rhlintemp As Single
Dim Tempc As Single
Dim Tempf As Single

Dim I As Byte , Tmp As Byte
Dim J As Byte
Dim Mybyte As Byte
Dim Wenshi As String * 40
Dim Tmp_str8 As String * 8
Dim Count As Byte

Const C1 = -4
Const C2 = 0.0405
Const C3 = -0.0000028
Const T1c = .01
Const T2 = .00008
Const T1f = .018

Sck Alias Portb.0
Dataout Alias Portb.1
Datain Alias Pinb.1
'Redled Alias Portb.2

Dht_put Alias Portb.2 ' PODLACZONY CZUJNIK DHT11
Dht_get Alias Pinb.2 ' PODLACZONY CZUJNIK DHT11
Dht_io_set Alias Ddrb.2 ' PODLACZONY CZUJNIK DHT11

Dht_put2 Alias Portb.3 ' PODLACZONY CZUJNIK DHT11
Dht_get2 Alias Pinb.3 ' PODLACZONY CZUJNIK DHT11
Dht_io_set2 Alias Ddrb.3 ' PODLACZONY CZUJNIK DHT11

Declare Sub Getit()
Declare Sub Get_wenshidu(wendu As Byte , Shidu As Byte)
Declare Sub Get_wenshidu2(wendu As Byte , Shidu As Byte)

Ddrb = &B11111111 'all port b are output
Config Pinb.0 = Output 'sck
Config Pinb.1 = Output 'datain

Config Serialin = Buffered , Size = 128
Config Serialout = Buffered , Size = 128

'reset the serial communications first, it is easily confused!
Set Dataout
For Ctr = 1 To 12
Set Sck
Waitus 2
Reset Sck
Waitus 2
Next Ctr
'=============
Enable Interrupts

Set Dht_io_set
Set Dht_put

Set Dht_io_set2
Set Dht_put2

'sht set output
Config Portd.5 = Output
Config Portd.6 = Output
Config Portd.7 = Output
Fan3 Alias Portd.5
Blower3 Alias Portd.6
Lamp3 Alias Portd.7
'sht set output

'dht1 set output
Config Portc = Output
Fan1 Alias Portc.0
Blower1 Alias Portc.1
Lamp1 Alias Portc.2
Heater1 Alias Portc.3
'dht1 set output

'dht2 set output
Config Portc = Output
Fan2 Alias Portc.4
Blower2 Alias Portc.5
Lamp2 Alias Portc.6
Heater2 Alias Portc.7
'dht2 set output

Do

Cls 'continually read the tempfature and humidity
''SHT==========================================================================
Command = &B00000011
Call Getit 'Get the temperature, puts result in "dataword" for us
'
Tempf = T1f * Dataword
Tempf = Tempf - 40

Tempc = T1c * Dataword 'get celcius for later calculations and for "the rest of the world"
Tempc = Tempc - 40

Dis = Fusing(tempc , "###.##")
Print "Temperature = " ; Dis ; " (C)"

Locate 1 , 1
Lcd "T0:" ; Dis ; " (C)"

'kipas====
If Tempc > 34 Then
Fan3 = 0
Elseif Tempc < 31 Then
Fan3 = 1
End If
'kipas====
'Lamp3====
If Tempc > 31 Then
Lamp3 = 0
Elseif Tempc < 28 Then
Lamp3 = 1
End If
'Lamp3====


Command = &B00000101
Call Getit 'get the humidity
Calc = C2 * Dataword
Calc2 = Dataword * Dataword 'that "2" in the datasheet sure looked like a footnote for a couple days, nope it means "squared"!
Calc2 = C3 * Calc2
Calc = Calc + C1
Rhlinear = Calc + Calc2

Dis = Fusing(rhlinear , "##.##")
Print "Humidity adjusted for linear = " ; Dis

Locate 2 , 1
Lcd "H0:" ; Dis ; " (%)"

'blower====
If Rhlinear > 78 Then
Blower3 = 0
Elseif Rhlinear < 75 Then
Blower3 = 1
End If
'blower====


Calc = T2 * Dataword
Calc = Calc + T1c
Calc2 = Tempc - 25
Calc = Calc2 * Calc
Rhlintemp = Calc + Rhlinear

Dis = Fusing(rhlintemp , "##.##")
Print "Humidity adjusted for temperature = " ; Dis
Print

'SHT=========================================================================

Waitms 1500
Call Get_wenshidu(i , J)
Waitms 50
Print "T1: " ; I ; "C | H1: " ; J ; "%"

Cls
Locate 1 , 1
Lcd "T1:" ; I ; " (C)"
Locate 2 , 1
Lcd "H1:" ; J ; " (%)"

'kipas1====
If I > 34 Then
Fan1 = 0
Elseif I < 31 Then
Fan1 = 1
End If
'kipas1====

'Lamp1====
If I > 31 Then
Lamp1 = 0
Elseif I < 28 Then
Lamp1 = 1
End If
'Lamp1====

'blower1====
If J > 78 Then
Blower1 = 0
Elseif J < 75 Then
Blower1 = 1
End If
'blower1===

'heater1====
If J > 75 Then
Heater1 = 0
Elseif J < 72 Then
Heater1 = 1
End If
'heater1====

Waitms 1500
Call Get_wenshidu2(i , J)
Waitms 50
Print "T2: " ; I ; "C | H2: " ; J ; "%"

Cls
Locate 1 , 1
Lcd "T2:" ; I ; " (C)"
Locate 2 , 1
Lcd "H2:" ; J ; " (%)"

'kipas2====
If I > 34 Then
Fan2 = 0
Elseif I < 31 Then
Fan2 = 1
End If
'kipas2====

'Lamp2====
If I > 31 Then
Lamp2 = 0
Elseif I < 28 Then
Lamp2 = 1
End If
'Lamp2====

'blower2====
If J > 78 Then
Blower2 = 0
Elseif J < 75 Then
Blower2 = 1
End If
'blower2===

'heater2====
If J > 75 Then
Heater2 = 0
Elseif J < 72 Then
Heater2 = 1
End If
'heater2====

Wait 1
Loop


Sub Getit()

Local Datavalue As Word
Local Databyte As Byte

'start with "transmission start"
Set Sck
Reset Dataout
Reset Sck
Set Sck
Set Dataout
Reset Sck


'now send the command
Shiftout Dataout , Sck , Command , 1

Ddrb = &B11111101 'datain is now input
Config Pinb.1 = Input 'datain
Set Sck 'click one more off
Reset Sck
Waitus 10 'no idea why, but it doesn't work without it!
Bitwait Pinb.1 , Reset 'wait for the chip to have data ready

Shiftin Datain , Sck , Databyte , 1 'get the MSB
Datavalue = Databyte

Ddrb = &B11111111
Config Pinb.1 = Output

Reset Dataout 'this is the tricky part- Lot's of hair pulling- have to tick the ack!
Set Sck
Reset Sck

Ddrb = &B11111101 'datain is now input
Config Pinb.1 = Input

Shiftin Datain , Sck , Databyte , 1 'get the LSB
Shift Datavalue , Left , 8
Datavalue = Datavalue Or Databyte
'don't tick the clock or ack since we don't need the CRC value, leave it hanging!
Dataword = Datavalue

Ddrb = &B11111111
Config Pinb.1 = Output

Reset Dataout
Set Sck
Reset Sck

Ddrb = &B11111101 'datain is now input
Config Pinb.1 = Input

Shiftin Datain , Sck , Databyte , 1 'not using the CRC value for now- can't figure it out! Anybody know how to impliment?
'Print "CRC value was - " ; Databyte

Ddrb = &B11111111
Config Pinb.1 = Output

Set Dataout
Set Sck
Reset Sck
End Sub

Sub Get_wenshidu(wendu As Byte , Shidu As Byte)

Count = 0
Wenshi = ""
Set Dht_io_set
Reset Dht_put
Waitms 20


Set Dht_put
Waitus 40
Reset Dht_io_set
Waitus 40
If Dht_get = 1 Then
Shidu = 255
Exit Sub
End If

Waitus 80
If Dht_get = 0 Then
Shidu = 255
Exit Sub
End If

While Dht_get = 1 : Wend

Do
While Dht_get = 0 : Wend

Waitus 30
If Dht_get = 1 Then
Wenshi = Wenshi + "1"
While Dht_get = 1 : Wend
Else
Wenshi = Wenshi + "0"
End If
Incr Count
Loop Until Count = 40

Set Dht_io_set
Set Dht_put


Tmp_str8 = Left(wenshi , 8)
Shidu = Binval(tmp_str8)


Tmp_str8 = Mid(wenshi , 17 , 8)
Wendu = Binval(tmp_str8)


Tmp_str8 = Right(wenshi , 8)
Tmp = Binval(tmp_str8)

Mybyte = Wendu + Shidu
If Mybyte <> Tmp Then
Shidu = 255
End If
End Sub

Sub Get_wenshidu2(wendu As Byte , Shidu As Byte)

Count = 0
Wenshi = ""
Set Dht_io_set2
Reset Dht_put2
Waitms 20


Set Dht_put2
Waitus 40
Reset Dht_io_set2
Waitus 40
If Dht_get2 = 1 Then
Shidu = 255
Exit Sub
End If

Waitus 80
If Dht_get2 = 0 Then
Shidu = 255
Exit Sub
End If

While Dht_get2 = 1 : Wend

Do
While Dht_get2 = 0 : Wend

Waitus 30
If Dht_get2 = 1 Then
Wenshi = Wenshi + "1"
While Dht_get2 = 1 : Wend
Else
Wenshi = Wenshi + "0"
End If
Incr Count
Loop Until Count = 40

Set Dht_io_set2
Set Dht_put2


Tmp_str8 = Left(wenshi , 8)
Shidu = Binval(tmp_str8)


Tmp_str8 = Mid(wenshi , 17 , 8)
Wendu = Binval(tmp_str8)


Tmp_str8 = Right(wenshi , 8)
Tmp = Binval(tmp_str8)

Mybyte = Wendu + Shidu
If Mybyte <> Tmp Then
Shidu = 255
End If
End Sub

End

(BASCOM-AVR version : 1.11.9.0 , Latest : 2.0.7.7 )
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri May 09, 2014 2:30 pm    Post subject: Reply with quote

Try in the beggining of program Config and set ports.

For eg.
Code:

Config Portb = &B00011010 : Portb = &B11111111
Config Portd = &B01000000 : Portd = &B10111111
Config Porta = &B00000000 : Porta = &B11111111


This config and after Set configured pin to desired state

This is only example. First configure port input/output depends of Your hardware and..if You drive Fan, Blower or whatever in LOW state of pin You must SET port to HIGH on the start!
Somme of this settings You have in Your code but not at the beginining. You can change it later but if uC would for example drive directions of some motor then propably in your code short both of directions and crash Razz
Pins responsible of drive something must be configured and set properly on the begin.
Back to top
View user's profile Visit poster's website
javanismechimol

Bascom Member



Joined: 15 Apr 2014
Posts: 9

PostPosted: Fri May 09, 2014 4:26 pm    Post subject: Reply with quote

can you fix my project ?
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Fri May 09, 2014 5:28 pm    Post subject: Reply with quote

How You drive Lamp, Blower and Heater 0V or 5V (depends of hardware, You must know..) Relays trough transistor, ULN2003 or MOSFET?
In code You can change Waitms 1500 to Wait 2 , read all values from LCD and write it here. This is for know what of value uC read from sensors.
In code for the test You can change
Quote:
Portd.5 = 0 '<<<<<
Portd.6 = 0 '<<<<<
Portd.7 = 0 '<<<<<
Portc = 0 '<<<<<

from 0 to 1 ( search lines with " <<<<<<")
Back to top
View user's profile Visit poster's website
javanismechimol

Bascom Member



Joined: 15 Apr 2014
Posts: 9

PostPosted: Tue Dec 02, 2014 3:59 pm    Post subject: Reply with quote

EDC wrote:
How You drive Lamp, Blower and Heater 0V or 5V (depends of hardware, You must know..) Relays trough transistor, ULN2003 or MOSFET?
In code You can change Waitms 1500 to Wait 2 , read all values from LCD and write it here. This is for know what of value uC read from sensors.
In code for the test You can change
Quote:
Portd.5 = 0 '<<<<<
Portd.6 = 0 '<<<<<
Portd.7 = 0 '<<<<<
Portc = 0 '<<<<<

from 0 to 1 ( search lines with " <<<<<<")



i use moc3041 to drive lamp and heater. to drive blower i use tip41
Back to top
View user's profile
snipsnip

Bascom Member



Joined: 10 Feb 2014
Posts: 74
Location: Melbourne

australia.gif
PostPosted: Tue Dec 02, 2014 9:33 pm    Post subject: Reply with quote

Quote:
why all my actuators (fan, lamp, heater, blower) is on when play


Quote:
i use moc3041 to drive lamp and heater


Are you using AC power? just a thought. DC would latch on.

Got a schematic.
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Wed Dec 03, 2014 3:04 pm    Post subject: Reply with quote

It`s sad what I`m write here, but this is true.
If You don`t know how to set up Your micro ports on the beginning of programm then You don`t know basic stuff.
If You set port to output on wrong pin then You can damage/destroy microcontroller.
Programming is not "copy - paste". I don`t wait for answears - I search them everywhere.
Read one book or even first chapter.
If You search and don`t understand something or need someone with "fresh look" for Your code then post Very Happy
Back to top
View user's profile Visit poster's website
bzijlstra

Bascom Ambassador



Joined: 30 Dec 2004
Posts: 1179
Location: Tilburg - Netherlands

netherlands.gif
PostPosted: Thu Dec 04, 2014 11:46 am    Post subject: step by step.. Reply with quote

If I am correct, all your lamps, blowers, heaters, fans are connected to PortC. Do what EDC suggest and put the config of the out/input ports at the top of your program. After Dim and declares. And after that put a STOP in your program. At start connect just one lamp or fan on your board and start your microcontroller. Work your way so that the fan or lamp doesn't get on while starting your microcontroller. Not even blink. From there on you can continue. Why not just try to get temperature and humidity on your display as your next step. And so on...

Furthermore I suggest to permanently put JTAG of in the fusebit settings.

My two cents...
Ben Zijlstra
Back to top
View user's profile Visit poster's website
javanismechimol

Bascom Member



Joined: 15 Apr 2014
Posts: 9

PostPosted: Tue Dec 09, 2014 6:54 pm    Post subject: Reply with quote

EDC wrote:
It`s sad what I`m write here, but this is true.
If You don`t know how to set up Your micro ports on the beginning of programm then You don`t know basic stuff.
If You set port to output on wrong pin then You can damage/destroy microcontroller.
Programming is not "copy - paste". I don`t wait for answears - I search them everywhere.
Read one book or even first chapter.
If You search and don`t understand something or need someone with "fresh look" for Your code then post Very Happy


thanks for your information. I've completed this project i'm change the code and it's work. Smile. i have destroyed 1 microcontroller Smile .
Back to top
View user's profile
javanismechimol

Bascom Member



Joined: 15 Apr 2014
Posts: 9

PostPosted: Tue Dec 09, 2014 7:05 pm    Post subject: Reply with quote

snipsnip wrote:
Quote:
why all my actuators (fan, lamp, heater, blower) is on when play


Quote:
i use moc3041 to drive lamp and heater


Are you using AC power? just a thought. DC would latch on.

Got a schematic.


yup, i'm using ac power Smile

this the schematic
Back to top
View user's profile
Deanus

Bascom Member



Joined: 26 May 2006
Posts: 188
Location: Adelaide

australia.gif
PostPosted: Tue Dec 09, 2014 8:26 pm    Post subject: Reply with quote

Hi Javanismechimol,

How about doing the circuit diagram in a JPG or other format, not everybody has Circuit drawing programs like Eagle.
I use Protel and therefore can't open your files.

Dean
Back to top
View user's profile
Dibor

Bascom Member



Joined: 04 Dec 2004
Posts: 137
Location: ISRAEL

israel.gif
PostPosted: Tue Dec 09, 2014 9:57 pm    Post subject: Reply with quote

Hi.
This is Proteus design(schematics and simulation by Labcenter) , not Eagle.
Back to top
View user's profile MSN Messenger
javanismechimol

Bascom Member



Joined: 15 Apr 2014
Posts: 9

PostPosted: Wed Dec 10, 2014 4:14 am    Post subject: Reply with quote

Deanus wrote:
Hi Javanismechimol,

How about doing the circuit diagram in a JPG or other format, not everybody has Circuit drawing programs like Eagle.
I use Protel and therefore can't open your files.

Dean
Back to top
View user's profile
Deanus

Bascom Member



Joined: 26 May 2006
Posts: 188
Location: Adelaide

australia.gif
PostPosted: Wed Dec 10, 2014 8:51 am    Post subject: Reply with quote

Dibor,
I was making the point that other people might not be able to see the files, Eagle was just an example.

And surprise, I don't have Proteus either, so I still couldn't see the schematic. Now in JPG, format I can.



Hi Javanismechimol,

Now that I can see the schematic, Lets try debugging this in a modular approach.

Place these lines at the point shown (in your program)

I think these should be PORT Not Pin. (PIN is for inputs)

Config Pinb.0 = Output 'sck
Config Pinb.1 = Output 'datain


'Existing code
Blower2 Alias Portc.5
Lamp2 Alias Portc.6
Heater2 Alias Portc.7
'dht2 set output

'=========================
'Insert the lines here in your program at the point shown.

Portd=0 ' This makes ALL Port D outputs LOW, and therefore OFF to all circuits connected to those Port bits

Do 'Loop here after turning all outputs and devices OFF
Loop

'=========================
'Existing code
Do

Cls 'continually read the tempfature and humidity
''SHT==========================================================================
Command = &B00000011
Call Getit 'Get the temperature, puts result in "dataword" for us


If all the devices are now OFF. We move onto the next part.
If they are still ON, then the problem lies in the configuration section above, so debug that before moving to the next section.

Questions
- Is the LCD working OK, is any text printed or is it blank or is it black, does it have random characters ?
- Do any of the buttons work.
- What are the results in Proteus.

If all the devices are now OFF, then change the inserted part to this.

'=========================
'Insert the lines here in your program at the point shown.

Portd=0 ' This makes ALL Port D outputs LOW, and therefore OFF to all circuits connected to those Port bits


Portd.1 = 1 'Turn ON Blower 1

Do 'Loop here after turning all outputs and devices OFF
Loop

'=========================

If that turns blower 1 on, then repeat the process for all other used Port D outputs to make sure that the circuit(s) are working as expected.

If that works, then remove that section of code (the inserted bit)

If the LCD is NOT working, then the next step would be to get that working.
If the LCD is working, then print variables (from within your program) to the LCD to see what's going on.

Place the

Do
Loop

in your code as a break to see if the code is getting to that point at the time of running.
Use One of your outputs that has a LED attached ( Lamp or Heater ) as an indication that your code has reached a certain point. (without the Lamp or heater connected)

Rem out certain modules while you debug others to sort out any conflicts.

Work through the code like this until things start working as expected.

Dean
Back to top
View user's profile
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 971

poland.gif
PostPosted: Wed Dec 10, 2014 11:21 am    Post subject: Reply with quote

The code presented half a year ago, lacked the configuration for that port to be configured as an output. Now Javanismechimol wrote that the code is already running. August guess that completed the missing config in the code.
I'd like to now turn our attention to that in the control circuit, there are two LEDs, one after the other with a 10k resistor and the voltage is only 5V.
When damaged TIP41 can damage the microcontroller. No protection for the re-voltage
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum