USART doesn't work while using PWM (dc motor control)

Go To Last Post
73 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

hello, i have a problem with USART with PWM dc motor control

 

this is my MCU

 

Atmega328p_AU (16Mhz External Crystal)

MAX485 (RS485)

IRLR024N (PWM dc motor control)

 

i have two controller MASTER and SLAVE both has a MAX485 (9600 bps)

 

this part is very "interesting"

 

if i connect VCC through power supply, it works good

 

but if i connect bettery (24v for car) it didn't work. (i used lm7805)

 

and it works good with out PWM.

 

but if i using PWM control, usart is dead.

 

this is my code

 

 

MASTER:

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <stdio.h>

/*_____________________USART VARIABLES SET_____________________*/
//
#define FOSC 16000000
#define BAUD 9600
#define MYUBRR (FOSC/16/BAUD)-1

volatile unsigned char DATA_RESULT = 0;
volatile unsigned char DATA_POWER = 0;
volatile unsigned char DATA_MODE  = 0;

int usart_timer = 0;
//________________________________________________________


void send_data(){
	if(usart_timer > 2000){
		if(DATA_POWER == 0){
			DATA_MODE = 0;
			DATA_LEVEL = 0;
		}
		else if(DATA_POWER == 1){
			DATA_RESULT = 47 +  DATA_POWER + DATA_MODE  ;
			USART_Transmit(DATA_RESULT);
			USART_Transmit('\r');
			USART_Transmit('\n');
		}
		
		usart_timer = 0;
	}

}

int main(void)
{
	/*Usart init*/
	USART_Init(MYUBRR);

	//Usart set sending mode
	USART_DE_ON;

	_delay_ms(10);
	
    while (1) 
    {
        send_data();
    }
}

 

PWM.c

/*_____________________PWM REGISTER INFO_____________________*/
//
//-----------TCCR1A
//-----------COM1A1  COM1A0  COM1B1  COM1B0  -  -  WGM11  WGM10
//
//-----------TCCR1B
//-----------ICNC1  ICES1  -  WGM13  WGM13  CS12  CS11  CS10
//
//-----------TCCR1A
//-----------COM1A1  COM1A0  COM1B1  COM1B0  -  -  WGM11  WGM10
//
//-----------FAST PWM PORT  -->  PB1
//========================================================

#define F_CPU 16000000UL

#define PRESCALER 8

#include <avr/io.h>

#include "PWM.h"

int _freq=0;

/*_____________________PWM INIT_____________________*/
//
void PWM_INIT(int freq){
	
	PWM_DIR |= (1 << PWM_PIN);
	
	
	//SET FAST PWM MODE
	//USING TOP to ICR1
	TCCR1A |= (1 << WGM11);
	TCCR1B |= (1 << WGM13) | (1 << WGM12);
	
	//SET PRESCALER
	//USING CLK/8
	TCCR1B |= (1 << CS11);
	
	//freq --> 1Khz  : 1000
	//         10Khz : 10000
	//         20Khz : 20000
	ICR1 = (F_CPU/PRESCALER/freq) - 1;
	_freq = freq;
	
}
//
//========================================================


/*_____________________PWM SET DUTY_____________________*/
//
void PWM_SET_DUTY(int duty){
	OCR1A = (((F_CPU/PRESCALER/_freq) - 1)*duty)/100;
}
//
//========================================================


/*_____________________PWM ON_____________________*/
//
void PWM_ON(){
	//NON-INVERTION MODE
	//SET ON
	TCCR1A |= (1 << COM1A1);
}
//
//========================================================

/*_____________________PWM OFF_____________________*/
void PWM_OFF(){
	//NON-INVERTION MODE
	//SET OFF
	TCCR1A &= ~(1 << COM1A1);
}
//
//========================================================

 

MAX485.h (rs485)

#define F_CPU 16000000UL

#include <avr/io.h>
#include <util/delay.h>

#include "MAX485.h"


/*_____________________USART INIT_____________________*/
//
void USART_Init( unsigned int ubrr)
{	
	USART_DE_PORT_DIR |= (1 << USART_DE);
	
	UBRR0H = (unsigned char)(ubrr>>8);
	UBRR0L = (unsigned char)ubrr;

	UCSR0B = (1<<RXEN0)|(1<<TXEN0)|(1 << RXCIE0);
	UCSR0C = (1<<USBS0)|(3<<UCSZ00);
}
//
//========================================================

/*_____________________USART TRANSMIT_____________________*/
//
void USART_Transmit( unsigned char data )
{
	while ( !( UCSR0A & (1<<UDRE0)) );
	UDR0 = data;
}
//
//========================================================

/*_____________________USART RECEIVE_____________________*/
//
unsigned char USART_Receive( void )
{
	while ( !(UCSR0A & (1<<RXC0)) );
	return UDR0;
}
//
//========================================================

/*_____________________USART TRANSMIT_____________________*/
//
void USART_Transmit_Str(unsigned char *data){
	//USART_DE_ON;
	//_delay_ms(15);
	
	while(*data != '\0'){
		USART_Transmit(*data);
		data++;
	}
	
	//USART_DE_OFF;
	//_delay_ms(15);
}
//

 

 

