LGT8F328P - clone of ATMega328 with lots of extras! - Page 1
Forum section: electronics / general

author: Fungus
author_rank: Super Contributor
timestamp: June 02, 2022, 06:30:38 am
content: Given the rocketing price of AVR Aduinos, I've just been looking at these things...https://www.aliexpress.com/wholesale?SearchText=LGT8F328PThe extra features are very interesting for a project I have in mind, both for the DAC and for the extra ALU.I ordered a few (some purple, some green) and so far so good. They work just like Arduinos in the Arduino IDE.Information on the extra features isverysparse though. There's the original datasheet that's been machine-translated by google on a couple of github repositories and very little else.(FWIW: Google translate has improved, you can re-translate the manual today and google does a much better job)I'm programming mine in assembly language. I'm particularly interested in the 16-bit ALUs. Has anybody else use them or does everybody simply switch them to 32MHz mode and use them as a faster Arduinos?The way the new ALU works is not by having extra instructions (there's no bits left in the Atmel instruction set) but by monitoring the bus and seeing what you're doing.eg. The 2kB of RAM is mapped twice in the address space on these chips. If you access the copy of RAM at 0x2000 then you can load/store 16-bit values in a single clock.Example problem:This instruction loads r0 with an 8-bit value from address 0x0100:Code:LDS R0,0x100This instruction loads the DX register in the new ALU with a 16-bit value at 0x0100 and leaves r0 unchanged:Code:LDS R0,0x2100(ie. same opcode, different result depending on the RAM address)Problem:It doesn't seem to work for any register other than DX. I believe I'm supposed to be able to load any register but it fails:Code:LDS R1,0x2100 ; Supposed to load the DY resister, but the value goes into DX instead!Conversely, using the STS instruction tostore16-bit numbers works just fine:Code:STS 0x2100,R0 ; Store DX resister to location 0x100STS 0x2102,R1 ; Store DY resister to location 0x102 - works perfectly!STS 0x2104,R2 ; Store DAH to location 0x104 - works!STS 0x2106,R3 ; Store DAL to location 0x106 - works!

author: GromBeestje
author_rank: Frequent Contributor
timestamp: June 03, 2022, 01:11:16 pm
content: How do you verify the values in the registers?I've looked at the Logic Green datasheets way back when, and it mentioned some debugging interface. However, I haven't found a suitable debugger or any information about the debugging protocol. Have you found a suitable debugger to debug this chip?

author: Fungus
author_rank: Super Contributor
timestamp: June 03, 2022, 02:07:57 pm
content: Quote from: GromBeestje on June 03, 2022, 01:11:16 pmI read them back (using the instructions that I found to work) and send them to the serial monitor in the Arduino IDE."dump_cpu" gives me a printout like this:Code:Program started OKuDSC test 5r1:r0 cafe r9:r8 0000 r17:r16 0000 r25:r24 0135r3:r2 cafe r11:r10 0000 r19:r18 4567 X: 2106r5:r4 0000 r13:r12 0000 r21:r20 89ab Y: 05d3r7:r6 0000 r15:r14 0000 r23:r22 cdef Z: 210cDX: dead DY: beef DA: cafebabeI can send you a copy of the code if you want to fiddle around.

author: Fungus
author_rank: Super Contributor
timestamp: June 03, 2022, 08:04:41 pm
content: Here's a copy of the code I've been using. Decompress it in your sketches folder.nb. You don't need to do anything to the Arduino IDE, just connect up your LGT8F328P and select "Pro Mini" in the boards menu. It should work.

author: Peabody
author_rank: Super Contributor
timestamp: June 03, 2022, 09:09:45 pm
content: Is there any information on how deeply they sleep? A bare bones 328P will sleep at some fraction of a microamp. Will this one do that too?

author: Fungus
author_rank: Super Contributor
timestamp: June 04, 2022, 05:40:06 am
content: Quote from: Peabody on June 03, 2022, 09:09:45 pmI imagine it will.The datasheet I've got doesn't have any hard numbers but it talks a lot about the chip's power saving features. It has all the power saving modes of the 328P plus a few more.I only have boards with built-in serial chip so it's a bit hard to test bare chip consumption.

