clearbit

johnlong

Senior Member
Hi All
Having problems with the clearbit command within this code section
the set and clear bit update setting and clearing dirsD as they should
The problem is that outpinsD=dirsD is not occuring
It does at start up but after being set outpinsD to %11110000
but once the outpinsD are set to 1 by the sub routine they do not clear back to 0
the dirsD set to 1 and clear to 0 as expected
Code:
symbol pointer=b5
symbol marker=b6
symbol N=34
hsersetup   B9600_8, %001
inti:
	hserflag=0
	dirsD=%11110000	
	outpinsD=dirsD
	pause 500
main:
	do
	if hserflag <>0 then:get 0,pointer:get 1,marker:endif	
if pointer="L" or pointer="M" or pointer="N"or pointer="O" or pointer="P" then:gosub but_on:endif 
	
	loop
but_on:
		select pointer
		case "L"	
       		if dirD.0=0 then
				 setbit dirsD,0     
				 
				outpinsD=dirsD
				hserout 0, ("cf.pic0=",N,"10")
				elseif dirD.0=1 then
				 clearbit dirsD,0
				 
				outpinsD=dirsD
				hserout 0, ("cf.pic1=",N,"9")
				endif 				
	case "M"	
       		if dirD.1=0 then
				setbit dirsD,1       
			
				outpinsD=dirsD
				hserout 0, ("th.pic0=",N,"18")
			elseif dirD.1=1 then
				clearbit dirsD,1
				
				outpinsD=dirsD
				hserout 0, ("th.pic1=",N,"17")
			endif				
	case "N"	
       		if dirD.2=0 then
				 setbit dirsD,2       
				 
				 outpinsD=dirsD
				 hserout 0, ("tde.pic0=",N,"16")
			elseif dirD.2=1 then
				clearbit dirsD,2
				
				outpinsD=dirsD
				hserout 0, ("tde.pic1=",N,"15")
			endif	
			
		case "O"	
       		if dirD.3=0 then
				setbit dirsD,3       
				
				outpinsD=dirsD
				hserout 0, ("thu.pic0=",N,"14")
			elseif dirD.3=1 then
				clearbit dirsD,3
				
				outpinsD=dirsD
				hserout 0, ("thu.PIC1=",N,"13")
		endif
	endselect 
	gosub NF
	gosub HP
	return
		NF:
		hserout 0,(N,$ff,$ff,$ff)
		return
		HP:
		pointer=0
		marker=0
		hserflag=0
		hserptr=0
		return
regards
john
 

Buzby

Senior Member
It's unclear as to why you need to do dirsD at all. Is there some specific reason why the pins must go Hi-Z when not driven to '1' ?

Are you sure whatever they are connected to is not keeping them 'high' when you clear the dir control bit ?.
 

johnlong

Senior Member
Hi Buzby
To set the pins on the drive D to an on or off state just a glorified switch for ssr relays
no components attached at present just running it through the terminal in the simulator
and observed that the once the outpins increassed by there respective value determined by dirsD setting
they do not revert back after the alternative dirsD setting clearbit works ok, outpinsD=dirsD does not
update
 

Buzby

Senior Member
I wouldn't even bother with using dirs in this case, just use 'HIGH d.x' and 'LOW d.x' in the right places.

The behaviour you are seeing in the simulator could be an artifact.

Consider this :
1 your code sets d.x high, so the sim makes the 'output is high' colour green.
2 your code sets d.x as input, but the simulated pin is still high, so the sim changes to 'input high' which is yellow.

You can tell dirs has worked, because (a) the pin is yellow, and (b) you can click the pin to toggle it's input state, which you can't do if it's green.
 

johnlong

Senior Member
Hi Buzby
so using the dirs like this does not result in an ON/OFF or does change of state to input high
act like an OFF for the other drive related variables "outpinsD,pinD ect"

john
 

hippy

Technical Support
Staff member
I would suggest starting which much simpler code to investigate the behaviour of the code you are concerned about. Something like ...

Code:
Do

  setbit dirsD, 0     
  outpinsD = dirsD

  clearbit dirsD, 0
  outpinsD = dirsD

