First tests with STC15L204 + NRF24L01 boards – SDCC code

Posted by

I have been searching for low cost wireless boards in the past few months to interface with PIR detectors & temperature sensors to create a small wireless network at home, and I have found the following product :

on many (Chinese) websites (dx.com / wayengineer.com / etc…) for around $7 (includes the nrf24l01+ module).

I bought some (via www.wayengineer.com), and only found limited number of documents describing them (mostly the datasheet on stcmcu.com).

The schematics were found on : http://www.electrodragon.com/w/index.php?title=NRF24L01_UART_w/STC15F204_IC

The 8051 core only has Timers/ADC/Interrupts – Serial interface has to be software emulated (example provided in the datasheet)

And around the same time, Ishan Kehribar (Little Wire…) submitted the following code for nrf24l01 : https://github.com/kehribar/nrf24L01_plus . That library is simple to port and only consist in 4 files (nrf24.c / nrf24.h / nRF24L01.h and radioPinFunctions.c ). I decided to use it and port (? only radioPinFunctions.c has to be changed…)

After some tests/trials/bugs etc.. trying to get sdcc to work (I used version 3.3.0), here is the result (MCU@18.432 Mhz, Serial @9600bps) :

TX device :

> TX device ready
> Tranmission went OK
> Retranmission count:2
> Tranmission went OK
> Retranmission count:0
> Tranmission went OK
> Retranmission count:0
> Tranmission went OK
> Retranmission count:0
> Tranmission went OK
> Retranmission count:0

RX device :

> RX device ready
> 00 AA 55 00
> 00 AA 55 01
> 00 AA 55 02
> 00 AA 55 03
> 00 AA 55 04
> 00 AA 55 05
> 00 AA 55 06
> 00 AA 55 07
> 00 AA 55 08
> 00 AA 55 09

Code is currently using around 1.5KB (1K for the nrf24 lib, 128b for the soft UART). You can download it here.

I will play with the external interrupts and Power Down modes later to interface with the PIR detectors / Temp sensors / ADC…

August 2013 : I have added code to monitor power batteries (via ADC), powerdown wakeup via port interrupt, DS1820 with a onewire library Code is here.

September 2013 : Added code to dump memory content (see this article), and created a Makefile (with separate src / build folders) – Code here

Schematics (TX) :

