John_Swenson Posted February 20, 2006 Report Share Posted February 20, 2006 I'm building some simple boxes with just a few buttons. In the past I did this using the J5 DIN routines written in ASM. I don't see equivalents in the C wrapper. I want to do these projects with C, so I need some way of getting the J5 functionality in C. I presume I could either rewrite the ASM routines in C, or leave them in ASM and link them in with the C main program, but then I would need a J5 wrapper, correct? Given that one of the functions is an ISR, that might be a little tough to write in C. Any hints on the best way to do this?Thanks,John S. Quote Link to comment Share on other sites More sharing options...
TK. Posted February 21, 2006 Report Share Posted February 21, 2006 Hi John,I presume I could either rewrite the ASM routines in C, or leave them in ASM and link them in with the C main program, but then I would need a J5 wrapper, correct?yes. And you would have to overwork some parts of the j5_din source, since it uses macros, which are not compatible with gpasm (this reminds me, that I wanted to search for a solution for this compatibility problem)Given that one of the functions is an ISR, that might be a little tough to write in C.I guess, that the code in J5_DIN_Update wouldn't run much longer if written in C, because there isn't that much data handling. It should look similar to this:unsigned char j5_din_status, j5_din_status_changed;void j5_din_update(void){ unsigned char tmp = (PORTA & 0x0f) | ((PORTA & 0x20)>>1) | (PORTE << 5); j5_din_status_changed |= j5_din_status ^ tmp; j5_din_status = tmp;} [/code]Best Regards, Thorsten. Quote Link to comment Share on other sites More sharing options...
HawThorn Posted June 1, 2006 Report Share Posted June 1, 2006 It's a little bit hard for me to understand.I'm really bad in ASM, so what's the easiest way to use the j5 as a digital input? I download the zip who explains this with the code, but I don't understand everything. How use the ASM code with my existing C code? Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 1, 2006 Report Share Posted June 1, 2006 Hawthorn take a look at the code above, and compare it with the same function in the J5 DIN example , this might help demonstrate. You can find out how to mix ASM and C on the wiki, but that's not necessary here. Quote Link to comment Share on other sites More sharing options...
HawThorn Posted June 1, 2006 Report Share Posted June 1, 2006 ??? well it's really hard for me. I compare the code but I don’t understand. If I want to use j5 as a digital input, I must put some files of j5_din_v1_3 in my sdcc_skeleton, but not all. The link between the files is hard to understand for me. I make some tests, but it’s not a good way. Programming is not my cup of tea but I try to understand. :'( Quote Link to comment Share on other sites More sharing options...
stryd_one Posted June 1, 2006 Report Share Posted June 1, 2006 May I suggest reading all of this: http://publications.gbdirect.co.uk/c_book/ and all of the PIC18f452 datasheet, and all of the ASM source code. Once you understand all of that, you'll understand what the code is doing :) Maybe I've misunderstood though, if there is something in particular that you'd like to know then ask away :) Quote Link to comment Share on other sites More sharing options...
audiocommander Posted June 2, 2006 Report Share Posted June 2, 2006 Hi HawThorn,there are two different ways on how to use buttons without the need of a DIN-Board and that is what you want, right?Further, I assume you want to code your own application rather than using a pre-existing one?Option 1: wire your switches to some spare AIN-pins (J5 of Core). Whenever a pin-state changes, you'll be notified by AIN_NotifyChange(). You just have to check if (pin_value > 512)Option 2: wire your switches with a resistor (!) to any spare pin of the core module. Consult "mios_pin_list.txt" (it's also in the download section of uCApps.de) to find the appropriate pins and their equivalent names (e.g. RA5 for J5, pin4). With this option you don't have no handy notifier function, you'll have to implement some checking method within a timer. If you are not used to C, I'd recommend option 1!However, there is a relatively new thread that covers most of these two concepts in detail: http://www.midibox.org/forum/index.php?topic=6754.0Hope this helps,;) Michael Quote Link to comment Share on other sites More sharing options...
HawThorn Posted June 2, 2006 Report Share Posted June 2, 2006 Hello...Well I think I understand (and it's working).I use this code void Init(void) __wparam { // initialize the AIN driver MIOS_AIN_NumberSet(2); // just 2 buttons for my test MIOS_AIN_UnMuxed(); // no AINX4 modules are used MIOS_AIN_DeadbandSet(0xFE); // 0 or 254... } And this one void AIN_NotifyChange(unsigned char pin, unsigned int pin_value) __wparam { MIOS_LCD_Clear(); MIOS_LCD_CursorSet(0x00); MIOS_LCD_PrintBCD3(pin); // to see the pot when he change his state MIOS_LCD_CursorSet(0x40); MIOS_LCD_PrintBCD3(pin_value); // to see the value of the pot } I use switches with resistor like this http://www.ucapps.de/mios/j5_din.pdfAnd it’s working :)Some question more:Ideally the value of my switch must be 0 or 255… but some time it’s 254 or 48 or anything else. Is it a problem of my button (really bad one)? It’s not a problem for my application but I want to know.slowly but surelyLaurent Quote Link to comment Share on other sites More sharing options...
audiocommander Posted June 2, 2006 Report Share Posted June 2, 2006 You should set the AIN_DeadbandSet(7), so that it's optimized for 7bit values. This is also the default setting.Remember that it's a 10bit value in <pin_value>. 10 bit means a range from 0 to 1023!And that means you should invoke an if-statement:[tt]if(pin_value > 512) { // ON} else { // OFF}[/tt];)Michael Quote Link to comment Share on other sites More sharing options...
demym Posted January 19, 2009 Report Share Posted January 19, 2009 HI, another noob question:can i use J5 for mixed DIN/DOUT ? (for example, 4 leds and 4 buttons ?)thanks in advance Quote Link to comment Share on other sites More sharing options...
ilmenator Posted January 19, 2009 Report Share Posted January 19, 2009 Yes, you can - you can configure each port pin separately to be input or output. Quote Link to comment Share on other sites More sharing options...
TK. Posted January 19, 2009 Report Share Posted January 19, 2009 Example:with J5_IO_Init(0x0f), J5.0..3 will be used as inputs, and J5.4..7 as outputsBest Regards, Thorsten. Quote Link to comment Share on other sites More sharing options...
demym Posted February 2, 2009 Report Share Posted February 2, 2009 Hi Thorsten,can't find the function J5_IO_Init() in function reference (i am coding in C).And more, how do i set any single pin of J5 using this function ? Is J5 events handled by AIN_NotifyChange as normal AIN ?Thanks very much Quote Link to comment Share on other sites More sharing options...
audiocommander Posted February 2, 2009 Report Share Posted February 2, 2009 Hi demym,I never tried this, but I am quite sure that you can use the function, though it's not (yet?) documented in the function list. But it is defined in mios (j5_io.inc in mios_base_v1_0b) : ;; -------------------------------------------------------------------------- ;; FUNCTION: J5_IO_Init ;; DESCRIPTION: This function initializes J5 as digital IO port. ;; The "TRIS" (tristate) flags are expected as parameter. ;; - if 1: pin used as input ;; - if 0: pin used as output ;; ;; Pin Mapping: ;; Pin #0: RA.0 ;; Pin #1: RA.1 ;; Pin #2: RA.2 ;; Pin #3: RA.3 ;; Pin #4: RA.5 ;; Pin #5: RE.0 ;; Pin #6: RE.1 ;; Pin #7: RE.2 ;; IN: tristate flags in WREG ;; OUT: - ;; USES: MIOS_PARAMETER1 ;; -------------------------------------------------------------------------- _J5_IO_Init ; (for C) Best,MichaelEdit:the function is part of a module, that can be additionally added to enhance the available base functionalities of mios. The extra modules can be found inside the mios_base folder inside /modules Quote Link to comment Share on other sites More sharing options...
demym Posted February 2, 2009 Report Share Posted February 2, 2009 So,can't i use that function in C ?I've put J5_IO_Init(0x0f) in my Init event, but doesn't compile, this is the output:main.c:76: warning 112: function 'J5_IO_Init' implicit declarationmain.c:76: error 101: too many parametersmake: *** [_output/main.o] Error 1thx Quote Link to comment Share on other sites More sharing options...
audiocommander Posted February 2, 2009 Report Share Posted February 2, 2009 Hi,You have to add the j5_io module to your project ;)Here's how that works:Integration Hints~~~~~~~~~~~~~~~~~C based Applications~~~~~~~~~~~~~~~~~~~~ 1) Makefile: add "j5_io.o" to OBJS variable 2) Makefile: include j5_io.mk' date=' and enhance J5_IO_DEFINES if required:---# include J5_IO driverJ5_IO_DEFINES += -DJ5_IO_DONT_USE_INPUT_FUNCTIONS=1include $(MIOS_PATH)/modules/j5_io/j5_io.mk--- 3) main.c: #include <j5_io.h> 4) main.c: add J5_IO_Init(<tris-value>) to Init() hook <tris-value>: e.g. 0x00 for output, 0xff for input, 0xf0 for upper pins input and lower pins output[/quote']Hope that helpsBest,Michael Quote Link to comment Share on other sites More sharing options...
demym Posted February 2, 2009 Report Share Posted February 2, 2009 It worked, thanks very much Thorsten Quote Link to comment Share on other sites More sharing options...
demym Posted February 24, 2009 Report Share Posted February 24, 2009 Hi, i'm still trying to do mixed DIN/DOUT using J5.So i used J5_IO_Init(0x0f) in my code (J5 0-3 for DIN and J5 4-7 for DOUT).The DOUT part of the circuit works (i've connected 4 leds, with a 470 ohm resistor, and leds switch on-off correctly, based on my midi commands to the core).But for the DIN part, i have connected 10k resistors to J5 0-3, and also connected the switches (which, as for the little i can understand, when in ON state they send those pins to ground).I've also implemented this code in AIN_NotifyChange (based on some earlier info in this thread) : valore=0; if (pin_value>512) valore=1; for (i=0; i<4; i++) { J5_IO_PinSet(i, valore); } MIOS_MIDI_TxBufferPut(192); MIOS_MIDI_TxBufferPut(2);(i wanted to generate a MIDI event to see, in MIOS Studio, if something happens when i press the button...)But nothing happens..Also, i've noticed that using a 10k resistor, as shown in the j5_din.pdf, voltage on the pins is 0.3V when the button is open, and, obviously, 0V when i press the button.. Is it correct that this small voltage variation could be catched in AIN_NotifyChange ?What am i doing wrong ? I followed the schematics in j5_din.pdf (with the only exception that i'm using pins 4-7 for DOUT and 0-3 for DIN).Also, in the init function i also have this code (copied from this thread), after the J5_IO_Init(0x0f) : ADCON1 = 0x07; TRISA &= 0xd0; TRISE &= 0xf8;I've tried to comment out these three instructions, but the device seems to be working in the same exact way. Are those instructions necessary, as i'm using the J5_IO_Init(0xF) ?Thanks in advance.Have a nice time Quote Link to comment Share on other sites More sharing options...
stryd_one Posted February 24, 2009 Report Share Posted February 24, 2009 But for the DIN part, i have connected 10k resistors to J5 0-3, and also connected the switches (which, as for the little i can understand, when in ON state they send those pins to ground).Yeh kinda... the pins are tied to +5V normally, and when the switch is on, it allows the 5V to flow to GND (via the resistor, otherwise it would be a short circuit) instead of the PIC pin, I've also implemented this code in AIN_NotifyChange (based on some earlier info in this thread) : valore=0; if (pin_value>512) valore=1; for (i=0; i<4; i++) { J5_IO_PinSet(i, valore); } MIOS_MIDI_TxBufferPut(192); MIOS_MIDI_TxBufferPut(2);That code was intended to show you how to use an AIN as a binary input... doesn't really apply here... plus, you're trying to set the pins 0...3 which you have configured as an input ;)Also, i've noticed that using a 10k resistor, as shown in the j5_din.pdf, voltage on the pins is 0.3V when the button is open, and, obviously, 0V when i press the button.. Is it correct that this small voltage variation could be catched in AIN_NotifyChange ?AIN Notify has nothing to do with it, you are not using the MIOS AIN functions any more, as you have used the J5IO module instead, to interface those pins.Anyway, those pins should be tied high with no switch in place, so you should be reading 5V from them. Check your wiring... (you can post a pic if you like!)Also, in the init function i also have this code (copied from this thread), after the J5_IO_Init(0x0f) : ADCON1 = 0x07; TRISA &= 0xd0; TRISE &= 0xf8;Firstly that is not required as J5IO does that already (in the init as I think you've guessed). That probably breaks it (in addition to the above issues). Code-wise, just follow the example app, and you'll be set.Secondly that code does not support newer PICs like 4620 and 4685.HTH! Quote Link to comment Share on other sites More sharing options...
demym Posted February 24, 2009 Report Share Posted February 24, 2009 Ok, that was a very clear explanation...But.. maybe it's obvious....so where do i catch J5 DIN events ? Into function DIN_ToggleNotify ? Thought it was only reserved for 'normal' DIN operation, not J5The schematics i've used is this:In this schematic it seems that closing the button ties pins directly to ground... is it correct ?Sorry for missing of understanding :-)Thanks a lot, by the way Quote Link to comment Share on other sites More sharing options...
philetaylor Posted February 24, 2009 Report Share Posted February 24, 2009 Yes that is what it is doing, it is tied high by default through the resistor.CheersPhil Quote Link to comment Share on other sites More sharing options...
demym Posted February 24, 2009 Report Share Posted February 24, 2009 And where to catch J5 DIN events ? DIN_NotifyToggle ?Thx a lot ! Quote Link to comment Share on other sites More sharing options...
philetaylor Posted February 24, 2009 Report Share Posted February 24, 2009 Sorry I missed that bit :)Somebody will correct me if i'm wrong but I 'think' that you will need to call J5_IO_PinGet() (or J5_IO_Get()) and then compare the result with the result of the previous call to see if anything has changed. I would think that it will need polling fairly frequently.....CheersPhil Quote Link to comment Share on other sites More sharing options...
demym Posted February 24, 2009 Report Share Posted February 24, 2009 Oh, i knew i was surely missing something !And where do i do this polling ? Maybe into the Timer function ? (after setting the interval with MIOS_Timer_set ?).Thanks very much Quote Link to comment Share on other sites More sharing options...
philetaylor Posted February 24, 2009 Report Share Posted February 24, 2009 I would use the Tick() function, it should get called regularly enough to make sure that button presses aren't missed.CheersPhil Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.