Forbiden tech from China has arrived :-)

Go To Last Post
152 posts / 0 new

Pages

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Something very weird just happened I can't edit, please delete the above post, and this one.

I bet I know what.  You copied __DELAY_BACKWARD_COMPATIBLE__ from the online AVR Libc docs, and tried to paste into a forum post.  The docs use a BBcode-like formatting which employs square brackets.  The forum software will happily display it correctly once posted, but the editor Javascript will not.  You paste, but see nothing.  It's there, so when you click post, it appears.

 

I guess you pasted a few times, wondering why you couldn't see it... ;-)

 

Blame the saxophone players.

"Experience is what enables you to recognise a mistake the second time you make it."

"Good judgement comes from experience.  Experience comes from bad judgement."

"Wisdom is always wont to arrive late, and to be a little approximate on first possession."

"When you hear hoofbeats, think horses, not unicorns."

"Fast.  Cheap.  Good.  Pick two."

"We see a lot of arses on handlebars around here." - [J Ekdahl]

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeap, it was exactly that cheeky

But anyway, __DELAY_BACKWARD_COMPATIBLE__ combined with editing this line:

__tmp2 = ((F_CPU) / 4e6) * __us;

replacing 4e6 by a define that can be 4e6 for normal AVRs or 3e6 for LGT AVRs would work and it's just a minor edit.

 

edit: well, for _delay_us at least... _delay_ms also needs edits.

Last Edited: Thu. Sep 28, 2017 - 11:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

As the timing is actually delivered by __builtin_avr_delay_cycles(), that is what should be modified.

 So ... what would be the procedure/logistics for getting separate "clone" CPUs that are "almost like X" added to avr-gcc ?  Atmel (Microchip) is more-or-less gatekeeper of new devices for avr-gcc, right?

SBIW is not the only timing difference with these chips; I'll bet that things like VUSB and Atomic Zombie's video code break as well...

 

(BTW: these are very good observations - thank you for poking around at the chip!)

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have found what seems to be another Arduino clone based on the LGT AVR clones <- Too much cloning going on :P

 

These are called "MassDuino" and the MCU is the "MD-328D" but I think it's just a re-branded LGT8F328D.

http://www.inhaos.com/index.php

https://www.aliexpress.com/whole...

 

They seem to be better quality boards than the ones mentioned in this thread. Maybe I'll buy one.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would be interested in the boards with proper Uno headers.

None of the items seem to be available from the Ebay links.

I have ordered the Uno clone from AliExpress.

 

Yes,  I suspect that they use the LGT8F328D chip (or similar)

The LGT8F328P chip is a lot more attractive.

Both -D and -P give less than 32kB Flash.   It is worth removing the EEPROM "functionality" if you want reasonable Flash program storage.

 

I know that you have the 328P breakout board i.e. like a Nano.

 

Do you want a 328D breakout board?   i.e. like a Pro-Mini  (needs external USB Serial)

I can mail one to you.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

david.prentice wrote:

 

Do you want a 328D breakout board?   i.e. like a Pro-Mini  (needs external USB Serial)

I can mail one to you.

 

 

Thanks, but actually I have one of those too. It's the one in post #55, right? Maybe someone else would like to have it for testing?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

These are called "MassDuino"

No religious topics please. cheeky

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

@El Tangas,

 

Have you tried bidirectional SPI?

 

My MassDuino Uno LC board arrived a couple of days ago.

 

It contains an LGT8F328D and works nicely at 3.3V

 

I am trying to get Bidirectional SPI Master to work.   i.e. setting DUAL bit in SPCR.

My Logic Analyser shows the MOSI line becoming an input but reading SPDR returns the wrong values.

 

I get the same values and behaviour with LGT8F328P

The 328P has other features with its SPI buffers.

 

Bidirectional SPI on a STM32 is also painful.   This seems crazy.   Bidirectional only involves changing MOSI from output to input and routeing the MISO signal to this pin.    Writing to SPDR sends 8 SCK clocks.

 

David.

Last Edited: Fri. Jan 26, 2018 - 11:00 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I haven't tested that. You mean 3-wire SPI, that is, clock, select and a single bidirectional data wire?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes.   As used by ST7735, ILI9163, ... and an option for most TFT controllers.

 

Oh,  I struggled for ages to read the BandGap.    You have to enable ACBG for the BandGap to be accessible to the ADC input multiplexer.

