본문 바로가기
임베디드/Atmega128

04_HBridgeCircuit.c DC 모터

by sj0020 2020. 12. 29.

 

/*
 * 04_HBridgeCircuit.c
 *
 * Created: 2020-12-29 오전 10:54:04
 * Author : User
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

#define PRESSED			1
#define RELEASED		0
#define DEBOUNCE_MS		300

#define AC	PB6
#define	BD	PB7
#define MOTOR_PORT	PORTB
#define MOTOR_DDR	DDRB
#define CLEAR	(MOTOR_PORT & 0x3F)

#define MOTOR_STOP	(1<<AC | 1<<BD)
#define MOTOR_FORE	(1<<BD)
#define MOTOR_BACK	(1<<AC)

unsigned char digit[] = {0x06,0x5B,0x4F,0x66,0x06D,0x7D,0x27,0x7F,0x6F};
	
void	display_7segled(unsigned char led[], unsigned int number)
{	PORTE = led[number];	}	


int main(void)
{
	int number;
	int before;
	
	DDRE = 0xFF;			//7-segment led port
	DDRG = DDRG &~(1<<PG0); // switch input pin
	PORTG = PORTG | 1<<PG0;	// 스위치 입력 핀 풀업저항 연결
	
	MOTOR_DDR |= 1<<AC | 1<<BD;		// 트랜지스터 제어신호 출력 방향 
	MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_STOP; //초기 stop
	
	number = 0;
	before = RELEASED;
	 
    while (1) 
    {
		display_7segled(digit, number % 10);
		
		if ( before == RELEASED && !(PING & 1<<PG0) ){
			number++;
			_delay_ms(DEBOUNCE_MS);
			before = PRESSED;			
		}else if ( before == PRESSED && (PING & 1<<PG0)){
			_delay_ms(DEBOUNCE_MS);
			before = RELEASED;
		}
		
		if ( number % 10 == 1 )  //number가 1일때 = 2가 display 될 때 -> 모터 정회전
			MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_FORE;
		else if ( number % 10 == 3) //number가 3일때 = 4가 display 될 때 -> 모터 역회전
			MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_BACK;
		else // number가 1과 3 이외의 숫자일 때 -> 모터 정지
			MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_STOP;		
    }
	return 0;
}

 

 

 

 

* 처음 멈춰있음 -> 버튼 누를때마다 정회전 / 역회전 

 

/*
 * 04_HBridgeCircuit.c
 *
 * Created: 2020-12-29 오전 10:54:04
 * Author : User
 */ 
#define F_CPU 16000000UL
#include <avr/io.h>
#include <util/delay.h>

#define PRESSED			1
#define RELEASED		0
#define DEBOUNCE_MS		300

#define AC	PB6
#define	BD	PB7
#define MOTOR_PORT	PORTB
#define MOTOR_DDR	DDRB
#define CLEAR	(MOTOR_PORT & 0x3F)

#define MOTOR_STOP	(1<<AC | 1<<BD)
#define MOTOR_FORE	(1<<BD)
#define MOTOR_BACK	(1<<AC)

int main(void)
{
	int number;
	int before;
	
	DDRG = DDRG &~(1<<PG0); // switch input pin
	PORTG = PORTG | 1<<PG0;	// 스위치 입력 핀 풀업저항 연결
	
	MOTOR_DDR |= 1<<AC | 1<<BD;		// 트랜지스터 제어신호 출력 방향 
	MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_STOP; //초기 stop
	
	number = 0;
	before = RELEASED;
	 
    while (1) 
    {
		
		if ( before == RELEASED && !(PING & 1<<PG0) ){
			number++;
			_delay_ms(DEBOUNCE_MS);
			before = PRESSED;			
		}
		else if ( before == PRESSED && (PING & 1<<PG0)){
			_delay_ms(DEBOUNCE_MS);
			before = RELEASED;
		}
		
		if (number == 0){
			MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_STOP; //초기 stop
		}
		
		else{
			if ( number % 2 == 1 )  //모터 정회전
			MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_FORE;
			else //모터 역회전
			MOTOR_PORT = (MOTOR_PORT & CLEAR) | MOTOR_BACK;
		}
	}
	return 0;	
}

04_HBridgeCircuit_PNP,NPNcopy.DSN
0.11MB

'임베디드 > Atmega128' 카테고리의 다른 글

pwm duty cycle  (0) 2021.01.06
atmega128A datasheet 데이터시트  (0) 2021.01.04
ATmega128 timer / counter  (0) 2020.12.27
모터 L 298  (0) 2020.12.23
atmega128- 03_external_pullup.c , internal_pullup 내부풀업 외부풀업  (0) 2020.12.22