author: eblc1388
author_rank: Frequent Contributor
timestamp: June 04, 2022, 11:14:14 am
content: I hated datasheet that doesn't contains bookmarks for sections. Attached is the V1.0.5 document with three levels of bookmarks added for easy reference. It only takes me a few hours of work to do that.

author: Fungus
author_rank: Super Contributor
timestamp: June 04, 2022, 04:08:53 pm
content: Quote from: eblc1388 on June 04, 2022, 11:14:14 amI've been doing the same as I go along. Not so systematic/detailed though.

author: Fungus
author_rank: Super Contributor
timestamp: June 04, 2022, 07:09:15 pm
content: Summing up what I've found so far about the uDSC data transfer instructions:Works:Code:in DX/DY/DA,addressout address,DX/DY/DAsts address,DX/DY/DA ; (via register names r0/r1/r2/r3)ld DX/DY/DA,X/Y/Zld DX/DY/DA,X/Y/Z+ld DX/DY/DA,-X/Y/Zldd DX/DY/DA,Z+qst X/Y/Z,DX/DY/DAst DX/DY/DA,X/Y/Z+st DX/DY/DA,-X/Y/Zstd DX/DY/DA,Z+qDoesn't work:Code:lds address,DX/DY/DA ; nb. DX works, the others don'tldd Rn,Z+qstd Rn,Z+q(I'm not even sure what the last two are supposed to do, I can't get them to do anything at all... anybody?)nb. I've tested on different boards (purple ones and green ones) so I don't think it's a faulty chip.The datasheet goes on and on for two pages about how the "LDD Rn,Z+q" modes update Z after execution but it's the exact opposite of what's needed IMHO. I'd much rather Z stayed where it is so I can access a bunch of variables in a struct using Z as a base. It's made doubly painful by q only being positive so you can't go backwards. I'm predicting a lot of saving/restoring of the Z register!There's even a half page diagram explaining how "Z+2" can give sequential access ... but if I want sequential data access I can just use "LD Rn,Z+". It works perfectly, changing Z by 2 after execution.Meanwhile: The other instructions get almost no coverage at all, eg. The only clue that LDS/STS instructions can be used in this context is in the sentence"LDS/STS in the LGT8XM instruction set can access the 0x2100~0x28FF area, but the LDD/STD Y/Z+q addressing mode is more efficient."(it's not more efficient if you have to set up Z first, eg. to load a single variable. I'd much rather have a working LDS instruction instead, but that's broken!)As always, I guess I'll just have to muddle through using the instructions that seem to work... it's still going to be a win without 16-bit LDS.

author: Kerlin
author_rank: Regular Contributor
timestamp: June 07, 2022, 12:06:58 am
content: Thanks for the update.Interesting to see how other information seems to have been deliberately derailed then locked out.No such problems here.

author: tepalia02
author_rank: Regular Contributor
timestamp: June 19, 2022, 02:14:21 pm
content: Datasheet available?

author: Fungus
author_rank: Super Contributor
timestamp: June 19, 2022, 02:40:05 pm
content: Quote from: tepalia02 on June 19, 2022, 02:14:21 pmI grabbed the Chinese datasheet and ran it through Google translate.Datasheet:http://www.lgtic.com/upload/lgt8fx8p/LGT8FX8P_databook_v1.0.4.pdfTranslate it here:https://translate.google.com/?sl=auto&tl=en&op=docsThat's how all the other "English datasheets" on the web have been made, it's worth doing it again because they're mostly three or four years old and google constantly evolves.There's enough information in there to figure out what you need to know. Don't forget that it's ATMega328 compatible so you can refer to the Atmel datasheet for most things and only use the translated datasheet for LGT-specific features.There's a few Githubs with other info and system files that are tweaked to fix millis(), serial baud rates, etc. for 32MHz:https://github.com/dbuezas/lgt8fxhttps://github.com/RalphBacon/LGT8F328P-Arduino-Clone-Chip-ATMega328P

author: tepalia02
author_rank: Regular Contributor
timestamp: June 25, 2022, 10:22:28 am
content: Thank you so much for all this info and links.