Loop
Maybe replace SETBIT with 'dirD.0=1" and CLRBIT with "dirD.0=0" to check the behaviour is the same. If so then it's likely any issue isn't with the CLRBIT or SETBIT per se.
 

johnlong

Senior Member
Hi Hippy
using dirD.0=1 ect does the same its not a question of the set and clear not working they do a treat altering as expected
outpinsD=dirsD once set to 1 can then not be altered back once its gone through the gosub as Buzby comented in #4
turns it into an input high
yet at the begining dirsD=%11110110: outpinsD=dirsD puts those values into outpinsD
with the output high would this ineffect act as an OFF button for the pin driving an LED

john
 

Buzby

Senior Member
Generic_PIC_pin.JPG

This diagram from the PIC datasheet shows that dirs ( TRIS ) does not affect the data latch, it only makes the pin 'float'.

There is no need to use dirs if you only need to turn a pin 'high' and 'low'. Using dirs will make the pin 'float', and the result on a real chip will be unpredictable.

The simulator can't do 'unpredictable', so it's best guess is to use whatever the level was before the pin went to 'float'.

Try this :
Code:
[color=Black]inti:
      [/color][color=Purple]hserflag[/color][color=DarkCyan]=[/color][color=Navy]0
      [/color][color=Purple]dirsD[/color][color=DarkCyan]=[/color][color=Navy]%11111111   [/color][color=Green]' Make all pins outputs
      [/color][color=Blue]pause [/color][color=Navy]500[/color]
[color=Black]main:
      [/color][color=Blue]do
      if [/color][color=Purple]hserflag [/color][color=DarkCyan]<>[/color][color=Navy]0 [/color][color=Blue]then[/color][color=Black]:[/color][color=Blue]get [/color][color=Navy]0[/color][color=Black],[/color][color=Purple]pointer[/color][color=Black]:[/color][color=Blue]get [/color][color=Navy]1[/color][color=Black],[/color][color=Purple]marker[/color][color=Black]:[/color][color=Blue]endif 
if [/color][color=Purple]pointer[/color][color=DarkCyan]=[/color][color=Red]"L" [/color][color=DarkCyan]or [/color][color=Purple]pointer[/color][color=DarkCyan]=[/color][color=Red]"M" [/color][color=DarkCyan]or [/color][color=Purple]pointer[/color][color=DarkCyan]=[/color][color=Red]"N"[/color][color=DarkCyan]or [/color][color=Purple]pointer[/color][color=DarkCyan]=[/color][color=Red]"O" [/color][color=DarkCyan]or [/color][color=Purple]pointer[/color][color=DarkCyan]=[/color][color=Red]"P" [/color][color=Blue]then[/color][color=Black]:[/color][color=Blue]gosub [/color][color=Black]but_on:[/color][color=Blue]endif 
      
      loop[/color]