51 comments

  1. Nice work. I recently bought a unit too. Which tool did you use to flash the stc15? Mine does not respond to the stc standard flash tool.
    And have you already tried to attach a sensor to the board?

    1. I have used a 6.51 version (if I remember correctly) that I found on stcmcu.com website.
      Best results were with an old PC running XP and a regular rs232 to ttl level shifter (max232 like). I sometimes had to lower bitrate and/or disable the ‘high speed’ programming algorithm.
      I agree that the process does not look 100% failsafe.
      Till now, I have succesfully attached a ds1820, used ADC and the proposed wakeup via port interrupt to emulate watchdog and use the sleep modes.
      I had no time to package/document it yet.

      1. Thanks! I got it working using the latest version of the programmer 6.53. I managed to get it to talk to an arduino with the rf24 lib. Cost me a while but I just had to adjust the crc length.

        I am trying to use the ds1820 as well but without much success yet. So I am really looking forward to your findings!

        1. Bammetje, I have just uploaded some code for you to test – I have currently limited connectivity, but I think that everything needed to test DS1820 with onewire is correctly included.

  2. Wow, the onewire library did the trick. I tried a couple of different ds1820 libs but this one works great. I can now send the temperature to an arduino. Next I’m going to try to send it to a raspberry pi.
    I modified your code and stripped the interrupts for now. I’m not very good at working with interrupts yet but I’m going to try it real soon.
    Thank you for your help and I’ll keep you updated on my progress!

  3. Please explain the toolchain for the STC15L204 code flash. When I install SW STC-ISP 6v57 on my PC with COMx to rs232(or USB)-to-TTL5V dongle, what shall I do as the next step(s) using rs232 (or USB) interface? Or I must have some kind od Serial to SPI dongle and use SPI interface for STC15Lxxx code flash?

    1. Just press ‘download/program’ , then power the stc15 board on (the bootloader is only active after a reset and for a very short time – I have tried a usb-rs232 without any success : I guess the stc15 was powered via the RX line.) . Right now, I am using the rx232 level converter that is on an old stk200 Avr evaluation board.

      1. Press download and then shortly disconnect the ground wire and then reconnect it again. That always works for me.

        1. Bammtje,

          Using the ground is a very good idea that I will have to test : since idle state of the rs232 is high level, it should prevent powering via the signal lines… Well done, that’s just brilliant !
          By the way, how are you progressing with your own experiments ?

          1. It’s going very well actually. I’ve programmed multiple stc boards (off course thanks to your great groundwork) and put them together into a sensor network. I am using an iboard now as a central hub to capture all the nrf24 data and convert it to json messages on a web server running on the iboard. (I abandoned the raspberry pi because of performance problems)

            Next step is to broadcast messages from the iboard back to other nrf devices to switch devices or display messages.

            Have you already written something to use the eeprom on the stc15? That would be great to give the units a unique address without changing the source code every time.

            And did you make any other advances on the stc15 field?

          2. Well, then you are way ahead from my work, which has not evolved at all…

            Except, and that’s really funny you are mentioning that, making some tests on the STC15 memory.
            Using EEPROM is easy, in fact, as it follows the code section : try getting code values in the 0x1000-0x13FF
            range and you will get the E2PROM values…

            I will post my code – probably in a new topic…

            I am also trying to figure out the ISP protocol – to program from Linux, but also I would be interested in knowing if the ISP code is upgradable too.

            Regards,

            JJ

  4. Yeah, it would be a lot handier to be able to program it all directly from linux but luckily with virtual machines it is not that annoying anymore 🙂

    I recently bought a stc auto programmer from ebay for a few bucks. This programmer resets the stc automatically, so no need disconnecting the ground wire anymore. Really handy! It is a chinese no name brand; gold coloured with a 10 pin male interface on the end.

    1. Concerning your concern with the device address, there are already 2 solutions :

      – the one I am using : since I have a DS1820 connected, I am using its unique address in the transmitted frame (adding 8 bytes to the message).

      – from the STC memory : use the STC device ID located at 0xFF9-0xFFF

  5. Hi all,
    I’m a french guy and i want buy an STC15L204 with NRF24 but i’m a newbie in programming.
    I want use this couple to make a full wireless sensor (PIR, Temperature, humdity and luminosity…).
    Someone can make a project with draws and source code ??
    Just an idea, develop a protocole and a librarie to work with arduino.

    best resgards

    1. Ca tombe bien, moi aussi je suis Francais : normal, avec un site en free.fr…

      L’utilisation du STC15/NRF24L01 avec tous ces capteurs est sans doute possible: j’ai
      actuellement un PIR (IO avec Interruption) et un DS18B20 pour la temperature. Le STC15
      possede un ADC 10 bits avec plusieurs cananux, donc je pense que l’humidite et la luminosite
      c’est OK aussi. Mais effectivement, le plus complexe, c’est gerer tout ca et si possible
      en consommant peu.

      Cote reception, il suffit d’un autre NRF avec n’importe quel host (Bammetje – actif dans la discussion – a successivement utilise un Raspberry PI, puis une carte iBoard. Un arduino
      conviendrait aussi, les librairies ‘qui vont bien’ existent…)

      Desole, mais je n’ai pas de solution ‘clef en main’.

  6. That’s actually a really good idea to use the ds18b address. However the payload gets a lot bigger so for now I’m sticking to the single byte address I put in the eeprom manually. I don’t think I will be using more than 256 sensors any time soon.

    The eeprom solution with the program memory really solved my problem by the way. That works great! Thanks.

    The next challenge is the battery life of the sensors. I’m using a 9v block to power the sensors, but I think the on board power converter is not very efficient. It only lasts a couple of days with the standard RC-circuit. It’s waking up every 20 seconds. My next step is to connect a 3.3v battery directly to the circuit. I’ll let you know what my findings are.

    1. You probably already did that, but just in case… : one of the first thing I modified on the STC board itself was to remove the LED (I lifted R1 and reconnected one of its end to an IO , P3.2 in my case, to get a visual feedback of my PIR sensor). The LED itself is draining lots of power.
      And I also have the same kind of reflexion for powering the board : at the moment, I am using 4 NI-MH cells (rated at 1900mAh), and it is running ok since mid-august (around 1 month now). In the future, I will perhaps have a look at this product http://touchstonesemi.com/products/ts3310 , which would allow to
      use only 1 or 2 cells, and it has a switched output that could be used to switch sensors only when needed (although the DS18b20 is not consumming much in idle state & I would have to find a breakout board to evaluate first).

      1. I connected two AA batteries straight to the 3.3v input, I don’t have any sensors that need 5v. So I’ll see how long those batteries last.

        But more importantly I have a question. I am trying to set the data rate to 250kbps, but it is not working.
        In nrf24.c I changed the following:

        nrf24_configRegister(RF_SETUP, (0x02<<RF_DR)|((0x03)<<RF_PWR));

        (RF_DR was 0 for 1mbps) I am using the + version so this rate should be supported…
        But it does not work. Do you know what I am missing here??

        1. DR bits are numbered 5 & 3, thus I would try with 0x04 instead of 0x02.
          But I have not tried.
          Are you doing it to improve distance between sensors ?

        2. Well, I dove into it and solved it myself. I noticed that in the RF lib the RF_DR_LOW was not defined so I added
          RF_DR_LOW 5
          RF_DR_HIGH 3
          as definitions and set the correct bits (LOW:1 HIGH:0) in the setup.

          Just for those who would like to use 250kbit mode as well.

  7. Ah, I didn’t see your comment (didn’t refresh :). But I think 0x04 might just do the trick as well 🙂 Thx

    1. And by the way, yeah I’m doing it to improve the distance. And at first sight it appears to be working well. And I don’t need high speeds for this purpose anyway. The sender and receiver are now 15 metres apart and with two concrete walls in-between. I have not lost a packet yet.

  8. Speaking of improving the distance. Have you already tried setting the channel to something else then 2? If I set the channel for something else then i don’t receive anything on the other side. (And the stc15 is a little harder to debug than the arduino 🙂 )

    1. I never tried to change it, sorry.
      Code is perhaps easier to debug on the Arduino, but the code is much clearer on Ishan Kehribar nrf library side…
      [ I was never able to have two Arduino+nrf24l01 discuss together – maybe the modules (they were not the one supplied with the stc15) were just dead. ]

      1. I think something is going wrong in the library. It doesn’t matter which channel I select. It never works with the stc modules.

        As far as your issues, I think you might want to check all the settings. I found out the crc was 8 bit on the arduino and 16 on the stc (or the other way around). You have to be certain that everything is exactly the same. Payload crc channel etc.

        Now if I can only change the channel on the stc 🙂

        1. This is driving me crazy. It keeps returning to channel 2 (the default channel). It does not matter what channel I choose.
          The library seems fine on closer inspection. Can you do me a favor and run a check?

  9. Got it!!
    The channel should be set after the other parameters. I put the set channel instruction just before the poweruprx line in nrf24.c It is now on line 63 and working fine. Maybe you can put it there in your codebase as well, just in case people want to change the channels.

  10. Bonjour et merci pour votre reponse.

    Cote consommation n’y a t il pas un mode sleep ?
    Comment programmer un STC15 ? Plus dur que pour arduino ?

    Je debute avec les micro controleurs mais je cherche quand meme a faire ma domotique maison.

    Par avance merci

    1. Le STC15 est vraiment très basique : pas d’UART matériel par exemple. Il y a bien un mode sleep, mais pour le plus intéressant au niveau conso, il faut prévoir un réveil via une interruption externe (juste 1 resist et 1 condo, mais un wdt aurait simplifié les choses…).
      Pour la programmation , c’est un coeur de 8051, donc il existe du code (C ou asm) disponible un peu partout sur la toile, moyennant qques adaptations, mais rien d’impossible. D’ailleurs le code pour le nrf24 que j’ai utilisé avait été pensé pour un avr à l’origine.
      Pour la programmation matérielle : un soft sous windows, et une interface rs232->ttl, c’est tout.

  11. Many thanks for ALL of your combined efforts on this topic. More over, your time to document and post them. Thank you very much.

    There’s very little information for this MCU that’s available unless you speak (read) Mandarin. I think I’m close, but was hoping that someone might help me understand why I cannot seem to program this mcu?

    I’m using the STC15L205, along with the nRF24L01+, and trying to program it with the stc-isp-15xx-v1.06-english.exe on a Windows 7 machine. I’m also using the Adafruit FTDI USB RS232->ttl adapter. I swapped the rx and tx between the STC15 and the FTDI with no success.

    Specifically, I ‘Open Code File’ mem.hex (2k) and set the correct COM, min/max baud, trim, etc.. I’m sure these are correct cause I can use Putty and get the ‘READY!’. When I press ‘Program’, then power-cycle (pull and replace the GND to the MCU), the FTDI lights flicker and occasionally the green one will illuminate – but the programmer never finishes. It simply says ‘Connect to target MCU …’ and will go on indefinitely (I’ve only waited 15 minutes which should be plenty of time for this small hex file.

    I’m waiting to receive a different adapter/programmer from China but was hoping someone would read my post and help me with something I may be overlooking or another angle to try.

    Thanks in advance to any help and again thank you all for this thread.

    1. I don’t know the stc15l205….
      The ISP software I have been using is a must much higher version : 6.5x found on stcmcu.com
      Using a USB-serial dongle only caused some problems with tuning the clock oscillator precisely, but programming the chip was ok. Switching via the GND as proposed by Bammetje always worked very well – programming usually takes around 30 seconds, not much more.
      To my knowledge, there has never been any ‘ready’ answer from the stc when powered on, unless yours already has some kind of firmware already programmed.

  12. Hi and thanks for your quick reply.

    The ‘READY!’, I’m assuming, was pre-programmed. I’m currently trying different versions of the programming software to see what differences they make.

    Couple Silly Questions: do you supply the +5v to the mcu in addition to the usb voltage?
    Also, are you swapping the rx/tx between the mcu and converter?

    Apologies for the simple questions. At this point, I’m trying to rule out anything that could be causing the lack of communication between the programmer and chip.

    Thanks again for your help!

    Jeff

  13. Thanks for your prompt response. I will look at other versions of the programming software. The ‘READY!’ prompt I receive from the mcu happens right after I power cycle the chip. I suspect that it’s pre-programmed for some earlier application.

    I have two simple questions, just to rule out other causes of this problem. First is: are you swapping RX and TX between the mcu and the serial converter? Also, are you supplying the +5v to the mcu, in addition to the USB supplied power? It acts the same regardless.

    Again, thanks for your time and help.

    Jeff

  14. Excellent! I found what appeared to be an older version: stc-isp-15xx-v6.58C.exe, and that appears to have done the job. I haven’t tested the results yet, but….. fingers are crossed. I’m certainly further along then earlier.

    Thanks!

  15. Hi!

    I’m having trouble compiling the code, I got sdcc running on mint but it’s lacking stc12.h. Where can I get this file from?

    Thanks

      1. Thank you for your reply.

        stc12.h was not a part of my sdcc install (Linux mint 15). The newest version of sdcc from the webpage has this file, while the one in the repo does not. I took the header file from the newest version and dumped it with the rest of the headers in /usr/share/sdcc/include/mcs51 which made it compile.

        1. I am glad you were able to solve your problem.
          I usually recompile compilers from the latest sources, thus I did not encounter the same problem.
          Thanks for the feedback which can help future users/readers of this blog (if anyone…).
          Good luck with those chineese 8051 clones.

          JJ

  16. Could you please send me the hex files to my email address archidehex@gmail.com at an earliest possible? I just want to flash two modules of my stc15l204ea in order to use it as an uart breakout board for nrf240l+ by simply connecting the RX-TX pins in the appropriate places of the uC pins and communicating between the two microcontrollers. It would also be kind of you if you send me the software to flash the hex file.

    1. My files are not intended to provide a nrf24l01-to-uart passthrough.
      It would require a full protocol/API to be able to control the nrf24l01.
      But tou can use them as a basis to develop your own solution.

  17. Hi to all! I have 5 of these microcontrollers and would like to make a network. But I can not make these microcontrollers communicate with the PC. I tried via USB and RS232 setting to @ 9600 @ baub and 18384 MHz and disconnecting and reconnecting the GND. But nothing.
    Can anyone help me?

    thanks

  18. Hello! Thanks for the reply. Some improvement. Leaving the microcontroller with GND disconnected, pressing download version 6.85H and then reconnecting recognize … WOOOWWW !! after a month of expletives is a first success. But I do not load the program with the following message: Download failed! Please using the newest software.
    Thanks for any response.

    1. In the stc-isp software you can try to limit lower & upper speed on the serial link, and there is also a check box about a ‘fast algorithm’ or something similar that you could try to disable.
      Sorry I am not using the software anymore those days…

      JJ

  19. Hi! Always me! Some news. I have MAX3232 and STC15L204EA.
    I tried to talk 2 MAX voltage of 3V but I have some problem in RX, instead with 3.6 V voltages the two MAX is running fine.
    So I try to apply to MAX and STC the 3.6 V, but never happen.
    In desperation I applied 5V (from USB) to the MAX and the STC: BINGO !!
    … And I was able to program the STC!
    But … Only one STC, with other STC it does not work.
    In addition, after a while time the MAX warms terribilemte and creating a short circuit so that the STC remains powered even when I unplug the GND.

    Have I burned everything?
    The voltage of 5V can be applied to the MAX3232 and all ‘STC?

    Thanks for any attention and thanks for any respons

  20. Any idea why I become no correct string back? 8·¿8¿¿·¿¿¿8·8·¿·¿888¯8¿¯8·888¿¿¿88
    isn’t it 9200 baud on softserial?

Leave a Reply

Your email address will not be published. Required fields are marked *