SLAVE:

#define F_CPU 16000000UL

#include <avr/io.h>
#include <stdio.h>
#include <string.h>
#include <util/delay.h>
#include <avr/interrupt.h>

#include "PORT_SET.h"
#include "MAX485.h"
#include "ADC.h"
#include "PWM.h"

/*_____________________USART VARIABLES SET_____________________*/
//
#define FOSC 16000000
#define BAUD 9600
//#define BAUD 115200
#define MYUBRR (FOSC/16/BAUD)-1

volatile unsigned char rx_char = 0;
volatile unsigned char DATA_RESULT = 0;
volatile unsigned char DATA_POWER = 0;
volatile unsigned char DATA_MODE  = 0;

int usart_ping_timer1 = 0;
int usart_ping_timer2 = 0;
volatile unsigned char usart_flag = 0;
//________________________________________________________

ISR(USART_RX_vect){
    rx_char = UDR0;
    
    switch(rx_char){
        //0
        case 48:
            DATA_RESULT = 0;
            usart_flag = 1;
            break;
        //1
        case 49:
            DATA_RESULT = 1;
            usart_flag = 1;
            break;
        //2
        case 50:
            DATA_RESULT = 2;
            usart_flag = 1;
            break;
        default:
            usart_flag = 0;
            break;
    }
}

void update_fan_level(){
    if(DATA_RESULT == 0){
        PWM_OFF();
    }
    else if(DATA_REUSLT == 1){
        PWM_SET_DUTY(50);
        PWM_ON();
    }
    else if(DATA_RESULT == 2){
        PWM_SET_DUTY(80);
        PWM_ON(); 
    }
}

int main(void)
{
    /*DISABLE INTERRUPT*/
    cli();
    
    /*USART INIT*/
    //PWM_INIT(5000); //5 Khz
    PWM_INIT(8000); //8 Khz
    //PWM_INIT(10000); //10 Khz
    //PWM_INIT(12000); //12 Khz
    
    /*USART INIT*/
    USART_Init(MYUBRR);
    
    //USART START LISTEN
    USART_DE_OFF;
    _delay_ms(10);
        
    /*ENABLE INTERRUPT*/
    sei();
    while(1){
        if(usart_flag == 1){
            update_fan_level();
        }
    }
}

 

 

i read manu thread about this.

 

i followed some tips

 

1. in ISR must use 'volatile'

 