author: agarza
author_rank: Newbie
timestamp: July 26, 2022, 11:21:38 pm
content: Quote from: Fungus on June 04, 2022, 05:40:06 amThe best power consumption on sleep I have achieved is ~170µA, nowhere near the 1µA@3.3V that is mention on the manual

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2022, 12:17:09 am
content: Quote from: agarza on July 26, 2022, 11:21:38 pmThat seems very high. What's your sleep code?

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2022, 12:31:20 am
content: Just thought I'd throw this out there...Does anybody here have a deepl account? Can you translate the datasheet using that and upload it here to see if it's any better?https://www.deepl.com/translator

author: Peabody
author_rank: Super Contributor
timestamp: July 27, 2022, 01:53:21 am
content: Here's my sleep test code for the 328P. It produces sleep current less than 1uA if the chip is a genuine 328P (not all are). It works on a Pro Mini with regulator and LED removed. I don't know if it's at all appropriate for this new chip.Code:#include <Wire.h>#include <avr/sleep.h>#include <avr/wdt.h>int i;void setup(){for (i = 0; i < 20; i++) { // all pins to one rail or the otherpinMode(i,OUTPUT);digitalWrite(i,LOW);}ADCSRA = 0; // disable ADC for power savingwdt_disable(); // disable WDT for power savingset_sleep_mode (SLEEP_MODE_PWR_DOWN); // Deep sleepsleep_enable();sleep_bod_disable(); // disable brownout detector during sleepsleep_cpu(); // now go to sleep}void loop(){}

author: rooppoorali
author_rank: Regular Contributor
timestamp: July 27, 2022, 09:35:10 am
content: Are they interchangeable? Can we write code for LGT8F328P using Atmel Studio and load the hex file using AVRdude? Just like the way we do with ATMega328?

author: Peabody
author_rank: Super Contributor
timestamp: July 27, 2022, 02:41:48 pm
content: Ralph Bacon made a very good video on this chip a few years ago.

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2022, 03:52:03 pm
content: Quote from: Peabody on July 27, 2022, 01:53:21 amThanks. That doesn't prove it 100% though, there could be a problem in the libraries. Maybe a peripheral is left powered on or something.Quote from: Peabody on July 27, 2022, 01:53:21 amThe best sort of Pro Mini.

author: Fungus
author_rank: Super Contributor
timestamp: July 27, 2022, 03:54:35 pm
content: Quote from: rooppoorali on July 27, 2022, 09:35:10 amThe code is interchangeable, yes.I can use one by selecting "Pro Mini" in the Arduino IDE so I guess the bootloaders are compatible, too.The main differences are lack of EEPROM, no fuse bits, no ICSP (it has a JTAG-like interface for programming/debugging).

author: westfw
author_rank: Super Contributor
timestamp: July 28, 2022, 11:54:00 pm
content: QuoteSome of the instruction timings are slightly different. (Several traditionally 2-cycle instructions only take one cycle. Although this is also true of the newer AVRs, though.)

author: Fungus
author_rank: Super Contributor
timestamp: July 29, 2022, 06:45:59 am
content: Quote from: westfw on July 28, 2022, 11:54:00 pmAnd several traditionally 3-cycle instructions only take two cycles.

author: rooppoorali
author_rank: Regular Contributor
timestamp: July 29, 2022, 08:08:54 am
content: Thanks for answering to my questions , dear all.

author: Fungus
author_rank: Super Contributor
timestamp: July 29, 2022, 08:21:31 am
content: This doc has a comparison of the instructions that are faster.Some of those will have abigimpact on performance, especially the faster push/pop.

author: Fungus
author_rank: Super Contributor
timestamp: July 29, 2022, 08:26:07 am
content: This doc lists the advantages:

author: DavidAlfa
author_rank: Super Contributor
timestamp: July 29, 2022, 03:20:29 pm
content: Given the descriptions, more chinese, definitely!