Mind you,  the LGT has a "better" method of reading AVCC via a potential divider.   Instead of the AVR method of reading BG with AVCC as ref.

 

If the chips had a usable 32kB of Flash,   both 328D and 328P would be very attractive.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#113 Do you really use more than 30K of code? (it must be fonts!?)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes,   fonts,  bitmaps, ... take a massive amount of flash memory.

 

My standard Arduino library demo sketch needs to use every class method.

I can reduce binary size by:

Eliminating calls to the String class.

Removing any f-p maths.

Eliminating calls to Serial class.

Removing or reducing size of bitmaps.

 

Since my library is designed for Uno-style shields I would like to fit the same demo on Duemilanove, Uno, Leonardo, MassDuino, (LogicGreen), ...

The Leonardo and Duemilanove have very big bootloaders but are not very common targets.

 

Obviously there are no size problems with Mega, Zero, Due, STM32, Teensy3.x

 

Yes,  I quite agree.   Simple apps are unlikely to use every class method.

And any human would avoid the Arduino String class like the Plague.

 

SPI on a single bidirectional SDA pin is used by many integrated circuits.   Even regular 3-Wire chips tend to have MOSI sequences followed by MISO sequences.   If the SPI Slave has low traffic it is fine to bit-bash in software.

 

But Display controllers have enormous traffic.   Hence the interest in Hardware SPI.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

From what I understand from google translate of the datasheet, the "dual" mode means that bits are read from both MOSI and MISO with each clock. The master cannot send data in this mode.

So this is not actually a bidirectional mode, I'm not sure which kind of devices use this protocol? Some EEPROMs, I think.

 

Host two-wire receiver
The SPI module's two-wire mode is valid only in host operating mode. Unlike the one-wire mode, both MOSI and MISO are used by the host to receive data. Each SPCK clock receives two bits of data simultaneously (before the data on the MISO line, MOSI line data in the post), after receiving two bytes of data, the hardware sets the transfer completion flag SPIF, the data is saved to the receive buffer and shift register.
At this point the software must read the SPDR register twice to get the two bytes of data it receives.
It should be noted that although the host does not send data to the slave in 2-wire mode, the software still needs to write data to the SPDR register to start the clock generator to generate the communication clock. Once the SPDR register is written, it can receive two bytes of data

Last Edited: Tue. Jan 30, 2018 - 02:57 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I would expect the DUAL bit to:

1.  change MOSI pin from output to input

2.  disconnect MOSI

3.  connect MISO

4.  write SPDR starts SCK

5.  read SPDR reads RX buffer

 

This would mean any write to TX e.g. SPDR = 0 to still instigate the SCK pulses

And any read from RX e.g. reply = SPDR would read the RX buffer

 

Observing the MOSI pin with a Logic Analyser shows that (1) is as expected.

(2) is as expected.

(4) is as expected

(5) returns consistent values but they bear no relation to the data from the Logic Analyser.

 

All operations on 25xxxx EEPROMs are half-duplex.

All operations on SD Cards are half-duplex

All operations on TFT controllers are half-duplex

 

ICSP via SPI on AVR chips are full-duplex.   i.e. you require separate MOSI, MISO lines

 

Since the AVR SPI peripheral has inherent haywire-ability the hardware is capable of (1), (2), (3) but it only does this with the MSTR bit

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You have MISO and MOSI connected together (shorted?), right?

 

So see what happens if you send 8 bits of data: 0bx7x6x5x4x3x2x1x0, I expect that SPDR will have 2 bytes available for reading. These bytes will be: 0bx7x7x6x6x5x5x4x4 0bx3x3x2x2x1x1x0x0, that is, all bits will be doubled because they are read alternately from MOSI and MISO, which will be the same since they are shorted.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No, I just have AVR MOSI connected to SDA on the ST7735
The whole poit of half-duplex is to reduce the number of lines.
.
I tried your theory of 16 alternate bits but this does not work. It would also mean hard work to extract the 8-bits.
And there is no point in keeping track of a TX signal that is disconnected.
.
I am sure there is a simple explanation but I see no mention in the Chinese Forum. And the -P works the same as the -D version.
.
David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I thought you were doing this:

 

 

That is: on output, MOSI sends data, that is echoed in MISO, SPDR is read and discarded. Then MOSI becomes high impedance for the input phase, you write a dummy byte so that SCK sends pulses, and data is received by MISO.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