2. never doing variable operation in ISR (it doesn't matter in my situdation, not works too)

 

3. if you use interrupt make sure define ISR

 

if i don't use PWM, just turn on LED or RELAY it works fine.

 

i think 'USART inteerupt ISR' part is problem.

 

but i don't have any idea for testing this.

 

i just tested code changed BPS or PWM hz things...

 

how can i find any clue for fix this??

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

The first question you could answer is how this condition ever becomes true:

	if(usart_timer > 2000){

I don't see any increment or even any write.

 

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

Are the master and slave GND connected?

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

Since this looks like a two micro operation, please show schematics and pictures of your setup. 

 

 

Keys to wealth:

Invest for cash flow, not capital gains!

Wealth is attracted, not chased! 

Income is proportional to how many you serve!

 

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
	if(DATA_POWER == 0){
		DATA_MODE = 0;
		DATA_LEVEL = 0;
	}
	else if(DATA_POWER == 1){
		DATA_RESULT = 47 +  DATA_POWER + DATA_MODE  ;
		USART_Transmit(DATA_RESULT);
		USART_Transmit('\r');
		USART_Transmit('\n');
	}

 

sorry this is right that one is old one

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

kabasan wrote:

Are the master and slave GND connected?

yes of course!

 

ki0bk wrote:

Since this looks like a two micro operation, please show schematics and pictures of your setup. 

 

yes this is my schematics

 

same MAX485 circuit, same LM7805 circuit

 

All VCC(+24v) and GND is connected (Motor + is same as VCC)

 

Attachment(s): 

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

if i connect VCC through power supply, it works good

So I would think that the software was likely OK, and that the battery power supply could not supply enough current, and was dropping its voltage when the device being PWM'd kicked in.

 

Although a vehicle battery usually can supply a LOT of current.

 

I'm not on the right computer to check the Vin max for the 7805, but 24 V is certainly pretty high.

Know that the more current you draw, the more power it has to dissipate.

I hope it has a large heat sink.

It could also be going into thermal shutdown.

 

JC  

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

Where is the micro?  The micro quits sending, but you don't think it is important to show it??

 

Show the circuit board as well

 

Important---show a block diagram of both boards, power units, load, PC ---everything that forms the system & the connections between them.

Even though you use '485 (good idea), you can form gnd loops

 

You forgot to show the motor  M+ connection,, good placement is important  24V 'here' may not be 24v 'there'.

 

How many amps does the motor take?  5A? 10A?

 

What if you take the motor off, but leave the PWM on (is the trouble related to high/large current)...if the trouble happens with no load, probably a software trouble.

 

Add maybe 10 ohms ahead of D8, if it kicks in, it will try to destroy itself, or more likely D1, especially if the source is a battery like a large lithium-ion pack ("no" current limit).

10 ohm will greatly help & at 100 ma (if that), 10ohm will barely be much drop (only 1V)  ---you have plenty of excess volts

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Jul 24, 2021 - 04:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

this is my circuit

 

motor + is connected VCC

 

BAT VCC  ----   MASTER, SLAVE, MOTOR +

 

BAT GND  ---- MASTER, SLAVE -

 

FET ---- MOTOR -

Attachment(s): 

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

avrcandies wrote:

Where is the micro?  The micro quits sending, but you don't think it is important to show it??

 

Show the circuit board as well

 

You forgot to show the motor  M+ connection,, good placement is important.

 

How many amps does the motor take?  5A? 10A?

 

What if you take the motor off, but leave the PWM on (is the trouble related to high/large current)...if the trouble happens with no load, prob a software trouble.

 

Add maybe 10 ohms ahead of D8, if it kicks in, it will try to destroy itself, or more likely D1, especially if the source is a battery like a large lithium-ion pack ("no" current limit).

10 ohm will greatly help & at 100 ma (if that), 10ohm will barely be much drop (only 1V)  ---you have plenty of excess volts

 

 

 

Motor is 24V / 3A (Maximum)

 

and both board current is only 70mA so no need heatsink it's under 35 ºc

 

board picture is not here i'll take tomorrow!

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

this is my board

 

 

Attachment(s): 

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

avrcandies wrote:

Add maybe 10 ohms ahead of D8, if it kicks in, it will try to destroy itself, or more likely D1, especially if the source is a battery like a large lithium-ion pack ("no" current limit).

10 ohm will greatly help & at 100 ma (if that), 10ohm will barely be much drop (only 1V)  ---you have plenty of excess volts

 

 

10 ohms between D8 and VCC?

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

 

Again, try with no load...that can help narrow it down

 

why have this floating around in the middle of the schematic?

How does it help having it like this?---I spent a good time scrolling around the AVR looking for it.

You can invite many unseen errors this way---be extra careful.  Schematics are not just a bunch of parts dumped in random places on the paper.

The drawing is neat and tidy, so that is helpful.

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Jul 24, 2021 - 04:58 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


 

Try changing R3 to 220 Ohms.

Currently with 10 Ohms the microcontroller will try to source 500 mA (briefly) to the mosfet gate capacitor when it switches ON.

 

With 220 Ohms you limit it to about 20mA. Not sure how this will affect your USART problem but it's worth a try.

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

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

Try changing R3 to 220 Ohms.

I was going to suggest temporary adding a small cap to the gate (like maybe 0.01uF) to slow the gate down to eliminate spikes that might cause trouble. This slow switching is not desirable, if it causes too much heating, but can be a test.

First take off the load ..that is an easy test.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:
First take off the load ..that is an easy test.

+1

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

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

thank you for reply!

 

i'm sorry my schematics is really bad. i know.

 

i have to study to draw schematics by the book.

 

1. no load

   - i tested that this way

      a) connected motor

      b) no using motor

      c) just turn on relay, led

      d) doesn't works

     so, if i use PWM, USART codes together in my MCU. it doesn't working even i don't turn DC motor.

 

  (with out USART, only PWM codes -> it works fine)

 

 

i'll try tommorow this ways

 

  1. add 10 ohm sbefore LM7805 for limit power

  2. 10 ohms -> 220 ohms to limit MCU I/O current

  3. instead LM7805 -> LM2576-5

      - not two LM7805 -> one LM2576-5

 

      - present my circuit is one 24v - master LM7805

                                                       - salve LM7805

 

      -                                         24v - LM2576-5 - master LM7805

                                                                          - salve LM7805

 

like this

Last Edited: Sat. Jul 24, 2021 - 05:24 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i'm sorry my schematics is really bad. i know.

It is actually rather nice compared to many others.  Try to keep things in together in the same area, or use actual wires, unless it is a hardship. 

The c1_out, c2_out could be easier drawn as wires since you can put the connector right where it is needed (by the relay)with no hardship.   Then is is also easier to follow what is going on.

 

if i connect VCC through power supply, it works good

    Do you mean both the UART & PWM are working OK at the same time with the bench power supply?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

 

if i connect VCC through power supply, it works good

    Do you mean both the UART & PWM are working OK at the same time with the bench power supply?

 

Yes both are working OK when i use power supply.

 

but if i use battery for car it didn't working (battery for 24V, but actually about 25v)

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

Why haven't you shown the battery---is it a car battery or high power li-ion  pack?

 

How many amps is the motor 5? 10?  that is important

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

Why haven't you shown the battery---is it a car battery or high power li-ion  pack?

 

How many amps is the motor 5? 10?  that is important

 

battery is like this! for (truck or bus? most common used)

 

DC motor is 24V/3A max

 

90% duty -> 2.8A

50% duty -> 1.5A

Attachment(s): 

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