author: AndyBeez
author_rank: Frequent Contributor
timestamp: July 29, 2022, 04:43:57 pm
content: Barely known outside of China, these 'LG' ( LogicGreen ) chips exist mainly for deployment in dirt cheap Chinese consumer products: toys to appliances and to more toys. Don't expect the reliability or support of the original Atmel products. So avoid using in your self driving car project. Since chipaggedon started, these are being sold in arduino clone boards at arduino 'compatible' prices. If you want extras with documentation, play with STM32s.

author: Fungus
author_rank: Super Contributor
timestamp: July 29, 2022, 08:31:24 pm
content: Quote from: AndyBeez on July 29, 2022, 04:43:57 pmIs that bad thing?Who doesn't want a cheaper Arduino-compatible chip with extra timers, extra PWMs and a DAC for hobby use?

author: Fungus
author_rank: Super Contributor
timestamp: July 30, 2022, 02:12:19 am
content: I created a DeepL account to re-translate the datasheet. It seems nicer in some areas, especially the diagrams.Copy attached below.Now let's see if I can really cancel the "subscription" before they charge me anything.

author: DavidAlfa
author_rank: Super Contributor
timestamp: July 30, 2022, 02:49:35 am
content: Remember you can do the same thing using Google translate, for free.

author: Fungus
author_rank: Super Contributor
timestamp: July 30, 2022, 06:29:50 am
content: Quote from: DavidAlfa on July 30, 2022, 02:49:35 amLike in this post?https://www.eevblog.com/forum/microcontrollers/lgt8f328p-clone-of-atmega328-with-lots-of-extras-anybody-use-them/msg4248262/#msg4248262I was just wondering if it would be any better.

author: agarza
author_rank: Newbie
timestamp: September 21, 2022, 03:00:46 am
content: That seems very high. What's your sleep code?This is the code I use to sleep. On a ATMEGA328p the current is ~1µA. On a LGT8F328P ~150µA.set_sleep_mode(SLEEP_MODE_PWR_SAVE);TCCR2B = (1 << CS20) | (1 << CS22);// Wait until TC2 is updatedwhile ((ASSR & ((1 << OCR2AUB) | (1 << OCR2BUB) | (1 << TCR2AUB) | (1 << TCR2BUB) | (1<< TCN2UB))));sleep_mode(); //sleep!There are many other Power registers on the LGT8F328P. I've tried several with no luck.

author: elecdonia
author_rank: Frequent Contributor
timestamp: December 29, 2022, 03:46:51 pm
content: Quote from: Fungus on June 02, 2022, 06:30:38 amRegarding clone Arduino Nano boards with LGT8F328P MCU:1)GreenLGT8F328P Nano boards are the cheapest. They use the internal RC clock of the MCU.I wonder how stable (or unstable) the internal RC clock frequency is in these new ultra-cheap MCU chips?My experience with the “calibrated” internal RC oscillator in genuine Atmel AVR chips is that it is sufficiently stable enough for applications which don’t requireprecisetiming.A big advantage of internal RC clock is that MCU starts up much faster compared to using crystal oscillator. This is important for applications which must wake up quickly from a deep sleep state where the clock is shut down.2)PurpleLGT8F328P Nano boards have an actual 32MHz crystal for the oscillator. This is similar to nearly all other Nano boards which have a genuine ATmega328 MCU, except the clock crystal for the purple LGT8F328P is 32MHz rather than the 16 or 8MHz crystal found in ATmega328 Nano boards.3) After using many clone Nano and Arduino-pro boards that came with a genuine Atmel ATmega328 MCU, I must warn users that the Arduino clone board manufacturers use the very cheapest LDR voltage regulator chips. Many of these can tolerate only 9V maximum input voltage. I fried several clone Nano boards by applying 12V. I developed a habit of changing the voltage regulator chips for applications where the raw power input voltage might exceed 9V.