No.   I just had TFT_SDA  connected to AVR_MOSI.    In a perfect world,  I could use the DUAL bit in LGT's SPSR to swap sides in half-duplex.

 

I also tried this form of connection.   i.e. TFT_SDA connected to AVR_MOSI via 10k resistor with AVR_MISO

This arrangement looks as if it should work with any SPI peripheral on any MCU.    The MCU MOSI would win when in TX direction.   The Slave SDA would win in the RX direction.    I can always put the MOSI port driver to input in the RX phase if I need to.  

enter image description here

 

This is what I get for reading 3 bytes from reg(0x04).    The MISO line samples the MOSI line in the TX phase.   The MOSI and MISO lines just go high in the RX phase.

 

and this is what I get without linking MISO i.e. how the ST7735 behaves in real life when presented with a reg(0x04) RDID4 command:

 

 

Ignore the MISO trace in the "unlinked" picture.   The line is floating and just picks up noise.

I can go back to bit-bashing in software but it would be so nice to use the hardware as Nature intended.

 

David.

Last Edited: Tue. Jan 30, 2018 - 02:51 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

A translation that was completed yesterday :

GitHub - RalphBacon/LGT8F328P-Arduino-Clone-Chip-ATMega328P: An improved ATMega328P with this cheap clone Logic Green LGT8F328P

...

 

[download a 2.7MB PDF]

A dual Chinese/English translated datasheet - better than the other one

 

...

via

#156 LGT8F328P Replacement for ATMEGA328P (at 32MHz 😲) (YouTube, Aug 2 2019, 26m14s)

by Ralph S Bacon

There are more manufacturers of LogicGreen AVR function-like boards.

 

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hello everyone,

 

I have a lgt8f328p and accidentally disabled the reset pin, now I can't program it.

 

From what I understood I need to program it through SWD, I found this guide:

 

https://www.itead.cc/blog/instal...

 

Is this correct or is there an alternative? If this was an ATmega328p I could program it with a High Voltage Programmer but with the lgt8f328p I don't think that's possible because I can't find information about it.

 

Thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You should be able to re-install the Bootloader with LarduinoISP.ino sketch running on a regular Arduino.

 

Which LGT board do you have?

Do you own an Arduino?

 

Your Itead link is fairly ancient.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

"Dare to be naïve." - Buckminster Fuller

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I have a WAVGAT UNO R3 (lgt8f328p) ,

 

https://www.aliexpress.com/item/...

 

and I have an Arduino Mega2560 (atmega2560).

 

Ok doesn't it use the reset?

 

https://github.com/LGTMCU/Lardui...

 

EDIT: I took a look at the code,

 

  digitalWrite(RESET, HIGH);
  pinMode(RESET, OUTPUT);
  delay(20);
  digitalWrite(RESET, LOW);
  
  SWD_init();
  SWD_Idle(10);

I don't think this will work. I am not an expert, so I hope I am wrong.

Last Edited: Wed. Sep 4, 2019 - 07:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ypur WAVGAT board seems to have a chip called AVGA328P.

I don't see any SWC, SWD pins.  Nor PC6, PC7 pins
 

Note that most Chinese boards seem to have scrubbed the chip marking.

Does this have a LGT8F328P-32 chip ?

I am wary of chips with scrubbed markings.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Well it's a LGT8F328 because when I download the folder hardware for the Arduino IDE it contains information for that chip.

 

Right now I am not home but I will check which package is that chip.

 

Yes, this board was a mistake, I never thought they would clone the Atmega328p.

 

EDIT: Yes, it's a LGT8F328P-32.

Last Edited: Wed. Sep 4, 2019 - 10:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

The WAVGAT board seems to be 5V GPIO only.   Many other boards have a switch for 3.3V/5V GPIO.

And without any SWC, SWD headers will be difficult to re-program.

 

First off.   Try the bootloader via avrdude e.g.

avrdude -c arduino -p m328p -P com5 - b 57600

Change the com5 for the actual COM#.   Most LGT bootloaders are 57600 baud.   Try 115200 baud too.

Your WAVGAT has a crystals on the pcb.   So I would expect accurate baud rates.

 

If the bootloader is running on the RC you might need to tweak the baudrate.

My LGT8F328P-48 needs -b59000 for reliable bootload.

 

If you don't have any luck with the bootloader,   you will have to solder fine wires to the SWC, SWD pins on the TQFP32 chip and use LarduinoISP.ino sketch to restore the Bootloader.

 