Battery looks good.  maybe too good....if it delivers a solid "punch" that surge can cause disruption....but that surge would go away with no motor.

 

So again to be clear connecting the motor or disconnecting it makes no difference at all in any situation--correct??

 

is a PC involved in the missing 485? With power supply the system may be grounded & on battery the system is floating--that could disrupt a PC connection (unless the pc gnd tied to the circuit)....but not sure you are even using a PC terminal.

 

Please show a pic of the actual board & your wiring setup

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:
With power supply the system may be grounded & on battery the system is floating--that could disrupt a PC connection

Reminds me of those USB USART modules which are opto-isolated. Very handy. 

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

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

Sorry i'm late!

 

i tried many different ways.

 

first i remove all, and start from zero.

 

1. what i'm going to? (block_diagram.gif)

 

   - 1:1 USART(rs485) communication

      (MCU = Atmega328p_au  /  RS485 IC = MAX485  / 9600bps)

 

   - Power -> 24v+ Battery  (24V+ Iron battery for car)

 

   - Master -> input button, buzzer, led

 

   - Slave   -> fast PWM Fan speed control, Relay ON/OFF (FAN 24v / 3A)

                    (distance between Master&Slave = under 1m)

 

2. where am i stuck?

 

   - when i using 1:1 USART communication & PWM at the same time, Slave can't receive data.

 

   - there is my circuit MASTER_VER1_0.pdf , SLAVE_VER1_0.pdf

 

   - if i use Power source 'supply' it works / '24v+ battery' don't works

 

3. what i tried?

 

   - changed bps : 4800 ~ 115200 (x)

 

   - changed PWM frequency : 5Khz to 15Khz (x)

 

   - changed MOSFET gate resistor (o)

       ( i changed 10 Ohm to 220 Ohm)

 

 

So.. there is problem. i think if i use 10 Ohm my MCU got damaged.

and i changed 220 Ohm it works good. but my MCU get to hot. (40 'C)

 

MAX 20mA out is too dangerous for MCU?..

 

so i think i have to use BJT or Optocoupler

 

here is my circuit that i want test. is this right?

 

i want calculate resistor value in mosfet's gate. but i confuse.

 

i started 1K Ohm at the gate resistor. low frequency this is not a problem. (under 5Khz)

 

but high frequency (over 10Khz) fet get's too hot. i think it can't charge and discharge fully.

 

this is my conclusion) 24V 3A Fan is not a high current. but, it is not enough to direct driving.

 

is this resistor value can work? (test_circuit.png)  or gate driving 15v+ (24v to 15v with zener diode) ?

Attachment(s): 

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

So.. there is problem. i think if i use 10 Ohm my MCU got damaged.

