PIC BTFSC and BTFSS not working on PORTA - output
I've always blinked leds on PORTB in PIC16F628A.
Nowdays i need to do that on PORTA because I'm trying keypad matrix on PORTB.
The code below runs perfectly on RB3 of PORTB, but I doesn't in PORTA.
Here is the example in PORTA.
I've tested and the problem is in BTFSS and BTFSC function... Because if I turn led off or on manually is functioning well.
(also I've ommited delay_1s_routine code)
main
;*********CONFIGURACION LED**********
clrf Puerto_Led
movlw 0x07
movwf CMCON
bsf STATUS,RP0
clrf TRISA
bcf STATUS,RP0
loop
call prende_apaga_Led
call delay_1s_routine
goto loop
prende_apaga_Led
btfsc PORTA,RA1 ;si esta en 0 salta el GOTO
goto $+3
bsf PORTA,RA1 ;Pongo en '1' el bit del Led Verde
return
bcf PORTA,RA1 ;Pongo en '1' el bit del Led Verde
return
Thanks in Advice!!
EDITED: Put real values on code
PD: Tested this instruction separately and works great
- bsf PORTA,RA1
- bcf PORTA, RA1
I prefer:
Instead of going into techno quirks and philosophical debates, bypass the problem..
I always use shadow registers for port states and test and drive over them.
So use a bit in your RAM to reflect the state of PORTA, RA1. Trust me it will work seamlessly.
In your code after the label prende_apaga_Led,
You seem to be treating the LED pin once as an input (testing using btfsc), and once as an output (bsf and bcf). The pin can not be used as both at the same time. Either you set the associated TRISA bit as input or output and stick to it.
It seems to me that the code you are trying to do toggles the value of the LED based on its previous state. The way you are doing it won't work. An easier way to do it is by using the xor function as follows:
movlw 0xYY; where YY is the pattern in which 0 means no change to the pin output, 1 means toggle then follow by
xorlw PORTA
so simply it will be as:
clrf Puerto_Led
movlw 0x07
movwf CMCON
bsf STATUS,RP0
clrf Conf_Led
bcf STATUS,RP0
loop
movlw 0xYY ; change YY, i.e. if the LED is on RA2 YY will be 0x04
xorlw Puerto_Led
call delay_1s_routine
goto loop
Related
Stop a Execute Callback Function - MATLAB
i check every two seconds if a EXE is running. If its not running then check in a .mat file a value. If value is 1 then the program should stop(t). At moment the stop(t) isnt working or if i leave it out then i have infinite loop. t=timer; t.period=2; t.TasksToExecute=inf; t.ExecutionMode='fixedRate'; t.TimerFcn=#check_exe; start(t) function check_exe(mTimer,~) %check if EXE is already running [~,b]=system('tasklist'); IsRunning=contains(b,'Digitale_Ablegeschablone'); if IsRunning(1) fprintf('Digitale_Ablegeschablone ist bereits geöffnet!\n'); else load('Parameter.mat', 'Speicherstatus') load('Parameter.mat', 'Block_beendet') load('Parameter.mat', 'Save_Session') if Save_Session==1 fprintf('Sitzung ist gespeichert. Digitale Ablegeschablone wird nicht geöffnet\n'); stop(t) %stop elseif Block_beendet==1 fprintf('Sitzung ist beendet. Digitale Ablegeschablone wird nicht geöffnet\n'); stop(t) %stop else %start again (if its crashed) system('C:\Users\christian\Desktop\TEST\EXE\Digitale_Ablegeschablone.exe &') end end end
You are calling stop(t) inside a function which doesn't "know" what t is - it is not defined anywhere in the scope of that function, hence the error message you get Unrecognized function or variable 't'. The first input to the callback function check_exe, which you're not using at all currently, is actually the timer object. However, you've called it mTimer instead of t. You need to either use stop(mTimer) or change the input name to t. A simpler complete example which just checks if some test file exists can be used to test this: function check(t,~) if exist( 'C:\Users\wfeavyou\Desktop\test.txt', 'file' ) fprintf( 'Stopping at time=%dsec!\n', (t.TasksExecuted-1)*t.Period ); stop(t) %stop else fprintf( 'Check failed, continuing at time=%dsec!\n', (t.TasksExecuted-1)*t.Period ); end end This produces the following output where I manually made the test.txt file after 6 seconds. I can't test it because your example isn't stand-alone, but it should work in the same way just by changing the first input name to t Check failed, continuing at time=0sec! Check failed, continuing at time=2sec! Check failed, continuing at time=4sec! Check failed, continuing at time=6sec! Stopping at time=8sec!
Sentence-count function not returning total count
So i've been trying to create a sentence-count function which will cycle through the following 'story': let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.'; And I realise the problem I'm having but I cannot find a way around this. Basically I want my code to return a the total sCount below but seeing as I've returned my sCount after my loop, it's only adding and returning the one count as a total: const sentenceTotal = (word) => { let sCount = 0; if (word[word.length-1] === "." || word[word.length-1] === "!" || word[word.length-1] === "?") { sCount += 1; }; return sCount; }; // console.log(sentenceTotal(story)) returns '1'. I've tried multiple ways around this, such as returning sentenceTotal(word) instead of sCount but console.log will just log the function name. I can make it return the correct sCount total if I remove the function element of it, but that's not what I want.
I don't see any loop or iterator which would go through story to count the number of occurrences of ., ?, or !. Having recently tackled "counting sentences" myself I know it is a non-trivial problem with many edge cases. For a simple use-case though you can use split and a regular expression; story.split(/[?!.]/).length So you could wrap that in your function like so: const sentenceTotal = (word) => { return word.split(/[?.!]/).length }; let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey. The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side. An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson. Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.'; sentenceTotal(story) => 13
There a several strange things about you question so I'll do it in 3 steps : First step : The syntax. What you wrote is the assignement to a const of an anonymous variable. So what it does is : Create a const name 'sentenceCount' To this const, assign the anonymous function (words) => {...} Now you have this : sentenceCount(words){...} And that's all. Because what you wrote : ()=>{} is not the calling of a function, but the declaration of an anonym function, you should read this : https://www.w3schools.com/js/js_function_definition.asp If you want a global total, you must have a global total variable(not constant) so that the total isn't lost. So : let sCount = 0; //<-- have sCount as a global variable not a const function isEndOfSentence(word) { if (word[word.length-1] === "." || word[word.length-1] === "!" || word[word.length-1] === "?") { sCount += 1; }; }; If you are forbidden from using a global variable (and it's best to not do so), then you have to register the total as a return of your function and store the total in the calling 'CountWords(sentence)' function. function isEndOfSentence(words) {...} callingFunction(){ //decalaration let total; //...inside your loop total += isEndOfSentence(currentWord) } The algorithm Can you provide more context as how you use you function ? If your goal is to count the words until there is a delimiter to mark the end of a sentence, your function will not be of great usage . As it is written, your function will only ever be able to return 0 or 1. As it does the following : The function is called. It create a var called sCount and set it to 0 It increment or not sCount It return sCount so 1 or 0 It's basically a 'isEndOfSentence' function that would return a boolean. It's usage should be in an algorithm like : // var totalSentence = 0 // for each word // if(isEndOfSentence(word)) // totalSentence + totalSentence = 1 // endfor Also this comes back to just counting the punctuation to count the number of sentence. The quick and small solution Also I tried specifically to keep the program in an algorithm explicit form since I guess that's what you're dealing with. But I feel that you wanted to write something small and with as little characters as possible so for your information, there are faster way of doing this with a tool called regex and the native JS 'split(separator)' function of a string. A regex is a description of a string that it can match to and when used can return those match. And it can be used in JS to split a string: story.split(/[?!.]/) //<-- will return an array of the sentences of your story. story.split(/[?!.]/).length //<-- will return the number of element of the array of the sentences of your story, so the sentence count That does what you wanted but with one line of code. But If you want to be smart about you problem, remember that I said Also this comes back to just counting the punctuation to count the number of sentence. So we'll just do that right ? story.match(/(\.\.\.)|[.?!]/g).length Have fun here ;) : https://regexr.com/ I hope that helps you ! Good luck !
Applying torque control in PyByllet makes object fly away from the secene
So I grabbed latest code from https://github.com/bulletphysics/pybullet_robots I modified laikago.py example to the following, I'm only controlling one joint using torque. Strangely enough poor robot flies away from the scene :) There are two ways to fix it: either decrease simulation time step (line 10) or change torque value to 60 (line 33). Any ideas what am I doing wrong? import pybullet as p import time import os os.chdir(os.path.dirname(os.path.abspath(__file__))) p.connect(p.GUI) plane = p.loadURDF("data/plane.urdf") p.setGravity(0,0,-9.8) timeStep = 1./300 # HERE!!! changing it to 1./400 fixes the problem p.setTimeStep(timeStep) urdfFlags = p.URDF_USE_SELF_COLLISION quadruped = p.loadURDF("laikago/laikago_toes.urdf",[0,0,.5],[0,0.5,0.5,0], flags = urdfFlags,useFixedBase=False) #enable collision between lower legs (2,5,8 and 11) lower_legs = [2,5,8,11] for l0 in lower_legs: for l1 in lower_legs: if (l1>l0): p.setCollisionFilterPair(quadruped, quadruped, l0, l1, 1) #set damping for j in range (p.getNumJoints(quadruped)): p.changeDynamics(quadruped, j, linearDamping=0, angularDamping=100) p.setJointMotorControl2(quadruped, j, p.VELOCITY_CONTROL, force=0) #run the simulation p.setRealTimeSimulation(0) for i in range (1000): p.setJointMotorControl2(quadruped, 13, p.TORQUE_CONTROL, force=62) # HERE!!! changing it to force=60 fixes the problem p.stepSimulation() time.sleep(timeStep)
I am a bit late to the party, but maybe you are still in search of an answer. The reason is given here: https://pybullet.org/Bullet/phpBB3/viewtopic.php?t=12644 and a note was added to: https://docs.google.com/document/d/10sXEhzFRSnvFcl3XxNGhnD4N2SedqwdAvK3dsihxVUA/edit#heading=h.jxof6bt5vhut The reason: "You need to first unlock/disable the default velocity/position motor. [...] Otherwise, your force/torque has to exceed the default motor." (see first link). To fix do (same link): pybullet.setJointMotorControl2(objUid, linkIndex, p.VELOCITY_CONTROL, force=0)
GCC produces unaligned function address on Cortex M3
I just placed a function to a specific address using a section and then I output the address of that function and the result is the chosen section address + 1. This is what I did: void __attribute__((section (".my_fct_address"))) Fct_Ptr_Test (void) { ... } and void (*fct_ptr) (void); fct_ptr = Fct_Ptr_Test; printf ("0X%X\r\n", (uint32_t)(fct_ptr)); fct_ptr (); in ld-file: .my_fct_address 0x800F000 : { KEEP(*(.my_fct_address)) /* keep my variable even if not referenced */ } > FLASH The above printf statement outputs 0x800F001 and Fct_Ptr_Test is called properly If I set fct_ptr = 0x800F000; the system crashes. If I set fct_ptr = 0x800F001; everything is fine again. If I don't place Fct_Ptr_Test in its own section , ie let the linker place it anywhere I also get an odd address. Now I wonder how 0x800F001 can be a proper address on a 32 bit controller (ARM cortex M3) and what is stored in 0x800F000. Even more strange: map-file always shows the even addresses Can anybody help? Thanks Martin
Linker sets the least-significant bit of Thumb functions to 1 to facilitate interworking (see docs). Perhaps that's your case?
Scilab - calling another GUI within a GUI. Functions not working
I'm quite new to scilab, I have created two GUIs (see example below), with script 2 being called from script 1. However the function in script 2 don't seem to work. Can anyone help? Script 1 '////////// f=figure('figure_position',[0,0],'figure_size',[1250,650]); ////////// delmenu(f.figure_id,gettext('File')) delmenu(f.figure_id,gettext('?')) delmenu(f.figure_id,gettext('Tools')) toolbar(f.figure_id,'off') handles.dummy = 0 ; handles.exam=uicontrol(f,'unit','normalized','BackgroundColor', [0.5,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.5,0.5,0.1,0.05],'Relief','flat','SliderStep',[0.01,0.1],'String','exam','Style','pushbutton','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','obj102','Callback','exam_callback(handles)') function exam_callback(handles) close(f); clear exec('costs0-1.sce',-1) ; endfunction` Script 2 ////////// Defining the figure (size, name etc)///////////////////////////// f=figure('figure_position',[0,0],'figure_size',[1250,650],'auto_resize','on','background',[8]); ////////// delmenu(f.figure_id,gettext('File')) delmenu(f.figure_id,gettext('?')) delmenu(f.figure_id,gettext('Tools')) toolbar(f.figure_id,'off') //Cabinet - TEXT handles.obj17=uicontrol(f,'unit','normalized','BackgroundColor',[1,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[0,0,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.15,0.93,0.1,0.05],'Relief','flat','SliderStep',[0.01,0.1],'String','Cabinet','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','obj17','Callback','') // Cabinet - POP UP MENU handles.service=uicontrol(f,'unit','normalized','BackgroundColor',[0.8,0.8,0.8],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[12],'FontUnits','points','FontWeight','normal','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.25,0.93,0.15,0.05],'Relief','flat','SliderStep',[0.01,0.1],'String','1|2','Style','popupmenu','Value',[1],'VerticalAlignment','middle','Visible','on','Tag','service','Callback','service_callback(handles)') // CALCULATE PUSHBUTTON handles.Calculate=uicontrol(f,'unit','normalized','BackgroundColor',[0,0.8,0],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[16],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.22,0.02,0.15,0.08],'Relief','raised','SliderStep',[0.01,0.1],'String','CALCULATE','Style','pushbutton','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','Calculate','Callback','Calculate_callback(handles)') // Resources- TEXT handles.Resourcestxt=uicontrol(f,'unit','normalized','BackgroundColor',[1,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.75,0.95,0.20,0.05],'SliderStep',[0.01,0.1],'String','Resources in hours','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','','Callback','') // TOTAL hours - TEXT handles.totalhourstxt=uicontrol(f,'unit','normalized','BackgroundColor',[1,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.75,0.75,0.12,0.05],'SliderStep',[0.01,0.1],'String','Total Hours','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','','Callback','') // hardware hours - text handles.totalhours=uicontrol(f,'unit','normalized','BackgroundColor',[0.95,1,1],'Enable','on','FontAngle','normal','FontName','helvetica','FontSize',[14],'FontUnits','points','FontWeight','bold','ForegroundColor',[0,0.5,0],'HorizontalAlignment','center','ListboxTop',[],'Max',[1],'Min',[0],'Position',[0.88,0.75,0.08,0.05],'SliderStep',[0.01,0.1],'String','','Style','text','Value',[0],'VerticalAlignment','middle','Visible','on','Tag','totalhours','Callback','') function Calculate_callback(handles) if handles.service.value == 1 then resource_hrs = 2 end if handles.service.value == 2 then resource_hrs = 10 end set(handles.totalhours,'String',string(resource_hrs)); endfunction
Problem It is a scoping problem. When function exam_callback() gets called it runs the other script with exec('costs0-1.sce',-1). In that script you define the function Calculate_callback(handles). This goes out of scope and is deleted when exam_callback() is finished and as such can't be called when the button is pressed. The second problem is that the handles are not globally affected, so when leaving exam_callback() the handles of the second Cost Gui are not added to handles. Solution You can move the generating of the GUI into a function createCostGui() and then load script2 at the start of script1 with exec('costs0-1.sce',-1);. To make Calculate_callback(handles) function discard the handles argument and use the tags to find the handles function Calculate_callback() serviceHandle = findobj('tag','service'); if serviceHandle.value == 1 then resource_hrs = 2 end if serviceHandle.value == 2 then resource_hrs = 10 end totalHoursHandle = findobj('tag','totalhours'); set(totalHoursHandle,'String',string(resource_hrs)); endfunction Further remarks Text elements are generally static and thus don't need a Callback argument. If you want an argument to stay at its default value, you don't need to specify them. From the Scilab documentation: h = uicontrol(PropertyName, PropertyValue,...) creates an uicontrol and assigns the specified properties and values to it. It assigns the default values to any properties you do not specify. The default uicontrol style is a "Pushbutton". The default parent is the current figure. See the Properties section for information about these and other properties. Small remark on your question Next time an error message could help with making your question more specific.