If the WAVGAT board had a 3V GPIO switch I would buy one.   I have little interest in a 5V GPIO.

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok thank you David,

 

I always programmed the board with avrdude and it always worked like a charm.

 

The problem is that I accidentally disabled the reset function. Unlike ATmega328p, in the LGT8F328P

I can modify the reset pin, select extern or internal clock, etc in the main function, you don't use fuses.

 

So I think my problem right now is that I cannot program the chip because I don't have a reset function.

 

Besides that, the problem with SWD and SWC is that I don't find documentation about it for this chip, so I am not even sure if I bought the programmer this would solve my problem. I just bought another Arduino Uno.

 

Anyway, I would like to solve this problem, it's a shame to have a board that I cannot use. And this chip theoretically has some cool features.

 

I am not an expert, so I may not being clear or not using the right words, please if I need to clarify my sentences just tell me

 

Thank you

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Please just try avrdude.   And write notes on paper.

 

Then you can document what steps you have taken,  and with what result.

 

As far as I know you can't disable the RESET pin.

 

I presume tht there is a current sketch running on your board.

What happens if you press the RESET button?

 

You don't need any complex hardware.   Just solder a 30AWG wire to SWC and SWD pins of the TQFP32.   (e.g. wirewrap wire is 30AWG )

 

David.

 

p.s. there are 4 holes near the CH340 chip.   What do they do?

They might be SWD, SWC, PC6, PC7

Much easier to mount header pins than soldering fine wires.

Last Edited: Thu. Sep 5, 2019 - 12:34 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I will try the avrdude. If that fails I will solder the wires to SWC and SWD, but after that, I don't know what to do.

 

I attached a pdf file, page 57, changing the PMX2 register I can disable external reset,

 

    uint8_t btmp = PMX2 | (1 << C6EN);
    PMX2 = 0x80;
    PMX2 = btmp;

My current sketch is a simple blinking led,

 

int main(void) {

    sysClockSource(EXT_OSC);
    sysClockPrescale(SYSCLK_DIV_0);
    sysEXRESETDisable();

    /* Replace with your application code */
    DDRB = 0b11111111;
    PORTB = 0b00000000;

    while (1) {
        PORTB ^= 1 << LED_BUILTIN;
        _delay_ms(1000);
    }
}

That 4 holes are connected to the CH340.

 

EDIT: I forgot to say that the reset button does nothing. Before I disabled the external reset it worked fine.

Attachment(s): 

Last Edited: Thu. Sep 5, 2019 - 01:12 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

You can only do this in software e.g.

    uint8_t btmp = PMX2 | (1 << C6EN);
    PMX2 = 0x80;
    PMX2 = btmp;

A Power On Reset will still work.   Of course the pin will be disabled as soon as your sketch starts running (and executes the PMX2 sequence)

 

You should hold the Reset button down as you supply power.

An external programmer will control the Reset button.

 

I suspect that you can run the bootloader in the same way.   i.e. force a Power-On reset instead of a pin-Reset

But it might take some nimble fingering.

 

I think it will be safer to use LarduinoISP after you have connected the SWC, SWD pins.

 

At least PMX2 should be recoverable with low voltage.

Punters generally enable the AVR RSTDISBL fuse.   Which requires HVPP to recover.   (AVR users have an overwhelming desire to shoot their own feet)

 

David.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

I ran a script and tried these baud rates, 

 

BAUD_LIST=('2400' '4800' '9600' '14400' '19200' '28800' '38400' '57600' '59000' '76800' '115200' '230400' '250000')

 

Always with the same result,

 

avrdude: stk500_getsync() attempt 1 of 10: not in sync: resp=0x00
avrdude: stk500_recv(): programmer is not responding

 

Ok so,

- Solder wires to SWD and SWC pins

- Connect those pins to Arduino Mega, pin 12 and 13.

- Upload LarduinoISP.ino sketch to Arduino Mega

- Using Arduino IDE burn bootloader to the WAVGAT UNO board

 

Do I have to hold reset when I connect WAVGAT board to Arduino Mega and only release it after I start programming it?

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

First off,   power up your WAVGAT with Reset button held down.    The blink should not start.

Then power up normally.   The blink should blink.

 

I will dig out my LarduinoISP.   I am sure that I wrote some significant improvements (in 2017)

I need to find  it first !!

 