and i changed 220 Ohm it works good. but my MCU get to hot. (40 'C)

Very unlikely---even if you short the IO direct to gnd, it probably won't get  hot.  If it gets ho t with 220 ohm, you have other issues.

 

Is this a PCB?  , otherwise what you have wired vs the schematic might have some wiring mismatch error---check carefully.

 

It is time for you to break out the scope & look at signals---you won't get too far otherwise.  What can take days to figure out will be revealed in a few minutes with the scope.  

 

Also, disconnect the PWM/fet (be sure to keep the gate pulled down with gate pulldown resistor) & wire up an resistor/led in it's place    .can you vary the led PWM brightness & communicate? If so, you have a "power"/wiring issue, possibly even noise injection.

 

Why didn't you take a picture of your setup??????  Much to be seen

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Fri. Jul 30, 2021 - 04:28 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i'm sorry i have picture my PCB i missed uploading

 

below PCB_FET.jpg , PCB_POWER.jpg

 

i bought scope yesterday but i have to learn how to use!

 

this is my scope FNIRSI-1014D

 

probe GND with PCB GND and prob SIG with FET Gate right?

Attachment(s): 

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

avrcandies wrote:

 

Also, disconnect the PWM/fet (be sure to keep the gate pulled down with gate pulldown resistor) & wire up an resistor/led in it's place    .can you vary the led PWM brightness & communicate? If so, you have a "power"/wiring issue, possibly even noise injection.

 

 

if i wired LED => works good and MCU gets no heat

 

i think it's some problem with my MOSFET

 

this is my MOSFET datasheet (IRLR024N.pdf)

 

 

Attachment(s): 

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

Your mosfet is fine.   If driving with 200 ohm gate, the avr won't heat up & the fet shouldn't be won't be terrible at a few khz....though if the gate wave is sloppy, the fet may get warm from low switching efficiency...you could try a lower PWM freq.

Try a 1 KHz pwm.  Really, the scope will give the gate picture. 

 

Try removing the diode you added---you could be forcing flyback current into a bad copper pathway (though looks good on paper).   The diode is not generally needed (unless dealing with high power, or weak FETs), since the FET has an inherent Zener.  Avalanche-rated FETs are specifically  designed for this purpose, but I think you are running very low current anyhow.

 

Make sure your motor leads are kept away from your serial lines!     A diode RIGHT AT, across,  the motor keeps the recirculating current close to the motor (Again, you prob don't need a diode at all, but moving it very close to the motor might be helpful)

Try twisting your serial lines (twisted pair).

 

Do not hook your scope GND to either motor lead unless the scope or circuit is floating (such as battery power)...no need to destroy your new scope on day one

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

Your mosfet is fine.   If driving with 200 ohm gate, the avr won't heat up & the fet shouldn't be won't be terrible at a few khz....though if the gate wave is sloppy, the fet may get warm from low switching efficiency...you could try a lower PWM freq.

Try a 1 KHz pwm.  Really, the scope will give the gate picture.

 

thank you for relpy

 

my dc motor make frequency high noise under 14Khz PWM. but i'll try lower frequency and let you know

 

avrcandies wrote:

Try removing the diode you added---you could be forcing flyback current into a bad copper pathway (though looks good on paper).   The diode is not generally needed (unless dealing with high power, or weak FETs), since the FET has an inherent Zener.  Avalanche-rated FETs are specifically  designed for this purpose, but I think you are running very low current anyhow.

 

but if i remove the diode dc motor will destroy fet right?

 

avrcandies wrote:

Make sure your motor leads are kept away from your serial lines!     A diode RIGHT AT, across,  the motor keeps the recirculating current close to the motor (Again, you prob don't need a diode at all, but moving it very close to the motor might be helpful)

Try twisting your serial lines (twisted pair).

 

yes i already twisted my serial lines.

 

i'll try remove the diode & very low frequency.

(i think is more about pull-down resisotr that i used in fet. should i try more stronger? like 4.7k 2.2k 1k)

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

KimJiHoon wrote:

Yes both are working OK when i use power supply but if i use battery for car it didn't working


Try to insert some delay (100ms) before of Master USART init.

 

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

grohote wrote:

 

Try to insert some delay (100ms) before of Master USART init.

 

 

i already tried.

 

i think the problem is in main power source

 

one main power source (battery) is connected 'MCU (regulator)' & 'dc motor VCC' at the same time

 

power supply has a current limit but battery is not.

 

i think that is main problem.

 

and then i used gate resistor 10 ohm so it couldn't limit mcu's i/o port.

 

i changed 10 ohm to 220 ohm it works good. but! MCU get's too hot (40'C)

 

i tried no load test and led test. works fine and no heat.

 

so all problem is result in MOSFET circuit.

 

gate resistor and pull-down resistor or maybe diode.

 

i have a question. is it help add gate resistor 22pF capacitor parallel?

 

small cap could help charging and discharging right?

Last Edited: Sat. Jul 31, 2021 - 11:47 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KimJiHoon wrote:
so all problem is result in MOSFET circuit

Start with PNP/NPN driver. Use 220 ohm to both bases, and 10 ohm from both emitters to MOSFET gate.

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

KimJiHoon wrote:

ICR1 = (F_CPU/PRESCALER/freq) - 1;

What is the ICR1 use, please.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
ICR1 = (F_CPU/PRESCALER/freq) - 1;

F_CPU = 16000000

  - i use external crystal 16Mhz

 

PRESCALER = 8

   - fast pwm mode prescaler 1 / 8 / 32 / 64 / 256 / 1024

      (i selected 8)

 

freq = 12000 hz (=12Khz)

   - i want 12Khz to run my dc motor

     (value that i can't here any noise sound)

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

Mind, this is ICR1. Do you know what is ICR1, please.

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

   - i want 12Khz to run my dc motor

You will almost certainly need a gate driver  (see FAN3100 driver), but use the scope just to check--maybe you get lucky with that fet gate ( some have less charging than others) 

 

Use a low freq & 220 resistor for now----should work with no problem.  Even a five hundred HZ is ok PWM for motor, at least for a test.

 

The fet does not require a diode, it has a diode (zener) built in.  If you want a diode, put it right at the motor, don't put a diode anywhere else.

 

If your AVR is hot, you have different troubles...a 220 ohm gate drive is not the cause (unless fet is shorted & trying to deliver high volts to avr).\

 

Forget guessing--start using your scope!!

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sat. Jul 31, 2021 - 06:43 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

sorry i'm late, i spend whole day how to use scope.

 

1. this is 'function gen 1Khz'

   function_gen_scope.jpg

 

2. this is fet scope

   fet_scope.jpg   -> how i connected

 

   fet_scope_display_1.jpg   ->   result fet scope

   fet_scope_display_2.jpg   ->   result fet scope

 

fet_scope_display_1 & fet_scope_display_2 are same connected but it very hard too read because the screen is too shaking

i think it's too many noise here.

 

 

Attachment(s): 

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

grohote wrote:

Mind, this is ICR1. Do you know what is ICR1, please.

 

ICR1 is set up for PWM frequency

 

and OCRA is set up for PWM duty right?

 

i set up ICR1 to 12Khz and changing OCRA to duty (speed)

 

avrcandies wrote:

   - i want 12Khz to run my dc motor

You will almost certainly need a gate driver  (see FAN3100 driver), but use the scope just to check--maybe you get lucky with that fet gate ( some have less charging than others) 

 

Use a low freq & 220 resistor for now----should work with no problem.  Even a five hundred HZ is ok PWM for motor, at least for a test.

 

The fet does not require a diode, it has a diode (zener) built in.  If you want a diode, put it right at the motor, don't put a diode anywhere else.

 

If your AVR is hot, you have different troubles...a 220 ohm gate drive is not the cause (unless fet is shorted & trying to deliver high volts to avr).\

 

Forget guessing--start using your scope!!

 

i changed 500Hz and MCU gets no heat!

 

but motor make noise in low duty (under 80%)

 

if i remove the diode MCU gets 70'C !!!

 

(EDIT)

I add 470uF/50V CAP Between

 

VCC (MOTOR +) - CAP - GND

 

i changed pull-down resistor 10K -> 4.7K

 

i set up PWM hz 14Khz

 

MCU get no heat & fet get heat 35'C (max when 90% duty)

 

LM7805 regulator get heat 40'C

Last Edited: Sun. Aug 1, 2021 - 03:28 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i changed 500Hz and MCU gets no heat!

You have some sort of gnd problem,,,there should be no reason for heat at the avr whether you are at 500 Hz or 50000hz 

 

The gate can ony be a straight squarewave, it is impossible for it to go up hill or down hill, unless the AVR has a gnd problem  (or your scope is not tied to gnd, or the scope (OR SCOPE PROBE)  is set to ac coupling by mistake.

 

Exactly how do you tie the avr gnd to the fet source gnd on the pcb (show the traces)?  Is that connection rock solid?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

The gate can ony be a straight squarewave, it is impossible for it to go up hill or down hill, unless the AVR has a gnd problem  (or your scope is not tied to gnd, or the scope (OR SCOPE PROBE)  is set to ac coupling by mistake.

 

Exactly how do you tie the avr gnd to the fet source gnd on the pcb (show the traces)?  Is that connection rock solid?

 

this is my scope&supply (scope_supply.jpg)

 

regardless of whether i connected GND or not it same.

 

i connected scope with supply GND but it same

 

avrcandies wrote:

You have some sort of gnd problem,,,there should be no reason for heat at the avr whether you are at 500 Hz or 50000hz

 

it was my mistake. i think i had a wrong connection or wrong measurement

Attachment(s): 

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

Ok, so show you proper gate (Vgs) waveforms.  Also,  how/where is fet source tied to AVR?

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

avrcandies wrote:

Ok, so show you proper gate (Vgs) waveforms.  Also,  how/where is fet source tied to AVR?

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

avrcandies wrote:

Ok, so show you proper gate (Vgs) waveforms.  Also,  how/where is fet source tied to AVR?

 

PCB_wire.png

this is how i connected

 

fet_scope_display_1.jpg

fet_scope_display_2.jpg

this is gate waveforms but it shaking too much up and down i guess GND is unstable

(it same result scope GND -> PCB GND connected or not)

Attachment(s): 

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


Why is there a rather bad connection between the source & gnd??...the source goes to a nice large plane, but the plane looks like only a thin connection to gnd??   how does the soucre gnd plane connect over to the gnd wire?....also WHERE is the connection to the avr gnd?  It must tie rather directly to the source gnd to maintain Vgs.

 

Also your photo is wrong--your wire does not show the gate lead  (though it is also nice to show the AVR pin too).  None of your photos look like the AVR pin, which will look very square like.

 

for good results, toss the scope probe gnd wire in the trashcan & gnd it directly to the plane near the gate  Solder some solid wire, a few inches, to the gnd plane & twist it tight around the probe gnd, the probe tip right where you want to measure...there should be no wires  (over an inch or so (less is better) to the scope probe. here is a slightly different example...again, the idea is "zero" length WIRES to the probe tip & gnd

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

this is i trying to DC 5v

 

am i wrong?

 

probe connection is wrong? or oscilloscope setting is wrong?

 

avrcandies wrote:

Why is there a rather bad connection between the source & gnd??...the source goes to a nice large plane, but the plane looks like only a thin connection to gnd??   how does the soucre gnd plane connect over to the gnd wire?....also WHERE is the connection to the avr gnd?  It must tie rather directly to the source gnd to maintain Vgs.

 

FET Source is connected PCB GND (GND plane)

 

EDIT:

I don't know why but my oscilloscope ch1 is dead...

 

ch2 is really good working

 

this is my display

 

PWM-duty_50.jpg

PWM-duty_70.jpg

PWM-duty_90.jpg

 

 

Attachment(s): 

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

this is i trying to DC 5v

 

am i wrong?

what do you mean---this make no sense.

 

The 5v "wave"  has noise spikes that are common if you don't gnd the probe like I mentioned.  Avoid using any leads  (sig or gnd) at all on the scope probe to get the most noise-free readings.

 

The PWM waves look nice...now how about the gate pin (or is this photo right at the gate, or at the AVR?)  We need to see gate waves, not avr waves  (at low freq they will look very similar to each other).

 

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Sun. Aug 1, 2021 - 11:15 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

avrcandies wrote:

 

The PWM waves look nice...now how about the gate pin (or is this photo right at the gate, or at the AVR?)  We need to see gate waves, not avr waves  (at low freq they will look very similar to each other).

 

 

 

this is my fet gate waves (not avr wave, after gate resistor. i connected directly fet gate pin)

 

duty_0.jpg

duty_50.jpg

duty_70.jpg

duty_90.jpg

 

and duty_pick.jpg

 

duty_pick.jpg you can see that some smooth start? that is kind of soft start in my code

 

 if(fan_timer1 >= 100){
					 if(fan_pwm_duty <= 85){
						 fan_pwm_duty = fan_pwm_duty + 5;
						 PWM_SET_DUTY(fan_pwm_duty);
						 PWM_ON();
					 }
					 else if(fan_pwm_duty >= 95){
						 fan_pwm_duty = fan_pwm_duty - 5;
						 PWM_SET_DUTY(fan_pwm_duty);
						 PWM_ON();
					 }
				 }

like this

Attachment(s): 

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

 

Your wave looks nice and solid---ignore the thin spike now, it is likely to be noise related to your probing, or from things other than the fet or pwm., like the AVR

Your gate wave also looks completely WRONG, it is impossible for the gate to be flat-negative when driven by the AVR

 

Isn't your AVR grounded to the fet source & main power gnd??? ...where are all 3 tied together...check and ensure that they ARE tied!  How are you measuring  a steady negative voltage anywhere.  You must clip your scope gnd to you main incoming ground & start looking. 

Does the AVR PWM pin itself show a negative voltage?  Of course it must not. 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Aug 2, 2021 - 04:24 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

my pcb has a ground plane.

 

AVR gnd, FET source are same gnd

 

so i have one GND

 

my scope gnd is connected to FET leg directely.(source)

 

my scope sig is connected to FET leg directely.(gate)

 

i changed gate resistor value

 

10 ohm , 47 ohm, 100 ohm, 220 ohm, 470 ohm

 

what kind of wave is good?

 

it seems like good 100 ohme or 220ohm (more square)

Attachment(s): 

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

The  problem is that your signals ALL show that the gate signal is negative part of the time. That is not possible for gate drive that comes through a resistor from the MCU port pin.

 

Connect the tip of the probe to ground and verify that the trace is at zero volts. If not, adjust vertical position or vertical offset until it is. Then repeat the measurement of the gate drive.

 

Jim

 

Until Black Lives Matter, we do not have "All Lives Matter"!

 

 

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

when PWM duty is zero

 

gnd is clear i think.

 

but when PWM duty is on that gnd is under CH2 line

 

this is problem right? it means ground is not working, floating?

Attachment(s): 

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

this is my scope point and two CH wave

 

EDIT:

i changed my scope gnd pins, and double checked GND lines all together.

 

but still gnd don't change..

Attachment(s): 

Last Edited: Mon. Aug 2, 2021 - 07:27 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

50.jpeg yellow channel 1, it is a typical AC (and not DC) waveform. Do you have AC/DC switch on the probe, perhaps.

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

my scope like this.

(scope_info.jpg)

 

and this is my setup

(setup.jpg)

Attachment(s): 

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

As avrcandies in #48 said, the voltage can not be negative.
Neither it can be AC-looking ie. negative a lot.
What is wrong, we can not determine without your help. Use another scope, perhaps.

 

Last Edited: Mon. Aug 2, 2021 - 10:10 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

is there any possible that my oscilloscope is damaged?

 

my power strip Desktop, Light(LED), Oscilloscope, Power Supply,..

 

actually i turn off the Light tnan osciiloscope doesn't shaking.

 

 

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

 

grohote wrote:
Do you have AC/DC switch on the probe, perhaps.

 

Or setting in the scope like this :-

 

 

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

Last Edited: Mon. Aug 2, 2021 - 10:55 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#54

 

there is coupling setting, i changed that setting AC, DC but same...

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

Yes, the probe is good at FET G & S

However, I have asked several times & you didn't say..HOW EXACTLY is the avr grounded??  You need to show that explicitly.

 

If the AVR GND is tied to source the it is not possible to generate a negative voltage (with respect to either the gnd/source/AVRgnd)

 

Show exactly (the planes/traces) where the AVR GNS ties to the source & power gnd.  Are ALL avr pins solid to gnd?  

 

If it was AC coupling it would probably center around the avg level, rather than just going a little negative

You may  have gnd issues--that explains a lot.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Aug 2, 2021 - 11:32 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

i'm sorry i tried to explain my gnd lines. but i guess it was not enough because i'm just beginner so, i can't fully understand what you mean.

 

my PCB is like this (PCB_1.png, PCB_2.png)

 

it is two layer and both are GND plane.

 

because it is GND plane there is no trance GND, just all PCB pannel is GND.

 

of course FET's source is GND too. AVR GND too and they connected by GND plane.

 

below the pcb ellipse shaped GND1 is main GND pin. by soldeling wire.

 

i connected my scope to GND1 & GND3 & GND2, and i got same result.

Attachment(s): 

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

KimJiHoon wrote:
it is two layer and both are GND plane.

Next time on two layer boards, Route components, signals and power paths on layer 1 and ground return on layer 2. Since you are a beginner, this will make debugging a lot easier.

 

For now, I think this is a board design issue. Not sure though.

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

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

Heisen wrote:

KimJiHoon wrote:
it is two layer and both are GND plane.

Next time on two layer boards, Route components, signals and power paths on layer 1 and ground return on layer 2. Since you are a beginner, this will make debugging a lot easier.

 

For now, I think this is a board design issue. Not sure though.

 

thank you for your reply

 

than

 

bottom layer -> gnd plane

 

top layer -> power plane

 

signal -> trace

 

is this right?

 

i don't have any clue with EMC or EMI design.

 

i designed gnd plane both of PCB's layer because my pcb is low speed and very simple.

 

and i read some tips

 

"if you don't know how, (two layer and simple) make gnd plane"

 

EDIT:

 

it is very embarrassing. my PCB is designed like Multipoint....

(PCB_guide.png)

 

Attachment(s): 

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

bottom layer -> gnd plane

 

top layer -> power plane  Power paths, signal tracks and almost everything.

 

KimJiHoon wrote:
i designed gnd plane both of PCB's layer because my pcb is low speed and very simple.

Not really a reason for GND plane on both layers. Being slow speed has nothing to do with it.

 

KimJiHoon wrote:
i don't have any clue with EMC or EMI design.

If you have no clue, then you probably don't have to worry about it.

 

KimJiHoon wrote:

and i read some tips

 

"if you don't know how, (two layer and simple) make gnd plane"

Again, 2 layer board. 1 layer dedicated to GND plane. You don't really have to have a Power plane, power paths/tracks/traces are fine on top layer. 

 

 

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

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

It may be my tired old eyes, but it appears that pin 21 is not connected to GND. It looks like it connects to a via that is in an isolated area of the plane. At least, I do not see it connected to any of the main GND plane.

 

Again, it may just be my eyes not seeing the connection...

David

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

really thank you it's very helpful!

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

frog_jr wrote:

It may be my tired old eyes, but it appears that pin 21 is not connected to GND. It looks like it connects to a via that is in an isolated area of the plane. At least, I do not see it connected to any of the main GND plane.

 

Again, it may just be my eyes not seeing the connection...

 

you mean this pin?

 

via is connected to GND (bottom and top layer are both gnd plane).

 

#60 PCB_2.png

 

i think gerber img is miss captured. it's my fault your right (top layer display off gnd)

 

thank you for your reply!!

Attachment(s): 

Last Edited: Mon. Aug 2, 2021 - 04:08 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

KimJiHoon wrote:
bottom and top layer are both gnd plane

How many GND through- connection you have. The most important one should be at the Source /but, I can not see it/.

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

sorry i don't know what that mean

 

is these files helpful?

Attachment(s): 

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

 

The gnd plane looks rather awful...why put the parts where there is no clear path from the gnd  connection to the source?????  That is rather foolish when you could move the fet & diode and have a WIDE and DIRECT path?  Next time, takea good look at the layout?

 

Trace out with a marker and show how each GND pin on the AVR can get to the GND through the most direct path you can draw...it can't be seen from what you posted.  

If you can't draw a path, it does not exist

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Mon. Aug 2, 2021 - 05:04 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0


Next time keep your gnd pane on the bottom, it will come out much better.  A PLANE is a solid, WIDE connection.  A little via between two layers DOES NOT CREATE A PLANE.

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

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

this is my new circuit

 

POWER & GND lines are okay?

 

new_circuit_1.png

left BAT_VCC is for relay COM & NO pin, i draw not a line it's area

 

 

new_circuit_2.png

my fet lines

 

new_circuit_3.png

GND & POWER lines for MCU

Attachment(s): 

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

 

 

 

Why do so many things look crooked...a lot of cleanup is needed...all over.  Also why not have your isp connector closer to the avr??...then you don't slice up the gnd plane as much.  Work hard to make that happen.

 

 

 

watch you don't connect two large pipes together with a small hole (I can't tell the sizes you are using)

 

help!   leave pads looking like pads.   Try to have one trace going to a pad, sometimes two if hard to avoid, but never 3

 

why is this cap not spund 180 deg?   keep looking at everything

spin these relays 180  & maybe avoid many of the crossovers...keep your eyes open for opportunity

??

Each time you will get better.  After you throw it in the trashcan & repeat you will see many improvements each time.

You have to practice, practice, practice to play good golf, same with PCB. Then you get a free t-shirt.

 

 

 

When in the dark remember-the future looks brighter than ever.   I look forward to being able to predict the future!

Last Edited: Tue. Aug 3, 2021 - 07:53 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Also the crystal caps should come after the crystal, No?

Right now it's in-between the MCU pins and the crystal.

 

 

“Everyone knows that debugging is twice as hard as writing a program in the first place. So if you're as clever as you can be when you write it, how will you ever debug it?” - Brian W. Kernighan

Last Edited: Tue. Aug 3, 2021 - 08:03 AM