[color=Black]but_on:
            [/color][color=Blue]select [/color][color=Purple]pointer
            [/color][color=Blue]case [/color][color=Red]"L"    
                  [/color][color=Blue]if [/color][color=Purple]pinD.0[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then
                        high D.0     
                        hserout [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"cf.pic0="[/color][color=Black],[/color][color=Blue]N[/color][color=Black],[/color][color=Red]"10"[/color][color=Blue])
                  else 
                        low D.0
                        hserout [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"cf.pic1="[/color][color=Black],[/color][color=Blue]N[/color][color=Black],[/color][color=Red]"9"[/color][color=Blue])
                  endif                         
      case [/color][color=Red]"M"    
                  [/color][color=Blue]if [/color][color=Purple]pinD.1[/color][color=DarkCyan]=[/color][color=Navy]0 [/color][color=Blue]then
                        high D.1    
                        hserout [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"th.pic0="[/color][color=Black],[/color][color=Blue]N[/color][color=Black],[/color][color=Red]"18"[/color][color=Blue])
                  else
                        low D.1
                        hserout [/color][color=Navy]0[/color][color=Black], [/color][color=Blue]([/color][color=Red]"th.pic1="[/color][color=Black],[/color][color=Blue]N[/color][color=Black],[/color][color=Red]"17"[/color][color=Blue])
                  endif                   
      case [/color][color=Red]"N"    [/color]

Cheers,

Buzby
 
Last edited:

johnlong

Senior Member
Morning Buzby
works like a dream had a read around concerning the floating pin issue it may well have caused problems, but certainly increased
the power draw as from what I have read on stack exchange floating pins pull power "Your route gives a diffined "0" or "1", which
is what I was after
Code:
symbol pointer=b5
symbol marker=b6
symbol N=34

hsersetup   B9600_8, %001
inti:
      hserflag=0
      dirsD=%11111111   ' Make all pins outputs
	outpinsD=%11110000 'set the system constant outputs
      pause 500
main:
      do
      if hserflag <>0 then:get 0,pointer:get 1,marker:endif 
if pointer="L" or pointer="M" or pointer="N"or pointer="O" or pointer="P" then:gosub but_on:endif 
      
      loop
but_on:
            select pointer
            case "L"    
                  if pinD.0=0 then
                        high D.0     
                        hserout 0, ("cf.pic0=",N,"10")
                  else 
                        low D.0
                        hserout 0, ("cf.pic1=",N,"9")
                  endif                         
      case "M"    
                  if pinD.1=0 then
                        high D.1    
                        hserout 0, ("th.pic0=",N,"18")
                  else
                        low D.1
                        hserout 0, ("th.pic1=",N,"17")
		endif         
		 case "N"    
                  if pinD.2=0 then
                        high D.2    
                        hserout 0, ("th.pic0=",N,"18")
                  else
                        low D.2
                        hserout 0, ("th.pic1=",N,"17")
		endif  
		 case "O"    
                  if pinD.3=0 then
                        high D.3    
                        hserout 0, ("th.pic0=",N,"18")
                  else
                        low D.3
                        hserout 0, ("th.pic1=",N,"17")
                  endif        
	endselect 
		gosub NF
	gosub HP
	return
		NF:
		hserout 0,(N,$ff,$ff,$ff)
		return
		HP:
		pointer=0
		marker=0
		hserflag=0
		hserptr=0
		return
		return
will slot that into its respective area and have the picaxe and nextion thumping along together
thank you
john
 

hippy

Technical Support
Staff member
Code:
            case "L"    
                  if pinD.0=0 then
                        high D.0     
                        hserout 0, ("cf.pic0=",N,"10")
                  else 
                        low D.0
                        hserout 0, ("cf.pic1=",N,"9")
                  endif
Not sure what you are trying to achieve, but it looks like all you need to have the same functionality is ...

Code:
            case "L"    
                  Toggle D.0
 

johnlong

Senior Member
Hi
both the toggle and high D.1 work 7byte diffrence between the 2
The toggle function is to be used via the Nextion to turn on or off mains attached appliances
isolating the pin from functioning so the SSR on that pin is defunked
I needed to determine the pins state as the Duel state button function in the Nextion send both the pressed and realease command
at the same time (you would have thourght they would have separated them)
the case statement alters the pin state and visually lets you know by changing the button image from OFF to ON, on the display
Code:
#picaxe 40x2
#no_data
#Slot 1
table 0,("JANUARY",0,"FEBRUARY",0,"MARCH",0,"APRIL",0,"MAY",0,"JUNE",0,"JULY",0,"AUGUST",0,"SEPTEMBER",0,"OCTOBER",0,"NOVEMBER",0,"DECEMBER",0,0) 
table 88,("MONDAY",0,"TUESDAY",0,"WEDQTESDAY",0,"THURSDAY",0,"FRIDAY",0,"SATURDAY",0,"SUQTDAY",0,0)
symbol usbON=b1   ' Data storage ON or off
symbol pointer=b2  'setpointer and equipment selectiON in keypad    
symbol marker =b3      
symbol themode=b4  'program locatiON pointer
symbol vT = b9
symbol vH = b15
'***********************

'PICAXE VARIABLES FOR CONVERTED VALUES FROM I2C CLOCK
symbol sec =b26
symbol hour=b27
symbol mins=b28  'Read back variables from the DS1307 usiQTg the DS1307 as the
symbol day=b29   'Timer for the Data LoggiQTg and Display
symbol month=b30  'hour = hourbcd / 16 * $FA + hourbcd Hippy's solutiON for BCD to Dec cONversiON
symbol year=b31
symbol date=b32

symbol ii=b48
symbol Auto=b49

symbol QT=34
symbol control=$10
inti:
	hi2csetup i2cmaster, %11010000, i2cslow_8, i2cbyte 
  	hi2csetup i2cslave, %11010000
	hsersetup   B9600_8, %001	
	dirsD=%11111111	
	outpinsD=%00000000	
	hserflag=0
	pause 1000
main:

  do 
		'b0 section for test purposes will be determined from sensor values
	if b0<=10 then:pinD.7=1:pinD.6=0:pinD.5=0:pinD.4=0:endif 		
	if b0>=11 and b0<=20 then:pinD.7=0:pinD.6=1:pinD.5=0:pinD.4=0:endif 			
	if b0>=21 and b0<=30 then:pinD.7=0:pinD.6=0:pinD.5=1:pinD.4=0:endif 				
	if b0>=31 then:pinD.7=0:pinD.6=0:pinD.5=0:pinD.4=1:endif 				
				inc b0
			if b0=41 then:b0=0:endif
			if b0=5 or b0=15 or b0=25 or b0=35 then gosub dis 'test purpose will be controlled via ds1307''s timing loop
			'if hour=23 and mins>=58 then gosub calnd
	sertxd ("IQT SLOT 1 AUTO MODE",cr,lf)
	sertxd ("DRIVE D=",32,#dirsD,32,"outpinsD=",32,#outpinsD,cr,lf)	 
	
		
	if hserflag <>0 then:get 0,pointer:get 1,marker	
	if pointer="Z" and Marker="Z" then:run 0:endif		
	if pointer="Z" and marker="X" then: gosub dis:endif
	if pointer="L" or pointer="M" or pointer="N"or pointer="O" or pointer="P" then: gosub but_on:endif
	endif 
 	 
	
	if day=0 or day>7then:day=1:endif
	
	if month=0 or month>12 then:month=1:endif
	
if date=0 or date>31 then: date=1:endif

if hour>24 then: hour=1:endif
  loop
dis:
	hsersetup B9600_8, %0
	
	hserout 0, ("page 0",$FF,$FF,$FF):gosub HP	
	hserout 0, ("svt.txt=",QT,#vt):gosub QTF:gosub HP 
	hserout 0, ("hr.txt=",QT,#hour):gosub QTF:gosub HP 'rtc clock hour	
	hserout 0, ("mins.txt=",QT,#mins):gosub QTF:gosub HP 'rtc clock minutes
	hserout 0, ("svh.txt=",QT,#vh):gosub QTF:gosub HP 
	if hour>12 then
			hour=hour-12
			hserout 0, ("tam.txt=",QT,"PM")
		else
			hserout 0, ("tam.txt=",QT,"AM") 
			endif:gosub QTF:gosub HP
	if pinD.4 =1 then:hserout 0, ("fs.txt=",QT,"Slow"):gosub QTF:gosub HP:endif		
	if pinD.5=1 then:hserout 0, ("fs.txt=",QT,"Low"):gosub QTF:gosub HP:endif		
	if pinD.6=1 then:hserout 0, ("fs.txt=",QT,"High"):gosub QTF:gosub HP:endif		
	if pinD.7=1 then:	hserout 0, ("fs.txt=",QT,"Fast"):gosub QTF:gosub HP:endif 
		'return
	'**************************
'calnd:
	select case day
	case 1 ii=88
	case 2 ii=95
	case 3 ii=103
	case 4 ii=113
	case 5 ii=122
	case 6 ii=129
	case 7 ii=138
	endselect	
		bptr=ii			
			hserout 0, ("day.txt=",QT) 
 			readtable bptr,ii
 			 inc bptr
 			 do until ii = 0
   			 hserout 0, (ii)
    			readtable bptr,ii
     			inc bptr
  			loop
			 gosub QTF
 			ii=bptr
			gosub HP
		'***************************
	hserout 0, ("date.txt=",QT,#date):gosub QTF:gosub HP 'clock date
		if date=1 then hserout 0, ("tQTt.txt=",QT,"ST") 
		elseif date=2 then hserout 0, ("tQTt.txt=",QT,"ND") 
		elseif date=3 then hserout 0, ("tQTt.txt=",QT,"RD") 
		elseif date=31 then hserout 0, ("tQTt.txt=",QT,"ST")
		else  hserout 0, ("tQTt.txt=",QT,"TH"):endif 
		gosub QTF
		gosub HP
		'&&&&&&&&&
		select case month
		case 1 auto=0
		case 2 auto=8
		case 3 auto=17
		case 4 auto=23
		case 5 auto=29
		case 6 auto=33
		case 7 auto=38
		case 8 auto=43
		case 9 auto=50
		case 10 auto=60
		case 11 auto=68
		case 12 auto=77
			endselect
		bptr=auto			
			hserout 0, ("mth.txt=",QT) 
 			readtable bptr,auto
 			 inc bptr
 			 do until auto = 0
   			 hserout 0, (auto)
    			readtable bptr,auto
     			inc bptr
  			loop
			 gosub QTF
 			auto= bptr
		gosub HP
		
		hserout 0, ("yr.txt=",QT,"201",#year):gosub QTF:gosub HP
		'return
		'******** solely for test perposes when simulating delete ON program
		
	if pinD.0=1 then:hserout 0, ("cf.txt=",QT,"ON"):gosub QTF:gosub HP
	elseif pinD.0=0 then hserout 0, ("cf.txt=",QT,"OFF"):gosub QTF:gosub HP:endif 'heater	
	if pinD.1 =1 then:hserout 0, ("th.txt=",QT,"ON"):gosub QTF:gosub HP
	elseif pinD.1=0 then: hserout 0, ("th.txt=",QT,"OFF"):gosub QTF:gosub HP:endif'dehumidifier		
	if pinD.2 =1 then:hserout 0, ("tde.txt=",QT,"ON"):gosub QTF:gosub HP
	elseif pinD.2=0 then: hserout 0, ("tde.txt=",QT,"OFF"):gosub QTF:gosub HP:endif 'humidifier		
	if pinD.3=1 then:hserout 0, ("thu.txt=",QT,"ON"):gosub QTF:gosub HP
	elseif pinD.3=0 then: hserout 0, ("thu.txt=",QT,"OFF"):gosub QTF:gosub HP:endif 'circulatiON fan	
		
	if usbon=1 then:hserout 0, ("sd.txt=",QT,"ON"):gosub QTF:gosub HP:endif 'data aquasitiON
	if usbon=0 then:hserout 0, ("sd.txt=",QT,"OFF"):gosub QTF:gosub HP:endif 'data aquasition
		hserout 0, ("tm.txt=",QT,"AUTO"):gosub QTF:gosub HP 
		
	 hsersetup   B9600_8, %001
	 hserflag=0
	inc day:inc month:inc date:inc hour 
return

but_on:
            select pointer
      case "L"    
                  Toggle D.0
			if pinD.0=0 then
				hserout 0, ("cf.pic0=",QT,"10")
		else 
			 hserout 0, ("cf.pic1=",QT,"9")
                  endif 
      case "M"    
                 Toggle D.1
		     if pinD.1=0 then                            
                        hserout 0, ("th.pic0=",QT,"18")
                  else                        
                        hserout 0, ("th.pic1=",QT,"17")
		endif         
	case "N"    
                 Toggle D.2
		     if pinD.2=0 then    
                        hserout 0, ("th.pic0=",QT,"18")
                  else
                        hserout 0, ("th.pic1=",QT,"17")
		endif  
	 case "O"    
                 Toggle D.3
		     if pinD.3=0 then    
                        hserout 0, ("th.pic0=",QT,"18")
                  else
                        hserout 0, ("th.pic1=",QT,"17")
		endif 
	case "P"	if usbon=0 then
				usbon=1
				hserout 0, ("sd.pic1=",QT,"12") 
			elseif usbon=1 then
				hserout 0, ("sd.pic0=",QT,"11") 
			endif
	endselect 
		gosub QTF
		gosub HP
	return

QTF:
		hserout 0,(QT,$ff,$ff,$ff)
		return
HP:
		hserflag=0
		hserptr=0
		return
		return
Hi Ibenson pinched your table idea replaced N as sugested
this is not the finished code
a work in progress but coming on
john
 
Last edited:

lbenson

Senior Member
johnlong said:
... pinched your table idea ...
Pinch away. I no doubt pinched the underlying idea some 45+ years ago.

johnlong said:
... the Dual state button function in the Nextion sends both the pressed and released commands ...
In my practice, I put code in only one of those for any clickable entity, usually "Released", for instance, 'print "!a"'. If you are uncomfortable with sending only a "toggle" notification (because your program might have lost track), then send the value--for example, 'print "!a"' for dual-state button activated, 'print "!b"' for deactivated.

Then in my code I look for "!" from Nextion, and then SELECT to get the individual responses to user action, e.g., "a". Anything else from the Nextion that you don't expect is ignored (what else can you do in code of this sort with what you don't expect?).

I started this usage of 2-character codes to take advantage of the 2-character background serial receive on the PICAXE M2 devices, but find it also works well for the X2 PICAXEs.
 

lbenson

Senior Member
With this statement:

if hserflag <>0 then:get 0,pointer:get 1,marker

Do you need to make sure that "marker" is available? With, for instance,

if hserflag <>0 then : if hserptr < 2 then: pause 10 : endif : get 0,pointer:get 1,marker
 

johnlong

Senior Member
Hi Ibenson
tried that with the Nextion button presses event "print L+" in the release event "print L-" but I found that it sends
both commands (pressed and released) together at the same time,but did not alter the oriantation of the data, thats why I looked into the state of the pin to
determine the action by the Picaxe
I followed you advice and minimised the data from the Nextion to just the 2 bytes and the basics are fine
just polishing
I will say that this is an extension that I have being playing with for sometime and have running using a 4 button keypad
for the selection and setting the clock using an LCD required all bar a few bytes of Slot0 the Nextion has halved
this and delivering more information to the user not to mention freeing up 3 pins win win
I think it makes an interesting addition to the Picaxe armoury and looking at what I have seen reading around far
easier to code with the Picaxe than the ARshallnotbenamedDWINO
Lets face it I struggle with the PIcaxe god bless I did'nt come across the wino first
regards
john
 

johnlong

Senior Member
Hi Ibenson
I just reset ever read and write back to 0 for both the hserflag and hserptr some legacy commands come from nextion in 2 bytes
with the Picace discarding marker and operating fine on the pointer
my main concern was with the ON/OFF buttons thanks to buzby and hippy that is nolonger an issue
just pointed the Picaxe to look at what I wanted to recieve the gosubs seem to offer the Nextion enough time to deal with the data
In the Nextion after every print command I have placed in code_c to clear its buffer of all commands so its blank after every write
figured it would stop any stray data being sent
 

lbenson

Senior Member
john--I didn't know about the "code_c" command to clear the Nextion buffers (thanks)--but haven't seemed to have needed to use it, since I ignore anything which I am not prepared to respond to.

I would worry, perhaps needlessly, about the possibility of having hserflag going to non-zero and the picaxe program responding in less than the approximately one millisecond it takes for the second character to be sent (at 9600 bps), and so missing the "marker" value--thus the wait until hserptr can become > 1.
 

johnlong

Senior Member
Hi Ibenson
Had not considered the hserflag going to less than 0 I always thourgth it did'nt do negative numbers
but on that note I will take your advice as its always best to listen to experiance
and stick in your line from post#14
thank you for that sometimes (all the time) I need it dumming down for it to register
regards john
 

lbenson

Senior Member
John,

I didn't think that any picaxe number could be less than one, just that the first character of two arrives (hserflag becomes 1 and hserptr becomes 1) and the picaxe could look for the second character in the millisecond before it arrives and hserptr becomes 2.

As I said, I have no experience that says that will happen, but I see a possibility that it could.
 
Top