David.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yes, it's exactly like you said, if I hold the reset button when I connect the board to power, the blink won't start. After I release the reset button, the led starts blinking as expected.

 

Ok ok thank you very much

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Nice. Now you have a chance, as soon as you release the RESET button, the bootloader will wait for commands for a short while so you need to run avrdude at about the same time as you release the button.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

By hand it's impossible to sync.

 

I was hoping with larduino sketch to reach something like that but by software.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

1.  File->Preferences: Verbose Compile

2.  write a FastBlinky i.e. without PMX2 sequence  and different speed to your current Blinky

3.  plug in the WAVGAT with the Reset button held down.

4.  click on [Upload] icon

5.  watch the build lines.

6.  when it says "Global variables use NNN bytes" release the button

7.  if successful the FastBlinky should upload and run.    Reset button should stop it.

 

8.  it is worth trying a few times until successful.   Much easier than soldering wires to a TQFP.

Last Edited: Thu. Sep 5, 2019 - 05:17 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Ok I will try that.

 

I already soldered the wires

 

EDIT: I am uploading the sketch through the terminal, so I assume that as soon as I press "Enter" to execute the command I should release the reset button.

It's not working, I already tried different delay (release the button) but it doesn't work.

EDIT2: I added on the larduino sketch,

 

  pinMode(10, OUTPUT);
  delay(20);
  digitalWrite(10, LOW);

to keep WAVGAT under reset till I hit the button to program it through Arduino Mega. It didn't work.

Last Edited: Thu. Sep 5, 2019 - 06:37 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Board Package for Logic Green LGT8F328x and LGT8F88D via Boards Manager Urls. The easy way :)

https://github.com/dbuezas/lgt8fx
 

  • Support 32 Mhz and other clock speeds via menus with automatic prescaler setup
  • Differential Amplifier API
  • Better Boards Menu
  • Installation via Board Manager Urls
  • Added SoftwareSerial @32Mhz
  • This is based on their "old" more compatible version LarduinoHSP v3.6

 

Did it mostly for myself, I hope others find it useful.

 

Add LGT8fx support to the Arduino IDE via Boards Manager Urls:
https://github.com/dbuezas/lgt8fx

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, since this thread is not completely dead, I would like to bounce off a problem about these chip's ADC

.

I have two Wemos XI (retired) boards I want to use  for a simple LiPo discharger and use my Atmel chips for something else. As the Arduino IDE is working with the board, I am going with it.  The chip I have is the LGT8F328D. I simply ported my code from an Atmel 328P with the correct board selected and apart from setting a few minor things, it works correctly, so far so good...

 