author: Fungus
author_rank: Super Contributor
timestamp: December 30, 2022, 07:42:48 am
content: Quote from: elecdonia on December 29, 2022, 03:46:51 pmThe datasheet say's they're laser-trimmed at the factory. The frequency seems very accurate on the few chips I've measured.Quote from: elecdonia on December 29, 2022, 03:46:51 pmYep. I'm not sure what the advantage of the crystal is.The main advantage of the purple ones is the built-in CH340 USB interface.(although that's a bad thing for low-power projects).I've gone through a few dozen of the green ones lately and not had any problems.

author: elecdonia
author_rank: Frequent Contributor
timestamp: December 30, 2022, 06:45:24 pm
content: Quote from: Fungus on December 30, 2022, 07:42:48 amThanks for the info!Revised features ofgreenvs.purpleLGT8F328P Arduino clones:Greenboard resembles traditionalArduino Pro: Very small PC boardwithoutUSB. MCU clock is internal factory-trimmed clock oscillatorPurpleboard resemblesArduino Nano: USB interface included. MCU clock is crystal on PC board

author: ADI8421
author_rank: Newbie
timestamp: January 11, 2023, 06:34:04 am
content: Hi Fungus,I've taken an interest in writing code for these in assembly language too.A brief video of my DAC test here:Cheers Jim

author: Fungus
author_rank: Super Contributor
timestamp: January 11, 2023, 02:24:58 pm
content: Quote from: ADI8421 on January 11, 2023, 06:34:04 amQuote from: ADI8421 on January 11, 2023, 06:34:04 amThe DAC works great at audio frequencies.

author: ADI8421
author_rank: Newbie
timestamp: January 13, 2023, 04:34:23 am
content: Yes, the audio is pretty good. The data sheet advises an opamp buffer should be used on the DAC output, but it works OK into a Tecsun radio's Aux input without one.

author: Fungus
author_rank: Super Contributor
timestamp: January 13, 2023, 06:28:51 am
content: Quote from: ADI8421 on January 13, 2023, 04:34:23 amNo surprise, an aux input will be high impedance just like an op-amp.

author: yurishnol
author_rank: Newbie
timestamp: March 01, 2023, 02:11:25 pm
content: Hi, this is about measure of VCC.The bug (?) in board support lib package - currently Larduino_HSP_v3.6c still has it.To measure VCC, one can use internal mux channel to internal VRef=1.024V (default),by A/D reference=VCC, default. analogReference(DEFAULT);uint16_t AtoD = analogRead(IVREF); // IVREF=27 for lgt8fThen, calculate VCC=(1.024/AtoD)*4096; //Volt.or else, VCC=4194304/AtoD,mV. (0x400000/AtoD)For original AVR' 328p, instead of 4096 use 1024, for AtoD is 10 bit.Currently, for lgt8f the code do not work, analogRead(IVREF) returns 0..2.To fix the problem, need set the bit BGEN, bit7 in register ADCSRD,ADC Control Status Register D.e.g.: ADCSRD = 0x80;Then, AtoD of IVREF: uint16_t AtoD = analogRead(IVREF);will return correct value. e.g.:for VCC=3.36 V, analogRead(IVREF) returns 1249. VCC=1.024/1249*4096=3.358Vfor VCC=4.72 V, analogRead(IVREF) returns 889. VCC=1.024/889*4096=4.718V

author: Gjeskog
author_rank: Newbie
timestamp: March 11, 2023, 04:00:49 am
content: Hi all. I'm one of those idiots taking an interest in these LogicGreen microcontrollers, so I ordered a bunch a while back. And out of frustration with not having a proper manual to learn from, I set out to translate the manual for this chip. I have used Google, but in small chunks to be able to properly recreate context and readabillity.Personally I'm pretty pleased with the result, allthough it's not yet properly proofread, but I'm thinking this might be of some interest for others with simmilar interest in cheap Chinese MCUs.https://github.com/Skogmus/Datasheet_translations/tree/main/LGT8FX8P_databook_v1.0.5Have a look at it, and feel free to pitch in corrections if you feel like it.

author: Fungus
author_rank: Super Contributor
timestamp: March 11, 2023, 05:50:01 pm
content: Quote from: Gjeskog on March 11, 2023, 04:00:49 amThat's awesome, especially the diagrams and tables. The machine translators really made a mess of the diagrams/tables when I tried them.

author: Fungus
author_rank: Super Contributor
timestamp: March 11, 2023, 05:52:37 pm
content: FWIW I've used a lot of these lately. I'm pretty much doing all my gadgets using them and haven't had any problems.