Now I use three ADC pins to read the battery voltage through a voltage divider, (10k and 3.3k) I noticed that the voltage on the divider jumps by 80mV as soon as I connect the ADC pin. In fact, if I disconnect the battery, there is always 80mV. As a test, when I connect all three pins to together, the voltage goes to 240mV! It really looks like the ADC have some voltage bias that keeps adding. Something is pulling that high, and I wonder if there is some setting I need to do, but I cannot find anything specific in the datasheet. For now, I simply subtract the 80mV as it is linear. I am posting my code below. (Lots of junk bits in it, but if you can give me a tip to fix that, it will be great!

 

Line 122 is where I compensate for the 80mV.

 

Regards

#define MIN_1S 3.2 
#define _1S 4.35     //this is for 1S HV LiPo
#define _2S 8.4
#define MIN_2S 7.4

//calibration values not yet in use
#define V1 7.82
#define V2 7.82
#define V2 7.82
#define FIRST_PWM_PIN 9   //added to use older retired Wemos XI




/*   Battery discharge table for ULN2003A data sheet. Only valid for 5W resistor...
  Row is for 1S battery, column is for 2S battery
  From data sheet it was determined the max duty cycle for the ULN2003A. Only three outputs will be used
		0	1	2	3				
	0	0	100	70	40
	1	100	80	60	x
	2	100	70	x	x
	3	100	x	x	x 
	
	Becasue of a 1W resistor, at 8.4V (2S) the max Power is limited to 40% of full power for any 2S battery.
	The resistor limits the power dissapation, which can be calculated as (Vbat - 1V)^2 / 22 Ohm = 2.5W
	As the voltage drop, it should be possible to increase the duty cycle to stick closer to 1W.
	Thus 40% duty cycle = 102
*/
	

float Volt[3];
int cell_count[3]; 
uint8_t need_discharge[3];
const long measure_interval = 5000;		//measure interval
unsigned long previousMillis = 0;

void setup() {
  //pinMode(A0,INPUT);
  analogReference(INTERNAL2V56);
	Serial.begin(115200);
	for(int i = FIRST_PWM_PIN; i < 12; i++){
		analogWrite(i,0);	//ensure all analog outputs is off
	}
	read_V();
	for(int i = 0; i < 3; i++)
	{
		Serial.println(Volt[i]);
		//Serial.println();
	}
	Serial.println();
	//add LED's if required

}

void loop() {
		
	if(millis() >= previousMillis + measure_interval){
		read_V();
		duty_cycle();
		for(int i = 0; i < 3; i++)
	{
		Serial.println(Volt[i]);
		//Serial.println(need_discharge[i]);
		
	}
		Serial.println();
		previousMillis = millis();
	}
		
}


//determine the discharge duty cycle, and then check if a battery need a discharge
void duty_cycle(){
  int num_1s = 0;
  int num_2s = 0;
  int num_0s = 0;
  int pwm = 0;
  for (int i = 0; i < 3; i++){
    if(cell_count[i] == 0){
      num_0s++;
    }
    if(cell_count[i] == 1){
      num_1s++;
    }
    if(cell_count[i] == 2){
      num_2s++;
	  
    }Serial.print("cellcount= ");Serial.println(cell_count[i]);
  }
  
  //Duty cycle is set per output.  Outputs will double up to drive two resistors
  for(int i = 0; i < 3; i++){
	if (need_discharge[i]){
		if(num_1s == 3) {
			analogWrite(i + FIRST_PWM_PIN, 178);		//set duty cycle to 70%, there will be no 2S in this case
		}
		else if (cell_count[i] == 1 && num_1s < 3 ){
			analogWrite(i + FIRST_PWM_PIN, 255);		//set duty cycle to 100. 1S is always 100% except when there is 3 x 1S
		}
		else if (cell_count[i] == 2 && num_2s < 3){
			analogWrite(i + FIRST_PWM_PIN, 102);		//set duty cycle to 40%
		}
		else if (num_2s == 3){
			analogWrite(i + FIRST_PWM_PIN, 63);		//set duty cycle to 25%. There will be no 1S in this case
		}
	}
	else{
			analogWrite(i + FIRST_PWM_PIN, 0);  //make double sure nothing is on output if not required
		}
	}

}

//Determine if there is a battery that need to discharge and how many cells it has. A connected battery 
//that is already discharged, will list as cell count of zero, even if connected. It's voltage will still
//print out via serial port to indicate a low state.
//It also determines if a battery needs a discharge depending on it's cell count
void read_V(){
  for(int i = 0; i < 3; i++){
    //Volt[i] = analogRead(i)* (5.0 / 1023.0) * 2;    //convert to a voltage, using a 1:2 voltage divider
    Volt[i] = (analogRead(i)* (2.56 / 4096.0) - 0.08) /.24;    //convert to a voltage, using a 1:3 voltage divider. This ratio
                                                        //was as it was measured Vbat/Vread
    //Serial.println(Volt[i]);
	if(Volt[i] > 0 && Volt[i] <= MIN_1S){        //no need to discharge anything if the battery is within limits 
      cell_count[i] = 0;
      need_discharge[i] = 0;
	  //Serial.println("yes1");
    }
    else if(Volt[i] > MIN_1S && Volt[i] <= _1S){		
      cell_count[i] = 1;
      need_discharge[i] = 1;
	  //Serial.println("yes2");
    }
	else if(Volt[i] > MIN_1S && Volt[i] < MIN_2S){	//make sure no discharge for 2S within limits
		cell_count[i] = 0;		//if a 2S battery is within range, it won't discharge so no need to count towards max
							//current of ULN2003
		need_discharge[i] = 0;
		//Serial.println("yes3");
	}
    else if(Volt[i] > MIN_2S){       
      cell_count[i] = 2;
      need_discharge[i] = 1;
	  //Serial.println("yes4");
    }
	
  }
  
}

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bergie5737 wrote:

Now I use three ADC pins to read the battery voltage through a voltage divider, (10k and 3.3k) I noticed that the voltage on the divider jumps by 80mV as soon as I connect the ADC pin. In fact, if I disconnect the battery, there is always 80mV. As a test, when I connect all three pins to together, the voltage goes to 240mV! It really looks like the ADC have some voltage bias that keeps adding. Something is pulling that high, and I wonder if there is some setting I need to do, but I cannot find anything specific in the datasheet. For now, I simply subtract the 80mV as it is linear

You can work back from the divider resistance and voltage step, to calculate the injected current - here I get ~ 32uA

That's in the ballpark of a light pullup, so you may need to look in the chip data, to see if each pin or maybe each port, has a pullup control bit ?

 

Some MCUs disable pullups when they enable ADC, some do not, & some disable all digital features when ADC is enabled.

Personally, I prefer those that give the designer control.  eg I may want to measure the mV drop on a CMOS driver, using the ADC, to sense the load. 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hi, thanks for the tip. The datasheet for this specific chip, in English, is quite bad! Translated with Google... I see no mention of the fact of disabling the digital features, but I did try it with Ardiuno code but commented out the line where I disable the digital features. I may be wrong there, but did try various pin options as well. Initially I thought I had a "highish" ground or something from the battery, but my scope show it is all the same. 

 

What I do notice is that the ADC inputs goes to full Vcc if left floating, I would expect that to be well, floaty! I do think some pull up is enabled, but not sure how to disable it.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Bergie5737 wrote:
I do think some pull up is enabled, but not sure how to disable it.

 

A quick scan of the data sheet has enough English, to find this when translated 

PUD  Global pull-up inhibit bit
0 = global pull-up control is enabled  (Default)
1 = turn off all IO pull-up resistors

 

So looks like it is global Enables/disable, less than ideal.

Choices are to just live with the fixed offset, (tho it will vary over PVT), or put external pullups on pins that do need them, or maybe disable PUD briefly during ADC.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Hah! Now I see it as well! But a quick check of unused ports seems a bit hodgepodge, with some high, some low. I will investigate how to set it or read it at least. BTW, the correct digital pins for the corresponding ADC is 14, 15, 16 that I use. (A0 to A2 in Arduino)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
bitSet(MCUCR, PUD)

I added this line to the setup on top, MCUCR now read 16, which seems correct. It also makes no difference. All unused pins stay hodgepodge. I will investigate later, for now its homework with my kids... SIGHHHH....!

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yay!!! Success. It turns out the DIDR0 register by default keeps all digital inputs active and the Arduino add-on library does not touch it. (I am too useless to understand the code in the library.) So after staring at the ADC manual this morning, it is obvious.

When the ADCxD bit is set to "0", the digital input of the pin ADCx is enabled and the signal on the pin can be lost
Into the internal digital logic, then clear the ADEN bit, and turn off the analog comparator

Fixed the problem by adding in setup:

 

   bitSet(DIDR0,ADC0D);
   bitSet(DIDR0,ADC1D);
   bitSet(DIDR0,ADC2D);

Yeah I know I can do it in one line for my ADC channels, but this is keeping it simple. Now the annoying Vcc readings on the "floating" pins disappear. Before even a 3.3k pull-down did not give me zero volts. 

Anyway, that was fun. :-)

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

我来自中国。这颗芯片虽然非常优秀,但在国内默默无闻,了解他的人非常少。可能与他的推广力度有关。

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

chunhuazuo wrote:
我来自中国。这颗芯片虽然非常优秀,但在国内默默无闻,了解他的人非常少。可能与他的推广力度有关。

 

Google Translate as Chinese:

Wǒ láizì zhōngguó. Zhè kē xīnpiàn suīrán fēicháng yōuxiù, dàn zài guónèi mòmòwúwén, liǎojiě tā de rén fēicháng shǎo. Kěnéng yǔ tā de tuīguǎng lìdù yǒuguān.

 

To English:

I come from China. Although this chip is very good, it is unknown in the country, and there are very few people who know him. May be related to his promotion efforts.

 

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Yeah, it seems they have the ability to design chips but don't know how to sell them, apparently even inside China. Kind of weird, isn't it? Can't they hire a marketing team, go to trade fairs, stuff like that?

Probably it doesn't help that the chips are all unmarked, it all seems a bit shady. Also, translating the datasheets to English would help a lot.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Or maybe it's really just intended for their own, internal, use; and/or use by one (or a few) key partner(s) ... ?

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

don't know how to sell them, apparently even inside China.

That's one of the things that has made Espressif (ESP8266, ESP32) so fascinating...

 

Pages